public function HandlerFieldRoleTest::testRole

File

drupal/core/modules/user/lib/Drupal/user/Tests/Views/HandlerFieldRoleTest.php, line 32
Contains Drupal\user\Tests\Views\HandlerFieldRoleTest.

Class

HandlerFieldRoleTest
Tests the role field handler.

Namespace

Drupal\user\Tests\Views

Code

public function testRole() {

  // Create a couple of roles for the view.
  $rolename_a = 'a' . $this
    ->randomName(8);
  $rid_a = $this
    ->drupalCreateRole(array(
    'access content',
  ), $rolename_a, $rolename_a, 9);
  $rolename_b = 'b' . $this
    ->randomName(8);
  $rid_b = $this
    ->drupalCreateRole(array(
    'access content',
  ), $rolename_b, $rolename_b, 8);
  $rolename_not_assigned = $this
    ->randomName(8);
  $this
    ->drupalCreateRole(array(
    'access content',
  ), $rolename_not_assigned, $rolename_not_assigned);

  // Add roles to user 1.
  $user = entity_load('user', 1);
  $user->roles[1]->value = $rolename_a;
  $user->roles[2]->value = $rolename_b;
  $user
    ->save();
  $view = views_get_view('test_views_handler_field_role');
  $this
    ->executeView($view);
  $view->row_index = 0;

  // The role field is populated during pre_render.
  $view->field['rid']
    ->pre_render($view->result);
  $render = $view->field['rid']
    ->advancedRender($view->result[0]);
  $this
    ->assertEqual($rolename_b . $rolename_a, $render, 'View test_views_handler_field_role renders role assigned to user in the correct order.');
  $this
    ->assertFalse(strpos($render, $rolename_not_assigned), 'View test_views_handler_field_role does not render a role not assigned to a user.');
}