You are here

public function ViewsTermMergeWebTestCase::testTermReferenceFieldFilter in Term Merge 7

Test integration with Views Taxonomy Term reference filter.

File

./term_merge.test, line 1553
Test the Term Merge module.

Class

ViewsTermMergeWebTestCase
Test the integration between Term Merge module and Views module.

Code

public function testTermReferenceFieldFilter() {

  // We need to create a content type and attach a term reference field to
  // that bundle in order to have some term reference filter available in
  // Views.
  $this
    ->drupalPost('admin/structure/types/add', array(
    'name' => $this
      ->randomName(),
    'type' => 'term_merge_node',
  ), 'Save content type');
  $field_name = 'term_reference';
  $this
    ->drupalPost('admin/structure/types/manage/term-merge-node/fields', array(
    'fields[_add_new_field][label]' => 'Term Reference',
    'fields[_add_new_field][field_name]' => $field_name,
    'fields[_add_new_field][type]' => 'taxonomy_term_reference',
    'fields[_add_new_field][widget_type]' => 'taxonomy_autocomplete',
  ), 'Save');
  $field_name = 'field_' . $field_name;
  $this
    ->drupalPost(NULL, array(
    'field[settings][allowed_values][0][vocabulary]' => $this->vocabulary->machine_name,
  ), 'Save field settings');
  $this
    ->drupalPost(NULL, array(
    'field[cardinality]' => FIELD_CARDINALITY_UNLIMITED,
  ), 'Save settings');

  // Flushing fields API cache.
  _field_info_collate_fields(TRUE);

  // Loading field definition array of the term reference field we just
  // created.
  $field = field_info_field($field_name);

  // Creating terms to have stuff to work with.
  $terms = array(
    'branch' => FALSE,
    'trunk' => FALSE,
  );
  foreach ($terms as $term_type => $tmp) {
    $url = 'admin/structure/taxonomy/vocabulary/add';
    $name = $this
      ->randomName();
    $edit = array(
      'name' => $name,
    );
    $this
      ->drupalPost($url, $edit, 'Save');
    $terms[$term_type] = $this
      ->getLastTerm($this->vocabulary);
  }

  // Adding a taxonomy term reference filter to the view.
  $this->view
    ->set_display('default');

  // We use Field API info to look up necessary tables and columns.
  $table = array_keys($field['storage']['details']['sql']['FIELD_LOAD_CURRENT']);
  $table = reset($table);
  $columns = $field['storage']['details']['sql']['FIELD_LOAD_CURRENT'][$table];
  $this->view->display_handler->display->display_options['filters'][$columns['tid']]['id'] = $columns['tid'];
  $this->view->display_handler->display->display_options['filters'][$columns['tid']]['table'] = $table;
  $this->view->display_handler->display->display_options['filters'][$columns['tid']]['field'] = $columns['tid'];
  $this->view->display_handler->display->display_options['filters'][$columns['tid']]['value'] = array(
    $terms['branch']->tid => $terms['branch']->tid,
  );
  $this->view->display_handler->display->display_options['filters'][$columns['tid']]['type'] = 'select';
  $this->view->display_handler->display->display_options['filters'][$columns['tid']]['vocabulary'] = $this->vocabulary->machine_name;
  $this->view->display_handler->display->display_options['filters'][$columns['tid']]['hierarchy'] = 1;
  views_save_view($this->view);

  // After such merge we expect the view's filter to be changed from branch
  // term to trunk term.
  actions_do('term_merge_action', $terms['branch'], array(
    'term_trunk' => $terms['trunk']->tid,
    'term_branch_keep' => FALSE,
  ));

  // Loading again the view after merging action.
  $this->view = views_get_view($this->view->name);
  $this->view
    ->set_display('default');
  $filter = $this->view->display_handler->display->display_options['filters'][$columns['tid']]['value'];
  $this
    ->assertTrue(count($filter) == 1 && in_array($terms['trunk']->tid, array_keys($filter)), 'Views term reference filter gets updated to filter on trunk term instead of filtering on branch term if the branch term is instructed to be deleted during merging of terms.');
}