You are here

function TermTest::testTermReorder in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/taxonomy/src/Tests/TermTest.php \Drupal\taxonomy\Tests\TermTest::testTermReorder()

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

File

core/modules/taxonomy/src/Tests/TermTest.php, line 383
Contains \Drupal\taxonomy\Tests\TermTest.

Class

TermTest
Tests load, save and delete for taxonomy terms.

Namespace

Drupal\taxonomy\Tests

Code

function testTermReorder() {
  $this
    ->createTerm($this->vocabulary);
  $this
    ->createTerm($this->vocabulary);
  $this
    ->createTerm($this->vocabulary);
  $taxonomy_storage = $this->container
    ->get('entity.manager')
    ->getStorage('taxonomy_term');

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

  // 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
    ->drupalPostForm(NULL, $edit, t('Save'));
  $taxonomy_storage
    ->resetCache();
  $terms = $taxonomy_storage
    ->loadTree($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
    ->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary
    ->id() . '/overview', array(), t('Reset to alphabetical'));

  // Submit confirmation form.
  $this
    ->drupalPostForm(NULL, array(), t('Reset to alphabetical'));

  // Ensure form redirected back to overview.
  $this
    ->assertUrl('admin/structure/taxonomy/manage/' . $this->vocabulary
    ->id() . '/overview');
  $taxonomy_storage
    ->resetCache();
  $terms = $taxonomy_storage
    ->loadTree($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.');
}