X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fcomment%2Ftests%2Fsrc%2FFunctional%2FViews%2FDefaultViewRecentCommentsTest.php;fp=web%2Fcore%2Fmodules%2Fcomment%2Ftests%2Fsrc%2FFunctional%2FViews%2FDefaultViewRecentCommentsTest.php;h=5cb9680514133a43eef071dda7c078c9927266b2;hp=0000000000000000000000000000000000000000;hb=9917807b03b64faf00f6a1f29dcb6eafc454efa5;hpb=aea91e65e895364e460983b890e295aa5d5540a5 diff --git a/web/core/modules/comment/tests/src/Functional/Views/DefaultViewRecentCommentsTest.php b/web/core/modules/comment/tests/src/Functional/Views/DefaultViewRecentCommentsTest.php new file mode 100644 index 000000000..5cb968051 --- /dev/null +++ b/web/core/modules/comment/tests/src/Functional/Views/DefaultViewRecentCommentsTest.php @@ -0,0 +1,140 @@ +drupalCreateContentType(); + + // Add a node of the new content type. + $node_data = [ + 'type' => $content_type->id(), + ]; + + $this->addDefaultCommentField('node', $content_type->id()); + $this->node = $this->drupalCreateNode($node_data); + + // Force a flush of the in-memory storage. + $this->container->get('views.views_data')->clear(); + + // Create some comments and attach them to the created node. + for ($i = 0; $i < $this->masterDisplayResults; $i++) { + /** @var \Drupal\comment\CommentInterface $comment */ + $comment = Comment::create([ + 'status' => CommentInterface::PUBLISHED, + 'field_name' => 'comment', + 'entity_type' => 'node', + 'entity_id' => $this->node->id(), + ]); + $comment->setOwnerId(0); + $comment->setSubject('Test comment ' . $i); + $comment->comment_body->value = 'Test body ' . $i; + $comment->comment_body->format = 'full_html'; + + // Ensure comments are sorted in ascending order. + $time = REQUEST_TIME + ($this->masterDisplayResults - $i); + $comment->setCreatedTime($time); + $comment->changed->value = $time; + + $comment->save(); + } + + // Store all the nodes just created to access their properties on the tests. + $this->commentsCreated = Comment::loadMultiple(); + + // Sort created comments in descending order. + ksort($this->commentsCreated, SORT_NUMERIC); + } + + /** + * Tests the block defined by the comments_recent view. + */ + public function testBlockDisplay() { + $user = $this->drupalCreateUser(['access comments']); + $this->drupalLogin($user); + + $view = Views::getView('comments_recent'); + $view->setDisplay('block_1'); + $this->executeView($view); + + $map = [ + 'subject' => 'subject', + 'cid' => 'cid', + 'comment_field_data_created' => 'created' + ]; + $expected_result = []; + foreach (array_values($this->commentsCreated) as $key => $comment) { + $expected_result[$key]['subject'] = $comment->getSubject(); + $expected_result[$key]['cid'] = $comment->id(); + $expected_result[$key]['created'] = $comment->getCreatedTime(); + } + $this->assertIdenticalResultset($view, $expected_result, $map); + + // Check the number of results given by the display is the expected. + $this->assertEqual(count($view->result), $this->blockDisplayResults, + format_string('There are exactly @results comments. Expected @expected', + ['@results' => count($view->result), '@expected' => $this->blockDisplayResults] + ) + ); + } + +}