Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes 15: More ContentStorageHandler class methods #6500

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/Content/ContentStorageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,33 @@ public function ensure(VersionId $versionId, Language $language): void
*/
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;
}

/**
* Returns the related model
*/
public function model(): ModelWithContent
{
return $this->model;
}

/**
* Returns the modification timestamp of a version if it exists
*/
Expand Down
60 changes: 60 additions & 0 deletions tests/Content/ContentStorageHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,66 @@ public function testDynamicVersionsForUser()
$this->assertSame(VersionId::PUBLISHED, $versions[1]->value());
}

/**
* @covers ::from
*/
public function testFrom()
{
$this->setUpMultiLanguage();

$handlerA = new PlainTextContentStorageHandler(
model: $this->model
);

$versionPublished = VersionId::published();
$versionChanges = VersionId::changes();

$en = $this->app->language('en');
$de = $this->app->language('de');

// create all possible versions
$handlerA->create($versionPublished, $en, $publishedEN = [
'title' => 'Published EN'
]);

$handlerA->create($versionPublished, $de, $publishedDE = [
'title' => 'Published DE'
]);

$handlerA->create($versionChanges, $en, $changesEN = [
'title' => 'Changes EN'
]);

$handlerA->create($versionChanges, $de, $changesDE = [
'title' => 'Changes DE'
]);

// create a new handler with all the versions from the first one
$handlerB = TestContentStorageHandler::from($handlerA);

bastianallgeier marked this conversation as resolved.
Show resolved Hide resolved
$this->assertNotSame($handlerA, $handlerB);

$this->assertSame($publishedEN, $handlerB->read($versionPublished, $en));
$this->assertSame($publishedDE, $handlerB->read($versionPublished, $de));

$this->assertSame($changesEN, $handlerB->read($versionChanges, $en));
$this->assertSame($changesDE, $handlerB->read($versionChanges, $de));
}

/**
* @covers ::model
*/
public function testModel()
{
$this->setUpSingleLanguage();

$handler = new TestContentStorageHandler(
model: $this->model
);

$this->assertSame($this->model, $handler->model());
}

/**
* @covers ::move
*/
Expand Down
Loading