function TermTest::testTermReorder

Save, edit and delete a term using the user interface.

File

drupal/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermTest.php, line 367
Definition of Drupal\taxonomy\Tests\TermTest.

Class

TermTest
Tests for taxonomy term functions.

Namespace

Drupal\taxonomy\Tests

Code

function testTermReorder() {
  $this
    ->createTerm($this->vocabulary);
  $this
    ->createTerm($this->vocabulary);
  $this
    ->createTerm($this->vocabulary);

  // Fetch the created terms in the default alphabetical order, i.e. term1
  // precedes term2 alphabetically, and term2 precedes term3.
  drupal_static_reset('taxonomy_get_tree');
  drupal_static_reset('taxonomy_get_treeparent');
  drupal_static_reset('taxonomy_get_treeterms');
  list($term1, $term2, $term3) = taxonomy_get_tree($this->vocabulary
    ->id(), 0, NULL, TRUE);
  $this
    ->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary
    ->id());

  // Each term has four hidden fields, "tid:1:0[tid]", "tid:1:0[parent]",
  // "tid:1:0[depth]", and "tid:1:0[weight]". Change the order to term2,
  // term3, term1 by setting weight property, make term3 a child of term2 by
  // setting the parent and depth properties, and update all hidden fields.
  $edit = array(
    'terms[tid:' . $term2
      ->id() . ':0][term][tid]' => $term2
      ->id(),
    'terms[tid:' . $term2
      ->id() . ':0][term][parent]' => 0,
    'terms[tid:' . $term2
      ->id() . ':0][term][depth]' => 0,
    'terms[tid:' . $term2
      ->id() . ':0][weight]' => 0,
    'terms[tid:' . $term3
      ->id() . ':0][term][tid]' => $term3
      ->id(),
    'terms[tid:' . $term3
      ->id() . ':0][term][parent]' => $term2
      ->id(),
    'terms[tid:' . $term3
      ->id() . ':0][term][depth]' => 1,
    'terms[tid:' . $term3
      ->id() . ':0][weight]' => 1,
    'terms[tid:' . $term1
      ->id() . ':0][term][tid]' => $term1
      ->id(),
    'terms[tid:' . $term1
      ->id() . ':0][term][parent]' => 0,
    'terms[tid:' . $term1
      ->id() . ':0][term][depth]' => 0,
    'terms[tid:' . $term1
      ->id() . ':0][weight]' => 2,
  );
  $this
    ->drupalPost(NULL, $edit, t('Save'));
  drupal_static_reset('taxonomy_get_tree');
  drupal_static_reset('taxonomy_get_treeparent');
  drupal_static_reset('taxonomy_get_treeterms');
  $terms = taxonomy_get_tree($this->vocabulary
    ->id());
  $this
    ->assertEqual($terms[0]->tid, $term2
    ->id(), 'Term 2 was moved above term 1.');
  $this
    ->assertEqual($terms[1]->parents, array(
    $term2
      ->id(),
  ), 'Term 3 was made a child of term 2.');
  $this
    ->assertEqual($terms[2]->tid, $term1
    ->id(), 'Term 1 was moved below term 2.');
  $this
    ->drupalPost('admin/structure/taxonomy/manage/' . $this->vocabulary
    ->id(), array(), t('Reset to alphabetical'));

  // Submit confirmation form.
  $this
    ->drupalPost(NULL, array(), t('Reset to alphabetical'));
  drupal_static_reset('taxonomy_get_tree');
  drupal_static_reset('taxonomy_get_treeparent');
  drupal_static_reset('taxonomy_get_treeterms');
  $terms = taxonomy_get_tree($this->vocabulary
    ->id(), 0, NULL, TRUE);
  $this
    ->assertEqual($terms[0]
    ->id(), $term1
    ->id(), 'Term 1 was moved to back above term 2.');
  $this
    ->assertEqual($terms[1]
    ->id(), $term2
    ->id(), 'Term 2 was moved to back below term 1.');
  $this
    ->assertEqual($terms[2]
    ->id(), $term3
    ->id(), 'Term 3 is still below term 2.');
  $this
    ->assertEqual($terms[2]->parents, array(
    $term2
      ->id(),
  ), 'Term 3 is still a child of term 2.' . var_export($terms[1]
    ->id(), 1));
}