You are here

default_field_comparator.test in Changed Fields API 7.3

Same filename and directory in other branches
  1. 7.2 tests/default_field_comparator.test

Test default field comparator.

Check if API works correctly with Drupal core field types.

File

tests/default_field_comparator.test
View source
<?php

/**
 * @file
 * Test default field comparator.
 *
 * Check if API works correctly with Drupal core field types.
 */

/**
 * Class DefaultFieldComparatorWebTestCase.
 */
class DefaultFieldComparatorWebTestCase extends DrupalWebTestCase {

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return [
      'name' => 'Default field comparator test',
      'description' => 'Test default field comparator',
      'group' => 'Changed fields API',
    ];
  }

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp([
      'changed_fields_basic_usage',
    ]);

    // Create additional fields for article content type.
    $fields = [
      'text' => [
        'base' => [
          'field_name' => 'field_text_test',
          'type' => 'text',
          'cardinality' => -1,
        ],
        'instance' => [
          'field_name' => 'field_text_test',
          'entity_type' => 'node',
          'label' => 'Text field',
          'bundle' => 'article',
        ],
      ],
      'file' => [
        'base' => [
          'field_name' => 'field_file_test',
          'type' => 'file',
        ],
        'instance' => [
          'field_name' => 'field_file_test',
          'entity_type' => 'node',
          'label' => 'File',
          'bundle' => 'article',
          'settings' => [
            'description_field' => TRUE,
            'display_field' => TRUE,
          ],
        ],
      ],
      'image' => [
        'base' => [
          'field_name' => 'field_image_test',
          'type' => 'image',
        ],
        'instance' => [
          'field_name' => 'field_image_test',
          'entity_type' => 'node',
          'label' => 'Image',
          'bundle' => 'article',
          'settings' => [
            'alt_field' => TRUE,
            'title_field' => TRUE,
          ],
        ],
      ],
    ];
    foreach ($fields as $field) {
      field_create_field($field['base']);
      field_create_instance($field['instance']);
    }
  }

  /**
   * Helper method for file creation.
   *
   * @param string $file_path
   *   File path.
   *
   * @return mixed
   *   Object if file was created FALSE otherwise.b
   */
  private function createFile($file_path) {
    $file = (object) [
      'uid' => 1,
      'uri' => $file_path,
      'filemime' => file_get_mimetype($file_path),
      'status' => 1,
      'display' => 1,
    ];
    return (array) file_copy($file, 'public://');
  }

  /**
   * Check if API works correctly on node creation event.
   *
   * API should not fire CFObserverInterface::update() after node creation.
   */
  public function testNodeCreation() {
    $title = 'Title';
    $settings = [
      'type' => 'article',
      'promote' => 1,
      'title' => $title,
      'body' => [
        LANGUAGE_NONE => [
          [
            'value' => 'Body',
            'format' => filter_default_format(),
            'summary' => '',
          ],
        ],
      ],
    ];
    $node = $this
      ->drupalCreateNode($settings);
    $this
      ->assertEqual(!empty($node->changed_fields), FALSE, 'Fields were not changed after node creation.');
  }

  /**
   * Check comparison: first value was added.
   */
  public function testFirstValueWasAdded() {
    $node = $this
      ->drupalCreateNode([
      'type' => 'article',
      'promote' => 1,
      'title' => 'Title',
    ]);
    $node->field_text_test = [
      LANGUAGE_NONE => [
        [
          'value' => 'Text 1',
        ],
      ],
    ];
    node_save($node);

    // Let's ensure that only field_text_test field was changed.
    $is_field_text_test_updated = !empty($node->changed_fields) && count($node->changed_fields) == 1 && !empty($node->changed_fields['field_text_test']);
    $this
      ->assertEqual($is_field_text_test_updated, TRUE, 'Only field_text_test field was changed after node updating.');
    if ($is_field_text_test_updated) {
      $this
        ->assertEqual($node->changed_fields['field_text_test']['old_value'], FALSE, 'Old field_text_test value is empty.');
      $this
        ->assertEqual($node->changed_fields['field_text_test']['new_value'][0]['value'], 'Text 1', 'New field_text_test value is "Text 1".');
    }
  }

  /**
   * Check comparison: last value was deleted.
   */
  public function testLastValueWasDeleted() {
    $node = $this
      ->drupalCreateNode([
      'type' => 'article',
      'promote' => 1,
      'title' => 'Title',
      'field_text_test' => [
        LANGUAGE_NONE => [
          [
            'value' => 'Text 1',
          ],
        ],
      ],
    ]);
    unset($node->field_text_test[LANGUAGE_NONE][0]);
    node_save($node);

    // Let's ensure that only field_text_test field was changed.
    $is_field_text_test_updated = !empty($node->changed_fields) && count($node->changed_fields) == 1 && !empty($node->changed_fields['field_text_test']);
    $this
      ->assertEqual($is_field_text_test_updated, TRUE, 'Only field_text_test field was changed after node updating.');
    if ($is_field_text_test_updated) {
      $this
        ->assertEqual($node->changed_fields['field_text_test']['old_value'][0]['value'], 'Text 1', 'Old field_text_test value is "Text 1".');
      $this
        ->assertEqual($node->changed_fields['field_text_test']['new_value'], FALSE, 'New field_text_test value is empty.');
    }
  }

  /**
   * Check comparison of multi-value fields - add a field value.
   */
  public function testMultipleFieldValuesAddition() {
    $node = $this
      ->drupalCreateNode([
      'type' => 'article',
      'promote' => 1,
      'title' => 'Title',
      'body' => [
        LANGUAGE_NONE => [
          [
            'value' => 'Body',
            'format' => filter_default_format(),
            'summary' => 'Summary',
          ],
        ],
      ],
      'field_text_test' => [
        LANGUAGE_NONE => [
          [
            'value' => 'Text 1',
          ],
        ],
      ],
    ]);
    $node->field_text_test[LANGUAGE_NONE][] = [
      'value' => 'Text 2',
    ];
    node_save($node);

    // Let's ensure that only field_text_test field was changed.
    $is_field_text_test_updated = !empty($node->changed_fields) && count($node->changed_fields) == 1 && !empty($node->changed_fields['field_text_test']);
    $this
      ->assertEqual($is_field_text_test_updated, TRUE, 'Only field_text_test field was changed after node updating.');
    if ($is_field_text_test_updated) {
      $this
        ->assertEqual(count($node->changed_fields['field_text_test']['old_value']), 1, 'Old field_text_test is single.');
      $this
        ->assertEqual(count($node->changed_fields['field_text_test']['new_value']), 2, 'New field_text_test is multiple (2 values).');
      $this
        ->assertEqual($node->changed_fields['field_text_test']['old_value'][0]['value'], 'Text 1', 'Old field_text_test value is "Text 1".');
      $this
        ->assertEqual($node->changed_fields['field_text_test']['new_value'][0]['value'], 'Text 1', 'New field_text_test first value is "Text 1".');
      $this
        ->assertEqual($node->changed_fields['field_text_test']['new_value'][1]['value'], 'Text 2', 'New field_text_test second value is "Text 2".');
    }
  }

  /**
   * Check comparison of multi-value fields - delete a field value.
   */
  public function testMultipleFieldValuesDeletion() {
    $node = $this
      ->drupalCreateNode([
      'type' => 'article',
      'promote' => 1,
      'title' => 'Title',
      'body' => [
        LANGUAGE_NONE => [
          [
            'value' => 'Body',
            'format' => filter_default_format(),
            'summary' => 'Summary',
          ],
        ],
      ],
      'field_text_test' => [
        LANGUAGE_NONE => [
          [
            'value' => 'Text 1',
          ],
          [
            'value' => 'Text 2',
          ],
        ],
      ],
    ]);
    $node->field_text_test[LANGUAGE_NONE] = [
      [
        'value' => 'Text 2',
      ],
    ];
    node_save($node);

    // Let's ensure that only field_text_test field was changed.
    $is_field_text_test_updated = !empty($node->changed_fields) && count($node->changed_fields) == 1 && !empty($node->changed_fields['field_text_test']);
    $this
      ->assertEqual($is_field_text_test_updated, TRUE, 'Only field_text_test field was changed after node updating.');
    if ($is_field_text_test_updated) {
      $this
        ->assertEqual(count($node->changed_fields['field_text_test']['old_value']), 2, 'Old field_text_test is multiple (2 values).');
      $this
        ->assertEqual(count($node->changed_fields['field_text_test']['new_value']), 1, 'New field_text_test is single.');
      $this
        ->assertEqual($node->changed_fields['field_text_test']['old_value'][0]['value'], 'Text 1', 'Old first field_text_test value is "Text 1".');
      $this
        ->assertEqual($node->changed_fields['field_text_test']['old_value'][1]['value'], 'Text 2', 'Old second field_text_test first value is "Text 2".');
      $this
        ->assertEqual($node->changed_fields['field_text_test']['new_value'][0]['value'], 'Text 2', 'New field_text_test second value is "Text 2".');
    }
  }

  /**
   * Check comparison of multi-value fields - ordering.
   */
  public function testMultipleFieldValuesOrdering() {
    $node = $this
      ->drupalCreateNode([
      'type' => 'article',
      'promote' => 1,
      'title' => 'Title',
      'body' => [
        LANGUAGE_NONE => [
          [
            'value' => 'Body',
            'format' => filter_default_format(),
            'summary' => 'Summary',
          ],
        ],
      ],
      'field_text_test' => [
        LANGUAGE_NONE => [
          [
            'value' => 'Text 1',
          ],
          [
            'value' => 'Text 2',
          ],
        ],
      ],
    ]);
    $node->field_text_test[LANGUAGE_NONE] = [
      [
        'value' => 'Text 2',
      ],
      [
        'value' => 'Text 1',
      ],
    ];
    node_save($node);

    // Let's ensure that only field_text_test field was changed.
    $is_field_text_test_updated = !empty($node->changed_fields) && count($node->changed_fields) == 1 && !empty($node->changed_fields['field_text_test']);
    $this
      ->assertEqual($is_field_text_test_updated, TRUE, 'Only field_text_test field was changed after node updating.');
    if ($is_field_text_test_updated) {
      $this
        ->assertEqual(count($node->changed_fields['field_text_test']['old_value']), 2, 'Old field_text_test is multiple (2 values).');
      $this
        ->assertEqual(count($node->changed_fields['field_text_test']['new_value']), 2, 'New field_text_test is multiple (2 values).');
      $this
        ->assertEqual($node->changed_fields['field_text_test']['old_value'][0]['value'], 'Text 1', 'Old first field_text_test value is "Text 1".');
      $this
        ->assertEqual($node->changed_fields['field_text_test']['old_value'][1]['value'], 'Text 2', 'Old second field_text_test first value is "Text 2".');
      $this
        ->assertEqual($node->changed_fields['field_text_test']['new_value'][0]['value'], 'Text 2', 'New first field_text_test value is "Text 2".');
      $this
        ->assertEqual($node->changed_fields['field_text_test']['new_value'][1]['value'], 'Text 1', 'New second field_text_test first value is "Text 1".');
    }
  }

  /**
   * Check comparison of title node property values.
   */
  public function testTitleNodeProperty() {
    $node = $this
      ->drupalCreateNode([
      'type' => 'article',
      'promote' => 1,
      'title' => 'Title',
      'body' => [
        LANGUAGE_NONE => [
          [
            'value' => 'Body',
            'format' => filter_default_format(),
            'summary' => '',
          ],
        ],
      ],
    ]);
    $node->title .= ' changed';
    node_save($node);

    // Let's ensure that only title property was changed.
    $is_title_updated = !empty($node->changed_fields) && count($node->changed_fields) == 1 && !empty($node->changed_fields['title']);
    $this
      ->assertEqual($is_title_updated, TRUE, 'Only title property was changed after node updating.');
    if ($is_title_updated) {
      $this
        ->assertEqual($node->changed_fields['title']['old_value'], 'Title', 'Old title text is "Title".');
      $this
        ->assertEqual($node->changed_fields['title']['new_value'], 'Title changed', 'New title text is "Title changed".');
    }
  }

  /**
   * Check comparison of text_with_summary field.
   */
  public function testTextWithSummaryField() {
    $node = $this
      ->drupalCreateNode([
      'type' => 'article',
      'promote' => 1,
      'title' => 'Title',
      'body' => [
        LANGUAGE_NONE => [
          [
            'value' => 'Body',
            'format' => filter_default_format(),
            'summary' => 'Summary',
          ],
        ],
      ],
    ]);
    $node->body[LANGUAGE_NONE][0]['value'] .= ' changed';
    $node->body[LANGUAGE_NONE][0]['summary'] .= ' changed';
    node_save($node);

    // Let's ensure that only body field was changed.
    $is_body_updated = !empty($node->changed_fields) && count($node->changed_fields) == 1 && !empty($node->changed_fields['body']);
    $this
      ->assertEqual($is_body_updated, TRUE, 'Only body field was changed after node updating.');
    if ($is_body_updated) {
      $this
        ->assertEqual($node->changed_fields['body']['old_value'][0]['value'], 'Body', 'Old body text is "Body".');
      $this
        ->assertEqual($node->changed_fields['body']['new_value'][0]['value'], 'Body changed', 'New body text is "Body changed".');
      $this
        ->assertEqual($node->changed_fields['body']['old_value'][0]['summary'], 'Summary', 'Old body summary text is "Summary".');
      $this
        ->assertEqual($node->changed_fields['body']['new_value'][0]['summary'], 'Summary changed', 'New body summary text is "Summary changed".');
    }
  }

  /**
   * Check comparison of file field.
   */
  public function testFileField() {
    $node = $this
      ->drupalCreateNode([
      'type' => 'article',
      'promote' => 1,
      'title' => 'Title',
      'field_file_test' => [
        LANGUAGE_NONE => [
          $this
            ->createFile(drupal_get_path('module', 'changed_fields') . '/tests/test_files/test_1.txt'),
        ],
      ],
    ]);
    $node->field_file_test[LANGUAGE_NONE][0] = $this
      ->createFile(drupal_get_path('module', 'changed_fields') . '/tests/test_files/test_2.txt');
    node_save($node);

    // Let's ensure that only field_file_test field was changed.
    $is_field_file_test_updated = !empty($node->changed_fields) && count($node->changed_fields) == 1 && !empty($node->changed_fields['field_file_test']);
    $this
      ->assertEqual($is_field_file_test_updated, TRUE, 'Only field_file_test field was changed after node updating.');
    if ($is_field_file_test_updated) {
      $this
        ->assertEqual($node->changed_fields['field_file_test']['old_value'][0]['fid'], '1', 'Old field_file_test fid is "1".');
      $this
        ->assertEqual($node->changed_fields['field_file_test']['new_value'][0]['fid'], '2', 'New field_file_test fid is "2".');
    }
  }

  /**
   * Check comparison of file field (with additional description property).
   *
   * If 'description' option is enabled in field instance then API should take
   * in into consideration.
   */
  public function testFileFieldDescription() {
    $node = $this
      ->drupalCreateNode([
      'type' => 'article',
      'promote' => 1,
      'title' => 'Title',
      'field_file_test' => [
        LANGUAGE_NONE => [
          $this
            ->createFile(drupal_get_path('module', 'changed_fields') . '/tests/test_files/test_1.txt'),
        ],
      ],
    ]);
    $node->field_file_test[LANGUAGE_NONE][0]['description'] = 'Description changed';
    node_save($node);

    // Let's ensure that only field_file_test field was changed.
    $is_field_file_test_updated = !empty($node->changed_fields) && count($node->changed_fields) == 1 && !empty($node->changed_fields['field_file_test']);
    $this
      ->assertEqual($is_field_file_test_updated, TRUE, 'Only field_file_test field was changed after node updating.');
    if ($is_field_file_test_updated) {
      $this
        ->assertEqual($node->changed_fields['field_file_test']['old_value'][0]['description'], '', 'Old field_file_test description is empty.');
      $this
        ->assertEqual($node->changed_fields['field_file_test']['new_value'][0]['description'], 'Description changed', 'New field_file_test description is "Description changed".');
    }
  }

  /**
   * Check comparison of file field (with additional display property).
   *
   * If 'display' option is enabled in field instance then API should take
   * in into consideration.
   */
  public function testFileFieldDisplay() {
    $node = $this
      ->drupalCreateNode([
      'type' => 'article',
      'promote' => 1,
      'title' => 'Title',
      'field_file_test' => [
        LANGUAGE_NONE => [
          $this
            ->createFile(drupal_get_path('module', 'changed_fields') . '/tests/test_files/test_1.txt'),
        ],
      ],
    ]);
    $node->field_file_test[LANGUAGE_NONE][0]['display'] = 0;
    node_save($node);

    // Let's ensure that only field_file_test field was changed.
    $is_field_file_test_updated = !empty($node->changed_fields) && count($node->changed_fields) == 1 && !empty($node->changed_fields['field_file_test']);
    $this
      ->assertEqual($is_field_file_test_updated, TRUE, 'Only field_file_test field was changed after node updating.');
    if ($is_field_file_test_updated) {
      $this
        ->assertEqual($node->changed_fields['field_file_test']['old_value'][0]['display'], 1, 'Old field_file_test display is "TRUE".');
      $this
        ->assertEqual($node->changed_fields['field_file_test']['new_value'][0]['display'], 0, 'New field_file_test display is empty.');
    }
  }

  /**
   * Check comparison of image field (with additional alt property).
   *
   * If 'alt' option is enabled in field instance then API should take
   * in into consideration.
   */
  public function testImageAlt() {
    $node = $this
      ->drupalCreateNode([
      'type' => 'article',
      'promote' => 1,
      'title' => 'Title',
      'field_image_test' => [
        LANGUAGE_NONE => [
          $this
            ->createFile(drupal_get_path('module', 'changed_fields') . '/tests/test_files/test.png'),
        ],
      ],
    ]);
    $node->field_image_test[LANGUAGE_NONE][0]['alt'] = 'Alt changed';
    node_save($node);

    // Let's ensure that only field_image_test field was changed.
    $is_field_image_test_updated = !empty($node->changed_fields) && count($node->changed_fields) == 1 && !empty($node->changed_fields['field_image_test']);
    $this
      ->assertEqual($is_field_image_test_updated, TRUE, 'Only field_image_test field was changed after node updating.');
    if ($is_field_image_test_updated) {
      $this
        ->assertEqual($node->changed_fields['field_image_test']['old_value'][0]['alt'], '', 'Old field_image_test alt is empty.');
      $this
        ->assertEqual($node->changed_fields['field_image_test']['new_value'][0]['alt'], 'Alt changed', 'New field_image_test alt is "Alt changed".');
    }
  }

  /**
   * Check comparison of image field (with additional title property).
   *
   * If 'title' option is enabled in field instance then API should take
   * in into consideration.
   */
  public function testImageTitle() {
    $node = $this
      ->drupalCreateNode([
      'type' => 'article',
      'promote' => 1,
      'title' => 'Title',
      'field_image_test' => [
        LANGUAGE_NONE => [
          $this
            ->createFile(drupal_get_path('module', 'changed_fields') . '/tests/test_files/test.png'),
        ],
      ],
    ]);
    $node->field_image_test[LANGUAGE_NONE][0]['title'] = 'Title changed';
    node_save($node);

    // Let's ensure that only field_image_test field was changed.
    $is_field_image_test_updated = !empty($node->changed_fields) && count($node->changed_fields) == 1 && !empty($node->changed_fields['field_image_test']);
    $this
      ->assertEqual($is_field_image_test_updated, TRUE, 'Only field_image_test field was changed after node updating.');
    if ($is_field_image_test_updated) {
      $this
        ->assertEqual($node->changed_fields['field_image_test']['old_value'][0]['title'], '', 'Old field_image_test title is empty.');
      $this
        ->assertEqual($node->changed_fields['field_image_test']['new_value'][0]['title'], 'Title changed', 'New field_image_test title is "Title changed".');
    }
  }

}

Classes

Namesort descending Description
DefaultFieldComparatorWebTestCase Class DefaultFieldComparatorWebTestCase.