function NodeRevisionPermissionsTest::testNodeRevisionAccessPerType

Same name in other branches
  1. 9 core/modules/node/tests/src/Functional/NodeRevisionPermissionsTest.php \Drupal\Tests\node\Functional\NodeRevisionPermissionsTest::testNodeRevisionAccessPerType()

Tests revision access permissions for a specific content type.

File

core/modules/node/tests/src/Functional/NodeRevisionPermissionsTest.php, line 135

Class

NodeRevisionPermissionsTest
Tests user permissions for node revisions.

Namespace

Drupal\Tests\node\Functional

Code

public function testNodeRevisionAccessPerType() {
    // Create three users, one with each revision permission.
    foreach ($this->typeMap as $op => $permission) {
        // Create the user.
        $account = $this->drupalCreateUser([
            'access content',
            'edit any page content',
            'delete any page content',
            $permission,
        ]);
        $account->op = $op;
        $accounts[] = $account;
    }
    $parameters = [
        'op' => array_keys($this->typeMap),
        'account' => $accounts,
    ];
    // Test that the accounts have access to the corresponding page revision
    // permissions.
    $revision = $this->nodeRevisions['page'][1];
    $permutations = $this->generatePermutations($parameters);
    $node_revision_access = \Drupal::service('access_check.node.revision');
    $connection = \Drupal::database();
    foreach ($permutations as $case) {
        // Skip this test if there are no revisions for the node.
        if (!($revision->isDefaultRevision() && ($connection->query('SELECT COUNT(vid) FROM {node_field_revision} WHERE nid = :nid', [
            ':nid' => $revision->id(),
        ])
            ->fetchField() == 1 || $case['op'] == 'update' || $case['op'] == 'delete'))) {
            if (!empty($case['account']->is_admin) || $case['account']->hasPermission($this->typeMap[$case['op']])) {
                $this->assertTrue($node_revision_access->checkAccess($revision, $case['account'], $case['op']), "{$this->typeMap[$case['op']]} granted.");
            }
            else {
                $this->assertFalse($node_revision_access->checkAccess($revision, $case['account'], $case['op']), "{$this->typeMap[$case['op']]} not granted.");
            }
        }
    }
    // Test that the accounts have no access to the article revisions.
    $revision = $this->nodeRevisions['article'][1];
    foreach ($permutations as $case) {
        $this->assertFalse($node_revision_access->checkAccess($revision, $case['account'], $case['op']), "{$this->typeMap[$case['op']]} did not grant revision permission for articles.");
    }
}

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