Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / taxonomy / tests / src / Functional / VocabularyCrudTest.php
1 <?php
2
3 namespace Drupal\Tests\taxonomy\Functional;
4
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\taxonomy\Entity\Vocabulary;
7 use Drupal\field\Entity\FieldStorageConfig;
8
9 /**
10  * Tests loading, saving and deleting vocabularies.
11  *
12  * @group taxonomy
13  */
14 class VocabularyCrudTest extends TaxonomyTestBase {
15
16   /**
17    * Modules to enable.
18    *
19    * @var array
20    */
21   public static $modules = ['field_test', 'taxonomy_crud'];
22
23   protected function setUp() {
24     parent::setUp();
25
26     $admin_user = $this->drupalCreateUser(['create article content', 'administer taxonomy']);
27     $this->drupalLogin($admin_user);
28     $this->vocabulary = $this->createVocabulary();
29   }
30
31   /**
32    * Test deleting a taxonomy that contains terms.
33    */
34   public function testTaxonomyVocabularyDeleteWithTerms() {
35     // Delete any existing vocabularies.
36     foreach (Vocabulary::loadMultiple() as $vocabulary) {
37       $vocabulary->delete();
38     }
39     $query = \Drupal::entityQuery('taxonomy_term')->count();
40
41     // Assert that there are no terms left.
42     $this->assertEqual(0, $query->execute(), 'There are no terms remaining.');
43
44     $terms = [];
45     for ($i = 0; $i < 5; $i++) {
46       $terms[$i] = $this->createTerm($vocabulary);
47     }
48
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()];
51     $terms[2]->save();
52     $terms[4]->parent = [$terms[1]->id(), $terms[2]->id()];
53     $terms[4]->save();
54
55     // Assert that there are now 5 terms.
56     $this->assertEqual(5, $query->execute(), 'There are 5 terms found.');
57
58     $vocabulary->delete();
59
60     // Assert that there are no terms left.
61     $this->assertEqual(0, $query->execute(), 'All terms are deleted.');
62   }
63
64   /**
65    * Ensure that the vocabulary static reset works correctly.
66    */
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.');
71
72     // Change the name and description.
73     $vocabulary = $original_vocabulary;
74     $vocabulary->set('name', $this->randomMachineName());
75     $vocabulary->set('description', $this->randomMachineName());
76     $vocabulary->save();
77
78     // Load the vocabulary.
79     $new_vocabulary = Vocabulary::load($original_vocabulary->id());
80     $this->assertEqual($new_vocabulary->label(), $vocabulary->label(), 'The vocabulary was loaded.');
81
82     // Delete the vocabulary.
83     $this->vocabulary->delete();
84     $vocabularies = Vocabulary::loadMultiple();
85     $this->assertTrue(!isset($vocabularies[$this->vocabulary->id()]), 'The vocabulary was deleted.');
86   }
87
88   /**
89    * Tests for loading multiple vocabularies.
90    */
91   public function testTaxonomyVocabularyLoadMultiple() {
92
93     // Delete any existing vocabularies.
94     foreach (Vocabulary::loadMultiple() as $vocabulary) {
95       $vocabulary->delete();
96     }
97
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();
108
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.');
113
114     // Fetch the names for all vocabularies, confirm that they are keyed by
115     // machine name.
116     $names = taxonomy_vocabulary_get_names();
117     $this->assertEqual($names[$vocabulary1->id()], $vocabulary1->id(), 'Vocabulary 1 name found.');
118
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);
125
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.');
131
132     // Fetch vocabulary 2 by name and ID.
133     $vocabulary = current($controller->loadByProperties([
134       'name' => $vocabulary2->label(),
135       'vid' => $vocabulary2->id(),
136     ]));
137     $this->assertEqual($vocabulary->id(), $vocabulary2->id(), 'Vocabulary loaded successfully by name and ID.');
138   }
139
140   /**
141    * Test uninstall and reinstall of the taxonomy module.
142    */
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',
150       'type' => 'text',
151       'cardinality' => 4,
152     ];
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',
159     ];
160     FieldConfig::create($field_definition)->save();
161
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');
166
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']);
170
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();
179   }
180
181 }