3 namespace Drupal\Tests\taxonomy\Functional;
6 use Drupal\taxonomy\Entity\Vocabulary;
9 * Tests the taxonomy vocabulary interface.
13 class VocabularyUiTest extends TaxonomyTestBase {
16 * The vocabulary used for creating terms.
18 * @var \Drupal\taxonomy\VocabularyInterface
20 protected $vocabulary;
22 protected function setUp() {
24 $this->drupalLogin($this->drupalCreateUser(['administer taxonomy']));
25 $this->vocabulary = $this->createVocabulary();
26 $this->drupalPlaceBlock('local_actions_block');
27 $this->drupalPlaceBlock('page_title_block');
31 * Create, edit and delete a vocabulary via the user interface.
33 public function testVocabularyInterface() {
34 // Visit the main taxonomy administration page.
35 $this->drupalGet('admin/structure/taxonomy');
37 // Create a new vocabulary.
38 $this->clickLink(t('Add vocabulary'));
40 $vid = mb_strtolower($this->randomMachineName());
41 $edit['name'] = $this->randomMachineName();
42 $edit['description'] = $this->randomMachineName();
44 $this->drupalPostForm(NULL, $edit, t('Save'));
45 $this->assertRaw(t('Created new vocabulary %name.', ['%name' => $edit['name']]), 'Vocabulary created successfully.');
47 // Edit the vocabulary.
48 $this->drupalGet('admin/structure/taxonomy');
49 $this->assertText($edit['name'], 'Vocabulary name found in the vocabulary overview listing.');
50 $this->assertText($edit['description'], 'Vocabulary description found in the vocabulary overview listing.');
51 $this->assertLinkByHref(Url::fromRoute('entity.taxonomy_term.add_form', ['taxonomy_vocabulary' => $edit['vid']])->toString());
52 $this->clickLink(t('Edit vocabulary'));
54 $edit['name'] = $this->randomMachineName();
55 $edit['description'] = $this->randomMachineName();
56 $this->drupalPostForm(NULL, $edit, t('Save'));
57 $this->drupalGet('admin/structure/taxonomy');
58 $this->assertText($edit['name'], 'Vocabulary name found in the vocabulary overview listing.');
59 $this->assertText($edit['description'], 'Vocabulary description found in the vocabulary overview listing.');
61 // Try to submit a vocabulary with a duplicate machine name.
63 $this->drupalPostForm('admin/structure/taxonomy/add', $edit, t('Save'));
64 $this->assertText(t('The machine-readable name is already in use. It must be unique.'));
66 // Try to submit an invalid machine name.
67 $edit['vid'] = '!&^%';
68 $this->drupalPostForm('admin/structure/taxonomy/add', $edit, t('Save'));
69 $this->assertText(t('The machine-readable name must contain only lowercase letters, numbers, and underscores.'));
71 // Ensure that vocabulary titles are escaped properly.
73 $edit['name'] = 'Don\'t Panic';
74 $edit['description'] = $this->randomMachineName();
75 $edit['vid'] = 'don_t_panic';
76 $this->drupalPostForm('admin/structure/taxonomy/add', $edit, t('Save'));
78 $site_name = $this->config('system.site')->get('name');
79 $this->assertTitle(t("Don't Panic | @site-name", ['@site-name' => $site_name]), 'The page title contains the escaped character.');
83 * Changing weights on the vocabulary overview with two or more vocabularies.
85 public function testTaxonomyAdminChangingWeights() {
86 // Create some vocabularies.
87 for ($i = 0; $i < 10; $i++) {
88 $this->createVocabulary();
90 // Get all vocabularies and change their weights.
91 $vocabularies = Vocabulary::loadMultiple();
93 foreach ($vocabularies as $key => $vocabulary) {
94 $weight = -$vocabulary->get('weight');
95 $vocabularies[$key]->set('weight', $weight);
96 $edit['vocabularies[' . $key . '][weight]'] = $weight;
98 // Saving the new weights via the interface.
99 $this->drupalPostForm('admin/structure/taxonomy', $edit, t('Save'));
101 // Load the vocabularies from the database.
102 $this->container->get('entity.manager')->getStorage('taxonomy_vocabulary')->resetCache();
103 $new_vocabularies = Vocabulary::loadMultiple();
105 // Check that the weights are saved in the database correctly.
106 foreach ($vocabularies as $key => $vocabulary) {
107 $this->assertEqual($new_vocabularies[$key]->get('weight'), $vocabularies[$key]->get('weight'), 'The vocabulary weight was changed.');
112 * Test the vocabulary overview with no vocabularies.
114 public function testTaxonomyAdminNoVocabularies() {
115 // Delete all vocabularies.
116 $vocabularies = Vocabulary::loadMultiple();
117 foreach ($vocabularies as $key => $vocabulary) {
118 $vocabulary->delete();
120 // Confirm that no vocabularies are found in the database.
121 $this->assertFalse(Vocabulary::loadMultiple(), 'No vocabularies found.');
122 $this->drupalGet('admin/structure/taxonomy');
123 // Check the default message for no vocabularies.
124 $this->assertText(t('No vocabularies available.'));
128 * Deleting a vocabulary.
130 public function testTaxonomyAdminDeletingVocabulary() {
131 // Create a vocabulary.
132 $vid = mb_strtolower($this->randomMachineName());
134 'name' => $this->randomMachineName(),
137 $this->drupalPostForm('admin/structure/taxonomy/add', $edit, t('Save'));
138 $this->assertText(t('Created new vocabulary'), 'New vocabulary was created.');
140 // Check the created vocabulary.
141 $this->container->get('entity.manager')->getStorage('taxonomy_vocabulary')->resetCache();
142 $vocabulary = Vocabulary::load($vid);
143 $this->assertTrue($vocabulary, 'Vocabulary found.');
145 // Delete the vocabulary.
146 $this->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary->id());
147 $this->clickLink(t('Delete'));
148 $this->assertRaw(t('Are you sure you want to delete the vocabulary %name?', ['%name' => $vocabulary->label()]), '[confirm deletion] Asks for confirmation.');
149 $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.');
152 $this->drupalPostForm(NULL, NULL, t('Delete'));
153 $this->assertRaw(t('Deleted vocabulary %name.', ['%name' => $vocabulary->label()]), 'Vocabulary deleted.');
154 $this->container->get('entity.manager')->getStorage('taxonomy_vocabulary')->resetCache();
155 $this->assertFalse(Vocabulary::load($vid), 'Vocabulary not found.');