Skip to content

Commit

Permalink
Add unit tests for new collection methods
Browse files Browse the repository at this point in the history
  • Loading branch information
bastianallgeier committed Dec 7, 2024
1 parent 20cc97f commit c348f37
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Cms/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Kirby\Cms;

use Exception;
use Kirby\Exception\Exception;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Exception\NotFoundException;
use Kirby\Filesystem\F;
Expand Down
94 changes: 94 additions & 0 deletions tests/Cms/Files/FilesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Kirby\Cms;

use Kirby\Exception\Exception;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Filesystem\F;
use Kirby\Uuid\Uuids;
Expand Down Expand Up @@ -117,6 +118,99 @@ public function testAddInvalidObject()
$files->add($site);
}

/**
* @covers ::delete
*/
public function testDelete()
{
$app = new App([
'roots' => [
'index' => static::TMP
],
'site' => [
'files' => [
['filename' => 'b.jpg'],
['filename' => 'a.jpg']
]
]
]);

$app->impersonate('kirby');

$files = $app->site()->files();

$this->assertCount(2, $files);

$a = $files->get('a.jpg')->root();
$b = $files->get('b.jpg')->root();

// pretend the files exist
F::write($a, '');
F::write($b, '');

$this->assertFileExists($a);
$this->assertFileExists($b);

$files->delete([
'a.jpg',
'b.jpg',
]);

$this->assertCount(0, $files);

$this->assertFileDoesNotExist($a);
$this->assertFileDoesNotExist($b);
}

/**
* @covers ::delete
*/
public function testDeleteWithInvalidIds()
{
$app = new App([
'roots' => [
'index' => static::TMP
],
'site' => [
'files' => [
['filename' => 'b.jpg'],
['filename' => 'a.jpg']
]
]
]);

$app->impersonate('kirby');

$files = $app->site()->files();

$this->assertCount(2, $files);

$a = $files->get('a.jpg')->root();
$b = $files->get('b.jpg')->root();

// pretend the files exist
F::write($a, '');
F::write($b, '');

$this->assertFileExists($a);
$this->assertFileExists($b);

try {
$files->delete([
'a.jpg',
'c.jpg',
]);
} catch (Exception $e) {
$this->assertSame('Not all files could be deleted', $e->getMessage());
}

$this->assertCount(1, $files);
$this->assertSame('b.jpg', $files->first()->filename());

$this->assertFileDoesNotExist($a);
$this->assertFileExists($b);
}

/**
* @covers ::findByKey
* @covers ::findByUuid
Expand Down
95 changes: 95 additions & 0 deletions tests/Cms/Pages/PagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Kirby\Cms;

use Kirby\Exception\Exception;
use Kirby\Exception\InvalidArgumentException;
use Kirby\Filesystem\Dir;

class PagesTest extends TestCase
{
Expand Down Expand Up @@ -190,6 +192,99 @@ public function testChildren()
$this->assertSame($expected, $pages->children()->keys());
}

/**
* @covers ::delete
*/
public function testDelete()
{
$app = new App([
'roots' => [
'index' => static::TMP
],
'site' => [
'children' => [
['slug' => 'a'],
['slug' => 'b']
]
]
]);

$app->impersonate('kirby');

$pages = $app->site()->children();

$this->assertCount(2, $pages);

$a = $pages->get('a')->root();
$b = $pages->get('b')->root();

// pretend the files exist
Dir::make($a);
Dir::make($b);

$this->assertDirectoryExists($a);
$this->assertDirectoryExists($b);

$pages->delete([
'a',
'b',
]);

$this->assertCount(0, $pages);

$this->assertDirectoryDoesNotExist($a);
$this->assertDirectoryDoesNotExist($b);
}

/**
* @covers ::delete
*/
public function testDeleteWithInvalidIds()
{
$app = new App([
'roots' => [
'index' => static::TMP
],
'site' => [
'children' => [
['slug' => 'a'],
['slug' => 'b']
]
]
]);

$app->impersonate('kirby');

$pages = $app->site()->children();

$this->assertCount(2, $pages);

$a = $pages->get('a')->root();
$b = $pages->get('b')->root();

// pretend the files exist
Dir::make($a);
Dir::make($b);

$this->assertDirectoryExists($a);
$this->assertDirectoryExists($b);

try {
$pages->delete([
'a',
'c',
]);
} catch (Exception $e) {
$this->assertSame('Not all pages could be deleted', $e->getMessage());
}

$this->assertCount(1, $pages);
$this->assertSame('b', $pages->first()->slug());

$this->assertDirectoryDoesNotExist($a);
$this->assertDirectoryExists($b);
}

public function testDocuments()
{
$pages = Pages::factory([
Expand Down

0 comments on commit c348f37

Please sign in to comment.