4085f4f47bf5e319f7d8c20215215501bd5f3dad
[yaffs-website] / web / core / modules / comment / tests / src / Functional / Views / DefaultViewRecentCommentsTest.php
1 <?php
2
3 namespace Drupal\Tests\comment\Functional\Views;
4
5 use Drupal\comment\CommentInterface;
6 use Drupal\comment\Entity\Comment;
7 use Drupal\comment\Tests\CommentTestTrait;
8 use Drupal\views\Views;
9 use Drupal\Tests\views\Functional\ViewTestBase;
10
11 /**
12  * Tests results for the Recent Comments view shipped with the module.
13  *
14  * @group comment
15  */
16 class DefaultViewRecentCommentsTest extends ViewTestBase {
17
18   use CommentTestTrait;
19
20   /**
21    * Modules to install.
22    *
23    * @var array
24    */
25   public static $modules = ['node', 'comment', 'block'];
26
27   /**
28    * Number of results for the Master display.
29    *
30    * @var int
31    */
32   protected $masterDisplayResults = 5;
33
34   /**
35    * Number of results for the Block display.
36    *
37    * @var int
38    */
39   protected $blockDisplayResults = 5;
40
41   /**
42    * Number of results for the Page display.
43    *
44    * @var int
45    */
46   protected $pageDisplayResults = 5;
47
48   /**
49    * Will hold the comments created for testing.
50    *
51    * @var array
52    */
53   protected $commentsCreated = [];
54
55   /**
56    * Contains the node object used for comments of this test.
57    *
58    * @var \Drupal\node\Node
59    */
60   public $node;
61
62   protected function setUp($import_test_views = TRUE) {
63     parent::setUp($import_test_views);
64
65     // Create a new content type
66     $content_type = $this->drupalCreateContentType();
67
68     // Add a node of the new content type.
69     $node_data = [
70       'type' => $content_type->id(),
71     ];
72
73     $this->addDefaultCommentField('node', $content_type->id());
74     $this->node = $this->drupalCreateNode($node_data);
75
76     // Force a flush of the in-memory storage.
77     $this->container->get('views.views_data')->clear();
78
79     // Create some comments and attach them to the created node.
80     for ($i = 0; $i < $this->masterDisplayResults; $i++) {
81       /** @var \Drupal\comment\CommentInterface $comment */
82       $comment = Comment::create([
83         'status' => CommentInterface::PUBLISHED,
84         'field_name' => 'comment',
85         'entity_type' => 'node',
86         'entity_id' => $this->node->id(),
87       ]);
88       $comment->setOwnerId(0);
89       $comment->setSubject('Test comment ' . $i);
90       $comment->comment_body->value = 'Test body ' . $i;
91       $comment->comment_body->format = 'full_html';
92
93       // Ensure comments are sorted in ascending order.
94       $time = REQUEST_TIME + ($this->masterDisplayResults - $i);
95       $comment->setCreatedTime($time);
96       $comment->changed->value = $time;
97
98       $comment->save();
99     }
100
101     // Store all the nodes just created to access their properties on the tests.
102     $this->commentsCreated = Comment::loadMultiple();
103
104     // Sort created comments in descending order.
105     ksort($this->commentsCreated, SORT_NUMERIC);
106   }
107
108   /**
109    * Tests the block defined by the comments_recent view.
110    */
111   public function testBlockDisplay() {
112     $user = $this->drupalCreateUser(['access comments']);
113     $this->drupalLogin($user);
114
115     $view = Views::getView('comments_recent');
116     $view->setDisplay('block_1');
117     $this->executeView($view);
118
119     $map = [
120       'subject' => 'subject',
121       'cid' => 'cid',
122       'comment_field_data_created' => 'created',
123     ];
124     $expected_result = [];
125     foreach (array_values($this->commentsCreated) as $key => $comment) {
126       $expected_result[$key]['subject'] = $comment->getSubject();
127       $expected_result[$key]['cid'] = $comment->id();
128       $expected_result[$key]['created'] = $comment->getCreatedTime();
129     }
130     $this->assertIdenticalResultset($view, $expected_result, $map);
131
132     // Check the number of results given by the display is the expected.
133     $this->assertEqual(count($view->result), $this->blockDisplayResults,
134       format_string('There are exactly @results comments. Expected @expected',
135         ['@results' => count($view->result), '@expected' => $this->blockDisplayResults]
136       )
137     );
138   }
139
140 }