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