3 namespace Drupal\Tests\comment\Functional\Views;
5 use Drupal\comment\Entity\Comment;
6 use Drupal\Core\Render\RenderContext;
7 use Drupal\Core\Session\AnonymousUserSession;
8 use Drupal\user\RoleInterface;
9 use Drupal\views\Views;
12 * Tests the comment field name field.
16 class CommentFieldNameTest extends CommentTestBase {
19 * Views used by this test.
23 public static $testViews = ['test_comment_field_name'];
26 * The second comment entity used by this test.
28 * @var \Drupal\comment\CommentInterface
30 protected $customComment;
33 * The comment field name used by this test.
37 protected $fieldName = 'comment_custom';
42 protected function setUp($import_test_views = TRUE) {
43 parent::setUp($import_test_views);
44 $this->addDefaultCommentField('node', 'page', $this->fieldName);
45 $this->customComment = Comment::create([
46 'entity_id' => $this->nodeUserCommented->id(),
47 'entity_type' => 'node',
48 'field_name' => $this->fieldName,
50 $this->customComment->save();
54 * Test comment field name.
56 public function testCommentFieldName() {
57 /** @var \Drupal\Core\Render\RendererInterface $renderer */
58 $renderer = \Drupal::service('renderer');
59 // Grant permission to properly check view access on render.
60 user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['access comments']);
61 $this->container->get('account_switcher')->switchTo(new AnonymousUserSession());
62 $view = Views::getView('test_comment_field_name');
63 $this->executeView($view);
67 'cid' => $this->comment->id(),
68 'field_name' => $this->comment->getFieldName(),
71 'cid' => $this->customComment->id(),
72 'field_name' => $this->customComment->getFieldName(),
77 'comment_field_data_field_name' => 'field_name',
79 $this->assertIdenticalResultset($view, $expected_result, $column_map);
81 // Test that data rendered.
82 $output = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) {
83 return $view->field['field_name']->advancedRender($view->result[0]);
85 $this->assertEqual($this->comment->getFieldName(), $output);
86 $output = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) {
87 return $view->field['field_name']->advancedRender($view->result[1]);
89 $this->assertEqual($this->customComment->getFieldName(), $output);