function HandlerFieldPermissionTest::testFieldPermission

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

Tests the permission field handler output.

File

core/modules/user/tests/src/Kernel/Views/HandlerFieldPermissionTest.php, line 27

Class

HandlerFieldPermissionTest
Tests the permission field handler.

Namespace

Drupal\Tests\user\Kernel\Views

Code

public function testFieldPermission() : void {
    $this->setupPermissionTestData();
    $view = Views::getView('test_field_permission');
    $this->executeView($view);
    $view->initStyle();
    $view->render();
    $style_plugin = $view->style_plugin;
    $expected_permissions = [];
    $expected_permissions[$this->users[0]
        ->id()] = [];
    $expected_permissions[$this->users[1]
        ->id()] = [];
    $expected_permissions[$this->users[2]
        ->id()][] = 'Administer roles and permissions';
    // View user profiles comes first, because we sort by the permission
    // machine name.
    $expected_permissions[$this->users[3]
        ->id()][] = 'View user information';
    $expected_permissions[$this->users[3]
        ->id()][] = 'Administer roles and permissions';
    $expected_permissions[$this->users[3]
        ->id()][] = 'Administer users';
    foreach ($view->result as $index => $row) {
        $uid = $view->field['uid']
            ->getValue($row);
        $rendered_permission = $style_plugin->getField($index, 'permission');
        $expected_output = implode(', ', $expected_permissions[$uid]);
        $this->assertEquals($expected_output, $rendered_permission, 'The right permissions are rendered.');
    }
}

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