From 1651cf34bfbdf02f6c03f8d00b025a610d24df23 Mon Sep 17 00:00:00 2001
From: Dries Buytaert <dries@buytaert.net>
Date: Sun, 22 Aug 2010 15:45:03 +0000
Subject: [PATCH] - Patch #841798 by klausi: error deleting an invalid term.

---
 modules/taxonomy/taxonomy.module | 24 ++++++++++++------------
 modules/taxonomy/taxonomy.test   | 12 ++++++++++++
 2 files changed, 24 insertions(+), 12 deletions(-)

diff --git a/modules/taxonomy/taxonomy.module b/modules/taxonomy/taxonomy.module
index b060a8dcbf5a..cb1f255920be 100644
--- a/modules/taxonomy/taxonomy.module
+++ b/modules/taxonomy/taxonomy.module
@@ -570,18 +570,18 @@ function taxonomy_term_delete($tid) {
         }
       }
 
-      $term = taxonomy_term_load($tid);
-
-      db_delete('taxonomy_term_data')
-        ->condition('tid', $tid)
-        ->execute();
-      db_delete('taxonomy_term_hierarchy')
-        ->condition('tid', $tid)
-        ->execute();
-
-      field_attach_delete('taxonomy_term', $term);
-      module_invoke_all('taxonomy_term_delete', $term);
-      taxonomy_terms_static_reset();
+      if ($term = taxonomy_term_load($tid)) {
+        db_delete('taxonomy_term_data')
+          ->condition('tid', $tid)
+          ->execute();
+        db_delete('taxonomy_term_hierarchy')
+          ->condition('tid', $tid)
+          ->execute();
+
+        field_attach_delete('taxonomy_term', $term);
+        module_invoke_all('taxonomy_term_delete', $term);
+        taxonomy_terms_static_reset();
+      }
     }
 
     $tids = $orphans;
diff --git a/modules/taxonomy/taxonomy.test b/modules/taxonomy/taxonomy.test
index af50e5346267..a9938b087904 100644
--- a/modules/taxonomy/taxonomy.test
+++ b/modules/taxonomy/taxonomy.test
@@ -337,6 +337,18 @@ class TaxonomyTermUnitTest extends TaxonomyWebTestCase {
       'group' => 'Taxonomy',
     );
   }
+
+  function testTermDelete() {
+    $vocabulary = $this->createVocabulary();
+    $valid_term = $this->createTerm($vocabulary);
+    // Delete a valid term.
+    taxonomy_term_delete($valid_term->tid);
+    $terms = taxonomy_term_load_multiple(array(), array('vid' => $vocabulary->vid));
+    $this->assertTrue(empty($terms), 'Vocabulary is empty after deletion');
+
+    // Delete an invalid term. Should not throw any notices.
+    taxonomy_term_delete(42);
+  }
 }
 
 /**
-- 
GitLab