extended_field_comparator.test in Changed Fields API 7.3
Same filename and directory in other branches
Test extended field comparator.
Check if API works correctly with custom/contrib field types.
File
tests/extended_field_comparator.testView source
<?php
/**
* @file
* Test extended field comparator.
*
* Check if API works correctly with custom/contrib field types.
*/
/**
* Class CoreFieldsWebTestCase.
*/
class ExtendedFieldComparatorWebTestCase extends DrupalWebTestCase {
/**
* {@inheritdoc}
*/
public static function getInfo() {
return [
'name' => 'Extended field comparator test',
'description' => 'Test extended field comparator',
'group' => 'Changed fields API',
];
}
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp([
'changed_fields_custom_field',
]);
// Create additional field for article content type.
$field = [
'field_name' => 'custom_field',
'type' => 'changed_fields_custom_field',
];
$instance = [
'field_name' => 'custom_field',
'entity_type' => 'node',
'label' => 'Custom field',
'bundle' => 'article',
];
field_create_field($field);
field_create_instance($instance);
}
/**
* Custom field comparison.
*/
public function testGetDefaultComparablePropertiesMethod() {
// Create article with fields.
$node = $this
->drupalCreateNode([
'type' => 'article',
'promote' => 1,
'title' => 'Title',
'body' => [
LANGUAGE_NONE => [
[
'value' => 'Body',
'format' => 'plain_text',
'summary' => 'Summary',
],
],
],
'custom_field' => [
LANGUAGE_NONE => [
[
'value_1' => 'Value 1',
'value_2' => 'Value 2',
],
],
],
]);
// Change node custom_field values.
$node->custom_field[LANGUAGE_NONE][0]['value_1'] .= ' changed';
node_save($node);
// Let's ensure that only custom_field field was changed.
$is_custom_field_updated = !empty($node->changed_fields) && count($node->changed_fields) == 1 && !empty($node->changed_fields['custom_field']);
$this
->assertEqual($is_custom_field_updated, TRUE, 'Only custom_field field was changed after node updating.');
if ($is_custom_field_updated) {
// Check changed custom_field value_1.
$this
->assertEqual($node->changed_fields['custom_field']['old_value'][0]['value_1'], 'Value 1', 'Old custom_field value_1 is "Value 1".');
$this
->assertEqual($node->changed_fields['custom_field']['new_value'][0]['value_1'], 'Value 1 changed', 'New custom_field value_1 "Value 1 changed".');
}
}
/**
* Add property for custom field comparison.
*/
public function testExtendComparablePropertiesMethod() {
// Create article with fields.
$node = $this
->drupalCreateNode([
'type' => 'article',
'promote' => 1,
'title' => 'Title',
'body' => [
LANGUAGE_NONE => [
[
'value' => 'Body',
'format' => 'plain_text',
'summary' => 'Summary',
],
],
],
'custom_field' => [
LANGUAGE_NONE => [
[
'value_1' => 'Value 1',
'value_2' => 'Value 2',
],
],
],
]);
// Change node custom_field values.
$node->custom_field[LANGUAGE_NONE][0]['value_2'] .= ' changed';
node_save($node);
// Let's ensure that only custom_field field was changed.
$is_custom_field_updated = !empty($node->changed_fields) && count($node->changed_fields) == 1 && !empty($node->changed_fields['custom_field']);
$this
->assertEqual($is_custom_field_updated, TRUE, 'Only custom_field field was changed after node updating.');
if ($is_custom_field_updated) {
// Check changed custom_field value_1.
$this
->assertEqual($node->changed_fields['custom_field']['old_value'][0]['value_2'], 'Value 2', 'Old custom_field value_2 is "Value 2".');
$this
->assertEqual($node->changed_fields['custom_field']['new_value'][0]['value_2'], 'Value 2 changed', 'New custom_field value_2 "Value 2 changed".');
}
}
}
Classes
Name | Description |
---|---|
ExtendedFieldComparatorWebTestCase | Class CoreFieldsWebTestCase. |