Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bastianallgeier committed Dec 13, 2024
1 parent e05d18b commit 131bc94
Showing 1 changed file with 139 additions and 0 deletions.
139 changes: 139 additions & 0 deletions tests/Cms/Sections/mixins/BatchSectionMixinTest.php
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'
]));
}
}

0 comments on commit 131bc94

Please sign in to comment.