Skip to content

Commit

Permalink
First steps to create a new ModelWithContent with a simplified conten…
Browse files Browse the repository at this point in the history
…t/translation setup
  • Loading branch information
bastianallgeier committed Jun 3, 2024
1 parent dcb7d82 commit c75f31c
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/Content/LabPage.php
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;
}

}
104 changes: 104 additions & 0 deletions tests/Content/LabModelTest.php
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());
}
}

0 comments on commit c75f31c

Please sign in to comment.