3 namespace Drupal\Tests\taxonomy\Functional;
4 use Drupal\Component\Utility\Unicode;
7 use Drupal\taxonomy\Entity\Vocabulary;
10 * Tests the taxonomy vocabulary interface.
14 class VocabularyUiTest extends TaxonomyTestBase {
17 * The vocabulary used for creating terms.
19 * @var \Drupal\taxonomy\VocabularyInterface
21 protected $vocabulary;
23 protected function setUp() {
25 $this->drupalLogin($this->drupalCreateUser(['administer taxonomy']));
26 $this->vocabulary = $this->createVocabulary();
27 $this->drupalPlaceBlock('local_actions_block');
28 $this->drupalPlaceBlock('page_title_block');
32 * Create, edit and delete a vocabulary via the user interface.
34 public function testVocabularyInterface() {
35 // Visit the main taxonomy administration page.
36 $this->drupalGet('admin/structure/taxonomy');
38 // Create a new vocabulary.
39 $this->clickLink(t('Add vocabulary'));
41 $vid = Unicode::strtolower($this->randomMachineName());
42 $edit['name'] = $this->randomMachineName();
43 $edit['description'] = $this->randomMachineName();
45 $this->drupalPostForm(NULL, $edit, t('Save'));
46 $this->assertRaw(t('Created new vocabulary %name.', ['%name' => $edit['name']]), 'Vocabulary created successfully.');
48 // Edit the vocabulary.
49 $this->drupalGet('admin/structure/taxonomy');
50 $this->assertText($edit['name'], 'Vocabulary name found in the vocabulary overview listing.');
51 $this->assertText($edit['description'], 'Vocabulary description found in the vocabulary overview listing.');
52 $this->assertLinkByHref(Url::fromRoute('entity.taxonomy_term.add_form', ['taxonomy_vocabulary' => $edit['vid']])->toString());
53 $this->clickLink(t('Edit vocabulary'));
55 $edit['name'] = $this->randomMachineName();
56 $edit['description'] = $this->randomMachineName();
57 $this->drupalPostForm(NULL, $edit, t('Save'));
58 $this->drupalGet('admin/structure/taxonomy');
59 $this->assertText($edit['name'], 'Vocabulary name found in the vocabulary overview listing.');
60 $this->assertText($edit['description'], 'Vocabulary description found in the vocabulary overview listing.');
62 // Try to submit a vocabulary with a duplicate machine name.
64 $this->drupalPostForm('admin/structure/taxonomy/add', $edit, t('Save'));
65 $this->assertText(t('The machine-readable name is already in use. It must be unique.'));
67 // Try to submit an invalid machine name.
68 $edit['vid'] = '!&^%';
69 $this->drupalPostForm('admin/structure/taxonomy/add', $edit, t('Save'));
70 $this->assertText(t('The machine-readable name must contain only lowercase letters, numbers, and underscores.'));
72 // Ensure that vocabulary titles are escaped properly.
74 $edit['name'] = 'Don\'t Panic';
75 $edit['description'] = $this->randomMachineName();
76 $edit['vid'] = 'don_t_panic';
77 $this->drupalPostForm('admin/structure/taxonomy/add', $edit, t('Save'));
79 $site_name = $this->config('system.site')->get('name');
80 $this->assertTitle(t('Don\'t Panic | @site-name', ['@site-name' => $site_name]), 'The page title contains the escaped character.');
84 * Changing weights on the vocabulary overview with two or more vocabularies.
86 public function testTaxonomyAdminChangingWeights() {
87 // Create some vocabularies.
88 for ($i = 0; $i < 10; $i++) {
89 $this->createVocabulary();
91 // Get all vocabularies and change their weights.
92 $vocabularies = Vocabulary::loadMultiple();
94 foreach ($vocabularies as $key => $vocabulary) {
95 $weight = -$vocabulary->get('weight');
96 $vocabularies[$key]->set('weight', $weight);
97 $edit['vocabularies[' . $key . '][weight]'] = $weight;
99 // Saving the new weights via the interface.
100 $this->drupalPostForm('admin/structure/taxonomy', $edit, t('Save'));
102 // Load the vocabularies from the database.
103 $this->container->get('entity.manager')->getStorage('taxonomy_vocabulary')->resetCache();
104 $new_vocabularies = Vocabulary::loadMultiple();
106 // Check that the weights are saved in the database correctly.
107 foreach ($vocabularies as $key => $vocabulary) {
108 $this->assertEqual($new_vocabularies[$key]->get('weight'), $vocabularies[$key]->get('weight'), 'The vocabulary weight was changed.');
113 * Test the vocabulary overview with no vocabularies.
115 public function testTaxonomyAdminNoVocabularies() {
116 // Delete all vocabularies.
117 $vocabularies = Vocabulary::loadMultiple();
118 foreach ($vocabularies as $key => $vocabulary) {
119 $vocabulary->delete();
121 // Confirm that no vocabularies are found in the database.
122 $this->assertFalse(Vocabulary::loadMultiple(), 'No vocabularies found.');
123 $this->drupalGet('admin/structure/taxonomy');
124 // Check the default message for no vocabularies.
125 $this->assertText(t('No vocabularies available.'));
129 * Deleting a vocabulary.
131 public function testTaxonomyAdminDeletingVocabulary() {
132 // Create a vocabulary.
133 $vid = Unicode::strtolower($this->randomMachineName());
135 'name' => $this->randomMachineName(),
138 $this->drupalPostForm('admin/structure/taxonomy/add', $edit, t('Save'));
139 $this->assertText(t('Created new vocabulary'), 'New vocabulary was created.');
141 // Check the created vocabulary.
142 $this->container->get('entity.manager')->getStorage('taxonomy_vocabulary')->resetCache();
143 $vocabulary = Vocabulary::load($vid);
144 $this->assertTrue($vocabulary, 'Vocabulary found.');
146 // Delete the vocabulary.
147 $this->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary->id());
148 $this->clickLink(t('Delete'));
149 $this->assertRaw(t('Are you sure you want to delete the vocabulary %name?', ['%name' => $vocabulary->label()]), '[confirm deletion] Asks for confirmation.');
150 $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.');
153 $this->drupalPostForm(NULL, NULL, t('Delete'));
154 $this->assertRaw(t('Deleted vocabulary %name.', ['%name' => $vocabulary->label()]), 'Vocabulary deleted.');
155 $this->container->get('entity.manager')->getStorage('taxonomy_vocabulary')->resetCache();
156 $this->assertFalse(Vocabulary::load($vid), 'Vocabulary not found.');