annotate core/modules/taxonomy/tests/src/Functional/VocabularyCrudTest.php @ 19:fa3358dc1485 tip

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