Chris@0: drupalCreateUser(['create article content', 'administer taxonomy']); Chris@0: $this->drupalLogin($admin_user); Chris@0: $this->vocabulary = $this->createVocabulary(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test deleting a taxonomy that contains terms. Chris@0: */ Chris@0: public function testTaxonomyVocabularyDeleteWithTerms() { Chris@0: // Delete any existing vocabularies. Chris@0: foreach (Vocabulary::loadMultiple() as $vocabulary) { Chris@0: $vocabulary->delete(); Chris@0: } Chris@0: $query = \Drupal::entityQuery('taxonomy_term')->count(); Chris@0: Chris@0: // Assert that there are no terms left. Chris@0: $this->assertEqual(0, $query->execute(), 'There are no terms remaining.'); Chris@0: Chris@0: $terms = []; Chris@0: for ($i = 0; $i < 5; $i++) { Chris@0: $terms[$i] = $this->createTerm($vocabulary); Chris@0: } Chris@0: Chris@0: // Set up hierarchy. term 2 is a child of 1 and 4 a child of 1 and 2. Chris@0: $terms[2]->parent = [$terms[1]->id()]; Chris@0: $terms[2]->save(); Chris@0: $terms[4]->parent = [$terms[1]->id(), $terms[2]->id()]; Chris@0: $terms[4]->save(); Chris@0: Chris@0: // Assert that there are now 5 terms. Chris@0: $this->assertEqual(5, $query->execute(), 'There are 5 terms found.'); Chris@0: Chris@0: $vocabulary->delete(); Chris@0: Chris@0: // Assert that there are no terms left. Chris@0: $this->assertEqual(0, $query->execute(), 'All terms are deleted.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Ensure that the vocabulary static reset works correctly. Chris@0: */ Chris@0: public function testTaxonomyVocabularyLoadStaticReset() { Chris@0: $original_vocabulary = Vocabulary::load($this->vocabulary->id()); Chris@0: $this->assertTrue(is_object($original_vocabulary), 'Vocabulary loaded successfully.'); Chris@0: $this->assertEqual($this->vocabulary->label(), $original_vocabulary->label(), 'Vocabulary loaded successfully.'); Chris@0: Chris@0: // Change the name and description. Chris@0: $vocabulary = $original_vocabulary; Chris@0: $vocabulary->set('name', $this->randomMachineName()); Chris@0: $vocabulary->set('description', $this->randomMachineName()); Chris@0: $vocabulary->save(); Chris@0: Chris@0: // Load the vocabulary. Chris@0: $new_vocabulary = Vocabulary::load($original_vocabulary->id()); Chris@0: $this->assertEqual($new_vocabulary->label(), $vocabulary->label(), 'The vocabulary was loaded.'); Chris@0: Chris@0: // Delete the vocabulary. Chris@0: $this->vocabulary->delete(); Chris@0: $vocabularies = Vocabulary::loadMultiple(); Chris@0: $this->assertTrue(!isset($vocabularies[$this->vocabulary->id()]), 'The vocabulary was deleted.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests for loading multiple vocabularies. Chris@0: */ Chris@0: public function testTaxonomyVocabularyLoadMultiple() { Chris@0: Chris@0: // Delete any existing vocabularies. Chris@0: foreach (Vocabulary::loadMultiple() as $vocabulary) { Chris@0: $vocabulary->delete(); Chris@0: } Chris@0: Chris@0: // Create some vocabularies and assign weights. Chris@0: $vocabulary1 = $this->createVocabulary(); Chris@0: $vocabulary1->set('weight', 0); Chris@0: $vocabulary1->save(); Chris@0: $vocabulary2 = $this->createVocabulary(); Chris@0: $vocabulary2->set('weight', 1); Chris@0: $vocabulary2->save(); Chris@0: $vocabulary3 = $this->createVocabulary(); Chris@0: $vocabulary3->set('weight', 2); Chris@0: $vocabulary3->save(); Chris@0: Chris@0: // Check if third party settings exist. Chris@0: $this->assertEqual('bar', $vocabulary1->getThirdPartySetting('taxonomy_crud', 'foo'), 'Third party settings were added to the vocabulary.'); Chris@0: $this->assertEqual('bar', $vocabulary2->getThirdPartySetting('taxonomy_crud', 'foo'), 'Third party settings were added to the vocabulary.'); Chris@0: $this->assertEqual('bar', $vocabulary3->getThirdPartySetting('taxonomy_crud', 'foo'), 'Third party settings were added to the vocabulary.'); Chris@0: Chris@0: // Fetch the names for all vocabularies, confirm that they are keyed by Chris@0: // machine name. Chris@0: $names = taxonomy_vocabulary_get_names(); Chris@0: $this->assertEqual($names[$vocabulary1->id()], $vocabulary1->id(), 'Vocabulary 1 name found.'); Chris@0: Chris@0: // Fetch the vocabularies with entity_load_multiple(), specifying IDs. Chris@0: // Ensure they are returned in the same order as the original array. Chris@0: $vocabularies = Vocabulary::loadMultiple([$vocabulary3->id(), $vocabulary2->id(), $vocabulary1->id()]); Chris@0: $loaded_order = array_keys($vocabularies); Chris@0: $expected_order = [$vocabulary3->id(), $vocabulary2->id(), $vocabulary1->id()]; Chris@0: $this->assertIdentical($loaded_order, $expected_order); Chris@0: Chris@0: // Test loading vocabularies by their properties. Chris@0: $controller = $this->container->get('entity.manager')->getStorage('taxonomy_vocabulary'); Chris@0: // Fetch vocabulary 1 by name. Chris@0: $vocabulary = current($controller->loadByProperties(['name' => $vocabulary1->label()])); Chris@0: $this->assertEqual($vocabulary->id(), $vocabulary1->id(), 'Vocabulary loaded successfully by name.'); Chris@0: Chris@0: // Fetch vocabulary 2 by name and ID. Chris@0: $vocabulary = current($controller->loadByProperties([ Chris@0: 'name' => $vocabulary2->label(), Chris@0: 'vid' => $vocabulary2->id(), Chris@0: ])); Chris@0: $this->assertEqual($vocabulary->id(), $vocabulary2->id(), 'Vocabulary loaded successfully by name and ID.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test uninstall and reinstall of the taxonomy module. Chris@0: */ Chris@0: public function testUninstallReinstall() { Chris@0: // Field storages and fields attached to taxonomy term bundles should be Chris@0: // removed when the module is uninstalled. Chris@17: $field_name = mb_strtolower($this->randomMachineName() . '_field_name'); Chris@0: $storage_definition = [ Chris@0: 'field_name' => $field_name, Chris@0: 'entity_type' => 'taxonomy_term', Chris@0: 'type' => 'text', Chris@17: 'cardinality' => 4, Chris@0: ]; Chris@0: FieldStorageConfig::create($storage_definition)->save(); Chris@0: $field_definition = [ Chris@0: 'field_name' => $field_name, Chris@0: 'entity_type' => 'taxonomy_term', Chris@0: 'bundle' => $this->vocabulary->id(), Chris@0: 'label' => $this->randomMachineName() . '_label', Chris@0: ]; Chris@0: FieldConfig::create($field_definition)->save(); Chris@0: Chris@0: // Remove the third party setting from the memory copy of the vocabulary. Chris@0: // We keep this invalid copy around while the taxonomy module is not even Chris@0: // installed for testing below. Chris@0: $this->vocabulary->unsetThirdPartySetting('taxonomy_crud', 'foo'); Chris@0: Chris@17: require_once $this->root . '/core/includes/install.inc'; Chris@0: $this->container->get('module_installer')->uninstall(['taxonomy']); Chris@0: $this->container->get('module_installer')->install(['taxonomy']); Chris@0: Chris@0: // Now create a vocabulary with the same name. All fields Chris@0: // connected to this vocabulary name should have been removed when the Chris@0: // module was uninstalled. Creating a new field with the same name and Chris@0: // an instance of this field on the same bundle name should be successful. Chris@0: $this->vocabulary->enforceIsNew(); Chris@0: $this->vocabulary->save(); Chris@0: FieldStorageConfig::create($storage_definition)->save(); Chris@0: FieldConfig::create($field_definition)->save(); Chris@0: } Chris@0: Chris@0: }