summaryrefslogtreecommitdiff
path: root/modules/taxonomy
diff options
context:
space:
mode:
authorDries <dries@buytaert.net>2012-01-23 09:01:44 -0500
committerDries <dries@buytaert.net>2012-01-23 09:01:44 -0500
commitaf8bd618722a38e8aafc6732b0efacd362b3e631 (patch)
treefb861e28350c58dc49827fda00b7f12ac33c205b /modules/taxonomy
parent9318096df2f22e59f993c48db932c39a15bcc41f (diff)
downloadbrdo-af8bd618722a38e8aafc6732b0efacd362b3e631.tar.gz
brdo-af8bd618722a38e8aafc6732b0efacd362b3e631.tar.bz2
- Patch #1330554 by oriol_e9g, fiftyz, richthegeek: Fixed taxonomy_get_tree() incorrect depth on multiple parents.
Diffstat (limited to 'modules/taxonomy')
-rw-r--r--modules/taxonomy/taxonomy.module6
-rw-r--r--modules/taxonomy/taxonomy.test54
2 files changed, 57 insertions, 3 deletions
diff --git a/modules/taxonomy/taxonomy.module b/modules/taxonomy/taxonomy.module
index 7c11bccaf..ccbd7c523 100644
--- a/modules/taxonomy/taxonomy.module
+++ b/modules/taxonomy/taxonomy.module
@@ -1026,9 +1026,9 @@ function taxonomy_get_tree($vid, $parent = 0, $max_depth = NULL, $load_entities
break;
}
$term = $load_entities ? $term_entities[$child] : $terms[$vid][$child];
- if (count($parents[$vid][$term->tid]) > 1) {
- // We have a term with multi parents here. Clone the term,
- // so that the depth attribute remains correct.
+ if (isset($parents[$vid][$term->tid])) {
+ // Clone the term so that the depth attribute remains correct
+ // in the event of multiple parents.
$term = clone $term;
}
$term->depth = $depth;
diff --git a/modules/taxonomy/taxonomy.test b/modules/taxonomy/taxonomy.test
index 0a5bdf340..3fb5b64cc 100644
--- a/modules/taxonomy/taxonomy.test
+++ b/modules/taxonomy/taxonomy.test
@@ -410,6 +410,60 @@ class TaxonomyTermUnitTest extends TaxonomyWebTestCase {
// Delete an invalid term. Should not throw any notices.
taxonomy_term_delete(42);
}
+
+ /**
+ * Test a taxonomy with terms that have multiple parents of different depths.
+ */
+ function testTaxonomyVocabularyTree() {
+ // Create a new vocabulary with 6 terms.
+ $vocabulary = $this->createVocabulary();
+ $term = array();
+ for ($i = 0; $i < 6; $i++) {
+ $term[$i] = $this->createTerm($vocabulary);
+ }
+
+ // $term[2] is a child of 1 and 5.
+ $term[2]->parent = array($term[1]->tid, $term[5]->tid);
+ taxonomy_term_save($term[2]);
+ // $term[3] is a child of 2.
+ $term[3]->parent = array($term[2]->tid);
+ taxonomy_term_save($term[3]);
+ // $term[5] is a child of 4.
+ $term[5]->parent = array($term[4]->tid);
+ taxonomy_term_save($term[5]);
+
+ /**
+ * Expected tree:
+ * term[0] | depth: 0
+ * term[1] | depth: 0
+ * -- term[2] | depth: 1
+ * ---- term[3] | depth: 2
+ * term[4] | depth: 0
+ * -- term[5] | depth: 1
+ * ---- term[2] | depth: 2
+ * ------ term[3] | depth: 3
+ */
+
+ // Count $term[1] parents with $max_depth = 1.
+ $tree = taxonomy_get_tree($vocabulary->vid, $term[1]->tid, 1);
+ $this->assertEqual(1, count($tree), 'We have one parent with depth 1.');
+
+ // Count all vocabulary tree elements.
+ $tree = taxonomy_get_tree($vocabulary->vid);
+ $this->assertEqual(8, count($tree), 'We have all vocabulary tree elements.');
+
+ // Count elements in every tree depth.
+ foreach($tree as $element) {
+ if (!isset($depth_count[$element->depth])) {
+ $depth_count[$element->depth] = 0;
+ }
+ $depth_count[$element->depth]++;
+ }
+ $this->assertEqual(3, $depth_count[0], 'Three elements in taxonomy tree depth 0.');
+ $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.');
+ }
}
/**