View source
<?php
namespace Drupal\Tests\changed_fields\Unit;
use Drupal\changed_fields\Plugin\FieldComparator\DefaultFieldComparator;
use Drupal\Tests\UnitTestCase;
class DefaultFieldComparatorTest extends UnitTestCase {
private $fieldComparator;
private $fieldDefinitionMock;
public function invokeMethod(&$object, $methodName, array $parameters = []) {
$reflection = new \ReflectionClass(get_class($object));
$method = $reflection
->getMethod($methodName);
$method
->setAccessible(TRUE);
return $method
->invokeArgs($object, $parameters);
}
public function setUp() {
$this->fieldComparator = new DefaultFieldComparator([], '', []);
$this->fieldDefinitionMock = $this
->getMockBuilder('Drupal\\field\\Entity\\FieldConfig')
->disableOriginalConstructor()
->setMethods([
'getType',
'getSetting',
])
->getMock();
}
public function testStringFieldProperties() {
$this->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('string');
$properties = $this
->invokeMethod($this->fieldComparator, 'getComparableProperties', [
$this->fieldDefinitionMock,
]);
$this
->assertArrayEquals([
'value',
], $properties);
}
public function testStringLongFieldProperties() {
$this->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('string_long');
$properties = $this
->invokeMethod($this->fieldComparator, 'getComparableProperties', [
$this->fieldDefinitionMock,
]);
$this
->assertArrayEquals([
'value',
], $properties);
}
public function testTextFieldProperties() {
$this->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('text');
$properties = $this
->invokeMethod($this->fieldComparator, 'getComparableProperties', [
$this->fieldDefinitionMock,
]);
$this
->assertArrayEquals([
'value',
], $properties);
}
public function testTextLongFieldProperties() {
$this->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('text_long');
$properties = $this
->invokeMethod($this->fieldComparator, 'getComparableProperties', [
$this->fieldDefinitionMock,
]);
$this
->assertArrayEquals([
'value',
], $properties);
}
public function testBooleanFieldProperties() {
$this->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('boolean');
$properties = $this
->invokeMethod($this->fieldComparator, 'getComparableProperties', [
$this->fieldDefinitionMock,
]);
$this
->assertArrayEquals([
'value',
], $properties);
}
public function testIntegerFieldProperties() {
$this->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('integer');
$properties = $this
->invokeMethod($this->fieldComparator, 'getComparableProperties', [
$this->fieldDefinitionMock,
]);
$this
->assertArrayEquals([
'value',
], $properties);
}
public function testFloatFieldProperties() {
$this->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('float');
$properties = $this
->invokeMethod($this->fieldComparator, 'getComparableProperties', [
$this->fieldDefinitionMock,
]);
$this
->assertArrayEquals([
'value',
], $properties);
}
public function testDecimalFieldProperties() {
$this->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('decimal');
$properties = $this
->invokeMethod($this->fieldComparator, 'getComparableProperties', [
$this->fieldDefinitionMock,
]);
$this
->assertArrayEquals([
'value',
], $properties);
}
public function testDateTimeFieldProperties() {
$this->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('datetime');
$properties = $this
->invokeMethod($this->fieldComparator, 'getComparableProperties', [
$this->fieldDefinitionMock,
]);
$this
->assertArrayEquals([
'value',
], $properties);
}
public function testDateRangeFieldProperties() {
$this->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('daterange');
$properties = $this
->invokeMethod($this->fieldComparator, 'getComparableProperties', [
$this->fieldDefinitionMock,
]);
$this
->assertArrayEquals([
'value',
'end_value',
], $properties);
}
public function testEmailFieldProperties() {
$this->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('email');
$properties = $this
->invokeMethod($this->fieldComparator, 'getComparableProperties', [
$this->fieldDefinitionMock,
]);
$this
->assertArrayEquals([
'value',
], $properties);
}
public function testTelephoneFieldProperties() {
$this->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('telephone');
$properties = $this
->invokeMethod($this->fieldComparator, 'getComparableProperties', [
$this->fieldDefinitionMock,
]);
$this
->assertArrayEquals([
'value',
], $properties);
}
public function testListIntegerFieldProperties() {
$this->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('list_integer');
$properties = $this
->invokeMethod($this->fieldComparator, 'getComparableProperties', [
$this->fieldDefinitionMock,
]);
$this
->assertArrayEquals([
'value',
], $properties);
}
public function testListFloatFieldProperties() {
$this->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('list_float');
$properties = $this
->invokeMethod($this->fieldComparator, 'getComparableProperties', [
$this->fieldDefinitionMock,
]);
$this
->assertArrayEquals([
'value',
], $properties);
}
public function testListStringFieldProperties() {
$this->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('list_string');
$properties = $this
->invokeMethod($this->fieldComparator, 'getComparableProperties', [
$this->fieldDefinitionMock,
]);
$this
->assertArrayEquals([
'value',
], $properties);
}
public function testTextWithSummaryFieldProperties() {
$this->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('text_with_summary');
$properties = $this
->invokeMethod($this->fieldComparator, 'getComparableProperties', [
$this->fieldDefinitionMock,
]);
$this
->assertArrayEquals([
'value',
'summary',
], $properties);
}
public function testEntityReferenceFieldProperties() {
$this->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('entity_reference');
$properties = $this
->invokeMethod($this->fieldComparator, 'getComparableProperties', [
$this->fieldDefinitionMock,
]);
$this
->assertArrayEquals([
'target_id',
], $properties);
}
public function testLinkFieldProperties() {
$this->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('link');
$properties = $this
->invokeMethod($this->fieldComparator, 'getComparableProperties', [
$this->fieldDefinitionMock,
]);
$this
->assertArrayEquals([
'uri',
'title',
], $properties);
}
public function testFileFieldWithoutDescriptionProperties() {
$this->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('file');
$this->fieldDefinitionMock
->expects($this
->once())
->method('getSetting')
->with('description_field')
->willReturn(FALSE);
$properties = $this
->invokeMethod($this->fieldComparator, 'getComparableProperties', [
$this->fieldDefinitionMock,
]);
$this
->assertArrayEquals([
'target_id',
], $properties);
}
public function testFileFieldWithDescriptionProperties() {
$this->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('file');
$this->fieldDefinitionMock
->expects($this
->once())
->method('getSetting')
->with('description_field')
->willReturn(TRUE);
$properties = $this
->invokeMethod($this->fieldComparator, 'getComparableProperties', [
$this->fieldDefinitionMock,
]);
$this
->assertArrayEquals([
'target_id',
'description',
], $properties);
}
public function testImageWithoutAltAndTitleFieldProperties() {
$this->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('image');
$this->fieldDefinitionMock
->expects($this
->any())
->method('getSetting')
->willReturn(FALSE);
$properties = $this
->invokeMethod($this->fieldComparator, 'getComparableProperties', [
$this->fieldDefinitionMock,
]);
$this
->assertArrayEquals([
'width',
'height',
'target_id',
], $properties);
}
public function testImageWithAltWithoutTitleFieldProperties() {
$this->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('image');
$this->fieldDefinitionMock
->expects($this
->at(1))
->method('getSetting')
->with('alt_field')
->willReturn(TRUE);
$properties = $this
->invokeMethod($this->fieldComparator, 'getComparableProperties', [
$this->fieldDefinitionMock,
]);
$this
->assertArrayEquals([
'width',
'height',
'target_id',
'alt',
], $properties);
}
public function testImageWithoutAltWithTitleFieldProperties() {
$this->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('image');
$this->fieldDefinitionMock
->expects($this
->at(2))
->method('getSetting')
->with('title_field')
->willReturn(TRUE);
$properties = $this
->invokeMethod($this->fieldComparator, 'getComparableProperties', [
$this->fieldDefinitionMock,
]);
$this
->assertArrayEquals([
'width',
'height',
'target_id',
'title',
], $properties);
}
public function testImageWithAltAndTitleFieldProperties() {
$this->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('image');
$this->fieldDefinitionMock
->expects($this
->any())
->method('getSetting')
->willReturn(TRUE);
$properties = $this
->invokeMethod($this->fieldComparator, 'getComparableProperties', [
$this->fieldDefinitionMock,
]);
$this
->assertArrayEquals([
'width',
'height',
'target_id',
'alt',
'title',
], $properties);
}
public function testUnknownFieldProperties() {
$this->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('unknown');
$properties = $this
->invokeMethod($this->fieldComparator, 'getComparableProperties', [
$this->fieldDefinitionMock,
]);
$this
->assertArrayEquals([], $properties);
}
public function testFirstValueWasAdded() {
$this->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('string');
$this
->assertArrayEquals([
'old_value' => [],
'new_value' => [
[
'value' => 'Text 1',
],
],
], $this->fieldComparator
->compareFieldValues($this->fieldDefinitionMock, [], [
[
'value' => 'Text 1',
],
]));
}
public function testLastValueWasDeleted() {
$this->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('string');
$this
->assertArrayEquals([
'old_value' => [
[
'value' => 'Text 1',
],
],
'new_value' => [],
], $this->fieldComparator
->compareFieldValues($this->fieldDefinitionMock, [
[
'value' => 'Text 1',
],
], []));
}
public function testMultipleFieldValuesAddition() {
$this->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('string');
$this
->assertArrayEquals([
'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->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('string');
$this
->assertArrayEquals([
'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->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('string');
$this
->assertArrayEquals([
'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->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('string');
$this
->assertTrue($this->fieldComparator
->compareFieldValues($this->fieldDefinitionMock, [
[
'value' => 'Text 1',
],
[
'value' => 'Text 2',
],
], [
[
'value' => 'Text 1',
],
[
'value' => 'Text 2',
],
]));
}
public function testSameEmptyValues() {
$this->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('string');
$this
->assertTrue($this->fieldComparator
->compareFieldValues($this->fieldDefinitionMock, [], []));
}
public function testGetDefaultComparablePropertiesMethodNeverCalled() {
$this->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('string');
$field_comparator_mock = $this
->getMockBuilder('\\Drupal\\changed_fields\\Plugin\\FieldComparator\\DefaultFieldComparator')
->setMethods([
'getDefaultComparableProperties',
])
->disableOriginalConstructor()
->getMock();
$field_comparator_mock
->expects($this
->never())
->method('getDefaultComparableProperties');
$field_comparator_mock
->compareFieldValues($this->fieldDefinitionMock, [], []);
}
public function testExtendComparablePropertiesMethodCalled() {
$this->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('string');
$field_comparator_mock = $this
->getMockBuilder('\\Drupal\\changed_fields\\Plugin\\FieldComparator\\DefaultFieldComparator')
->setMethods([
'extendComparableProperties',
])
->disableOriginalConstructor()
->getMock();
$field_comparator_mock
->expects($this
->once())
->method('extendComparableProperties');
$field_comparator_mock
->compareFieldValues($this->fieldDefinitionMock, [], []);
}
}