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

EZP-31681: Fixed Image Value comparison via FT Comparable contract #3060

Merged
merged 19 commits into from
Nov 18, 2020
Merged
Show file tree
Hide file tree
Changes from 7 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: 26 additions & 1 deletion eZ/Publish/Core/FieldType/Image/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use eZ\Publish\Core\FieldType\Value as BaseValue;
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentType;
use eZ\Publish\API\Repository\Exceptions\PropertyNotFoundException;
use eZ\Publish\SPI\FieldType\Comparable;
use eZ\Publish\SPI\FieldType\Value as SPIValue;

/**
* Value for Image field type.
Expand All @@ -19,7 +21,7 @@
* @todo Mime type?
* @todo Dimensions?
*/
class Value extends BaseValue
class Value extends BaseValue implements Comparable
{
/**
* Image id.
Expand Down Expand Up @@ -177,4 +179,27 @@ public function __set($propertyName, $propertyValue)

throw new PropertyNotFoundException($propertyName, get_class($this));
}

public function isEquals(SPIValue $value): bool
mateuszdebinski marked this conversation as resolved.
Show resolved Hide resolved
{
return $this->toHash($value) === $this->toHash($this);
mateuszdebinski marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @return array
*/
public function toHash(SPIValue $value)
{
return [
'id' => $value->id,
'path' => $value->inputUri ?: $value->id,
'alternativeText' => $value->alternativeText,
'fileName' => $value->fileName,
'fileSize' => $value->fileSize,
'uri' => $value->uri,
'inputUri' => $value->inputUri,
'width' => $value->width,
'height' => $value->height,
];
}
alongosz marked this conversation as resolved.
Show resolved Hide resolved
}
74 changes: 74 additions & 0 deletions eZ/Publish/Core/FieldType/Tests/Image/ValueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace eZ\Publish\Core\FieldType\Tests\Image;
mateuszdebinski marked this conversation as resolved.
Show resolved Hide resolved

use eZ\Publish\Core\FieldType\Image\Value as ImageValue;
use PHPUnit\Framework\TestCase;

/**
* @group fieldType
* @group ezfloat
*/
class ValueTest extends TestCase
{
public function getImageInputPath()
mateuszdebinski marked this conversation as resolved.
Show resolved Hide resolved
{
return __DIR__ . '/squirrel-developers.jpg';
}

/**
* @dataProvider provideInputForIsEqual
*/
public function testIsEquals(ImageValue $imageValue, ImageValue $correctValue, ImageValue $incorrectValue)
mateuszdebinski marked this conversation as resolved.
Show resolved Hide resolved
{
$this->assertTrue($imageValue->isEquals($correctValue));

$this->assertFalse($imageValue->isEquals($incorrectValue));
mateuszdebinski marked this conversation as resolved.
Show resolved Hide resolved
}

public function provideInputForIsEqual()
mateuszdebinski marked this conversation as resolved.
Show resolved Hide resolved
{
return [
[
new ImageValue([
'id' => $this->getImageInputPath(),
'fileName' => 'Sindelfingen-Squirrels.jpg',
'fileSize' => 23,
'alternativeText' => 'This is so Sindelfingen!',
'imageId' => '123-12345',
'uri' => 'http://' . $this->getImageInputPath(),
'width' => 123,
'height' => 456,
]),
new ImageValue([
'id' => $this->getImageInputPath(),
'path' => $this->getImageInputPath(),
'fileName' => 'Sindelfingen-Squirrels.jpg',
'fileSize' => 23,
'alternativeText' => 'This is so Sindelfingen!',
'imageId' => '123-12346',
'uri' => 'http://' . $this->getImageInputPath(),
'inputUri' => null,
'width' => 123,
'height' => 456,
]),
new ImageValue([
'id' => $this->getImageInputPath(),
'path' => $this->getImageInputPath(),
'fileName' => 'Sindelfingen-Squirrels.jpg',
'fileSize' => 23,
'alternativeText' => 'This is so Sindelfingen!',
'imageId' => '123-12345',
'uri' => 'http://' . $this->getImageInputPath(),
'inputUri' => null,
'width' => 124,
'height' => 456,
]),
],
];
}
}
17 changes: 16 additions & 1 deletion eZ/Publish/Core/Repository/ContentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use eZ\Publish\Core\Repository\Values\Content\Content;
use eZ\Publish\Core\Repository\Values\Content\Location;
use eZ\Publish\API\Repository\Values\Content\Language;
use eZ\Publish\SPI\FieldType\FieldType;
use eZ\Publish\SPI\FieldType\Value;
use eZ\Publish\SPI\Persistence\Handler;
use eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct as APIContentUpdateStruct;
use eZ\Publish\API\Repository\Values\ContentType\ContentType;
Expand Down Expand Up @@ -1722,7 +1724,7 @@ protected function copyTranslationsFromPublishedVersion(APIVersionInfo $versionI

if ($newValue !== null
&& $field->value !== null
&& $fieldType->toHash($newValue) === $fieldType->toHash($field->value)) {
&& $this->isHashEqual($fieldType, $newValue, $field->value)) {
mateuszdebinski marked this conversation as resolved.
Show resolved Hide resolved
continue;
}

Expand All @@ -1742,6 +1744,19 @@ protected function copyTranslationsFromPublishedVersion(APIVersionInfo $versionI
$this->internalUpdateContent($versionInfo, $updateStruct);
}

protected function isHashEqual(FieldType $fieldType, Value $newValue, Value $fieldValue): bool
{
$newHash = $fieldType->toHash($newValue);
$currentHash = $fieldType->toHash($fieldValue);
if ($newValue instanceof \eZ\Publish\SPI\FieldType\Comparable) {
return $newValue->isEquals($newHash, $currentHash);
} elseif ($newHash === $currentHash) {
return true;
}

return false;
}

/**
* Publishes a content version.
*
Expand Down
14 changes: 14 additions & 0 deletions eZ/Publish/SPI/FieldType/Comparable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace eZ\Publish\SPI\FieldType;

interface Comparable
{
public function isEquals(Value $value): bool;

public function toHash(Value $value);
mateuszdebinski marked this conversation as resolved.
Show resolved Hide resolved
}