You are here

public function TermTest::testTaxonomyTermChildTerms 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::testTaxonomyTermChildTerms()

Tests that many terms with parents show on each page.

File

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

Class

TermTest
Tests load, save and delete for taxonomy terms.

Namespace

Drupal\Tests\taxonomy\Functional

Code

public function testTaxonomyTermChildTerms() {

  // Set limit to 10 terms per page. Set variable to 9 so 10 terms appear.
  $this
    ->config('taxonomy.settings')
    ->set('terms_per_page_admin', '9')
    ->save();
  $term1 = $this
    ->createTerm($this->vocabulary);
  $terms_array = [];
  $taxonomy_storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('taxonomy_term');

  // Create 40 terms. Terms 1-12 get parent of $term1. All others are
  // individual terms.
  for ($x = 1; $x <= 40; $x++) {
    $edit = [];

    // Set terms in order so we know which terms will be on which pages.
    $edit['weight'] = $x;

    // Set terms 1-20 to be children of first term created.
    if ($x <= 12) {
      $edit['parent'] = $term1
        ->id();
    }
    $term = $this
      ->createTerm($this->vocabulary, $edit);
    $children = $taxonomy_storage
      ->loadChildren($term1
      ->id());
    $parents = $taxonomy_storage
      ->loadParents($term
      ->id());
    $terms_array[$x] = Term::load($term
      ->id());
  }

  // Get Page 1. Parent term and terms 1-13 are displayed.
  $this
    ->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary
    ->id() . '/overview');
  $this
    ->assertSession()
    ->pageTextContains($term1
    ->getName());
  for ($x = 1; $x <= 13; $x++) {
    $this
      ->assertSession()
      ->pageTextContains($terms_array[$x]
      ->getName());
  }

  // Get Page 2. Parent term and terms 1-18 are displayed.
  $this
    ->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary
    ->id() . '/overview', [
    'query' => [
      'page' => 1,
    ],
  ]);
  $this
    ->assertSession()
    ->pageTextContains($term1
    ->getName());
  for ($x = 1; $x <= 18; $x++) {
    $this
      ->assertSession()
      ->pageTextContains($terms_array[$x]
      ->getName());
  }

  // Get Page 3. No parent term and no terms <18 are displayed. Terms 18-25
  // are displayed.
  $this
    ->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary
    ->id() . '/overview', [
    'query' => [
      'page' => 2,
    ],
  ]);
  $this
    ->assertSession()
    ->pageTextNotContains($term1
    ->getName());
  for ($x = 1; $x <= 17; $x++) {
    $this
      ->assertSession()
      ->pageTextNotContains($terms_array[$x]
      ->getName());
  }
  for ($x = 18; $x <= 25; $x++) {
    $this
      ->assertSession()
      ->pageTextContains($terms_array[$x]
      ->getName());
  }
}