1 user_views_handler_field_name.test UserViewsHandlerFieldNameTest::testUserName()

File

core/modules/user/tests/user_views_handler_field_name.test, line 15
Definition of UserViewsHandlerFieldNameTest.

Class

UserViewsHandlerFieldNameTest
Tests the field username handler.

Code

function testUserName() {
  $view = $this->view_user_name();
  $view->init_display();
  $this->executeView($view);

  $view->row_index = 0;

  $view->field['name']->options['link_to_user'] = TRUE;
  $user_id = 1;
  $user = user_load($user_id);
  $user_path = $user->path['alias'] ? $user->path['alias'] : 'user/' . $user_id;
  $username = $view->result[0]->users_name = $this->randomName();
  $view->result[0]->uid = $user_id;
  $render = $view->field['name']->advanced_render($view->result[0]);
  $this->assertTrue(strpos($render, $username) !== FALSE, 'If link to user is checked, the username should be part of the output.');
  $this->assertTrue(strpos($render, $user_path) !== FALSE, 'If link to user is checked, the link to the user should appear as well.');

  $view->field['name']->options['link_to_user'] = FALSE;
  $username = $view->result[0]->users_name = $this->randomName();
  $view->result[0]->uid = 1;
  $render = $view->field['name']->advanced_render($view->result[0]);
  $this->assertIdentical($render, $username, 'If the user is not linked, the username should be printed out for a normal user.');

  $view->result[0]->uid = 0;
  $anon_name = config_get('system.core', 'anonymous');
  $view->result[0]->users_name = '';
  $render = $view->field['name']->advanced_render($view->result[0]);
  $this->assertIdentical($render, $anon_name, 'For user0 it should use the default anonymous name by default.');

  $view->field['name']->options['overwrite_anonymous'] = TRUE;
  $anon_name = $view->field['name']->options['anonymous_text'] = $this->randomName();
  $render = $view->field['name']->advanced_render($view->result[0]);
  $this->assertIdentical($render, $anon_name, 'For user0 it should use the configured anonymous text, if overwrite_anonymous is checked.');
}