1<?php 2 3namespace Drupal\Tests\comment\Kernel\Views; 4 5use Drupal\comment\Entity\Comment; 6use Drupal\Core\Session\AnonymousUserSession; 7use Drupal\entity_test\Entity\EntityTest; 8use Drupal\Tests\views\Kernel\ViewsKernelTestBase; 9use Drupal\user\Entity\Role; 10use Drupal\user\Entity\User; 11use Drupal\views\Entity\View; 12use Drupal\views\Views; 13 14/** 15 * Tests comment user name field. 16 * 17 * @group comment 18 */ 19class CommentUserNameTest extends ViewsKernelTestBase { 20 21 /** 22 * Admin user. 23 * 24 * @var \Drupal\user\UserInterface 25 */ 26 protected $adminUser; 27 /** 28 * {@inheritdoc} 29 */ 30 protected static $modules = ['user', 'comment', 'entity_test']; 31 32 /** 33 * {@inheritdoc} 34 */ 35 protected function setUp($import_test_views = TRUE): void { 36 parent::setUp($import_test_views); 37 38 $this->installEntitySchema('user'); 39 $this->installEntitySchema('comment'); 40 $this->installEntitySchema('entity_test'); 41 // Create the anonymous role. 42 $this->installConfig(['user']); 43 44 // Create an anonymous user. 45 $storage = \Drupal::entityTypeManager()->getStorage('user'); 46 // Insert a row for the anonymous user. 47 $storage 48 ->create([ 49 'uid' => 0, 50 'name' => '', 51 'status' => 0, 52 ]) 53 ->save(); 54 55 $admin_role = Role::create([ 56 'id' => 'admin', 57 'permissions' => ['administer comments', 'access user profiles'], 58 ]); 59 $admin_role->save(); 60 61 /** @var \Drupal\user\RoleInterface $anonymous_role */ 62 $anonymous_role = Role::load(Role::ANONYMOUS_ID); 63 $anonymous_role->grantPermission('access comments'); 64 $anonymous_role->save(); 65 66 $this->adminUser = User::create([ 67 'name' => $this->randomMachineName(), 68 'roles' => [$admin_role->id()], 69 ]); 70 $this->adminUser->save(); 71 72 $host = EntityTest::create(['name' => $this->randomString()]); 73 $host->save(); 74 75 // Create some comments. 76 $comment = Comment::create([ 77 'subject' => 'My comment title', 78 'uid' => $this->adminUser->id(), 79 'name' => $this->adminUser->label(), 80 'entity_type' => 'entity_test', 81 'field_name' => 'comment', 82 'entity_id' => $host->id(), 83 'comment_type' => 'entity_test', 84 'status' => 1, 85 ]); 86 $comment->save(); 87 88 $comment_anonymous = Comment::create([ 89 'subject' => 'Anonymous comment title', 90 'uid' => 0, 91 'name' => 'barry', 92 'mail' => 'test@example.com', 93 'homepage' => 'https://example.com', 94 'entity_type' => 'entity_test', 95 'field_name' => 'comment', 96 'entity_id' => $host->id(), 97 'comment_type' => 'entity_test', 98 'created' => 123456, 99 'status' => 1, 100 ]); 101 $comment_anonymous->save(); 102 } 103 104 /** 105 * Tests the username formatter. 106 */ 107 public function testUsername() { 108 $view_id = $this->randomMachineName(); 109 $view = View::create([ 110 'id' => $view_id, 111 'base_table' => 'comment_field_data', 112 'display' => [ 113 'default' => [ 114 'display_plugin' => 'default', 115 'id' => 'default', 116 'display_options' => [ 117 'fields' => [ 118 'name' => [ 119 'table' => 'comment_field_data', 120 'field' => 'name', 121 'id' => 'name', 122 'plugin_id' => 'field', 123 'type' => 'comment_username', 124 ], 125 'subject' => [ 126 'table' => 'comment_field_data', 127 'field' => 'subject', 128 'id' => 'subject', 129 'plugin_id' => 'field', 130 'type' => 'string', 131 'settings' => [ 132 'link_to_entity' => TRUE, 133 ], 134 ], 135 ], 136 ], 137 ], 138 ], 139 ]); 140 $view->save(); 141 142 /** @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */ 143 $account_switcher = \Drupal::service('account_switcher'); 144 145 /** @var \Drupal\Core\Render\RendererInterface $renderer */ 146 $renderer = \Drupal::service('renderer'); 147 148 $account_switcher->switchTo($this->adminUser); 149 $executable = Views::getView($view_id); 150 $build = $executable->preview(); 151 $this->setRawContent($renderer->renderRoot($build)); 152 153 $this->assertLink('My comment title'); 154 $this->assertLink('Anonymous comment title'); 155 // Display plugin of the view is showing the name field. When comment 156 // belongs to an authenticated user the name field has no value. 157 $comment_author = $this->xpath('//div[contains(@class, :class)]/span[normalize-space(text())=""]', [ 158 ':class' => 'views-field-subject', 159 ]); 160 $this->assertTrue(!empty($comment_author)); 161 // When comment belongs to an anonymous user the name field has a value and 162 // it is rendered correctly. 163 $this->assertLink('barry (not verified)'); 164 165 $account_switcher->switchTo(new AnonymousUserSession()); 166 $executable = Views::getView($view_id); 167 $executable->storage->invalidateCaches(); 168 169 $build = $executable->preview(); 170 $this->setRawContent($renderer->renderRoot($build)); 171 172 // No access to user-profiles, so shouldn't be able to see links. 173 $this->assertNoLink($this->adminUser->label()); 174 // Note: External users aren't pointing to drupal user profiles. 175 $this->assertLink('barry (not verified)'); 176 $this->assertLink('My comment title'); 177 $this->assertLink('Anonymous comment title'); 178 } 179 180} 181