diff options
author | Dries Buytaert <dries@buytaert.net> | 2010-05-28 10:17:02 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2010-05-28 10:17:02 +0000 |
commit | 1613777a1e957be90e451aa9166199da8df7d189 (patch) | |
tree | 9a8911d351d847e1c0ffedba2d62f60e8aa6043f | |
parent | f5d08f5e82e899b0c6db1dbd04d5371c764796c6 (diff) | |
download | brdo-1613777a1e957be90e451aa9166199da8df7d189.tar.gz brdo-1613777a1e957be90e451aa9166199da8df7d189.tar.bz2 |
- Patch #811346 by Berdir: trying to load a term for deleting after vocabulary has been deleted does not work and results in notices.
-rw-r--r-- | modules/taxonomy/taxonomy.module | 9 | ||||
-rw-r--r-- | modules/taxonomy/taxonomy.test | 34 |
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() { |