-
-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e05d18b
commit 131bc94
Showing
1 changed file
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
<?php | ||
|
||
namespace Kirby\Cms; | ||
|
||
use Kirby\Exception\Exception; | ||
use Kirby\Exception\PermissionException; | ||
use Kirby\Filesystem\Dir; | ||
use Kirby\TestCase; | ||
|
||
class BatchSectionMixinTest extends TestCase | ||
{ | ||
public const TMP = KIRBY_TMP_DIR . '/Cms.BatchSectionMixin'; | ||
|
||
protected Page $page; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->app = new App([ | ||
'roots' => [ | ||
'index' => static::TMP | ||
], | ||
'translations' => [ | ||
'en' => [ | ||
'error.section.test.min.plural' => 'The section requires at least {min} items', | ||
] | ||
] | ||
]); | ||
|
||
$this->page = new Page([ | ||
'slug' => 'test', | ||
'children' => [ | ||
['slug' => 'a'], | ||
['slug' => 'b'], | ||
['slug' => 'c'] | ||
] | ||
]); | ||
|
||
Dir::make($this->page->root() . '/a'); | ||
Dir::make($this->page->root() . '/b'); | ||
Dir::make($this->page->root() . '/c'); | ||
|
||
Section::$types['test'] = [ | ||
'mixins' => ['batch'], | ||
'props' => [ | ||
'min' => function (int $min = 0) { | ||
return $min; | ||
} | ||
], | ||
'computed' => [ | ||
'models' => function () { | ||
return $this->model()->children(); | ||
}, | ||
'total' => function () { | ||
return $this->models()->count(); | ||
}, | ||
] | ||
]; | ||
|
||
$this->setUpTmp(); | ||
} | ||
|
||
public function tearDown(): void | ||
{ | ||
$this->tearDownTmp(); | ||
App::destroy(); | ||
} | ||
|
||
public function testBatchDisabled() | ||
{ | ||
$section = new Section('test', [ | ||
'model' => $this->page, | ||
]); | ||
|
||
$this->assertFalse($section->batch()); | ||
} | ||
|
||
public function testBatchEnabled() | ||
{ | ||
$section = new Section('test', [ | ||
'model' => $this->page, | ||
'batch' => true | ||
]); | ||
|
||
$this->assertTrue($section->batch()); | ||
} | ||
|
||
public function testDeleteSelectedWithoutIds() | ||
{ | ||
$section = new Section('test', [ | ||
'model' => $this->page, | ||
'batch' => true | ||
]); | ||
|
||
$this->assertTrue($section->deleteSelected([])); | ||
} | ||
|
||
public function testDeleteSelectedWhenDisabled() | ||
{ | ||
$section = new Section('test', [ | ||
'model' => $this->page, | ||
'batch' => false | ||
]); | ||
|
||
$this->expectException(PermissionException::class); | ||
$this->expectExceptionMessage('The section does not support batch actions'); | ||
|
||
$section->deleteSelected(['test/a', 'test/b']); | ||
} | ||
|
||
public function testDeleteSelectedWhenExceedingMin() | ||
{ | ||
$section = new Section('test', [ | ||
'model' => $this->page, | ||
'batch' => true, | ||
'min' => 2 | ||
]); | ||
|
||
$this->expectException(Exception::class); | ||
$this->expectExceptionMessage('The section requires at least 2 items'); | ||
|
||
$section->deleteSelected([ | ||
'test/a', 'test/b', 'test/c' | ||
]); | ||
} | ||
|
||
public function testDeleteSelected() | ||
{ | ||
$section = new Section('test', [ | ||
'model' => $this->page, | ||
'batch' => true, | ||
]); | ||
|
||
$this->app->impersonate('kirby'); | ||
|
||
$this->assertTrue($section->deleteSelected([ | ||
'test/a', 'test/b', 'test/c' | ||
])); | ||
} | ||
} |