function UserKernelTestBase::setupPermissionTestData

Same name in other branches
  1. 9 core/modules/user/tests/src/Kernel/Views/UserKernelTestBase.php \Drupal\Tests\user\Kernel\Views\UserKernelTestBase::setupPermissionTestData()
  2. 8.9.x core/modules/user/tests/src/Kernel/Views/UserKernelTestBase.php \Drupal\Tests\user\Kernel\Views\UserKernelTestBase::setupPermissionTestData()
  3. 10 core/modules/user/tests/src/Kernel/Views/UserKernelTestBase.php \Drupal\Tests\user\Kernel\Views\UserKernelTestBase::setupPermissionTestData()

Set some test data for permission related tests.

3 calls to UserKernelTestBase::setupPermissionTestData()
HandlerFieldPermissionTest::testFieldPermission in core/modules/user/tests/src/Kernel/Views/HandlerFieldPermissionTest.php
Tests the permission field handler output.
HandlerFilterCurrentUserTest::setUp in core/modules/user/tests/src/Kernel/Views/HandlerFilterCurrentUserTest.php
HandlerFilterPermissionTest::testFilterPermission in core/modules/user/tests/src/Kernel/Views/HandlerFilterPermissionTest.php
Tests the permission filter handler.

File

core/modules/user/tests/src/Kernel/Views/UserKernelTestBase.php, line 59

Class

UserKernelTestBase
Provides a common test base for user views tests.

Namespace

Drupal\Tests\user\Kernel\Views

Code

protected function setupPermissionTestData() {
    // Setup a role without any permission.
    $this->roleStorage
        ->create([
        'id' => 'authenticated',
        'label' => 'Authenticated',
    ])
        ->save();
    $this->roleStorage
        ->create([
        'id' => 'no_permission',
        'label' => 'No permission',
    ])
        ->save();
    // Setup a role with just one permission.
    $this->roleStorage
        ->create([
        'id' => 'one_permission',
        'label' => '1 permission',
    ])
        ->save();
    user_role_grant_permissions('one_permission', [
        'administer permissions',
    ]);
    // Setup a role with multiple permissions.
    $this->roleStorage
        ->create([
        'id' => 'multiple_permissions',
        'label' => 'Multiple permissions',
    ])
        ->save();
    user_role_grant_permissions('multiple_permissions', [
        'administer permissions',
        'administer users',
        'access user profiles',
    ]);
    // Setup a user without an extra role.
    $this->users[] = $account = $this->userStorage
        ->create([
        'name' => $this->randomString(),
    ]);
    $account->save();
    // Setup a user with just the first role (so no permission beside the
    // ones from the authenticated role).
    $this->users[] = $account = $this->userStorage
        ->create([
        'name' => 'first_role',
    ]);
    $account->addRole('no_permission')
        ->save();
    // Setup a user with just the second role (so one additional permission).
    $this->users[] = $account = $this->userStorage
        ->create([
        'name' => 'second_role',
    ]);
    $account->addRole('one_permission')
        ->save();
    // Setup a user with both the second and the third role.
    $this->users[] = $account = $this->userStorage
        ->create([
        'name' => 'second_third_role',
    ]);
    $account->addRole('one_permission')
        ->addRole('multiple_permissions')
        ->save();
}

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