Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / taxonomy / tests / src / Functional / VocabularyUiTest.php
1 <?php
2
3 namespace Drupal\Tests\taxonomy\Functional;
4 use Drupal\Component\Utility\Unicode;
5
6 use Drupal\Core\Url;
7 use Drupal\taxonomy\Entity\Vocabulary;
8
9 /**
10  * Tests the taxonomy vocabulary interface.
11  *
12  * @group taxonomy
13  */
14 class VocabularyUiTest extends TaxonomyTestBase {
15
16   /**
17    * The vocabulary used for creating terms.
18    *
19    * @var \Drupal\taxonomy\VocabularyInterface
20    */
21   protected $vocabulary;
22
23   protected function setUp() {
24     parent::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');
29   }
30
31   /**
32    * Create, edit and delete a vocabulary via the user interface.
33    */
34   public function testVocabularyInterface() {
35     // Visit the main taxonomy administration page.
36     $this->drupalGet('admin/structure/taxonomy');
37
38     // Create a new vocabulary.
39     $this->clickLink(t('Add vocabulary'));
40     $edit = [];
41     $vid = Unicode::strtolower($this->randomMachineName());
42     $edit['name'] = $this->randomMachineName();
43     $edit['description'] = $this->randomMachineName();
44     $edit['vid'] = $vid;
45     $this->drupalPostForm(NULL, $edit, t('Save'));
46     $this->assertRaw(t('Created new vocabulary %name.', ['%name' => $edit['name']]), 'Vocabulary created successfully.');
47
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'));
54     $edit = [];
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.');
61
62     // Try to submit a vocabulary with a duplicate machine name.
63     $edit['vid'] = $vid;
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.'));
66
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.'));
71
72     // Ensure that vocabulary titles are escaped properly.
73     $edit = [];
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'));
78
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.');
81   }
82
83   /**
84    * Changing weights on the vocabulary overview with two or more vocabularies.
85    */
86   public function testTaxonomyAdminChangingWeights() {
87     // Create some vocabularies.
88     for ($i = 0; $i < 10; $i++) {
89       $this->createVocabulary();
90     }
91     // Get all vocabularies and change their weights.
92     $vocabularies = Vocabulary::loadMultiple();
93     $edit = [];
94     foreach ($vocabularies as $key => $vocabulary) {
95       $weight = -$vocabulary->get('weight');
96       $vocabularies[$key]->set('weight', $weight);
97       $edit['vocabularies[' . $key . '][weight]'] = $weight;
98     }
99     // Saving the new weights via the interface.
100     $this->drupalPostForm('admin/structure/taxonomy', $edit, t('Save'));
101
102     // Load the vocabularies from the database.
103     $this->container->get('entity.manager')->getStorage('taxonomy_vocabulary')->resetCache();
104     $new_vocabularies = Vocabulary::loadMultiple();
105
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.');
109     }
110   }
111
112   /**
113    * Test the vocabulary overview with no vocabularies.
114    */
115   public function testTaxonomyAdminNoVocabularies() {
116     // Delete all vocabularies.
117     $vocabularies = Vocabulary::loadMultiple();
118     foreach ($vocabularies as $key => $vocabulary) {
119       $vocabulary->delete();
120     }
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.'));
126   }
127
128   /**
129    * Deleting a vocabulary.
130    */
131   public function testTaxonomyAdminDeletingVocabulary() {
132     // Create a vocabulary.
133     $vid = Unicode::strtolower($this->randomMachineName());
134     $edit = [
135       'name' => $this->randomMachineName(),
136       'vid' => $vid,
137     ];
138     $this->drupalPostForm('admin/structure/taxonomy/add', $edit, t('Save'));
139     $this->assertText(t('Created new vocabulary'), 'New vocabulary was created.');
140
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.');
145
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.');
151
152     // Confirm deletion.
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.');
157   }
158
159 }