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