89c7a67f55478a70588cb673e6c8ab49c0754468
[yaffs-website] / web / core / modules / taxonomy / src / Tests / VocabularyUiTest.php
1 <?php
2
3 namespace Drupal\taxonomy\Tests;
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     $this->assertNoTitle(t('Don&#039;t Panic | @site-name', ['@site-name' => $site_name]), 'The page title does not contain an encoded character.');
82   }
83
84   /**
85    * Changing weights on the vocabulary overview with two or more vocabularies.
86    */
87   public function testTaxonomyAdminChangingWeights() {
88     // Create some vocabularies.
89     for ($i = 0; $i < 10; $i++) {
90       $this->createVocabulary();
91     }
92     // Get all vocabularies and change their weights.
93     $vocabularies = Vocabulary::loadMultiple();
94     $edit = [];
95     foreach ($vocabularies as $key => $vocabulary) {
96       $weight = -$vocabulary->get('weight');
97       $vocabularies[$key]->set('weight', $weight);
98       $edit['vocabularies[' . $key . '][weight]'] = $weight;
99     }
100     // Saving the new weights via the interface.
101     $this->drupalPostForm('admin/structure/taxonomy', $edit, t('Save'));
102
103     // Load the vocabularies from the database.
104     $this->container->get('entity.manager')->getStorage('taxonomy_vocabulary')->resetCache();
105     $new_vocabularies = Vocabulary::loadMultiple();
106
107     // Check that the weights are saved in the database correctly.
108     foreach ($vocabularies as $key => $vocabulary) {
109       $this->assertEqual($new_vocabularies[$key]->get('weight'), $vocabularies[$key]->get('weight'), 'The vocabulary weight was changed.');
110     }
111   }
112
113   /**
114    * Test the vocabulary overview with no vocabularies.
115    */
116   public function testTaxonomyAdminNoVocabularies() {
117     // Delete all vocabularies.
118     $vocabularies = Vocabulary::loadMultiple();
119     foreach ($vocabularies as $key => $vocabulary) {
120       $vocabulary->delete();
121     }
122     // Confirm that no vocabularies are found in the database.
123     $this->assertFalse(Vocabulary::loadMultiple(), 'No vocabularies found.');
124     $this->drupalGet('admin/structure/taxonomy');
125     // Check the default message for no vocabularies.
126     $this->assertText(t('No vocabularies available.'));
127   }
128
129   /**
130    * Deleting a vocabulary.
131    */
132   public function testTaxonomyAdminDeletingVocabulary() {
133     // Create a vocabulary.
134     $vid = Unicode::strtolower($this->randomMachineName());
135     $edit = [
136       'name' => $this->randomMachineName(),
137       'vid' => $vid,
138     ];
139     $this->drupalPostForm('admin/structure/taxonomy/add', $edit, t('Save'));
140     $this->assertText(t('Created new vocabulary'), 'New vocabulary was created.');
141
142     // Check the created vocabulary.
143     $this->container->get('entity.manager')->getStorage('taxonomy_vocabulary')->resetCache();
144     $vocabulary = Vocabulary::load($vid);
145     $this->assertTrue($vocabulary, 'Vocabulary found.');
146
147     // Delete the vocabulary.
148     $this->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary->id());
149     $this->clickLink(t('Delete'));
150     $this->assertRaw(t('Are you sure you want to delete the vocabulary %name?', ['%name' => $vocabulary->label()]), '[confirm deletion] Asks for confirmation.');
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.');
152
153     // Confirm deletion.
154     $this->drupalPostForm(NULL, NULL, t('Delete'));
155     $this->assertRaw(t('Deleted vocabulary %name.', ['%name' => $vocabulary->label()]), 'Vocabulary deleted.');
156     $this->container->get('entity.manager')->getStorage('taxonomy_vocabulary')->resetCache();
157     $this->assertFalse(Vocabulary::load($vid), 'Vocabulary not found.');
158   }
159
160 }