summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--modules/taxonomy/taxonomy.module9
-rw-r--r--modules/taxonomy/taxonomy.test34
2 files changed, 39 insertions, 4 deletions
diff --git a/modules/taxonomy/taxonomy.module b/modules/taxonomy/taxonomy.module
index 6dd9b36c1..b85f0ec19 100644
--- a/modules/taxonomy/taxonomy.module
+++ b/modules/taxonomy/taxonomy.module
@@ -404,13 +404,14 @@ function taxonomy_vocabulary_save($vocabulary) {
function taxonomy_vocabulary_delete($vid) {
$vocabulary = (array) taxonomy_vocabulary_load($vid);
- db_delete('taxonomy_vocabulary')
- ->condition('vid', $vid)
- ->execute();
- $result = db_query('SELECT tid FROM {taxonomy_term_data} WHERE vid = :vid', array(':vid' => $vid))->fetchCol();
+ // Only load terms without a parent, child terms will get deleted too.
+ $result = db_query('SELECT t.tid FROM {taxonomy_term_data} t INNER JOIN {taxonomy_term_hierarchy} th ON th.tid = t.tid WHERE t.vid = :vid AND th.parent = 0', array(':vid' => $vid))->fetchCol();
foreach ($result as $tid) {
taxonomy_term_delete($tid);
}
+ db_delete('taxonomy_vocabulary')
+ ->condition('vid', $vid)
+ ->execute();
field_attach_delete_bundle('taxonomy_term', $vocabulary['machine_name']);
module_invoke_all('taxonomy', 'delete', 'vocabulary', $vocabulary);
diff --git a/modules/taxonomy/taxonomy.test b/modules/taxonomy/taxonomy.test
index d60f1e616..29b495fd0 100644
--- a/modules/taxonomy/taxonomy.test
+++ b/modules/taxonomy/taxonomy.test
@@ -218,6 +218,40 @@ class TaxonomyVocabularyUnitTest extends TaxonomyWebTestCase {
}
/**
+ * Test deleting a taxonomy that contains terms.
+ */
+ function testTaxonomyVocabularyDeleteWithTerms() {
+ // Delete any existing vocabularies.
+ foreach (taxonomy_get_vocabularies() as $vocabulary) {
+ taxonomy_vocabulary_delete($vocabulary->vid);
+ }
+
+ // Assert that there are no terms left.
+ $this->assertEqual(0, db_query('SELECT COUNT(*) FROM {taxonomy_term_data}')->fetchField());
+
+ // Create a new vocabulary and add a few terms to it.
+ $vocabulary = $this->createVocabulary();
+ $terms = array();
+ for ($i = 0; $i < 5; $i++) {
+ $terms[$i] = $this->createTerm($vocabulary);
+ }
+
+ // Set up hierarchy. term 2 is a child of 1 and 4 a child of 1 and 2.
+ $terms[2]->parent = array($terms[1]->tid);
+ taxonomy_term_save($terms[2]);
+ $terms[4]->parent = array($terms[1]->tid, $terms[2]->tid);
+ taxonomy_term_save($terms[4]);
+
+ // Assert that there are now 5 terms.
+ $this->assertEqual(5, db_query('SELECT COUNT(*) FROM {taxonomy_term_data}')->fetchField());
+
+ taxonomy_vocabulary_delete($vocabulary->vid);
+
+ // Assert that there are no terms left.
+ $this->assertEqual(0, db_query('SELECT COUNT(*) FROM {taxonomy_term_data}')->fetchField());
+ }
+
+ /**
* Ensure that the vocabulary static reset works correctly.
*/
function testTaxonomyVocabularyLoadStaticReset() {