HandlerFieldUserNameTest.php 2.27 KB
Newer Older
merlinofchaos's avatar
merlinofchaos committed
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\user\Tests\Views\HandlerFieldUserNameTest.
merlinofchaos's avatar
merlinofchaos committed
6 7
 */

8
namespace Drupal\user\Tests\Views;
9

10 11
use Drupal\views\Views;

merlinofchaos's avatar
merlinofchaos committed
12
/**
13
 * Tests the handler of the user: name field.
merlinofchaos's avatar
merlinofchaos committed
14
 *
15
 * @group user
merlinofchaos's avatar
merlinofchaos committed
16 17
 * @see views_handler_field_user_name
 */
18 19
class HandlerFieldUserNameTest extends UserTestBase {

20 21 22 23 24 25 26 27
  /**
   * Views used by this test.
   *
   * @var array
   */
  public static $testViews = array('test_views_handler_field_user_name');

  public function testUserName() {
28 29
    $this->drupalLogin($this->drupalCreateUser(array('access user profiles')));

30
    $view = Views::getView('test_views_handler_field_user_name');
merlinofchaos's avatar
merlinofchaos committed
31 32 33
    $this->executeView($view);

    $view->field['name']->options['link_to_user'] = TRUE;
34 35
    $username = $view->result[0]->users_field_data_name = $this->randomMachineName();
    $view->result[0]->users_field_data_uid = 1;
36
    $render = $view->field['name']->advancedRender($view->result[0]);
merlinofchaos's avatar
merlinofchaos committed
37 38 39 40
    $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/1') !== FALSE, 'If link to user is checked the link to the user should appear as well.');

    $view->field['name']->options['link_to_user'] = FALSE;
41 42
    $username = $view->result[0]->users_field_data_name = $this->randomMachineName();
    $view->result[0]->users_field_data_uid = 1;
43
    $render = $view->field['name']->advancedRender($view->result[0]);
merlinofchaos's avatar
merlinofchaos committed
44 45
    $this->assertIdentical($render, $username, 'If the user is not linked the username should be printed out for a normal user.');

46
    $view->result[0]->users_field_data_uid = 0;
47
    $anon_name = \Drupal::config('user.settings')->get('anonymous');
48
    $view->result[0]->users_field_data_name = '';
49
    $render = $view->field['name']->advancedRender($view->result[0]);
merlinofchaos's avatar
merlinofchaos committed
50 51 52
    $this->assertIdentical($render, $anon_name , 'For user0 it should use the default anonymous name by default.');

    $view->field['name']->options['overwrite_anonymous'] = TRUE;
53
    $anon_name = $view->field['name']->options['anonymous_text'] = $this->randomMachineName();
54
    $render = $view->field['name']->advancedRender($view->result[0]);
merlinofchaos's avatar
merlinofchaos committed
55 56 57 58
    $this->assertIdentical($render, $anon_name , 'For user0 it should use the configured anonymous text if overwrite_anonymous is checked.');
  }

}