Skip to content

Commit

Permalink
Make ::clone work with the help of the MemoryContentStorageHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
bastianallgeier committed Jun 10, 2024
1 parent 54a615f commit 9de6a89
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 7 deletions.
24 changes: 24 additions & 0 deletions src/Content/ContentStorageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
29 changes: 22 additions & 7 deletions src/Content/LabPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}
}
70 changes: 70 additions & 0 deletions tests/Content/LabPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}

0 comments on commit 9de6a89

Please sign in to comment.