DefaultViewRecentCommentsTest.php in Zircon Profile 8
File
core/modules/comment/src/Tests/Views/DefaultViewRecentCommentsTest.php
View source
<?php
namespace Drupal\comment\Tests\Views;
use Drupal\comment\CommentInterface;
use Drupal\comment\Entity\Comment;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\views\Views;
use Drupal\views\Tests\ViewTestBase;
class DefaultViewRecentCommentsTest extends ViewTestBase {
use CommentTestTrait;
public static $modules = array(
'node',
'comment',
'block',
);
protected $masterDisplayResults = 5;
protected $blockDisplayResults = 5;
protected $pageDisplayResults = 5;
protected $commentsCreated = array();
public $node;
protected function setUp() {
parent::setUp();
$content_type = $this
->drupalCreateContentType();
$node_data = array(
'type' => $content_type
->id(),
);
$this
->addDefaultCommentField('node', $content_type
->id());
$this->node = $this
->drupalCreateNode($node_data);
$this->container
->get('views.views_data')
->clear();
for ($i = 0; $i < $this->masterDisplayResults; $i++) {
$comment = entity_create('comment', array(
'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';
$time = REQUEST_TIME + ($this->masterDisplayResults - $i);
$comment
->setCreatedTime($time);
$comment->changed->value = $time;
$comment
->save();
}
$this->commentsCreated = Comment::loadMultiple();
ksort($this->commentsCreated, SORT_NUMERIC);
}
public function testBlockDisplay() {
$user = $this
->drupalCreateUser([
'access comments',
]);
$this
->drupalLogin($user);
$view = Views::getView('comments_recent');
$view
->setDisplay('block_1');
$this
->executeView($view);
$map = array(
'subject' => 'subject',
'cid' => 'cid',
'comment_field_data_created' => 'created',
);
$expected_result = array();
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);
$this
->assertEqual(sizeof($view->result), $this->blockDisplayResults, format_string('There are exactly @results comments. Expected @expected', array(
'@results' => count($view->result),
'@expected' => $this->blockDisplayResults,
)));
}
}