CommentUserNameTest.php 4.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
<?php

/**
 * @file
 * Contains \Drupal\comment\Tests\Views\CommentUserNameTest.
 */

namespace Drupal\comment\Tests\Views;

use Drupal\comment\Entity\Comment;
use Drupal\Core\Session\AnonymousUserSession;
use Drupal\user\Entity\Role;
use Drupal\user\Entity\User;
use Drupal\views\Entity\View;
use Drupal\views\Tests\ViewUnitTestBase;
use Drupal\views\Views;

/**
 * Tests comment user name field
 *
 * @group comment
 */
class CommentUserNameTest extends ViewUnitTestBase {

  /**
   * Admin user.
   *
   * @var \Drupal\user\UserInterface
   */
  protected $adminUser;
  /**
   * {@inheritdoc}
   */
  public static $modules = ['user', 'comment', 'entity_test'];

  /**
   * {@inheritdoc}
   */
  protected function setUp($import_test_views = TRUE) {
    parent::setUp($import_test_views);

    $this->installEntitySchema('user');
    $this->installEntitySchema('comment');
    // Create the anonymous role.
    $this->installConfig(['user']);

    // Create an anonymous user.
    $storage = \Drupal::entityManager()->getStorage('user');
    // Insert a row for the anonymous user.
    $storage
      ->create(array(
        'uid' => 0,
        'status' => 0,
      ))
      ->save();

    $admin_role = Role::create([
      'id' => 'admin',
      'permissions' => ['administer comments', 'access user profiles'],
    ]);
    $admin_role->save();

    /* @var \Drupal\user\RoleInterface $anonymous_role */
    $anonymous_role = Role::load(Role::ANONYMOUS_ID);
65
    $anonymous_role->grantPermission('access comments');
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
    $anonymous_role->save();

    $this->adminUser = User::create([
      'name' => $this->randomMachineName(),
      'roles' => [$admin_role->id()],
    ]);
    $this->adminUser->save();

    // Create some comments.
    $comment = Comment::create([
      'subject' => 'My comment title',
      'uid' => $this->adminUser->id(),
      'entity_type' => 'entity_test',
      'comment_type' => 'entity_test',
      'status' => 1,
    ]);
    $comment->save();

    $comment_anonymous = Comment::create([
      'subject' => 'Anonymous comment title',
      'uid' => 0,
      'name' => 'barry',
      'mail' => 'test@example.com',
      'homepage' => 'https://example.com',
      'entity_type' => 'entity_test',
      'comment_type' => 'entity_test',
      'created' => 123456,
      'status' => 1,
    ]);
    $comment_anonymous->save();
  }

  /**
   * Test the username formatter.
   */
  public function testUsername() {
    $view_id = $this->randomMachineName();
    $view = View::create([
      'id' => $view_id,
      'base_table' => 'comment_field_data',
      'display' => [
        'default' => [
          'display_plugin' => 'default',
          'id' => 'default',
          'display_options' => [
            'fields' => [
              'name' => [
                'table' => 'comment_field_data',
                'field' => 'name',
                'id' => 'name',
                'plugin_id' => 'field',
                'type' => 'comment_username'
              ],
              'subject' => [
                'table' => 'comment_field_data',
                'field' => 'subject',
                'id' => 'subject',
                'plugin_id' => 'field',
124 125 126 127
                'type' => 'string',
                'settings' => [
                  'link_to_entity' => TRUE,
                ],
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
              ],
            ],
          ],
        ],
      ],
    ]);
    $view->save();

    /* @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */
    $account_switcher = \Drupal::service('account_switcher');

    /* @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = \Drupal::service('renderer');

    $account_switcher->switchTo($this->adminUser);
    $executable = Views::getView($view_id);
    $build = $executable->preview();
    $this->setRawContent($renderer->render($build));
    $this->verbose($this->getRawContent());

    $this->assertLink('My comment title');
    $this->assertLink('Anonymous comment title');
    $this->assertLink($this->adminUser->label());
    $this->assertLink('barry (not verified)');

    $account_switcher->switchTo(new AnonymousUserSession());
    $executable = Views::getView($view_id);
155 156
    $executable->storage->invalidateCaches();

157 158 159 160 161
    $build = $executable->preview();
    $this->setRawContent($renderer->render($build));

    // No access to user-profiles, so shouldn't be able to see links.
    $this->assertNoLink($this->adminUser->label());
162 163
    // Note: External users aren't pointing to drupal user profiles.
    $this->assertLink('barry (not verified)');
164 165 166 167 168 169
    $this->verbose($this->getRawContent());
    $this->assertLink('My comment title');
    $this->assertLink('Anonymous comment title');
  }

}