From 9bc009b1b88f57117dedda44eab19b46001150c9 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Mon, 4 Aug 2014 10:35:25 +0100
Subject: [PATCH] Issue #242324 by rychannel, kbasarab, chx, cgalli, LinL,
 pfrenssen, wonder95, triclops | whatistocome: Fixed Going to page 2 on "list
 terms" page doesn't display additional terms.

---
 .../taxonomy/src/Form/OverviewTerms.php       | 11 ++--
 .../taxonomy/src/Tests/TaxonomyTestBase.php   | 14 ++++-
 core/modules/taxonomy/src/Tests/TermTest.php  | 51 +++++++++++++++++++
 3 files changed, 69 insertions(+), 7 deletions(-)

diff --git a/core/modules/taxonomy/src/Form/OverviewTerms.php b/core/modules/taxonomy/src/Form/OverviewTerms.php
index 23db546e1091..a9a0086ecbfb 100644
--- a/core/modules/taxonomy/src/Form/OverviewTerms.php
+++ b/core/modules/taxonomy/src/Form/OverviewTerms.php
@@ -100,10 +100,10 @@ public function buildForm(array $form, FormStateInterface $form_state, Vocabular
     // @todo taxonomy_get_tree needs to be converted to a service and injected.
     //   Will be fixed in http://drupal.org/node/1976298.
     $tree = taxonomy_get_tree($taxonomy_vocabulary->id(), 0, NULL, TRUE);
-    $term = current($tree);
+    $tree_index = 0;
     do {
       // In case this tree is completely empty.
-      if (empty($term)) {
+      if (empty($tree[$tree_index])) {
         break;
       }
       $delta++;
@@ -119,13 +119,14 @@ public function buildForm(array $form, FormStateInterface $form_state, Vocabular
       }
 
       // Do not let a term start the page that is not at the root.
+      $term = $tree[$tree_index];
       if (isset($term->depth) && ($term->depth > 0) && !isset($back_step)) {
         $back_step = 0;
-        while ($pterm = prev($tree)) {
+        while ($pterm = $tree[--$tree_index]) {
           $before_entries--;
           $back_step++;
           if ($pterm->depth == 0) {
-            prev($tree);
+            $tree_index--;
             // Jump back to the start of the root level parent.
             continue 2;
           }
@@ -159,7 +160,7 @@ public function buildForm(array $form, FormStateInterface $form_state, Vocabular
         $root_entries++;
       }
       $current_page[$key] = $term;
-    } while ($term = next($tree));
+    } while (isset($tree[++$tree_index]));
 
     // Because we didn't use a pager query, set the necessary pager variables.
     $total_entries = $before_entries + $page_entries + $after_entries;
diff --git a/core/modules/taxonomy/src/Tests/TaxonomyTestBase.php b/core/modules/taxonomy/src/Tests/TaxonomyTestBase.php
index 3840c6579c09..35e51179d7e8 100644
--- a/core/modules/taxonomy/src/Tests/TaxonomyTestBase.php
+++ b/core/modules/taxonomy/src/Tests/TaxonomyTestBase.php
@@ -9,6 +9,7 @@
 
 use Drupal\Core\Language\LanguageInterface;
 use Drupal\simpletest\WebTestBase;
+use Drupal\taxonomy\Entity\Vocabulary;
 
 /**
  * Provides common helper methods for Taxonomy module tests.
@@ -49,11 +50,20 @@ function createVocabulary() {
 
   /**
    * Returns a new term with random properties in vocabulary $vid.
+   *
+   * @param \Drupal\taxonomy\Entity\Vocabulary $vocabulary
+   *   The vocabulary object.
+   * @param array $values
+   *   (optional) An array of values to set, keyed by property name. If the
+   *   entity type has bundles, the bundle key has to be specified.
+   *
+   * @return \Drupal\taxonomy\Entity\Term
+   *   The new taxonomy term object.
    */
-  function createTerm($vocabulary) {
+  function createTerm(Vocabulary $vocabulary, $values = array()) {
     $filter_formats = filter_formats();
     $format = array_pop($filter_formats);
-    $term = entity_create('taxonomy_term', array(
+    $term = entity_create('taxonomy_term', $values + array(
       'name' => $this->randomName(),
       'description' => array(
         'value' => $this->randomName(),
diff --git a/core/modules/taxonomy/src/Tests/TermTest.php b/core/modules/taxonomy/src/Tests/TermTest.php
index ce47f3c47f7b..57a83f9eaeae 100644
--- a/core/modules/taxonomy/src/Tests/TermTest.php
+++ b/core/modules/taxonomy/src/Tests/TermTest.php
@@ -97,6 +97,57 @@ function testTaxonomyTermHierarchy() {
     $this->assertTrue(isset($parents[$term1->id()]) && isset($parents[$term3->id()]), 'Both parents found successfully.');
   }
 
+  /**
+   * Tests that many terms with parents show on each page
+   */
+  function testTaxonomyTermChildTerms() {
+    // Set limit to 10 terms per page. Set variable to 9 so 10 terms appear.
+    \Drupal::config('taxonomy.settings')->set('terms_per_page_admin', '9')->save();
+    $term1 = $this->createTerm($this->vocabulary);
+    $terms_array = '';
+
+    // Create 40 terms. Terms 1-12 get parent of $term1. All others are
+    // individual terms.
+    for ($x = 1; $x <= 40; $x++) {
+      $edit = array();
+      // 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_term_load_children($term1->id());
+      $parents = taxonomy_term_load_parents($term->id());
+      $terms_array[$x] = taxonomy_term_load($term->id());
+    }
+
+    // Get Page 1.
+    $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/overview');
+    $this->assertText($term1->getName(), 'Parent Term is displayed on Page 1');
+    for ($x = 1; $x <= 13; $x++) {
+      $this->assertText($terms_array[$x]->getName(), $terms_array[$x]->getName() . ' found on Page 1');
+    }
+
+    // Get Page 2.
+    $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/overview', array('query' => array('page' => 1)));
+    $this->assertText($term1->getName(), 'Parent Term is displayed on Page 2');
+    for ($x = 1; $x <= 18; $x++) {
+      $this->assertText($terms_array[$x]->getName(), $terms_array[$x]->getName() . ' found on Page 2');
+    }
+
+    // Get Page 3.
+    $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/overview', array('query' => array('page' => 2)));
+    $this->assertNoText($term1->getName(), 'Parent Term is not displayed on Page 3');
+    for ($x = 1; $x <= 17; $x++) {
+      $this->assertNoText($terms_array[$x]->getName(), $terms_array[$x]->getName() . ' not found on Page 3');
+    }
+    for ($x =18; $x <= 25; $x++) {
+      $this->assertText($terms_array[$x]->getName(), $terms_array[$x]->getName() . ' found on Page 3');
+    }
+  }
+
   /**
    * Test that hook_node_$op implementations work correctly.
    *
-- 
GitLab