function VocabularyPermissionsTest::testTaxonomyVocabularyOverviewPermissions

Same name and namespace in other branches
  1. 8.9.x core/modules/taxonomy/tests/src/Functional/VocabularyPermissionsTest.php \Drupal\Tests\taxonomy\Functional\VocabularyPermissionsTest::testTaxonomyVocabularyOverviewPermissions()
  2. 10 core/modules/taxonomy/tests/src/Functional/VocabularyPermissionsTest.php \Drupal\Tests\taxonomy\Functional\VocabularyPermissionsTest::testTaxonomyVocabularyOverviewPermissions()
  3. 11.x core/modules/taxonomy/tests/src/Functional/VocabularyPermissionsTest.php \Drupal\Tests\taxonomy\Functional\VocabularyPermissionsTest::testTaxonomyVocabularyOverviewPermissions()

Tests the vocabulary overview permission.

File

core/modules/taxonomy/tests/src/Functional/VocabularyPermissionsTest.php, line 68

Class

VocabularyPermissionsTest
Tests the taxonomy vocabulary permissions.

Namespace

Drupal\Tests\taxonomy\Functional

Code

public function testTaxonomyVocabularyOverviewPermissions() {
    // Create two vocabularies, one with two terms, the other without any term.
    
    /** @var \Drupal\taxonomy\Entity\Vocabulary $vocabulary1 , $vocabulary2 */
    $vocabulary1 = $this->createVocabulary();
    $vocabulary2 = $this->createVocabulary();
    $vocabulary1_id = $vocabulary1->id();
    $vocabulary2_id = $vocabulary2->id();
    $this->createTerm($vocabulary1);
    $this->createTerm($vocabulary1);
    // Assert expected help texts on first vocabulary.
    $vocabulary1_label = Unicode::ucfirst($vocabulary1->label());
    $edit_help_text = "You can reorganize the terms in {$vocabulary1_label} using their drag-and-drop handles, and group terms under a parent term by sliding them under and to the right of the parent.";
    $no_edit_help_text = "{$vocabulary1_label} contains the following terms.";
    $assert_session = $this->assertSession();
    // Logged in as admin user with 'administer taxonomy' permission.
    $admin_user = $this->drupalCreateUser([
        'administer taxonomy',
    ]);
    $this->drupalLogin($admin_user);
    $this->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary1_id . '/overview');
    $assert_session->statusCodeEquals(200);
    $assert_session->linkExists('Edit');
    $assert_session->linkExists('Delete');
    $assert_session->linkExists('Add term');
    $assert_session->buttonExists('Save');
    $assert_session->pageTextContains('Weight');
    $assert_session->fieldExists('Weight');
    $assert_session->pageTextContains($edit_help_text);
    // Visit vocabulary overview without terms. 'Add term' should be shown.
    $this->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary2_id . '/overview');
    $assert_session->statusCodeEquals(200);
    $assert_session->pageTextContains('No terms available');
    $assert_session->linkExists('Add term');
    // Login as a user without any of the required permissions.
    $no_permission_user = $this->drupalCreateUser();
    $this->drupalLogin($no_permission_user);
    $this->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary1_id . '/overview');
    $assert_session->statusCodeEquals(403);
    $this->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary2_id . '/overview');
    $assert_session->statusCodeEquals(403);
    // Log in as a user with only the overview permission, neither edit nor
    // delete operations must be available and no Save button.
    $overview_only_user = $this->drupalCreateUser([
        'access taxonomy overview',
    ]);
    $this->drupalLogin($overview_only_user);
    $this->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary1_id . '/overview');
    $assert_session->statusCodeEquals(200);
    $assert_session->linkNotExists('Edit');
    $assert_session->linkNotExists('Delete');
    $assert_session->buttonNotExists('Save');
    $assert_session->pageTextContains('Weight');
    $assert_session->fieldNotExists('Weight');
    $assert_session->linkNotExists('Add term');
    $assert_session->pageTextContains($no_edit_help_text);
    // Visit vocabulary overview without terms. 'Add term' should not be shown.
    $this->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary2_id . '/overview');
    $assert_session->statusCodeEquals(200);
    $assert_session->pageTextContains('No terms available');
    $assert_session->linkNotExists('Add term');
    // Login as a user with permission to edit terms, only edit link should be
    // visible.
    $edit_user = $this->createUser([
        'access taxonomy overview',
        'edit terms in ' . $vocabulary1_id,
        'edit terms in ' . $vocabulary2_id,
    ]);
    $this->drupalLogin($edit_user);
    $this->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary1_id . '/overview');
    $assert_session->statusCodeEquals(200);
    $assert_session->linkExists('Edit');
    $assert_session->linkNotExists('Delete');
    $assert_session->buttonExists('Save');
    $assert_session->pageTextContains('Weight');
    $assert_session->fieldExists('Weight');
    $assert_session->linkNotExists('Add term');
    $assert_session->pageTextContains($edit_help_text);
    // Visit vocabulary overview without terms. 'Add term' should not be shown.
    $this->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary2_id . '/overview');
    $assert_session->statusCodeEquals(200);
    $assert_session->pageTextContains('No terms available');
    $assert_session->linkNotExists('Add term');
    // Login as a user with permission only to delete terms.
    $edit_delete_user = $this->createUser([
        'access taxonomy overview',
        'delete terms in ' . $vocabulary1_id,
        'delete terms in ' . $vocabulary2_id,
    ]);
    $this->drupalLogin($edit_delete_user);
    $this->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary1_id . '/overview');
    $assert_session->statusCodeEquals(200);
    $assert_session->linkNotExists('Edit');
    $assert_session->linkExists('Delete');
    $assert_session->linkNotExists('Add term');
    $assert_session->buttonNotExists('Save');
    $assert_session->pageTextContains('Weight');
    $assert_session->fieldNotExists('Weight');
    $assert_session->pageTextContains($no_edit_help_text);
    // Visit vocabulary overview without terms. 'Add term' should not be shown.
    $this->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary2_id . '/overview');
    $assert_session->statusCodeEquals(200);
    $assert_session->pageTextContains('No terms available');
    $assert_session->linkNotExists('Add term');
    // Login as a user with permission to edit and delete terms.
    $edit_delete_user = $this->createUser([
        'access taxonomy overview',
        'edit terms in ' . $vocabulary1_id,
        'delete terms in ' . $vocabulary1_id,
        'edit terms in ' . $vocabulary2_id,
        'delete terms in ' . $vocabulary2_id,
    ]);
    $this->drupalLogin($edit_delete_user);
    $this->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary1_id . '/overview');
    $assert_session->statusCodeEquals(200);
    $assert_session->linkExists('Edit');
    $assert_session->linkExists('Delete');
    $assert_session->linkNotExists('Add term');
    $assert_session->buttonExists('Save');
    $assert_session->pageTextContains('Weight');
    $assert_session->fieldExists('Weight');
    $assert_session->pageTextContains($edit_help_text);
    // Visit vocabulary overview without terms. 'Add term' should not be shown.
    $this->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary2_id . '/overview');
    $assert_session->statusCodeEquals(200);
    $assert_session->pageTextContains('No terms available');
    $assert_session->linkNotExists('Add term');
    // Login as a user with permission to create new terms, only add new term
    // link should be visible.
    $edit_user = $this->createUser([
        'access taxonomy overview',
        'create terms in ' . $vocabulary1_id,
        'create terms in ' . $vocabulary2_id,
    ]);
    $this->drupalLogin($edit_user);
    $this->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary1_id . '/overview');
    $assert_session->statusCodeEquals(200);
    $assert_session->linkNotExists('Edit');
    $assert_session->linkNotExists('Delete');
    $assert_session->linkExists('Add term');
    $assert_session->buttonNotExists('Save');
    $assert_session->pageTextContains('Weight');
    $assert_session->fieldNotExists('Weight');
    $assert_session->pageTextContains($no_edit_help_text);
    // Visit vocabulary overview without terms. 'Add term' should not be shown.
    $this->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary2_id . '/overview');
    $assert_session->statusCodeEquals(200);
    $assert_session->pageTextContains('No terms available');
    $assert_session->linkExists('Add term');
    // Ensure the dynamic vocabulary permissions have the correct dependencies.
    $permissions = \Drupal::service('user.permissions')->getPermissions();
    $this->assertTrue(isset($permissions['create terms in ' . $vocabulary1_id]));
    $this->assertEquals([
        'config' => [
            $vocabulary1->getConfigDependencyName(),
        ],
    ], $permissions['create terms in ' . $vocabulary1_id]['dependencies']);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.