summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2014-10-05 10:47:56 (GMT)
committerAlex Pott2014-10-05 10:47:56 (GMT)
commit99876e6ac1ddc82e9874bd35abcd76650692a8ef (patch)
treee4a74c7cbf60265dac70b2cc81c782440ffd90b7
parent4300214f8858410baac68aa88543f7f1cd4e2669 (diff)
Issue #217676 by andypost, mr.baileys, Stevel, Pancho, catch, cburschka: Fixed taxonomy_term_load_parents_all() doesn't work correctly with multiple hierarchy terms.
-rw-r--r--core/modules/taxonomy/src/TermStorage.php17
-rw-r--r--core/modules/taxonomy/src/Tests/TermUnitTest.php24
2 files changed, 36 insertions, 5 deletions
diff --git a/core/modules/taxonomy/src/TermStorage.php b/core/modules/taxonomy/src/TermStorage.php
index 015baed..c686f11 100644
--- a/core/modules/taxonomy/src/TermStorage.php
+++ b/core/modules/taxonomy/src/TermStorage.php
@@ -161,11 +161,18 @@ class TermStorage extends SqlContentEntityStorage implements TermStorageInterfac
if (!isset($this->parentsAll[$tid])) {
$parents = array();
if ($term = $this->load($tid)) {
- $parents[] = $term;
- $n = 0;
- while ($parent = $this->loadParents($parents[$n]->id())) {
- $parents = array_merge($parents, $parent);
- $n++;
+ $parents[$term->id()] = $term;
+ $terms_to_search[] = $term->id();
+
+ while ($tid = array_shift($terms_to_search)) {
+ if ($new_parents = $this->loadParents($tid)) {
+ foreach ($new_parents as $new_parent) {
+ if (!isset($parents[$new_parent->id()])) {
+ $parents[$new_parent->id()] = $new_parent;
+ $terms_to_search[] = $new_parent->id();
+ }
+ }
+ }
}
}
diff --git a/core/modules/taxonomy/src/Tests/TermUnitTest.php b/core/modules/taxonomy/src/Tests/TermUnitTest.php
index 545bc4a..c33442f 100644
--- a/core/modules/taxonomy/src/Tests/TermUnitTest.php
+++ b/core/modules/taxonomy/src/Tests/TermUnitTest.php
@@ -58,6 +58,12 @@ class TermUnitTest extends TaxonomyTestBase {
$term[$i] = $this->createTerm($vocabulary);
}
+ // Set the weight on $term[1] so it appears before $term[5] when fetching
+ // the parents for $term[2], in order to test for a regression on
+ // \Drupal\taxonomy\TermStorageInterface::loadAllParents().
+ $term[1]->weight = -1;
+ $term[1]->save();
+
// $term[2] is a child of 1 and 5.
$term[2]->parent = array($term[1]->id(), $term[5]->id());
$term[2]->save();
@@ -98,5 +104,23 @@ class TermUnitTest extends TaxonomyTestBase {
$this->assertEqual(2, $depth_count[1], 'Two elements in taxonomy tree depth 1.');
$this->assertEqual(2, $depth_count[2], 'Two elements in taxonomy tree depth 2.');
$this->assertEqual(1, $depth_count[3], 'One element in taxonomy tree depth 3.');
+
+ /** @var \Drupal\taxonomy\TermStorageInterface $storage */
+ $storage = \Drupal::entityManager()->getStorage('taxonomy_term');
+ // Count parents of $term[2].
+ $parents = $storage->loadParents($term[2]->id());
+ $this->assertEqual(2, count($parents), 'The term has two parents.');
+
+ // Count parents of $term[3].
+ $parents = $storage->loadParents($term[3]->id());
+ $this->assertEqual(1, count($parents), 'The term has one parent.');
+
+ // Identify all ancestors of $term[2].
+ $ancestors = $storage->loadAllParents($term[2]->id());
+ $this->assertEqual(4, count($ancestors), 'The term has four ancestors including the term itself.');
+
+ // Identify all ancestors of $term[3].
+ $ancestors = $storage->loadAllParents($term[3]->id());
+ $this->assertEqual(5, count($ancestors), 'The term has five ancestors including the term itself.');
}
}