3 namespace Drupal\user\Tests\Views;
5 use Drupal\Component\Utility\Html;
6 use Drupal\user\Entity\User;
9 * Tests the handler of the user: role field.
12 * @see views_handler_field_user_name
14 class HandlerFieldRoleTest extends UserTestBase {
17 * Views used by this test.
21 public static $testViews = ['test_views_handler_field_role'];
23 public function testRole() {
24 // Create a couple of roles for the view.
25 $rolename_a = 'a' . $this->randomMachineName(8);
26 $this->drupalCreateRole(['access content'], $rolename_a, '<em>' . $rolename_a . '</em>', 9);
28 $rolename_b = 'b' . $this->randomMachineName(8);
29 $this->drupalCreateRole(['access content'], $rolename_b, $rolename_b, 8);
31 $rolename_not_assigned = $this->randomMachineName(8);
32 $this->drupalCreateRole(['access content'], $rolename_not_assigned, $rolename_not_assigned);
34 // Add roles to user 1.
35 $user = User::load(1);
36 $user->addRole($rolename_a);
37 $user->addRole($rolename_b);
40 $this->drupalLogin($this->createUser(['access user profiles']));
41 $this->drupalGet('/test-views-handler-field-role');
42 $this->assertText($rolename_b . Html::escape('<em>' . $rolename_a . '</em>'), 'View test_views_handler_field_role renders role assigned to user in the correct order and markup in role names is escaped.');
43 $this->assertNoText($rolename_not_assigned, 'View test_views_handler_field_role does not render a role not assigned to a user.');