e39a0d855540f2bfea9c3f7a9aeba4984608d989
[yaffs-website] / web / core / modules / taxonomy / tests / src / Functional / VocabularyUiTest.php
1 <?php
2
3 namespace Drupal\Tests\taxonomy\Functional;
4
5 use Drupal\Component\Utility\Unicode;
6
7 use Drupal\Core\Url;
8 use Drupal\taxonomy\Entity\Vocabulary;
9
10 /**
11  * Tests the taxonomy vocabulary interface.
12  *
13  * @group taxonomy
14  */
15 class VocabularyUiTest extends TaxonomyTestBase {
16
17   /**
18    * The vocabulary used for creating terms.
19    *
20    * @var \Drupal\taxonomy\VocabularyInterface
21    */
22   protected $vocabulary;
23
24   protected function setUp() {
25     parent::setUp();
26     $this->drupalLogin($this->drupalCreateUser(['administer taxonomy']));
27     $this->vocabulary = $this->createVocabulary();
28     $this->drupalPlaceBlock('local_actions_block');
29     $this->drupalPlaceBlock('page_title_block');
30   }
31
32   /**
33    * Create, edit and delete a vocabulary via the user interface.
34    */
35   public function testVocabularyInterface() {
36     // Visit the main taxonomy administration page.
37     $this->drupalGet('admin/structure/taxonomy');
38
39     // Create a new vocabulary.
40     $this->clickLink(t('Add vocabulary'));
41     $edit = [];
42     $vid = Unicode::strtolower($this->randomMachineName());
43     $edit['name'] = $this->randomMachineName();
44     $edit['description'] = $this->randomMachineName();
45     $edit['vid'] = $vid;
46     $this->drupalPostForm(NULL, $edit, t('Save'));
47     $this->assertRaw(t('Created new vocabulary %name.', ['%name' => $edit['name']]), 'Vocabulary created successfully.');
48
49     // Edit the vocabulary.
50     $this->drupalGet('admin/structure/taxonomy');
51     $this->assertText($edit['name'], 'Vocabulary name found in the vocabulary overview listing.');
52     $this->assertText($edit['description'], 'Vocabulary description found in the vocabulary overview listing.');
53     $this->assertLinkByHref(Url::fromRoute('entity.taxonomy_term.add_form', ['taxonomy_vocabulary' => $edit['vid']])->toString());
54     $this->clickLink(t('Edit vocabulary'));
55     $edit = [];
56     $edit['name'] = $this->randomMachineName();
57     $edit['description'] = $this->randomMachineName();
58     $this->drupalPostForm(NULL, $edit, t('Save'));
59     $this->drupalGet('admin/structure/taxonomy');
60     $this->assertText($edit['name'], 'Vocabulary name found in the vocabulary overview listing.');
61     $this->assertText($edit['description'], 'Vocabulary description found in the vocabulary overview listing.');
62
63     // Try to submit a vocabulary with a duplicate machine name.
64     $edit['vid'] = $vid;
65     $this->drupalPostForm('admin/structure/taxonomy/add', $edit, t('Save'));
66     $this->assertText(t('The machine-readable name is already in use. It must be unique.'));
67
68     // Try to submit an invalid machine name.
69     $edit['vid'] = '!&^%';
70     $this->drupalPostForm('admin/structure/taxonomy/add', $edit, t('Save'));
71     $this->assertText(t('The machine-readable name must contain only lowercase letters, numbers, and underscores.'));
72
73     // Ensure that vocabulary titles are escaped properly.
74     $edit = [];
75     $edit['name'] = 'Don\'t Panic';
76     $edit['description'] = $this->randomMachineName();
77     $edit['vid'] = 'don_t_panic';
78     $this->drupalPostForm('admin/structure/taxonomy/add', $edit, t('Save'));
79
80     $site_name = $this->config('system.site')->get('name');
81     $this->assertTitle(t("Don't Panic | @site-name", ['@site-name' => $site_name]), 'The page title contains the escaped 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 }