From 9de6a897e1cc0c3538d29504d7664c2107e5d5b4 Mon Sep 17 00:00:00 2001 From: Bastian Allgeier Date: Mon, 10 Jun 2024 14:06:46 +0200 Subject: [PATCH] Make ::clone work with the help of the MemoryContentStorageHandler --- src/Content/ContentStorageHandler.php | 24 +++++++++ src/Content/LabPage.php | 29 ++++++++--- tests/Content/LabPageTest.php | 70 +++++++++++++++++++++++++++ 3 files changed, 116 insertions(+), 7 deletions(-) diff --git a/src/Content/ContentStorageHandler.php b/src/Content/ContentStorageHandler.php index 8e285699fa..5a0676fd83 100644 --- a/src/Content/ContentStorageHandler.php +++ b/src/Content/ContentStorageHandler.php @@ -128,6 +128,30 @@ public function ensure(VersionId $versionId, Language $language): bool */ abstract public function exists(VersionId $versionId, Language $language): bool; + /** + * Creates a new storage instance with all the versions + * from the given storage instance. + */ + public static function from(self $fromStorage): static + { + $toStorage = new static( + model: $fromStorage->model() + ); + + // copy all versions from the given storage instance + // and add them to the new storage instance. + foreach ($fromStorage->all() as $versionId => $language) { + $toStorage->create($versionId, $language, $fromStorage->read($versionId, $language)); + } + + return $toStorage; + } + + public function model(): ModelWithContent + { + return $this->model; + } + /** * Returns the modification timestamp of a version if it exists */ diff --git a/src/Content/LabPage.php b/src/Content/LabPage.php index e46ae04246..891f680c89 100644 --- a/src/Content/LabPage.php +++ b/src/Content/LabPage.php @@ -14,6 +14,18 @@ */ class LabPage extends Page { + /** + * Creates a new instance with the same + * initial properties + * + * @todo eventually refactor without need of propertyData + */ + public function clone(array $props = []): static + { + $this->storage = MemoryContentStorageHandler::from($this->storage); + return parent::clone($props); + } + /** * Returns the content for the default version and given language code */ @@ -38,8 +50,9 @@ public function save( string|null $languageCode = null, bool $overwrite = false ): static { - $this->version()->save($data, $languageCode ?? 'current', $overwrite); - return $this; + $clone = $this->clone(); + $clone->version()->save($data, $languageCode ?? 'current', $overwrite); + return $clone; } /** @@ -49,8 +62,9 @@ protected function saveContent( array|null $data = null, bool $overwrite = false ): static { - $this->version()->save($data, 'current', $overwrite); - return $this; + $clone = $this->clone(); + $clone->version()->save($data, 'current', $overwrite); + return $clone; } /** @@ -61,8 +75,9 @@ protected function saveTranslation( string|null $languageCode = null, bool $overwrite = false ): static { - $this->version()->save($data, $languageCode ?? 'current', $overwrite); - return $this; + $clone = $this->clone(); + $clone->version()->save($data, $languageCode ?? 'current', $overwrite); + return $clone; } /** @@ -143,7 +158,7 @@ public function translations(): Translations */ public function writeContent(array $data, string|null $languageCode = null): bool { - $this->version()->save($data, $languageCode ?? 'default', true); + $this->clone()->version()->save($data, $languageCode ?? 'default', true); return true; } } diff --git a/tests/Content/LabPageTest.php b/tests/Content/LabPageTest.php index 79fd57d351..96329eb717 100644 --- a/tests/Content/LabPageTest.php +++ b/tests/Content/LabPageTest.php @@ -234,4 +234,74 @@ public function testTranslationsSingleLanguage() $this->assertCount(1, $translations); $this->assertSame($content, $translations->first()->content()); } + + public function testUpdateMultiLanguage() + { + $this->setUpMultiLanguage(); + + $page = new LabPage([ + 'slug' => 'test', + 'template' => 'article' + ]); + + // make sure to be authenticated + $this->app->impersonate('kirby'); + + // write something to the content file to make sure it + // can be read from disk for the test. + Data::write($page->root() . '/article.en.txt', $en = [ + 'title' => 'Test English' + ]); + + Data::write($page->root() . '/article.de.txt', $de = [ + 'title' => 'Test Deutsch' + ]); + + $updatedPage = $page->update([ + 'title' => 'Updated Test English' + ], 'en'); + + $updatedPage = $page->update([ + 'title' => 'Updated Test Deutsch' + ], 'de'); + + // check if the old version is still the same + $this->assertSame('Test English', $page->title()->value()); + + $this->assertSame('Updated Test English', $updatedPage->title()->value()); + $this->assertSame('Updated Test English', $updatedPage->content('en')->title()->value()); + $this->assertSame('Updated Test English', Data::read($updatedPage->root() . '/article.en.txt')['title']); + + $this->assertSame('Updated Test Deutsch', $updatedPage->content('de')->title()->value()); + $this->assertSame('Updated Test Deutsch', Data::read($updatedPage->root() . '/article.de.txt')['title']); + } + + public function testUpdateSingleLanguage() + { + $this->setUpSingleLanguage(); + + $page = new LabPage([ + 'slug' => 'test', + 'template' => 'article' + ]); + + // make sure to be authenticated + $this->app->impersonate('kirby'); + + // write something to the content file to make sure it + // can be read from disk for the test. + Data::write($page->root() . '/article.txt', $content = [ + 'title' => 'Test' + ]); + + $updatedPage = $page->update([ + 'title' => 'Updated title' + ]); + + // check if the old version is still the same + $this->assertSame('Test', $page->title()->value()); + + $this->assertSame('Updated title', $updatedPage->title()->value()); + $this->assertSame('Updated title', Data::read($updatedPage->root() . '/article.txt')['title']); + } }