View source
<?php
namespace Drupal\Tests\changed_fields\Unit;
use Drupal\changed_fields\Plugin\FieldComparator\DefaultFieldComparator;
use Drupal\field\Entity\FieldConfig;
use Drupal\Tests\UnitTestCase;
class DefaultFieldComparatorTest extends UnitTestCase {
private $fieldComparator;
private $fieldDefinitionMock;
public function setUp() {
$this->fieldComparator = new DefaultFieldComparator([], '', []);
$this->fieldDefinitionMock = $this
->getMockBuilder('Drupal\\field\\Entity\\FieldConfig')
->disableOriginalConstructor()
->getMock();
$this->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('string');
}
public function testFirstValueWasAdded() {
$this
->assertEquals([
'old_value' => [],
'new_value' => [
[
'value' => 'Text 1',
],
],
], $this->fieldComparator
->compareFieldValues($this->fieldDefinitionMock, [], [
[
'value' => 'Text 1',
],
]));
}
public function testLastValueWasDeleted() {
$this
->assertEquals([
'old_value' => [
[
'value' => 'Text 1',
],
],
'new_value' => [],
], $this->fieldComparator
->compareFieldValues($this->fieldDefinitionMock, [
[
'value' => 'Text 1',
],
], []));
}
public function testMultipleFieldValuesAddition() {
$this
->assertEquals([
'old_value' => [
[
'value' => 'Text 1',
],
],
'new_value' => [
[
'value' => 'Text 1',
],
[
'value' => 'Text 2',
],
],
], $this->fieldComparator
->compareFieldValues($this->fieldDefinitionMock, [
[
'value' => 'Text 1',
],
], [
[
'value' => 'Text 1',
],
[
'value' => 'Text 2',
],
]));
}
public function testMultipleFieldValuesDeletion() {
$this
->assertEquals([
'old_value' => [
[
'value' => 'Text 1',
],
[
'value' => 'Text 2',
],
],
'new_value' => [
[
'value' => 'Text 2',
],
],
], $this->fieldComparator
->compareFieldValues($this->fieldDefinitionMock, [
[
'value' => 'Text 1',
],
[
'value' => 'Text 2',
],
], [
[
'value' => 'Text 2',
],
]));
}
public function testMultipleFieldValuesOrdering() {
$this
->assertEquals([
'old_value' => [
[
'value' => 'Text 1',
],
[
'value' => 'Text 2',
],
],
'new_value' => [
[
'value' => 'Text 2',
],
[
'value' => 'Text 1',
],
],
], $this->fieldComparator
->compareFieldValues($this->fieldDefinitionMock, [
[
'value' => 'Text 1',
],
[
'value' => 'Text 2',
],
], [
[
'value' => 'Text 2',
],
[
'value' => 'Text 1',
],
]));
}
public function testSameValues() {
$this
->assertTrue($this->fieldComparator
->compareFieldValues($this->fieldDefinitionMock, [
[
'value' => 'Text 1',
],
[
'value' => 'Text 2',
],
], [
[
'value' => 'Text 1',
],
[
'value' => 'Text 2',
],
]));
}
public function testSameEmptyValues() {
$this
->assertTrue($this->fieldComparator
->compareFieldValues($this->fieldDefinitionMock, [], []));
}
public function testGetDefaultComparablePropertiesMethodNeverCalled() {
$fieldComparatorMock = $this
->getMockBuilder('\\Drupal\\changed_fields\\Plugin\\FieldComparator\\DefaultFieldComparator')
->setMethods([
'getDefaultComparableProperties',
])
->disableOriginalConstructor()
->getMock();
$fieldComparatorMock
->expects($this
->never())
->method('getDefaultComparableProperties');
$fieldComparatorMock
->compareFieldValues($this->fieldDefinitionMock, [], []);
}
public function testExtendComparablePropertiesMethodCalled() {
$fieldComparatorMock = $this
->getMockBuilder('\\Drupal\\changed_fields\\Plugin\\FieldComparator\\DefaultFieldComparator')
->setMethods([
'extendComparableProperties',
])
->disableOriginalConstructor()
->getMock();
$fieldComparatorMock
->expects($this
->once())
->method('extendComparableProperties');
$fieldComparatorMock
->compareFieldValues($this->fieldDefinitionMock, [], []);
}
}