class DefaultFieldComparatorTest in Changed Fields API 8.3
Same name and namespace in other branches
- 8.2 tests/src/Unit/DefaultFieldComparatorTest.php \Drupal\Tests\changed_fields\Unit\DefaultFieldComparatorTest
@coversDefaultClass \Drupal\changed_fields\Plugin\FieldComparator\DefaultFieldComparator
@group changed_fields
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\changed_fields\Unit\DefaultFieldComparatorTest
Expanded class hierarchy of DefaultFieldComparatorTest
File
- tests/
src/ Unit/ DefaultFieldComparatorTest.php, line 13
Namespace
Drupal\Tests\changed_fields\UnitView source
class DefaultFieldComparatorTest extends UnitTestCase {
/**
* Field comparator plugin instance.
*
* @var Drupal\changed_fields\Plugin\FieldComparator\DefaultFieldComparator
*/
private $fieldComparator;
/**
* Mocked instance of field config.
*
* @var Drupal\field\Entity\FieldConfig
*/
private $fieldDefinitionMock;
/**
* Call protected/private method of a class.
*
* @param object &$object
* Instantiated object that we will run method on.
* @param string $methodName
* Method name to call.
* @param array $parameters
* Array of parameters to pass into method.
*
* @return mixed
* Method return.
*/
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);
}
/**
* {@inheritdoc}
*/
public function setUp() {
$this->fieldComparator = new DefaultFieldComparator([], '', []);
$this->fieldDefinitionMock = $this
->getMockBuilder('Drupal\\field\\Entity\\FieldConfig')
->disableOriginalConstructor()
->setMethods([
'getType',
'getSetting',
])
->getMock();
}
/**
* Default field comparator: string field's property(s).
*/
public function testStringFieldProperties() {
$this->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('string');
$properties = $this
->invokeMethod($this->fieldComparator, 'getComparableProperties', [
$this->fieldDefinitionMock,
]);
$this
->assertArrayEquals([
'value',
], $properties);
}
/**
* Default field comparator: string_long field's property(s).
*/
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);
}
/**
* Default field comparator: text field's property(s).
*/
public function testTextFieldProperties() {
$this->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('text');
$properties = $this
->invokeMethod($this->fieldComparator, 'getComparableProperties', [
$this->fieldDefinitionMock,
]);
$this
->assertArrayEquals([
'value',
], $properties);
}
/**
* Default field comparator: text_long field's property(s).
*/
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);
}
/**
* Default field comparator: boolean field's property(s).
*/
public function testBooleanFieldProperties() {
$this->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('boolean');
$properties = $this
->invokeMethod($this->fieldComparator, 'getComparableProperties', [
$this->fieldDefinitionMock,
]);
$this
->assertArrayEquals([
'value',
], $properties);
}
/**
* Default field comparator: integer field's property(s).
*/
public function testIntegerFieldProperties() {
$this->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('integer');
$properties = $this
->invokeMethod($this->fieldComparator, 'getComparableProperties', [
$this->fieldDefinitionMock,
]);
$this
->assertArrayEquals([
'value',
], $properties);
}
/**
* Default field comparator: float field's property(s).
*/
public function testFloatFieldProperties() {
$this->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('float');
$properties = $this
->invokeMethod($this->fieldComparator, 'getComparableProperties', [
$this->fieldDefinitionMock,
]);
$this
->assertArrayEquals([
'value',
], $properties);
}
/**
* Default field comparator: decimal field's property(s).
*/
public function testDecimalFieldProperties() {
$this->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('decimal');
$properties = $this
->invokeMethod($this->fieldComparator, 'getComparableProperties', [
$this->fieldDefinitionMock,
]);
$this
->assertArrayEquals([
'value',
], $properties);
}
/**
* Default field comparator: datetime field's property(s).
*/
public function testDateTimeFieldProperties() {
$this->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('datetime');
$properties = $this
->invokeMethod($this->fieldComparator, 'getComparableProperties', [
$this->fieldDefinitionMock,
]);
$this
->assertArrayEquals([
'value',
], $properties);
}
/**
* Default field comparator: date range field's property(s).
*/
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);
}
/**
* Default field comparator: email field's property(s).
*/
public function testEmailFieldProperties() {
$this->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('email');
$properties = $this
->invokeMethod($this->fieldComparator, 'getComparableProperties', [
$this->fieldDefinitionMock,
]);
$this
->assertArrayEquals([
'value',
], $properties);
}
/**
* Default field comparator: telephone field's property(s).
*/
public function testTelephoneFieldProperties() {
$this->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('telephone');
$properties = $this
->invokeMethod($this->fieldComparator, 'getComparableProperties', [
$this->fieldDefinitionMock,
]);
$this
->assertArrayEquals([
'value',
], $properties);
}
/**
* Default field comparator: list_integer field's property(s).
*/
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);
}
/**
* Default field comparator: list_float field's property(s).
*/
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);
}
/**
* Default field comparator: list_string field's property(s).
*/
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);
}
/**
* Default field comparator: text_with_summary field's property(s).
*/
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);
}
/**
* Default field comparator: entity_reference field's property(s).
*/
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);
}
/**
* Default field comparator: link field's property(s).
*/
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);
}
/**
* Default field comparator: file field's property(s) (without description).
*/
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);
}
/**
* Default field comparator: file field's property(s) (with description).
*/
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);
}
/**
* Default field comparator: image field's property(s).
*
* Test image field properties without alt and title.
*/
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);
}
/**
* Default field comparator: image field's property(s)
*
* Test image field properties with alt but without title.
*/
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);
}
/**
* Default field comparator: image field's property(s).
*
* Test image field properties without alt but with title.
*/
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);
}
/**
* Default field comparator: image field's property(s) (with alt and title).
*/
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);
}
/**
* Default field comparator: unknown field's property(s).
*/
public function testUnknownFieldProperties() {
$this->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('unknown');
$properties = $this
->invokeMethod($this->fieldComparator, 'getComparableProperties', [
$this->fieldDefinitionMock,
]);
$this
->assertArrayEquals([], $properties);
}
/**
* Check comparison: first value was added.
*/
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',
],
]));
}
/**
* Check comparison: last value was deleted.
*/
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',
],
], []));
}
/**
* Check comparison of multi-value fields - add a field value.
*/
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',
],
]));
}
/**
* Check comparison of multi-value fields - delete a field value.
*/
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',
],
]));
}
/**
* Check comparison of multi-value fields - ordering.
*/
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',
],
]));
}
/**
* If values haven't changed - result should be TRUE.
*/
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',
],
]));
}
/**
* If values haven't changed - result should be TRUE.
*/
public function testSameEmptyValues() {
$this->fieldDefinitionMock
->expects($this
->once())
->method('getType')
->willReturn('string');
$this
->assertTrue($this->fieldComparator
->compareFieldValues($this->fieldDefinitionMock, [], []));
}
/**
* Check if getDefaultComparableProperties is never called.
*/
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, [], []);
}
/**
* Check if extendComparableProperties is called.
*/
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, [], []);
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DefaultFieldComparatorTest:: |
private | property | Field comparator plugin instance. | |
DefaultFieldComparatorTest:: |
private | property | Mocked instance of field config. | |
DefaultFieldComparatorTest:: |
public | function | Call protected/private method of a class. | |
DefaultFieldComparatorTest:: |
public | function |
Overrides UnitTestCase:: |
|
DefaultFieldComparatorTest:: |
public | function | Default field comparator: boolean field's property(s). | |
DefaultFieldComparatorTest:: |
public | function | Default field comparator: date range field's property(s). | |
DefaultFieldComparatorTest:: |
public | function | Default field comparator: datetime field's property(s). | |
DefaultFieldComparatorTest:: |
public | function | Default field comparator: decimal field's property(s). | |
DefaultFieldComparatorTest:: |
public | function | Default field comparator: email field's property(s). | |
DefaultFieldComparatorTest:: |
public | function | Default field comparator: entity_reference field's property(s). | |
DefaultFieldComparatorTest:: |
public | function | Check if extendComparableProperties is called. | |
DefaultFieldComparatorTest:: |
public | function | Default field comparator: file field's property(s) (with description). | |
DefaultFieldComparatorTest:: |
public | function | Default field comparator: file field's property(s) (without description). | |
DefaultFieldComparatorTest:: |
public | function | Check comparison: first value was added. | |
DefaultFieldComparatorTest:: |
public | function | Default field comparator: float field's property(s). | |
DefaultFieldComparatorTest:: |
public | function | Check if getDefaultComparableProperties is never called. | |
DefaultFieldComparatorTest:: |
public | function | Default field comparator: image field's property(s) (with alt and title). | |
DefaultFieldComparatorTest:: |
public | function | Default field comparator: image field's property(s) | |
DefaultFieldComparatorTest:: |
public | function | Default field comparator: image field's property(s). | |
DefaultFieldComparatorTest:: |
public | function | Default field comparator: image field's property(s). | |
DefaultFieldComparatorTest:: |
public | function | Default field comparator: integer field's property(s). | |
DefaultFieldComparatorTest:: |
public | function | Check comparison: last value was deleted. | |
DefaultFieldComparatorTest:: |
public | function | Default field comparator: link field's property(s). | |
DefaultFieldComparatorTest:: |
public | function | Default field comparator: list_float field's property(s). | |
DefaultFieldComparatorTest:: |
public | function | Default field comparator: list_integer field's property(s). | |
DefaultFieldComparatorTest:: |
public | function | Default field comparator: list_string field's property(s). | |
DefaultFieldComparatorTest:: |
public | function | Check comparison of multi-value fields - add a field value. | |
DefaultFieldComparatorTest:: |
public | function | Check comparison of multi-value fields - delete a field value. | |
DefaultFieldComparatorTest:: |
public | function | Check comparison of multi-value fields - ordering. | |
DefaultFieldComparatorTest:: |
public | function | If values haven't changed - result should be TRUE. | |
DefaultFieldComparatorTest:: |
public | function | If values haven't changed - result should be TRUE. | |
DefaultFieldComparatorTest:: |
public | function | Default field comparator: string field's property(s). | |
DefaultFieldComparatorTest:: |
public | function | Default field comparator: string_long field's property(s). | |
DefaultFieldComparatorTest:: |
public | function | Default field comparator: telephone field's property(s). | |
DefaultFieldComparatorTest:: |
public | function | Default field comparator: text field's property(s). | |
DefaultFieldComparatorTest:: |
public | function | Default field comparator: text_long field's property(s). | |
DefaultFieldComparatorTest:: |
public | function | Default field comparator: text_with_summary field's property(s). | |
DefaultFieldComparatorTest:: |
public | function | Default field comparator: unknown field's property(s). | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |