You are here

public function TermTest::testTermReorder in Drupal 8

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

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

File

core/modules/taxonomy/tests/src/Functional/TermTest.php, line 402

Class

TermTest
Tests load, save and delete for taxonomy terms.

Namespace

Drupal\Tests\taxonomy\Functional

Code

public function testTermReorder() {
  $assert = $this
    ->assertSession();
  $this
    ->createTerm($this->vocabulary);
  $this
    ->createTerm($this->vocabulary);
  $this
    ->createTerm($this->vocabulary);
  $taxonomy_storage = $this->container
    ->get('entity_type.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.
  $hidden_edit = [
    '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:' . $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:' . $term1
      ->id() . ':0][term][tid]' => $term1
      ->id(),
    'terms[tid:' . $term1
      ->id() . ':0][term][parent]' => 0,
    'terms[tid:' . $term1
      ->id() . ':0][term][depth]' => 0,
  ];

  // Because we can't post hidden form elements, we have to change them in
  // code here, and then submit.
  foreach ($hidden_edit as $field => $value) {
    $node = $assert
      ->hiddenFieldExists($field);
    $node
      ->setValue($value);
  }

  // Edit non-hidden elements within drupalPostForm().
  $edit = [
    'terms[tid:' . $term2
      ->id() . ':0][weight]' => 0,
    'terms[tid:' . $term3
      ->id() . ':0][weight]' => 1,
    'terms[tid:' . $term1
      ->id() . ':0][weight]' => 2,
  ];
  $this
    ->drupalPostForm(NULL, $edit, '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, [
    $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', [], t('Reset to alphabetical'));

  // Submit confirmation form.
  $this
    ->drupalPostForm(NULL, [], 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, [
    $term2
      ->id(),
  ], 'Term 3 is still a child of term 2.');
}