Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / taxonomy / tests / src / Functional / VocabularyPermissionsTest.php
1 <?php
2
3 namespace Drupal\Tests\taxonomy\Functional;
4
5 /**
6  * Tests the taxonomy vocabulary permissions.
7  *
8  * @group taxonomy
9  */
10 class VocabularyPermissionsTest extends TaxonomyTestBase {
11
12   protected function setUp() {
13     parent::setUp();
14
15     $this->drupalPlaceBlock('page_title_block');
16   }
17
18   /**
19    * Create, edit and delete a taxonomy term via the user interface.
20    */
21   public function testVocabularyPermissionsTaxonomyTerm() {
22     // Vocabulary used for creating, removing and editing terms.
23     $vocabulary = $this->createVocabulary();
24
25     // Test as admin user.
26     $user = $this->drupalCreateUser(['administer taxonomy']);
27     $this->drupalLogin($user);
28
29     // Visit the main taxonomy administration page.
30     $this->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary->id() . '/add');
31     $this->assertResponse(200);
32     $this->assertField('edit-name-0-value', 'Add taxonomy term form opened successfully.');
33
34     // Submit the term.
35     $edit = [];
36     $edit['name[0][value]'] = $this->randomMachineName();
37
38     $this->drupalPostForm(NULL, $edit, t('Save'));
39     $this->assertText(t('Created new term @name.', ['@name' => $edit['name[0][value]']]), 'Term created successfully.');
40
41     // Verify that the creation message contains a link to a term.
42     $view_link = $this->xpath('//div[@class="messages"]//a[contains(@href, :href)]', [':href' => 'term/']);
43     $this->assert(isset($view_link), 'The message area contains a link to a term');
44
45     $terms = taxonomy_term_load_multiple_by_name($edit['name[0][value]']);
46     $term = reset($terms);
47
48     // Edit the term.
49     $this->drupalGet('taxonomy/term/' . $term->id() . '/edit');
50     $this->assertResponse(200);
51     $this->assertText($edit['name[0][value]'], 'Edit taxonomy term form opened successfully.');
52
53     $edit['name[0][value]'] = $this->randomMachineName();
54     $this->drupalPostForm(NULL, $edit, t('Save'));
55     $this->assertText(t('Updated term @name.', ['@name' => $edit['name[0][value]']]), 'Term updated successfully.');
56
57     // Delete the vocabulary.
58     $this->drupalGet('taxonomy/term/' . $term->id() . '/delete');
59     $this->assertRaw(t('Are you sure you want to delete the @entity-type %label?', ['@entity-type' => 'taxonomy term', '%label' => $edit['name[0][value]']]), 'Delete taxonomy term form opened successfully.');
60
61     // Confirm deletion.
62     $this->drupalPostForm(NULL, NULL, t('Delete'));
63     $this->assertRaw(t('Deleted term %name.', ['%name' => $edit['name[0][value]']]), 'Term deleted.');
64
65     // Test as user with "edit" permissions.
66     $user = $this->drupalCreateUser(["edit terms in {$vocabulary->id()}"]);
67     $this->drupalLogin($user);
68
69     // Visit the main taxonomy administration page.
70     $this->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary->id() . '/add');
71     $this->assertResponse(403, 'Add taxonomy term form open failed.');
72
73     // Create a test term.
74     $term = $this->createTerm($vocabulary);
75
76     // Edit the term.
77     $this->drupalGet('taxonomy/term/' . $term->id() . '/edit');
78     $this->assertResponse(200);
79     $this->assertText($term->getName(), 'Edit taxonomy term form opened successfully.');
80
81     $edit['name[0][value]'] = $this->randomMachineName();
82     $this->drupalPostForm(NULL, $edit, t('Save'));
83     $this->assertText(t('Updated term @name.', ['@name' => $edit['name[0][value]']]), 'Term updated successfully.');
84
85     // Verify that the update message contains a link to a term.
86     $view_link = $this->xpath('//div[@class="messages"]//a[contains(@href, :href)]', [':href' => 'term/']);
87     $this->assert(isset($view_link), 'The message area contains a link to a term');
88
89     // Delete the vocabulary.
90     $this->drupalGet('taxonomy/term/' . $term->id() . '/delete');
91     $this->assertResponse(403, 'Delete taxonomy term form open failed.');
92
93     // Test as user with "delete" permissions.
94     $user = $this->drupalCreateUser(["delete terms in {$vocabulary->id()}"]);
95     $this->drupalLogin($user);
96
97     // Visit the main taxonomy administration page.
98     $this->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary->id() . '/add');
99     $this->assertResponse(403, 'Add taxonomy term form open failed.');
100
101     // Create a test term.
102     $term = $this->createTerm($vocabulary);
103
104     // Edit the term.
105     $this->drupalGet('taxonomy/term/' . $term->id() . '/edit');
106     $this->assertResponse(403, 'Edit taxonomy term form open failed.');
107
108     // Delete the vocabulary.
109     $this->drupalGet('taxonomy/term/' . $term->id() . '/delete');
110     $this->assertRaw(t('Are you sure you want to delete the @entity-type %label?', ['@entity-type' => 'taxonomy term', '%label' => $term->getName()]), 'Delete taxonomy term form opened successfully.');
111
112     // Confirm deletion.
113     $this->drupalPostForm(NULL, NULL, t('Delete'));
114     $this->assertRaw(t('Deleted term %name.', ['%name' => $term->getName()]), 'Term deleted.');
115
116     // Test as user without proper permissions.
117     $user = $this->drupalCreateUser();
118     $this->drupalLogin($user);
119
120     // Visit the main taxonomy administration page.
121     $this->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary->id() . '/add');
122     $this->assertResponse(403, 'Add taxonomy term form open failed.');
123
124     // Create a test term.
125     $term = $this->createTerm($vocabulary);
126
127     // Edit the term.
128     $this->drupalGet('taxonomy/term/' . $term->id() . '/edit');
129     $this->assertResponse(403, 'Edit taxonomy term form open failed.');
130
131     // Delete the vocabulary.
132     $this->drupalGet('taxonomy/term/' . $term->id() . '/delete');
133     $this->assertResponse(403, 'Delete taxonomy term form open failed.');
134   }
135
136 }