db backup prior to drupal security update
[yaffs-website] / web / core / modules / comment / src / Tests / Views / CommentFieldNameTest.php
1 <?php
2
3 namespace Drupal\comment\Tests\Views;
4
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;
10
11 /**
12  * Tests the comment field name field.
13  *
14  * @group comment
15  */
16 class CommentFieldNameTest extends CommentTestBase {
17
18   /**
19    * Views used by this test.
20    *
21    * @var array
22    */
23   public static $testViews = ['test_comment_field_name'];
24
25   /**
26    * The second comment entity used by this test.
27    *
28    * @var \Drupal\comment\CommentInterface
29    */
30   protected $customComment;
31
32   /**
33    * The comment field name used by this test.
34    *
35    * @var string
36    */
37   protected $fieldName = 'comment_custom';
38
39   /**
40    * {@inheritdoc}
41    */
42   protected function setUp() {
43     parent::setUp();
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,
49     ]);
50     $this->customComment->save();
51   }
52
53   /**
54    * Test comment field name.
55    */
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);
64
65     $expected_result = [
66       [
67         'cid' => $this->comment->id(),
68         'field_name' => $this->comment->getFieldName(),
69       ],
70       [
71         'cid' => $this->customComment->id(),
72         'field_name' => $this->customComment->getFieldName(),
73       ],
74     ];
75     $column_map = [
76       'cid' => 'cid',
77       'comment_field_data_field_name' => 'field_name',
78     ];
79     $this->assertIdenticalResultset($view, $expected_result, $column_map);
80
81     // Test that data rendered.
82     $output = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) {
83       return $view->field['field_name']->advancedRender($view->result[0]);
84     });
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]);
88     });
89     $this->assertEqual($this->customComment->getFieldName(), $output);
90   }
91
92 }