You are here

public function DuplicatesTermMergeWebTestCase::testDuplicates in Term Merge 7

Test merging duplicates feature of Term Merge module.

Test the following features:

  • Correctness of merging a group of duplicate terms, namely:

    • Correctness of merge operation when duplicates feature is invoked on the entire vocabulary
    • Correctness of merge operation when duplicates feature is invoked on a term (merge its children one into another)
  • Correctness of the mechanism that groups terms into sets of duplicate entries, namely:

    • Correctness of grouping by term name, i.e. unique terms should not be listed in any set of duplicate terms
    • Correctness of the initial set of terms, on which the duplicate tool is invoked, i.e. when invoked on a vocabulary, we search for duplicates in the whole vocabulary, but when invoked on a term, the tool should only search for duplicate among the children of that term

File

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

Class

DuplicatesTermMergeWebTestCase
Test the Merge Duplicate Terms feature of the Term Merge module.

Code

public function testDuplicates() {

  // Creating duplicate terms firstly.
  $groups = array(
    'single' => 1,
    'triple_different_parent' => 3,
    'random' => rand(2, 5),
    // We need some term, that will be a parent of some other terms.
    'parent' => 1,
  );
  $groups = $this
    ->createTerms($groups);

  // Let us make two of 'triple_different_parent' terms children of 'parent'
  // term.
  $groups['triple_different_parent'][1]->parent = $groups['parent'][0]->tid;
  taxonomy_term_save($groups['triple_different_parent'][1]);
  $groups['triple_different_parent'][2]->parent = $groups['parent'][0]->tid;
  taxonomy_term_save($groups['triple_different_parent'][2]);

  // Test duplicate suggestion plugin type. Make sure multiple duplicated
  // suggestions are properly handed and make sure each of the duplicate
  // suggestions does its function.
  $this
    ->drupalGet('admin/structure/taxonomy/' . $this->vocabulary->machine_name . '/merge/duplicates');
  $this
    ->assertSuggestedDuplicates(array_merge($groups['triple_different_parent'], $groups['random']), 'Filtering only by term names yields expected results.');
  $this
    ->drupalPost('admin/structure/taxonomy/' . $this->vocabulary->machine_name . '/merge/duplicates', array(
    'settings[duplicate_suggestion][name]' => FALSE,
    'settings[duplicate_suggestion][description]' => TRUE,
  ), 'Re-run duplicate search');
  $this
    ->assertSuggestedDuplicates(array_merge($groups['triple_different_parent'], $groups['random']), 'Filtering only by term description yields expected results.');
  $this
    ->drupalPost('admin/structure/taxonomy/' . $this->vocabulary->machine_name . '/merge/duplicates', array(
    'settings[duplicate_suggestion][name]' => FALSE,
    'settings[duplicate_suggestion][parent]' => TRUE,
  ), 'Re-run duplicate search');
  $expected_terms = array();
  $expected_terms = array_merge($expected_terms, $groups['single'], $groups['random'], $groups['parent']);
  $expected_terms[] = $groups['triple_different_parent'][0];
  $this
    ->assertSuggestedDuplicates($expected_terms, 'Filtering only by term parent yields expected results.');
  $this
    ->drupalPost('admin/structure/taxonomy/' . $this->vocabulary->machine_name . '/merge/duplicates', array(
    'settings[duplicate_suggestion][name]' => TRUE,
    'settings[duplicate_suggestion][parent]' => TRUE,
  ), 'Re-run duplicate search');
  $expected_terms = $groups['triple_different_parent'];
  unset($expected_terms[0]);
  $this
    ->assertSuggestedDuplicates($expected_terms, 'Filtering by term name and parent yields expected results, i.e. duplicate suggestions can be combined.');

  // Assuring the single term is not listed as duplicate.
  $this
    ->drupaLGet('admin/structure/taxonomy/' . $this->vocabulary->machine_name . '/merge/duplicates');
  $this
    ->assertNoText($groups['single'][0]->name, 'Single term is not listed as a duplicate.');

  // Making sure the term in 'triple_different_parent' that does not have a
  // parent, is not listed when we invoke duplicate tool on a parent term.
  $this
    ->drupalGet('taxonomy/term/' . $groups['parent'][0]->tid . '/merge/duplicates');
  $this
    ->assertNoFieldByName('group[' . $this
    ->duplicateHashTerm($groups['triple_different_parent'][0]) . '][duplicates][' . $groups['triple_different_parent'][0]->tid . ']', 'Duplicate term is not listed when it is not among children of a term, on which Term Merge module was invoked.');
  $edit = array();

  // Trying to merge a term into another, invoking Duplicate tool on a parent
  // term of both. Important note: we do not test merging options, because
  // supposedly those are tested in the main test of this module.
  $edit['group[' . $this
    ->duplicateHashTerm($groups['triple_different_parent'][1]) . '][trunk_tid]'] = $groups['triple_different_parent'][1]->tid;
  $edit['group[' . $this
    ->duplicateHashTerm($groups['triple_different_parent'][2]) . '][duplicates][' . $groups['triple_different_parent'][2]->tid . ']'] = TRUE;
  $groups['triple_different_parent'][2]->merged = TRUE;
  $this
    ->drupalPost('taxonomy/term/' . $groups['parent'][0]->tid . '/merge/duplicates', $edit, 'Submit');

  //  Trying to merge multiple terms. We merge all but the 1st term.
  $edit = array();
  $edit['group[' . $this
    ->duplicateHashTerm($groups['random'][0]) . '][trunk_tid]'] = $groups['random'][0]->tid;
  foreach ($groups['random'] as $k => $term) {
    if ($k != 0) {
      $edit['group[' . $this
        ->duplicateHashTerm($groups['random'][$k]) . '][duplicates][' . $term->tid . ']'] = TRUE;
      $groups['random'][$k]->merged = TRUE;
    }
  }
  $this
    ->drupalPost('admin/structure/taxonomy/' . $this->vocabulary->machine_name . '/merge/duplicates', $edit, 'Submit');

  // Asserting results of merging.
  foreach ($groups as $group) {
    foreach ($group as $term) {
      $this
        ->drupalGet('taxonomy/term/' . $term->tid);
      $code = isset($term->merged) && $term->merged ? 404 : 200;
      $message = isset($term->merged) && $term->merged ? 'Term #' . $term->tid . ' has been successfully merged.' : 'Term #' . $term->tid . ' has been successfully untouched during merging.';
      $this
        ->assertResponse($code, $message);
    }
  }
}