Commit 99876e6a authored by alexpott's avatar alexpott

Issue #217676 by andypost, mr.baileys, Stevel, Pancho, catch, cburschka: Fixed...

Issue #217676 by andypost, mr.baileys, Stevel, Pancho, catch, cburschka: Fixed taxonomy_term_load_parents_all() doesn't work correctly with multiple hierarchy terms.
parent 4300214f
...@@ -161,11 +161,18 @@ public function loadAllParents($tid) { ...@@ -161,11 +161,18 @@ public function loadAllParents($tid) {
if (!isset($this->parentsAll[$tid])) { if (!isset($this->parentsAll[$tid])) {
$parents = array(); $parents = array();
if ($term = $this->load($tid)) { if ($term = $this->load($tid)) {
$parents[] = $term; $parents[$term->id()] = $term;
$n = 0; $terms_to_search[] = $term->id();
while ($parent = $this->loadParents($parents[$n]->id())) {
$parents = array_merge($parents, $parent); while ($tid = array_shift($terms_to_search)) {
$n++; 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();
}
}
}
} }
} }
......
...@@ -58,6 +58,12 @@ function testTaxonomyVocabularyTree() { ...@@ -58,6 +58,12 @@ function testTaxonomyVocabularyTree() {
$term[$i] = $this->createTerm($vocabulary); $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] is a child of 1 and 5.
$term[2]->parent = array($term[1]->id(), $term[5]->id()); $term[2]->parent = array($term[1]->id(), $term[5]->id());
$term[2]->save(); $term[2]->save();
...@@ -98,5 +104,23 @@ function testTaxonomyVocabularyTree() { ...@@ -98,5 +104,23 @@ function testTaxonomyVocabularyTree() {
$this->assertEqual(2, $depth_count[1], 'Two elements in taxonomy tree depth 1.'); $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(2, $depth_count[2], 'Two elements in taxonomy tree depth 2.');
$this->assertEqual(1, $depth_count[3], 'One element in taxonomy tree depth 3.'); $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.');
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment