Skip to content

Commit

Permalink
NewFileCreateTest and others
Browse files Browse the repository at this point in the history
  • Loading branch information
distantnative committed Feb 20, 2025
1 parent 9735f09 commit 7e647a4
Show file tree
Hide file tree
Showing 3 changed files with 393 additions and 0 deletions.
106 changes: 106 additions & 0 deletions tests/Cms/NewFile/NewFileChangeNameTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

namespace Kirby\Cms;

use Kirby\Filesystem\F;
use PHPUnit\Framework\Attributes\CoversClass;

#[CoversClass(File::class)]
class NewFileChangeNameTest extends NewModelTestCase
{
public const TMP = KIRBY_TMP_DIR . '/Cms.NewFileChangeName';

public function testChangeName(): void
{
$file = new File([
'filename' => 'test.pdf',
'parent' => $this->app->site()
]);

// create an empty dummy file
$root = $file->root();
F::write($root, '');
// ...and an empty content file for it
$content = $file->version('latest')->contentFile('default');
F::write($content, '');

$this->assertFileExists($root);
$this->assertFileExists($content);

$result = $file->changeName('foo');

$this->assertNotSame($root, $result->root());
$this->assertSame('foo.pdf', $result->filename());
$this->assertFileDoesNotExist($root);
$this->assertFileDoesNotExist($content);
$this->assertFileExists($result->root());
$this->assertFileExists($result->version('latest')->contentFile('default'));
}

public function testChangeNameMultiLang(): void
{
$this->setUpMultiLanguage();
$this->app->impersonate('kirby');

$file = new File([
'filename' => 'test.pdf',
'parent' => $this->app->site()
]);

// create an empty dummy file
$root = $file->root();
F::write($root, '');
// ...and empty content files for it
$contentEn = $file->version('latest')->contentFile('en');
$contentDe = $file->version('latest')->contentFile('de');
F::write($contentEn, '');
F::write($contentDe, '');

$this->assertFileExists($file->root());
$this->assertFileExists($contentEn);
$this->assertFileExists($contentDe);

$result = $file->changeName('foo');

$this->assertNotEquals($file->root(), $result->root());
$this->assertSame('foo.pdf', $result->filename());
$this->assertFileExists($result->root());
$this->assertFileExists($result->version('latest')->contentFile('en'));
$this->assertFileExists($result->version('latest')->contentFile('de'));
}

public function testChangeNameHooks()
{
$calls = 0;
$phpunit = $this;

$this->app = $this->app->clone([
'hooks' => [
'file.changeName:before' => function (File $file, $name) use ($phpunit, &$calls) {
$phpunit->assertIsFile($file);
$phpunit->assertSame('foo', $name);
$phpunit->assertSame('test.pdf', $file->filename());
$calls++;
},
'file.changeName:after' => function (File $newFile, File $oldFile) use ($phpunit, &$calls) {
$phpunit->assertIsFile($newFile);
$phpunit->assertIsFile($oldFile);
$phpunit->assertSame('foo.pdf', $newFile->filename());
$phpunit->assertSame('test.pdf', $oldFile->filename());
$calls++;
},
]
]);

$this->app->impersonate('kirby');

$file = new File([
'filename' => 'test.pdf',
'parent' => $this->app->site(),
]);

$file->changeName('foo');

$this->assertSame(2, $calls);
}
}
34 changes: 34 additions & 0 deletions tests/Cms/NewFile/NewFileCopyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Kirby\Cms;

use Kirby\Filesystem\F;
use PHPUnit\Framework\Attributes\CoversClass;

#[CoversClass(File::class)]
class NewFileCopyTest extends NewModelTestCase
{
public const TMP = KIRBY_TMP_DIR . '/Cms.NewFileCopy';

public function testCopyRenewUuid()
{
// create dumy file
F::write($source = static::TMP . '/original.md', '# Foo');

$file = File::create([
'filename' => 'test.md',
'source' => $source,
'parent' => new Page(['slug' => 'test'])
]);

$oldUuid = $file->content()->get('uuid')->value();
$this->assertIsString($oldUuid);

$target = new Page(['slug' => 'newly']);
$copy = $file->copy($target);

$newUuid = $copy->content()->get('uuid')->value();
$this->assertIsString($newUuid);
$this->assertNotSame($oldUuid, $newUuid);
}
}
253 changes: 253 additions & 0 deletions tests/Cms/NewFile/NewFileCreateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
<?php

namespace Kirby\Cms;

use Kirby\Filesystem\F;
use Kirby\Filesystem\File as BaseFile;
use Kirby\Image\Image;
use PHPUnit\Framework\Attributes\CoversClass;

#[CoversClass(File::class)]
class NewFileCreateTest extends NewModelTestCase
{
public const FIXTURES = __DIR__ . '/fixtures/files';
public const TMP = KIRBY_TMP_DIR . '/Cms.NewFileCreate';

public function testCreate(): void
{
$parent = new Page(['slug' => 'test']);
$source = static::TMP . '/source.md';

// create the dummy source
F::write($source, '# Test');

$result = File::create([
'filename' => 'test.md',
'source' => $source,
'parent' => $parent
]);

$this->assertFileExists($source);
$this->assertFileExists($result->root());
$this->assertFileExists($parent->root() . '/test.md');
$this->assertInstanceOf(BaseFile::class, $result->asset());

// make sure file received UUID right away
$this->assertIsString($result->content()->get('uuid')->value());
}

public function testCreateDuplicate(): void
{
$parent = new Page(['slug' => 'test']);
$source = static::TMP . '/source.md';

// create the dummy source
F::write($source, '# Test');

$result = File::create([
'filename' => 'test.md',
'source' => $source,
'parent' => $parent
]);

$uuid = $result->content()->get('uuid')->value();

$duplicate = File::create([
'filename' => 'test.md',
'source' => $source,
'parent' => $parent
]);

$this->assertSame($uuid, $duplicate->content()->get('uuid')->value());
}

public function testCreateMove(): void
{
$parent = new Page(['slug' => 'test']);
$source = static::TMP . '/source.md';

// create the dummy source
F::write($source, '# Test');

$result = File::create([
'filename' => 'test.md',
'source' => $source,
'parent' => $parent
], true);

$this->assertFileDoesNotExist($source);
$this->assertFileExists($result->root());
$this->assertFileExists($parent->root() . '/test.md');
$this->assertInstanceOf(BaseFile::class, $result->asset());

// make sure file received UUID right away
$this->assertIsString($result->content()->get('uuid')->value());
}

public function testCreateWithDefaults(): void
{
$parent = new Page(['slug' => 'test']);
$source = static::TMP . '/source.md';

// create the dummy source
F::write($source, '# Test');

$result = File::create([
'filename' => 'test.md',
'source' => $source,
'parent' => $parent,
'blueprint' => [
'name' => 'test',
'fields' => [
'a' => [
'type' => 'text',
'default' => 'A'
],
'b' => [
'type' => 'textarea',
'default' => 'B'
]
]
]
]);

$this->assertSame('A', $result->a()->value());
$this->assertSame('B', $result->b()->value());
}

public function testCreateWithDefaultsAndContent(): void
{
$parent = new Page(['slug' => 'test']);
$source = static::TMP . '/source.md';

// create the dummy source
F::write($source, '# Test');

$result = File::create([
'content' => [
'a' => 'Custom A'
],
'filename' => 'test.md',
'source' => $source,
'parent' => $parent,
'blueprint' => [
'name' => 'test',
'fields' => [
'a' => [
'type' => 'text',
'default' => 'A'
],
'b' => [
'type' => 'textarea',
'default' => 'B'
]
]
]
]);

$this->assertSame('Custom A', $result->a()->value());
$this->assertSame('B', $result->b()->value());
}

public function testCreateImage(): void
{
$parent = new Page(['slug' => 'test']);
$source = static::FIXTURES . '/test.jpg';

$result = File::create([
'filename' => 'test.jpg',
'source' => $source,
'parent' => $parent
]);

$this->assertFileExists($result->root());
$this->assertFileExists($parent->root() . '/test.jpg');
$this->assertInstanceOf(Image::class, $result->asset());
}

public function testCreateImageAndManipulate(): void
{
$parent = new Page(['slug' => 'test']);
$source = static::FIXTURES . '/test.jpg';

$result = File::create([
'filename' => 'test.jpg',
'source' => $source,
'parent' => $parent,
'blueprint' => [
'name' => 'test',
'create' => [
'width' => 100,
'height' => 100,
'format' => 'webp'
]
]
]);

$this->assertFileExists($result->root());
$this->assertFileExists($parent->root() . '/test.webp');
$this->assertSame(100, $result->width());
$this->assertSame(100, $result->height());
$this->assertSame('webp', $result->extension());
$this->assertSame('test.webp', $result->filename());
}

public function testCreateManipulateNonImage(): void
{
$parent = new Page(['slug' => 'test']);
$source = static::FIXTURES . '/test.pdf';

$result = File::create([
'filename' => 'test.pdf',
'source' => $source,
'parent' => $parent,
'blueprint' => [
'name' => 'test',
'create' => [
'width' => 100,
'height' => 100,
'format' => 'webp'
]
]
]);

$this->assertFileEquals($source, $result->root());
}

public function testCreateHooks(): void
{
$parent = new Page(['slug' => 'test']);
$phpunit = $this;
$before = false;
$after = false;

$this->app = $this->app->clone([
'hooks' => [
'file.create:before' => function (File $file, BaseFile $upload) use (&$before) {
$before = true;
},
'file.create:after' => function (File $file) use (&$after, $phpunit) {
$phpunit->assertTrue($file->siblings(true)->has($file));
$phpunit->assertTrue($file->parent()->files()->has($file));
$phpunit->assertSame('test.md', $file->filename());

$after = true;
}
]
]);

$this->app->impersonate('kirby');

// create the dummy source
F::write($source = static::TMP . '/source.md', '# Test');

$result = File::create([
'filename' => 'test.md',
'source' => $source,
'parent' => $parent
]);

$this->assertTrue($before);
$this->assertTrue($after);
}
}

0 comments on commit 7e647a4

Please sign in to comment.