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