From c75f31cd68329798f00c3d97ddc4ea45b32fcc47 Mon Sep 17 00:00:00 2001 From: Bastian Allgeier Date: Mon, 3 Jun 2024 17:15:06 +0200 Subject: [PATCH] First steps to create a new ModelWithContent with a simplified content/translation setup --- src/Content/LabPage.php | 43 ++++++++++++++ tests/Content/LabModelTest.php | 104 +++++++++++++++++++++++++++++++++ 2 files changed, 147 insertions(+) create mode 100644 src/Content/LabPage.php create mode 100644 tests/Content/LabModelTest.php diff --git a/src/Content/LabPage.php b/src/Content/LabPage.php new file mode 100644 index 0000000000..5b90c1ea7f --- /dev/null +++ b/src/Content/LabPage.php @@ -0,0 +1,43 @@ +version()->content($languageCode ?? 'current'); + } + + /** + * Sets the content when initializing a model manually. + * This will switch to the in memory storage to keep the + * content there instead of on disk or in a database. + * The content will be created for the default version and language + */ + protected function setContent(array|null $content = null): static + { + // don't set anything if there's no content + if ($content === null) { + return $this; + } + + $this->storage = new MemoryContentStorageHandler( + model: $this + ); + + $this->version()->create($content, 'default'); + + return $this; + } + +} diff --git a/tests/Content/LabModelTest.php b/tests/Content/LabModelTest.php new file mode 100644 index 0000000000..ff3d79f185 --- /dev/null +++ b/tests/Content/LabModelTest.php @@ -0,0 +1,104 @@ +setUpMultiLanguage(); + + $page = new LabPage([ + 'slug' => 'test', + 'template' => 'article' + ]); + + // 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', $content = [ + 'title' => 'Test' + ]); + + $this->assertSame($content, $page->content('en')->toArray()); + + $this->markTestIncomplete('TODO: The following assertion is only correct as long as we don’t merge translated content. It’s a breaking change so far compared to the previous behavior and needs to be fixed.'); + $this->assertSame([], $page->content('de')->toArray()); + } + + public function testContentSingleLanguage() + { + $this->setUpSingleLanguage(); + + $page = new LabPage([ + 'slug' => 'test', + 'template' => 'article' + ]); + + // 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' + ]); + + $this->assertSame($content, $page->content()->toArray()); + } + + public function testContentNonExistingMultiLanguage() + { + $this->setUpMultiLanguage(); + + $page = new LabPage([ + 'slug' => 'test' + ]); + + $this->assertSame([], $page->content('en')->toArray()); + $this->assertSame([], $page->content('de')->toArray()); + } + + public function testContentNonExistingSingleLanguage() + { + $this->setUpSingleLanguage(); + + $page = new LabPage([ + 'slug' => 'test' + ]); + + $this->assertSame([], $page->content()->toArray()); + } + + public function testSetContentMultiLanguage() + { + $this->setUpMultiLanguage(); + + $page = new LabPage([ + 'slug' => 'test', + 'content' => $content = [ + 'title' => 'Test' + ] + ]); + + $this->assertSame($content, $page->content()->toArray()); + $this->assertSame($content, $page->content('en')->toArray()); + + $this->markTestIncomplete('TODO: The following assertion is only correct as long as we don’t merge translated content. It’s a breaking change so far compared to the previous behavior and needs to be fixed.'); + $this->assertSame([], $page->content('de')->toArray()); + } + + public function testSetContentSingleLanguage() + { + $this->setUpSingleLanguage(); + + $page = new LabPage([ + 'slug' => 'test', + 'content' => $content = [ + 'title' => 'Test' + ] + ]); + + $this->assertSame($content, $page->content()->toArray()); + } +}