3 namespace Drupal\Tests\taxonomy\Functional;
6 * Tests the taxonomy vocabulary permissions.
10 class VocabularyPermissionsTest extends TaxonomyTestBase {
12 protected function setUp() {
15 $this->drupalPlaceBlock('page_title_block');
19 * Create, edit and delete a taxonomy term via the user interface.
21 public function testVocabularyPermissionsTaxonomyTerm() {
22 // Vocabulary used for creating, removing and editing terms.
23 $vocabulary = $this->createVocabulary();
25 // Test as admin user.
26 $user = $this->drupalCreateUser(['administer taxonomy']);
27 $this->drupalLogin($user);
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.');
36 $edit['name[0][value]'] = $this->randomMachineName();
38 $this->drupalPostForm(NULL, $edit, t('Save'));
39 $this->assertText(t('Created new term @name.', ['@name' => $edit['name[0][value]']]), 'Term created successfully.');
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');
45 $terms = taxonomy_term_load_multiple_by_name($edit['name[0][value]']);
46 $term = reset($terms);
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.');
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.');
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.');
62 $this->drupalPostForm(NULL, NULL, t('Delete'));
63 $this->assertRaw(t('Deleted term %name.', ['%name' => $edit['name[0][value]']]), 'Term deleted.');
65 // Test as user with "edit" permissions.
66 $user = $this->drupalCreateUser(["edit terms in {$vocabulary->id()}"]);
67 $this->drupalLogin($user);
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.');
73 // Create a test term.
74 $term = $this->createTerm($vocabulary);
77 $this->drupalGet('taxonomy/term/' . $term->id() . '/edit');
78 $this->assertResponse(200);
79 $this->assertText($term->getName(), 'Edit taxonomy term form opened successfully.');
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.');
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');
89 // Delete the vocabulary.
90 $this->drupalGet('taxonomy/term/' . $term->id() . '/delete');
91 $this->assertResponse(403, 'Delete taxonomy term form open failed.');
93 // Test as user with "delete" permissions.
94 $user = $this->drupalCreateUser(["delete terms in {$vocabulary->id()}"]);
95 $this->drupalLogin($user);
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.');
101 // Create a test term.
102 $term = $this->createTerm($vocabulary);
105 $this->drupalGet('taxonomy/term/' . $term->id() . '/edit');
106 $this->assertResponse(403, 'Edit taxonomy term form open failed.');
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.');
113 $this->drupalPostForm(NULL, NULL, t('Delete'));
114 $this->assertRaw(t('Deleted term %name.', ['%name' => $term->getName()]), 'Term deleted.');
116 // Test as user without proper permissions.
117 $user = $this->drupalCreateUser();
118 $this->drupalLogin($user);
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.');
124 // Create a test term.
125 $term = $this->createTerm($vocabulary);
128 $this->drupalGet('taxonomy/term/' . $term->id() . '/edit');
129 $this->assertResponse(403, 'Edit taxonomy term form open failed.');
131 // Delete the vocabulary.
132 $this->drupalGet('taxonomy/term/' . $term->id() . '/delete');
133 $this->assertResponse(403, 'Delete taxonomy term form open failed.');