-
-
Notifications
You must be signed in to change notification settings - Fork 175
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
9735f09
commit 7e647a4
Showing
3 changed files
with
393 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,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); | ||
} | ||
} |
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,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); | ||
} | ||
} |
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,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); | ||
} | ||
} |