You are here

public function TermTest::testTermReorder in Drupal 9

Same name and namespace in other branches
  1. 8 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 435

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 submitForm().
  $edit = [
    'terms[tid:' . $term2
      ->id() . ':0][weight]' => 0,
    'terms[tid:' . $term3
      ->id() . ':0][weight]' => 1,
    'terms[tid:' . $term1
      ->id() . ':0][weight]' => 2,
  ];
  $this
    ->submitForm($edit, 'Save');
  $taxonomy_storage
    ->resetCache();
  $terms = $taxonomy_storage
    ->loadTree($this->vocabulary
    ->id());
  $this
    ->assertEquals($term2
    ->id(), $terms[0]->tid, 'Term 2 was moved above term 1.');
  $this
    ->assertEquals([
    $term2
      ->id(),
  ], $terms[1]->parents, 'Term 3 was made a child of term 2.');
  $this
    ->assertEquals($term1
    ->id(), $terms[2]->tid, 'Term 1 was moved below term 2.');
  $this
    ->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary
    ->id() . '/overview');
  $this
    ->submitForm([], 'Reset to alphabetical');

  // Submit confirmation form.
  $this
    ->submitForm([], 'Reset to alphabetical');

  // Ensure form redirected back to overview.
  $this
    ->assertSession()
    ->addressEquals('admin/structure/taxonomy/manage/' . $this->vocabulary
    ->id() . '/overview');
  $taxonomy_storage
    ->resetCache();
  $terms = $taxonomy_storage
    ->loadTree($this->vocabulary
    ->id(), 0, NULL, TRUE);
  $this
    ->assertEquals($term1
    ->id(), $terms[0]
    ->id(), 'Term 1 was moved to back above term 2.');
  $this
    ->assertEquals($term2
    ->id(), $terms[1]
    ->id(), 'Term 2 was moved to back below term 1.');
  $this
    ->assertEquals($term3
    ->id(), $terms[2]
    ->id(), 'Term 3 is still below term 2.');
  $this
    ->assertEquals([
    $term2
      ->id(),
  ], $terms[2]->parents, 'Term 3 is still a child of term 2.');
}