Chris@0: drupalLogin($this->drupalCreateUser(['administer taxonomy'])); Chris@0: $this->vocabulary = $this->createVocabulary(); Chris@0: $this->drupalPlaceBlock('local_actions_block'); Chris@0: $this->drupalPlaceBlock('page_title_block'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Create, edit and delete a vocabulary via the user interface. Chris@0: */ Chris@0: public function testVocabularyInterface() { Chris@0: // Visit the main taxonomy administration page. Chris@0: $this->drupalGet('admin/structure/taxonomy'); Chris@0: Chris@0: // Create a new vocabulary. Chris@0: $this->clickLink(t('Add vocabulary')); Chris@0: $edit = []; Chris@17: $vid = mb_strtolower($this->randomMachineName()); Chris@0: $edit['name'] = $this->randomMachineName(); Chris@0: $edit['description'] = $this->randomMachineName(); Chris@0: $edit['vid'] = $vid; Chris@0: $this->drupalPostForm(NULL, $edit, t('Save')); Chris@0: $this->assertRaw(t('Created new vocabulary %name.', ['%name' => $edit['name']]), 'Vocabulary created successfully.'); Chris@0: Chris@0: // Edit the vocabulary. Chris@0: $this->drupalGet('admin/structure/taxonomy'); Chris@0: $this->assertText($edit['name'], 'Vocabulary name found in the vocabulary overview listing.'); Chris@0: $this->assertText($edit['description'], 'Vocabulary description found in the vocabulary overview listing.'); Chris@0: $this->assertLinkByHref(Url::fromRoute('entity.taxonomy_term.add_form', ['taxonomy_vocabulary' => $edit['vid']])->toString()); Chris@0: $this->clickLink(t('Edit vocabulary')); Chris@0: $edit = []; Chris@0: $edit['name'] = $this->randomMachineName(); Chris@0: $edit['description'] = $this->randomMachineName(); Chris@0: $this->drupalPostForm(NULL, $edit, t('Save')); Chris@0: $this->drupalGet('admin/structure/taxonomy'); Chris@0: $this->assertText($edit['name'], 'Vocabulary name found in the vocabulary overview listing.'); Chris@0: $this->assertText($edit['description'], 'Vocabulary description found in the vocabulary overview listing.'); Chris@0: Chris@0: // Try to submit a vocabulary with a duplicate machine name. Chris@0: $edit['vid'] = $vid; Chris@0: $this->drupalPostForm('admin/structure/taxonomy/add', $edit, t('Save')); Chris@0: $this->assertText(t('The machine-readable name is already in use. It must be unique.')); Chris@0: Chris@0: // Try to submit an invalid machine name. Chris@0: $edit['vid'] = '!&^%'; Chris@0: $this->drupalPostForm('admin/structure/taxonomy/add', $edit, t('Save')); Chris@0: $this->assertText(t('The machine-readable name must contain only lowercase letters, numbers, and underscores.')); Chris@0: Chris@0: // Ensure that vocabulary titles are escaped properly. Chris@0: $edit = []; Chris@0: $edit['name'] = 'Don\'t Panic'; Chris@0: $edit['description'] = $this->randomMachineName(); Chris@0: $edit['vid'] = 'don_t_panic'; Chris@0: $this->drupalPostForm('admin/structure/taxonomy/add', $edit, t('Save')); Chris@0: Chris@0: $site_name = $this->config('system.site')->get('name'); Chris@0: $this->assertTitle(t("Don't Panic | @site-name", ['@site-name' => $site_name]), 'The page title contains the escaped character.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Changing weights on the vocabulary overview with two or more vocabularies. Chris@0: */ Chris@0: public function testTaxonomyAdminChangingWeights() { Chris@0: // Create some vocabularies. Chris@0: for ($i = 0; $i < 10; $i++) { Chris@0: $this->createVocabulary(); Chris@0: } Chris@0: // Get all vocabularies and change their weights. Chris@0: $vocabularies = Vocabulary::loadMultiple(); Chris@0: $edit = []; Chris@0: foreach ($vocabularies as $key => $vocabulary) { Chris@0: $weight = -$vocabulary->get('weight'); Chris@0: $vocabularies[$key]->set('weight', $weight); Chris@0: $edit['vocabularies[' . $key . '][weight]'] = $weight; Chris@0: } Chris@0: // Saving the new weights via the interface. Chris@0: $this->drupalPostForm('admin/structure/taxonomy', $edit, t('Save')); Chris@0: Chris@0: // Load the vocabularies from the database. Chris@0: $this->container->get('entity.manager')->getStorage('taxonomy_vocabulary')->resetCache(); Chris@0: $new_vocabularies = Vocabulary::loadMultiple(); Chris@0: Chris@0: // Check that the weights are saved in the database correctly. Chris@0: foreach ($vocabularies as $key => $vocabulary) { Chris@0: $this->assertEqual($new_vocabularies[$key]->get('weight'), $vocabularies[$key]->get('weight'), 'The vocabulary weight was changed.'); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test the vocabulary overview with no vocabularies. Chris@0: */ Chris@0: public function testTaxonomyAdminNoVocabularies() { Chris@0: // Delete all vocabularies. Chris@0: $vocabularies = Vocabulary::loadMultiple(); Chris@0: foreach ($vocabularies as $key => $vocabulary) { Chris@0: $vocabulary->delete(); Chris@0: } Chris@0: // Confirm that no vocabularies are found in the database. Chris@0: $this->assertFalse(Vocabulary::loadMultiple(), 'No vocabularies found.'); Chris@0: $this->drupalGet('admin/structure/taxonomy'); Chris@0: // Check the default message for no vocabularies. Chris@0: $this->assertText(t('No vocabularies available.')); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Deleting a vocabulary. Chris@0: */ Chris@0: public function testTaxonomyAdminDeletingVocabulary() { Chris@0: // Create a vocabulary. Chris@17: $vid = mb_strtolower($this->randomMachineName()); Chris@0: $edit = [ Chris@0: 'name' => $this->randomMachineName(), Chris@0: 'vid' => $vid, Chris@0: ]; Chris@0: $this->drupalPostForm('admin/structure/taxonomy/add', $edit, t('Save')); Chris@0: $this->assertText(t('Created new vocabulary'), 'New vocabulary was created.'); Chris@0: Chris@0: // Check the created vocabulary. Chris@0: $this->container->get('entity.manager')->getStorage('taxonomy_vocabulary')->resetCache(); Chris@0: $vocabulary = Vocabulary::load($vid); Chris@0: $this->assertTrue($vocabulary, 'Vocabulary found.'); Chris@0: Chris@0: // Delete the vocabulary. Chris@0: $this->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary->id()); Chris@0: $this->clickLink(t('Delete')); Chris@0: $this->assertRaw(t('Are you sure you want to delete the vocabulary %name?', ['%name' => $vocabulary->label()]), '[confirm deletion] Asks for confirmation.'); Chris@0: $this->assertText(t('Deleting a vocabulary will delete all the terms in it. This action cannot be undone.'), '[confirm deletion] Inform that all terms will be deleted.'); Chris@0: Chris@0: // Confirm deletion. Chris@0: $this->drupalPostForm(NULL, NULL, t('Delete')); Chris@0: $this->assertRaw(t('Deleted vocabulary %name.', ['%name' => $vocabulary->label()]), 'Vocabulary deleted.'); Chris@0: $this->container->get('entity.manager')->getStorage('taxonomy_vocabulary')->resetCache(); Chris@0: $this->assertFalse(Vocabulary::load($vid), 'Vocabulary not found.'); Chris@0: } Chris@0: Chris@0: }