From 824eb3cd6cb0ab7bd01531c7a1971b2d77ed8886 Mon Sep 17 00:00:00 2001 From: webchick Date: Thu, 1 Mar 2012 20:23:24 -0800 Subject: Issue #687180 by Island Usurper, catch, Berdir, Damien Tournoud, xjm, anthbel: Fixed Deleting a taxonomy vocabulary leaves term reference fields still pointing to it, and a PDO Exception when creating content. --- modules/taxonomy/taxonomy.module | 24 ++++++++ modules/taxonomy/taxonomy.test | 126 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 150 insertions(+) (limited to 'modules/taxonomy') diff --git a/modules/taxonomy/taxonomy.module b/modules/taxonomy/taxonomy.module index 28d488ec8..3879efdb6 100644 --- a/modules/taxonomy/taxonomy.module +++ b/modules/taxonomy/taxonomy.module @@ -475,6 +475,30 @@ function taxonomy_vocabulary_delete($vid) { module_invoke_all('taxonomy_vocabulary_delete', $vocabulary); module_invoke_all('entity_delete', $vocabulary, 'taxonomy_vocabulary'); + // Load all Taxonomy module fields and delete those which use only this + // vocabulary. + $taxonomy_fields = field_read_fields(array('module' => 'taxonomy')); + foreach ($taxonomy_fields as $field_name => $taxonomy_field) { + $modified_field = FALSE; + // Term reference fields may reference terms from more than one + // vocabulary. + foreach ($taxonomy_field['settings']['allowed_values'] as $key => $allowed_value) { + if ($allowed_value['vocabulary'] == $vocabulary->machine_name) { + unset($taxonomy_field['settings']['allowed_values'][$key]); + $modified_field = TRUE; + } + } + if ($modified_field) { + if (empty($taxonomy_field['settings']['allowed_values'])) { + field_delete_field($field_name); + } + else { + // Update the field definition with the new allowed values. + field_update_field($taxonomy_field); + } + } + } + cache_clear_all(); taxonomy_vocabulary_static_reset(); diff --git a/modules/taxonomy/taxonomy.test b/modules/taxonomy/taxonomy.test index 98f44000e..372982c9b 100644 --- a/modules/taxonomy/taxonomy.test +++ b/modules/taxonomy/taxonomy.test @@ -1436,6 +1436,11 @@ class TaxonomyTermFieldTestCase extends TaxonomyWebTestCase { $entity->content = field_attach_view('test_entity', $entity, 'full'); $this->content = drupal_render($entity->content); $this->assertText($term->name, t('Term name is displayed')); + + // Delete the vocabulary and verify that the widget is gone. + taxonomy_vocabulary_delete($this->vocabulary->vid); + $this->drupalGet('test-entity/add/test-bundle'); + $this->assertNoFieldByName("{$this->field_name}[$langcode]", '', 'Widget is not displayed'); } /** @@ -1479,6 +1484,127 @@ class TaxonomyTermFieldTestCase extends TaxonomyWebTestCase { } } +/** + * Tests a taxonomy term reference field that allows multiple vocabularies. + */ +class TaxonomyTermFieldMultipleVocabularyTestCase extends TaxonomyWebTestCase { + protected $instance; + protected $vocabulary1; + protected $vocabulary2; + + public static function getInfo() { + return array( + 'name' => 'Multiple vocabulary term reference field', + 'description' => 'Tests term reference fields that allow multiple vocabularies.', + 'group' => 'Taxonomy', + ); + } + + function setUp() { + parent::setUp('field_test'); + + $web_user = $this->drupalCreateUser(array('access field_test content', 'administer field_test content', 'administer taxonomy')); + $this->drupalLogin($web_user); + $this->vocabulary1 = $this->createVocabulary(); + $this->vocabulary2 = $this->createVocabulary(); + + // Set up a field and instance. + $this->field_name = drupal_strtolower($this->randomName()); + $this->field = array( + 'field_name' => $this->field_name, + 'type' => 'taxonomy_term_reference', + 'cardinality' => FIELD_CARDINALITY_UNLIMITED, + 'settings' => array( + 'allowed_values' => array( + array( + 'vocabulary' => $this->vocabulary1->machine_name, + 'parent' => '0', + ), + array( + 'vocabulary' => $this->vocabulary2->machine_name, + 'parent' => '0', + ), + ), + ) + ); + field_create_field($this->field); + $this->instance = array( + 'field_name' => $this->field_name, + 'entity_type' => 'test_entity', + 'bundle' => 'test_bundle', + 'widget' => array( + 'type' => 'options_select', + ), + 'display' => array( + 'full' => array( + 'type' => 'taxonomy_term_reference_link', + ), + ), + ); + field_create_instance($this->instance); + } + + /** + * Tests term reference field and widget with multiple vocabularies. + */ + function testTaxonomyTermFieldMultipleVocabularies() { + // Create a term in each vocabulary. + $term1 = $this->createTerm($this->vocabulary1); + $term2 = $this->createTerm($this->vocabulary2); + + // Submit an entity with both terms. + $langcode = LANGUAGE_NONE; + $this->drupalGet('test-entity/add/test-bundle'); + $this->assertFieldByName("{$this->field_name}[$langcode][]", '', 'Widget is displayed'); + $edit = array( + "{$this->field_name}[$langcode][]" => array($term1->tid, $term2->tid), + ); + $this->drupalPost(NULL, $edit, t('Save')); + preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match); + $id = $match[1]; + $this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), 'Entity was created.'); + + // Render the entity. + $entity = field_test_entity_test_load($id); + $entities = array($id => $entity); + field_attach_prepare_view('test_entity', $entities, 'full'); + $entity->content = field_attach_view('test_entity', $entity, 'full'); + $this->content = drupal_render($entity->content); + $this->assertText($term1->name, 'Term 1 name is displayed.'); + $this->assertText($term2->name, 'Term 2 name is displayed.'); + + // Delete vocabulary 2. + taxonomy_vocabulary_delete($this->vocabulary2->vid); + + // Re-render the content. + $entity = field_test_entity_test_load($id); + $entities = array($id => $entity); + field_attach_prepare_view('test_entity', $entities, 'full'); + $entity->content = field_attach_view('test_entity', $entity, 'full'); + $this->plainTextContent = FALSE; + $this->content = drupal_render($entity->content); + + // Term 1 should still be displayed; term 2 should not be. + $this->assertText($term1->name, 'Term 1 name is displayed.'); + $this->assertNoText($term2->name, 'Term 2 name is not displayed.'); + + // Verify that field and instance settings are correct. + $field_info = field_info_field($this->field_name); + $this->assertTrue(sizeof($field_info['settings']['allowed_values']) == 1, 'Only one vocabulary is allowed for the field.'); + + // The widget should still be displayed. + $this->drupalGet('test-entity/add/test-bundle'); + $this->assertFieldByName("{$this->field_name}[$langcode][]", '', 'Widget is still displayed'); + + // Term 1 should still pass validation. + $edit = array( + "{$this->field_name}[$langcode][]" => array($term1->tid), + ); + $this->drupalPost(NULL, $edit, t('Save')); + } +} + + /** * Test taxonomy token replacement in strings. */ -- cgit v1.2.3