3 namespace Drupal\comment\Tests\Views;
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\views\Tests\ViewTestBase;
12 * Tests results for the Recent Comments view shipped with the module.
16 class DefaultViewRecentCommentsTest extends ViewTestBase {
25 public static $modules = ['node', 'comment', 'block'];
28 * Number of results for the Master display.
32 protected $masterDisplayResults = 5;
35 * Number of results for the Block display.
39 protected $blockDisplayResults = 5;
42 * Number of results for the Page display.
46 protected $pageDisplayResults = 5;
49 * Will hold the comments created for testing.
53 protected $commentsCreated = [];
56 * Contains the node object used for comments of this test.
58 * @var \Drupal\node\Node
62 protected function setUp() {
65 // Create a new content type
66 $content_type = $this->drupalCreateContentType();
68 // Add a node of the new content type.
70 'type' => $content_type->id(),
73 $this->addDefaultCommentField('node', $content_type->id());
74 $this->node = $this->drupalCreateNode($node_data);
76 // Force a flush of the in-memory storage.
77 $this->container->get('views.views_data')->clear();
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(),
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';
93 // Ensure comments are sorted in ascending order.
94 $time = REQUEST_TIME + ($this->masterDisplayResults - $i);
95 $comment->setCreatedTime($time);
96 $comment->changed->value = $time;
101 // Store all the nodes just created to access their properties on the tests.
102 $this->commentsCreated = Comment::loadMultiple();
104 // Sort created comments in descending order.
105 ksort($this->commentsCreated, SORT_NUMERIC);
109 * Tests the block defined by the comments_recent view.
111 public function testBlockDisplay() {
112 $user = $this->drupalCreateUser(['access comments']);
113 $this->drupalLogin($user);
115 $view = Views::getView('comments_recent');
116 $view->setDisplay('block_1');
117 $this->executeView($view);
120 'subject' => 'subject',
122 'comment_field_data_created' => 'created'
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();
130 $this->assertIdenticalResultset($view, $expected_result, $map);
132 // Check the number of results given by the display is the expected.
133 $this->assertEqual(sizeof($view->result), $this->blockDisplayResults,
134 format_string('There are exactly @results comments. Expected @expected',
135 ['@results' => count($view->result), '@expected' => $this->blockDisplayResults]