-
-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First steps to create a new ModelWithContent with a simplified conten…
…t/translation setup
- Loading branch information
1 parent
dcb7d82
commit c75f31c
Showing
2 changed files
with
147 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,43 @@ | ||
<?php | ||
|
||
namespace Kirby\Content; | ||
|
||
use Kirby\Cms\Page; | ||
|
||
/** | ||
* Test Model to prototype the content and translation | ||
* mechanics | ||
*/ | ||
class LabPage extends Page | ||
{ | ||
/** | ||
* Returns the content for the default version and given language code | ||
*/ | ||
public function content(string|null $languageCode = null): Content | ||
{ | ||
return $this->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; | ||
} | ||
|
||
} |
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,104 @@ | ||
<?php | ||
|
||
namespace Kirby\Content; | ||
|
||
use Kirby\Data\Data; | ||
|
||
class LabModelTest extends TestCase | ||
{ | ||
public const TMP = KIRBY_TMP_DIR . '/Content.LabeModel'; | ||
|
||
public function testContentMultiLanguage() | ||
{ | ||
$this->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()); | ||
} | ||
} |