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

Version::isIdentical() #6781

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
2 changes: 1 addition & 1 deletion src/Api/Controller/Changes.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public static function save(ModelWithContent $model, array $input): array
language: 'current'
);

if ($latest->diff(version: $changes, language: 'current') === []) {
if ($changes->isIdentical(version: $latest, language: 'current')) {
$changes->delete();
}

Expand Down
71 changes: 45 additions & 26 deletions src/Content/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,32 +130,6 @@ public function delete(Language|string $language = 'default'): void
}
}

/**
* Returns the changed fields, compared to the given version
*/
public function diff(
Version|VersionId|string $version,
Language|string $language = 'default'
): array {
if (is_string($version) === true) {
$version = VersionId::from($version);
}

if ($version instanceof VersionId) {
$version = $this->model->version($version);
}


if ($version->id()->is($this->id) === true) {
return [];
}

$a = $this->content($language)->toArray();
$b = $version->content($language)->toArray();

return array_diff_assoc($b, $a);
}

/**
* Checks if a version exists for the given language
*/
Expand Down Expand Up @@ -187,6 +161,51 @@ public function id(): VersionId
return $this->id;
}

/**
* Returns whether the content of both versions
* is identical
*/
public function isIdentical(
Version|VersionId|string $version,
Language|string $language = 'default'
): bool {
if (is_string($version) === true) {
$version = VersionId::from($version);
}

if ($version instanceof VersionId) {
$version = $this->model->version($version);
}


if ($version->id()->is($this->id) === true) {
return true;
}

$language = Language::ensure($language);

// read fields low-level from storage
$a = $this->read($language) ?? [];
$b = $version->read($language) ?? [];

// apply same preparation as for content
$a = $this->prepareFieldsForContent($a, $language);
$b = $this->prepareFieldsForContent($b, $language);

// remove additional fields that should not be
// considered in the comparison
unset(
$a['uuid'],
$b['uuid']
);

// ensure both arrays of fields are sorted the same
ksort($a);
ksort($b);

return $a === $b;
}

/**
* Checks if the version is the latest version
*/
Expand Down
230 changes: 107 additions & 123 deletions tests/Content/VersionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,102 @@ public function testDeleteSingleLanguage(): void
}

/**
* @covers ::diff
* @covers ::exists
*/
public function testDiffMultiLanguage()
public function testExistsLatestMultiLanguage(): void
{
$this->setUpMultiLanguage();

$version = new Version(
model: $this->model,
id: VersionId::latest()
);

$this->assertDirectoryExists($this->model->root());

// the default version + default language exists without
// content file as long as the page directory exists
$this->assertTrue($version->exists('en'));
$this->assertTrue($version->exists($this->app->language('en')));

// the secondary language only exists as soon as the content
// file also exists
$this->assertFalse($version->exists('de'));
$this->assertFalse($version->exists($this->app->language('de')));

$this->createContentMultiLanguage();

$this->assertTrue($version->exists('de'));
$this->assertTrue($version->exists($this->app->language('de')));
}

/**
* @covers ::exists
*/
public function testExistsWithLanguageWildcard(): void
{
$this->setUpMultiLanguage();

$version = new Version(
model: $this->model,
id: VersionId::latest()
);

$this->createContentMultiLanguage();

$this->assertTrue($version->exists('en'));
$this->assertTrue($version->exists('de'));
$this->assertTrue($version->exists('*'));

// delete the German translation
$version->delete('de');

$this->assertTrue($version->exists('en'));
$this->assertFalse($version->exists('de'));

// The wildcard should now still return true
// because the English translation still exists
$this->assertTrue($version->exists('*'));
}

/**
* @covers ::exists
*/
public function testExistsLatestSingleLanguage(): void
{
$this->setUpSingleLanguage();

$version = new Version(
model: $this->model,
id: VersionId::latest()
);

$this->assertDirectoryExists($this->model->root());

// the default version exists without content file as long as
// the page directory exists
$this->assertTrue($version->exists());
}

/**
* @covers ::id
*/
public function testId(): void
{
$this->setUpSingleLanguage();

$version = new Version(
model: $this->model,
id: $id = VersionId::latest()
);

$this->assertSame($id, $version->id());
}

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

Expand Down Expand Up @@ -352,22 +445,16 @@ public function testDiffMultiLanguage()
], 'de');

// no changes in English
$diffEN = $a->diff(VersionId::changes(), 'en');
$expectedEN = [];

$this->assertSame($expectedEN, $diffEN);
$this->assertTrue($a->isIdentical(VersionId::changes(), 'en'));

// changed subtitle in German
$diffDE = $a->diff(VersionId::changes(), 'de');
$expectedDE = ['subtitle' => 'Subtitle (changed)'];

$this->assertSame($expectedDE, $diffDE);
$this->assertFalse($a->isIdentical(VersionId::changes(), 'de'));
}

/**
* @covers ::diff
* @covers ::isIdentical
*/
public function testDiffSingleLanguage()
public function testIsIdenticalSingleLanguage()
{
$this->setUpSingleLanguage();

Expand All @@ -381,7 +468,7 @@ public function testDiffSingleLanguage()
id: VersionId::changes()
);

$a->save([
$a->save($content = [
'title' => 'Title',
'subtitle' => 'Subtitle',
]);
Expand All @@ -391,19 +478,13 @@ public function testDiffSingleLanguage()
'subtitle' => 'Subtitle (changed)',
]);

$diff = $a->diff('changes');

// the result array should contain the changed fields
// the changed values
$expected = ['subtitle' => 'Subtitle (changed)'];

$this->assertSame($expected, $diff);
$this->assertFalse($a->isIdentical('changes'));
}

/**
* @covers ::diff
* @covers ::isIdentical
*/
public function testDiffWithoutChanges()
public function testIsIdenticalWithoutChanges()
{
$this->setUpSingleLanguage();

Expand All @@ -427,15 +508,13 @@ public function testDiffWithoutChanges()
'subtitle' => 'Subtitle',
]);

$diff = $a->diff(VersionId::changes());

$this->assertSame([], $diff);
$this->assertTrue($a->isIdentical('changes'));
}

/**
* @covers ::diff
* @covers ::isIdentical
*/
public function testDiffWithSameVersion()
public function testIsIdenticalWithSameVersion()
{
$this->setUpSingleLanguage();

Expand All @@ -449,102 +528,7 @@ public function testDiffWithSameVersion()
'subtitle' => 'Subtitle',
]);

$diff = $a->diff(VersionId::latest());

$this->assertSame([], $diff);
}

/**
* @covers ::exists
*/
public function testExistsLatestMultiLanguage(): void
{
$this->setUpMultiLanguage();

$version = new Version(
model: $this->model,
id: VersionId::latest()
);

$this->assertDirectoryExists($this->model->root());

// the default version + default language exists without
// content file as long as the page directory exists
$this->assertTrue($version->exists('en'));
$this->assertTrue($version->exists($this->app->language('en')));

// the secondary language only exists as soon as the content
// file also exists
$this->assertFalse($version->exists('de'));
$this->assertFalse($version->exists($this->app->language('de')));

$this->createContentMultiLanguage();

$this->assertTrue($version->exists('de'));
$this->assertTrue($version->exists($this->app->language('de')));
}

/**
* @covers ::exists
*/
public function testExistsWithLanguageWildcard(): void
{
$this->setUpMultiLanguage();

$version = new Version(
model: $this->model,
id: VersionId::latest()
);

$this->createContentMultiLanguage();

$this->assertTrue($version->exists('en'));
$this->assertTrue($version->exists('de'));
$this->assertTrue($version->exists('*'));

// delete the German translation
$version->delete('de');

$this->assertTrue($version->exists('en'));
$this->assertFalse($version->exists('de'));

// The wildcard should now still return true
// because the English translation still exists
$this->assertTrue($version->exists('*'));
}

/**
* @covers ::exists
*/
public function testExistsLatestSingleLanguage(): void
{
$this->setUpSingleLanguage();

$version = new Version(
model: $this->model,
id: VersionId::latest()
);

$this->assertDirectoryExists($this->model->root());

// the default version exists without content file as long as
// the page directory exists
$this->assertTrue($version->exists());
}

/**
* @covers ::id
*/
public function testId(): void
{
$this->setUpSingleLanguage();

$version = new Version(
model: $this->model,
id: $id = VersionId::latest()
);

$this->assertSame($id, $version->id());
$this->assertTrue($a->isIdentical('latest'));
}

/**
Expand Down
Loading