3 namespace Drupal\Tests\taxonomy\Functional;
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\taxonomy\Entity\Vocabulary;
8 use Drupal\field\Entity\FieldStorageConfig;
11 * Tests loading, saving and deleting vocabularies.
15 class VocabularyCrudTest extends TaxonomyTestBase {
22 public static $modules = ['field_test', 'taxonomy_crud'];
24 protected function setUp() {
27 $admin_user = $this->drupalCreateUser(['create article content', 'administer taxonomy']);
28 $this->drupalLogin($admin_user);
29 $this->vocabulary = $this->createVocabulary();
33 * Test deleting a taxonomy that contains terms.
35 public function testTaxonomyVocabularyDeleteWithTerms() {
36 // Delete any existing vocabularies.
37 foreach (Vocabulary::loadMultiple() as $vocabulary) {
38 $vocabulary->delete();
40 $query = \Drupal::entityQuery('taxonomy_term')->count();
42 // Assert that there are no terms left.
43 $this->assertEqual(0, $query->execute(), 'There are no terms remaining.');
46 for ($i = 0; $i < 5; $i++) {
47 $terms[$i] = $this->createTerm($vocabulary);
50 // Set up hierarchy. term 2 is a child of 1 and 4 a child of 1 and 2.
51 $terms[2]->parent = [$terms[1]->id()];
53 $terms[4]->parent = [$terms[1]->id(), $terms[2]->id()];
56 // Assert that there are now 5 terms.
57 $this->assertEqual(5, $query->execute(), 'There are 5 terms found.');
59 $vocabulary->delete();
61 // Assert that there are no terms left.
62 $this->assertEqual(0, $query->execute(), 'All terms are deleted.');
66 * Ensure that the vocabulary static reset works correctly.
68 public function testTaxonomyVocabularyLoadStaticReset() {
69 $original_vocabulary = Vocabulary::load($this->vocabulary->id());
70 $this->assertTrue(is_object($original_vocabulary), 'Vocabulary loaded successfully.');
71 $this->assertEqual($this->vocabulary->label(), $original_vocabulary->label(), 'Vocabulary loaded successfully.');
73 // Change the name and description.
74 $vocabulary = $original_vocabulary;
75 $vocabulary->set('name', $this->randomMachineName());
76 $vocabulary->set('description', $this->randomMachineName());
79 // Load the vocabulary.
80 $new_vocabulary = Vocabulary::load($original_vocabulary->id());
81 $this->assertEqual($new_vocabulary->label(), $vocabulary->label(), 'The vocabulary was loaded.');
83 // Delete the vocabulary.
84 $this->vocabulary->delete();
85 $vocabularies = Vocabulary::loadMultiple();
86 $this->assertTrue(!isset($vocabularies[$this->vocabulary->id()]), 'The vocabulary was deleted.');
90 * Tests for loading multiple vocabularies.
92 public function testTaxonomyVocabularyLoadMultiple() {
94 // Delete any existing vocabularies.
95 foreach (Vocabulary::loadMultiple() as $vocabulary) {
96 $vocabulary->delete();
99 // Create some vocabularies and assign weights.
100 $vocabulary1 = $this->createVocabulary();
101 $vocabulary1->set('weight', 0);
102 $vocabulary1->save();
103 $vocabulary2 = $this->createVocabulary();
104 $vocabulary2->set('weight', 1);
105 $vocabulary2->save();
106 $vocabulary3 = $this->createVocabulary();
107 $vocabulary3->set('weight', 2);
108 $vocabulary3->save();
110 // Check if third party settings exist.
111 $this->assertEqual('bar', $vocabulary1->getThirdPartySetting('taxonomy_crud', 'foo'), 'Third party settings were added to the vocabulary.');
112 $this->assertEqual('bar', $vocabulary2->getThirdPartySetting('taxonomy_crud', 'foo'), 'Third party settings were added to the vocabulary.');
113 $this->assertEqual('bar', $vocabulary3->getThirdPartySetting('taxonomy_crud', 'foo'), 'Third party settings were added to the vocabulary.');
115 // Fetch the names for all vocabularies, confirm that they are keyed by
117 $names = taxonomy_vocabulary_get_names();
118 $this->assertEqual($names[$vocabulary1->id()], $vocabulary1->id(), 'Vocabulary 1 name found.');
120 // Fetch the vocabularies with entity_load_multiple(), specifying IDs.
121 // Ensure they are returned in the same order as the original array.
122 $vocabularies = Vocabulary::loadMultiple([$vocabulary3->id(), $vocabulary2->id(), $vocabulary1->id()]);
123 $loaded_order = array_keys($vocabularies);
124 $expected_order = [$vocabulary3->id(), $vocabulary2->id(), $vocabulary1->id()];
125 $this->assertIdentical($loaded_order, $expected_order);
127 // Test loading vocabularies by their properties.
128 $controller = $this->container->get('entity.manager')->getStorage('taxonomy_vocabulary');
129 // Fetch vocabulary 1 by name.
130 $vocabulary = current($controller->loadByProperties(['name' => $vocabulary1->label()]));
131 $this->assertEqual($vocabulary->id(), $vocabulary1->id(), 'Vocabulary loaded successfully by name.');
133 // Fetch vocabulary 2 by name and ID.
134 $vocabulary = current($controller->loadByProperties([
135 'name' => $vocabulary2->label(),
136 'vid' => $vocabulary2->id(),
138 $this->assertEqual($vocabulary->id(), $vocabulary2->id(), 'Vocabulary loaded successfully by name and ID.');
142 * Test uninstall and reinstall of the taxonomy module.
144 public function testUninstallReinstall() {
145 // Field storages and fields attached to taxonomy term bundles should be
146 // removed when the module is uninstalled.
147 $field_name = Unicode::strtolower($this->randomMachineName() . '_field_name');
148 $storage_definition = [
149 'field_name' => $field_name,
150 'entity_type' => 'taxonomy_term',
154 FieldStorageConfig::create($storage_definition)->save();
155 $field_definition = [
156 'field_name' => $field_name,
157 'entity_type' => 'taxonomy_term',
158 'bundle' => $this->vocabulary->id(),
159 'label' => $this->randomMachineName() . '_label',
161 FieldConfig::create($field_definition)->save();
163 // Remove the third party setting from the memory copy of the vocabulary.
164 // We keep this invalid copy around while the taxonomy module is not even
165 // installed for testing below.
166 $this->vocabulary->unsetThirdPartySetting('taxonomy_crud', 'foo');
168 require_once \Drupal::root() . '/core/includes/install.inc';
169 $this->container->get('module_installer')->uninstall(['taxonomy']);
170 $this->container->get('module_installer')->install(['taxonomy']);
172 // Now create a vocabulary with the same name. All fields
173 // connected to this vocabulary name should have been removed when the
174 // module was uninstalled. Creating a new field with the same name and
175 // an instance of this field on the same bundle name should be successful.
176 $this->vocabulary->enforceIsNew();
177 $this->vocabulary->save();
178 FieldStorageConfig::create($storage_definition)->save();
179 FieldConfig::create($field_definition)->save();