3 namespace Drupal\Tests\taxonomy\Functional;
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\taxonomy\Entity\Vocabulary;
7 use Drupal\field\Entity\FieldStorageConfig;
10 * Tests loading, saving and deleting vocabularies.
14 class VocabularyCrudTest extends TaxonomyTestBase {
21 public static $modules = ['field_test', 'taxonomy_crud'];
23 protected function setUp() {
26 $admin_user = $this->drupalCreateUser(['create article content', 'administer taxonomy']);
27 $this->drupalLogin($admin_user);
28 $this->vocabulary = $this->createVocabulary();
32 * Test deleting a taxonomy that contains terms.
34 public function testTaxonomyVocabularyDeleteWithTerms() {
35 // Delete any existing vocabularies.
36 foreach (Vocabulary::loadMultiple() as $vocabulary) {
37 $vocabulary->delete();
39 $query = \Drupal::entityQuery('taxonomy_term')->count();
41 // Assert that there are no terms left.
42 $this->assertEqual(0, $query->execute(), 'There are no terms remaining.');
45 for ($i = 0; $i < 5; $i++) {
46 $terms[$i] = $this->createTerm($vocabulary);
49 // Set up hierarchy. term 2 is a child of 1 and 4 a child of 1 and 2.
50 $terms[2]->parent = [$terms[1]->id()];
52 $terms[4]->parent = [$terms[1]->id(), $terms[2]->id()];
55 // Assert that there are now 5 terms.
56 $this->assertEqual(5, $query->execute(), 'There are 5 terms found.');
58 $vocabulary->delete();
60 // Assert that there are no terms left.
61 $this->assertEqual(0, $query->execute(), 'All terms are deleted.');
65 * Ensure that the vocabulary static reset works correctly.
67 public function testTaxonomyVocabularyLoadStaticReset() {
68 $original_vocabulary = Vocabulary::load($this->vocabulary->id());
69 $this->assertTrue(is_object($original_vocabulary), 'Vocabulary loaded successfully.');
70 $this->assertEqual($this->vocabulary->label(), $original_vocabulary->label(), 'Vocabulary loaded successfully.');
72 // Change the name and description.
73 $vocabulary = $original_vocabulary;
74 $vocabulary->set('name', $this->randomMachineName());
75 $vocabulary->set('description', $this->randomMachineName());
78 // Load the vocabulary.
79 $new_vocabulary = Vocabulary::load($original_vocabulary->id());
80 $this->assertEqual($new_vocabulary->label(), $vocabulary->label(), 'The vocabulary was loaded.');
82 // Delete the vocabulary.
83 $this->vocabulary->delete();
84 $vocabularies = Vocabulary::loadMultiple();
85 $this->assertTrue(!isset($vocabularies[$this->vocabulary->id()]), 'The vocabulary was deleted.');
89 * Tests for loading multiple vocabularies.
91 public function testTaxonomyVocabularyLoadMultiple() {
93 // Delete any existing vocabularies.
94 foreach (Vocabulary::loadMultiple() as $vocabulary) {
95 $vocabulary->delete();
98 // Create some vocabularies and assign weights.
99 $vocabulary1 = $this->createVocabulary();
100 $vocabulary1->set('weight', 0);
101 $vocabulary1->save();
102 $vocabulary2 = $this->createVocabulary();
103 $vocabulary2->set('weight', 1);
104 $vocabulary2->save();
105 $vocabulary3 = $this->createVocabulary();
106 $vocabulary3->set('weight', 2);
107 $vocabulary3->save();
109 // Check if third party settings exist.
110 $this->assertEqual('bar', $vocabulary1->getThirdPartySetting('taxonomy_crud', 'foo'), 'Third party settings were added to the vocabulary.');
111 $this->assertEqual('bar', $vocabulary2->getThirdPartySetting('taxonomy_crud', 'foo'), 'Third party settings were added to the vocabulary.');
112 $this->assertEqual('bar', $vocabulary3->getThirdPartySetting('taxonomy_crud', 'foo'), 'Third party settings were added to the vocabulary.');
114 // Fetch the names for all vocabularies, confirm that they are keyed by
116 $names = taxonomy_vocabulary_get_names();
117 $this->assertEqual($names[$vocabulary1->id()], $vocabulary1->id(), 'Vocabulary 1 name found.');
119 // Fetch the vocabularies with entity_load_multiple(), specifying IDs.
120 // Ensure they are returned in the same order as the original array.
121 $vocabularies = Vocabulary::loadMultiple([$vocabulary3->id(), $vocabulary2->id(), $vocabulary1->id()]);
122 $loaded_order = array_keys($vocabularies);
123 $expected_order = [$vocabulary3->id(), $vocabulary2->id(), $vocabulary1->id()];
124 $this->assertIdentical($loaded_order, $expected_order);
126 // Test loading vocabularies by their properties.
127 $controller = $this->container->get('entity.manager')->getStorage('taxonomy_vocabulary');
128 // Fetch vocabulary 1 by name.
129 $vocabulary = current($controller->loadByProperties(['name' => $vocabulary1->label()]));
130 $this->assertEqual($vocabulary->id(), $vocabulary1->id(), 'Vocabulary loaded successfully by name.');
132 // Fetch vocabulary 2 by name and ID.
133 $vocabulary = current($controller->loadByProperties([
134 'name' => $vocabulary2->label(),
135 'vid' => $vocabulary2->id(),
137 $this->assertEqual($vocabulary->id(), $vocabulary2->id(), 'Vocabulary loaded successfully by name and ID.');
141 * Test uninstall and reinstall of the taxonomy module.
143 public function testUninstallReinstall() {
144 // Field storages and fields attached to taxonomy term bundles should be
145 // removed when the module is uninstalled.
146 $field_name = mb_strtolower($this->randomMachineName() . '_field_name');
147 $storage_definition = [
148 'field_name' => $field_name,
149 'entity_type' => 'taxonomy_term',
153 FieldStorageConfig::create($storage_definition)->save();
154 $field_definition = [
155 'field_name' => $field_name,
156 'entity_type' => 'taxonomy_term',
157 'bundle' => $this->vocabulary->id(),
158 'label' => $this->randomMachineName() . '_label',
160 FieldConfig::create($field_definition)->save();
162 // Remove the third party setting from the memory copy of the vocabulary.
163 // We keep this invalid copy around while the taxonomy module is not even
164 // installed for testing below.
165 $this->vocabulary->unsetThirdPartySetting('taxonomy_crud', 'foo');
167 require_once $this->root . '/core/includes/install.inc';
168 $this->container->get('module_installer')->uninstall(['taxonomy']);
169 $this->container->get('module_installer')->install(['taxonomy']);
171 // Now create a vocabulary with the same name. All fields
172 // connected to this vocabulary name should have been removed when the
173 // module was uninstalled. Creating a new field with the same name and
174 // an instance of this field on the same bundle name should be successful.
175 $this->vocabulary->enforceIsNew();
176 $this->vocabulary->save();
177 FieldStorageConfig::create($storage_definition)->save();
178 FieldConfig::create($field_definition)->save();