DefaultViewRecentCommentsTest.php in Drupal 10
File
core/modules/comment/tests/src/Functional/Views/DefaultViewRecentCommentsTest.php
View source
<?php
namespace Drupal\Tests\comment\Functional\Views;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\comment\CommentInterface;
use Drupal\comment\Entity\Comment;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\views\Views;
use Drupal\Tests\views\Functional\ViewTestBase;
class DefaultViewRecentCommentsTest extends ViewTestBase {
use CommentTestTrait;
protected static $modules = [
'node',
'comment',
'block',
];
protected $defaultTheme = 'stark';
protected $defaultDisplayResults = 5;
protected $blockDisplayResults = 5;
protected $pageDisplayResults = 5;
protected $commentsCreated = [];
public $node;
protected function setUp($import_test_views = TRUE, $modules = []) : void {
parent::setUp($import_test_views, $modules);
$content_type = $this
->drupalCreateContentType();
$node_data = [
'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->defaultDisplayResults; $i++) {
$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';
$time = REQUEST_TIME + ($this->defaultDisplayResults - $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 = [
'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);
$this
->assertCount($this->blockDisplayResults, $view->result, new FormattableMarkup('There are exactly @results comments. Expected @expected', [
'@results' => count($view->result),
'@expected' => $this->blockDisplayResults,
]));
}
}