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 17 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
8 changes: 7 additions & 1 deletion eZ/Publish/Core/FieldType/FieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
namespace eZ\Publish\Core\FieldType;

use eZ\Publish\SPI\FieldType\Comparable;
use eZ\Publish\SPI\FieldType\FieldType as FieldTypeInterface;
use eZ\Publish\Core\Persistence\TransformationProcessor;
use eZ\Publish\SPI\FieldType\Value as SPIValue;
Expand All @@ -30,7 +31,7 @@
* Field types are primed and pre-configured with the Field Definitions found in
* Content Types.
*/
abstract class FieldType implements FieldTypeInterface
abstract class FieldType implements FieldTypeInterface, Comparable
{
/**
* The setting keys which are available on this field type.
Expand Down Expand Up @@ -575,4 +576,9 @@ public function getRelations(SPIValue $fieldValue)
{
return [];
}

public function valuesEqual(SPIValue $value1, SPIValue $value2): bool
{
return $this->toHash($value1) === $this->toHash($value2);
}
}
10 changes: 10 additions & 0 deletions eZ/Publish/Core/FieldType/Image/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,4 +365,14 @@ public function fromPersistenceValue(FieldValue $fieldValue)

return $result;
}

public function valuesEqual(SPIValue $value1, SPIValue $value2): bool
{
$hashValue1 = $this->toHash($value1);
$hashValue2 = $this->toHash($value2);

unset($hashValue1['imageId'], $hashValue2['imageId']);

return $hashValue1 === $hashValue2;
}
}
20 changes: 20 additions & 0 deletions eZ/Publish/Core/FieldType/Tests/FieldTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,11 @@ public function provideInvalidDataForValidate()
];
}

public function provideInputForValuesEqual(): array
{
return $this->provideInputForFromHash();
}

/**
* Retrieves a test wide cached version of the field type under test.
*
Expand Down Expand Up @@ -974,5 +979,20 @@ protected function doValidate($fieldDefinitionData, $value)
return $validationErrors;
}

/**
* @dataProvider provideInputForValuesEqual
*/
public function testValuesEqual($inputValue1, SPIValue $inputValue2): void
alongosz marked this conversation as resolved.
Show resolved Hide resolved
{
$fieldType = $this->getFieldTypeUnderTest();

$inputValue1 = $fieldType->fromHash($inputValue1);

self::assertTrue(
$fieldType->valuesEqual($inputValue1, $inputValue2),
'valuesEqual() method did not create expected result.'
);
}

// @todo: More test methods …
}
35 changes: 35 additions & 0 deletions eZ/Publish/Core/FieldType/Tests/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -811,4 +811,39 @@ public function provideInvalidDataForValidate()
],
];
}

/**
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
*/
public function provideInputForValuesEqual(): array
{
return [
[
[
'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(
mateuszdebinski marked this conversation as resolved.
Show resolved Hide resolved
[
'id' => $this->getImageInputPath(),
'path' => $this->getImageInputPath(),
'fileName' => 'Sindelfingen-Squirrels.jpg',
'fileSize' => 23,
'alternativeText' => 'This is so Sindelfingen!',
'imageId' => '123-12317',
'uri' => 'http://' . $this->getImageInputPath(),
'inputUri' => null,
'width' => 123,
'height' => 456,
]
),
],
];
}
}
26 changes: 25 additions & 1 deletion eZ/Publish/Core/Repository/ContentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
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\Comparable;
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 @@ -1723,7 +1726,8 @@ protected function copyTranslationsFromPublishedVersion(APIVersionInfo $versionI

if ($newValue !== null
&& $field->value !== null
&& $fieldType->toHash($newValue) === $fieldType->toHash($field->value)) {
&& $this->fieldValuesAreEqual($fieldType, $newValue, $field->value)
) {
continue;
}

Expand All @@ -1743,6 +1747,26 @@ protected function copyTranslationsFromPublishedVersion(APIVersionInfo $versionI
$this->internalUpdateContent($versionInfo, $updateStruct);
}

protected function fieldValuesAreEqual(FieldType $fieldType, Value $value1, Value $value2): bool
alongosz marked this conversation as resolved.
Show resolved Hide resolved
{
if ($fieldType instanceof Comparable) {
return $fieldType->valuesEqual($value1, $value2);
} else {
@trigger_error(
\sprintf(
'In eZ Platform 2.5 and 3.x %s should implement %s. ' .
'Since the 4.0 major release FieldType\Comparable contract will be a part of %s',
get_class($fieldType),
Comparable::class,
FieldType::class
),
E_USER_DEPRECATED
);

return $fieldType->toHash($value1) === $fieldType->toHash($value2);
}
}

/**
* 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.
*/
declare(strict_types=1);

namespace eZ\Publish\SPI\FieldType;

interface Comparable
{
public function valuesEqual(Value $value1, Value $value2): bool;
}