You are here

extended_field_comparator.test in Changed Fields API 7.2

Same filename and directory in other branches
  1. 7.3 tests/extended_field_comparator.test

Test extended field comparator.

Check if API works correctly with custom/contrib field types.

File

tests/extended_field_comparator.test
View 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 array(
      'name' => 'Extended field comparator test',
      'description' => 'Test extended field comparator',
      'group' => 'Changed fields API',
    );
  }

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp(array(
      'changed_fields_custom_field',
    ));

    // Create additional field for article content type.
    $field = array(
      'field_name' => 'custom_field',
      'type' => 'changed_fields_custom_field',
    );
    $instance = array(
      '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(array(
      'type' => 'article',
      'promote' => 1,
      'title' => 'Title',
      'body' => array(
        LANGUAGE_NONE => array(
          array(
            'value' => 'Body',
            'format' => 'plain_text',
            'summary' => 'Summary',
          ),
        ),
      ),
      'custom_field' => array(
        LANGUAGE_NONE => array(
          array(
            '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(array(
      'type' => 'article',
      'promote' => 1,
      'title' => 'Title',
      'body' => array(
        LANGUAGE_NONE => array(
          array(
            'value' => 'Body',
            'format' => 'plain_text',
            'summary' => 'Summary',
          ),
        ),
      ),
      'custom_field' => array(
        LANGUAGE_NONE => array(
          array(
            '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

Namesort descending Description
ExtendedFieldComparatorWebTestCase Class CoreFieldsWebTestCase.