Commit 1613777a authored by Dries's avatar Dries
Browse files

- Patch #811346 by Berdir: trying to load a term for deleting after vocabulary...

- Patch #811346 by Berdir: trying to load a term for deleting after vocabulary has been deleted does not work and results in notices.
parent f5d08f5e
...@@ -404,13 +404,14 @@ function taxonomy_vocabulary_save($vocabulary) { ...@@ -404,13 +404,14 @@ function taxonomy_vocabulary_save($vocabulary) {
function taxonomy_vocabulary_delete($vid) { function taxonomy_vocabulary_delete($vid) {
$vocabulary = (array) taxonomy_vocabulary_load($vid); $vocabulary = (array) taxonomy_vocabulary_load($vid);
db_delete('taxonomy_vocabulary') // Only load terms without a parent, child terms will get deleted too.
->condition('vid', $vid) $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();
->execute();
$result = db_query('SELECT tid FROM {taxonomy_term_data} WHERE vid = :vid', array(':vid' => $vid))->fetchCol();
foreach ($result as $tid) { foreach ($result as $tid) {
taxonomy_term_delete($tid); taxonomy_term_delete($tid);
} }
db_delete('taxonomy_vocabulary')
->condition('vid', $vid)
->execute();
field_attach_delete_bundle('taxonomy_term', $vocabulary['machine_name']); field_attach_delete_bundle('taxonomy_term', $vocabulary['machine_name']);
module_invoke_all('taxonomy', 'delete', 'vocabulary', $vocabulary); module_invoke_all('taxonomy', 'delete', 'vocabulary', $vocabulary);
......
...@@ -217,6 +217,40 @@ class TaxonomyVocabularyUnitTest extends TaxonomyWebTestCase { ...@@ -217,6 +217,40 @@ class TaxonomyVocabularyUnitTest extends TaxonomyWebTestCase {
$this->assertTrue($vocabulary->vid == $vid, t('Valid vocabulary vid is the same as our previously invalid one.')); $this->assertTrue($vocabulary->vid == $vid, t('Valid vocabulary vid is the same as our previously invalid one.'));
} }
/**
* 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. * Ensure that the vocabulary static reset works correctly.
*/ */
......
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