class DefaultViewRecentCommentsTest
Same name and namespace in other branches
- 11.x core/modules/comment/tests/src/Functional/Views/DefaultViewRecentCommentsTest.php \Drupal\Tests\comment\Functional\Views\DefaultViewRecentCommentsTest
- 10 core/modules/comment/tests/src/Functional/Views/DefaultViewRecentCommentsTest.php \Drupal\Tests\comment\Functional\Views\DefaultViewRecentCommentsTest
- 8.9.x core/modules/comment/tests/src/Functional/Views/DefaultViewRecentCommentsTest.php \Drupal\Tests\comment\Functional\Views\DefaultViewRecentCommentsTest
Tests results for the Recent Comments view shipped with the module.
@group comment
Hierarchy
- class \Drupal\Tests\BrowserTestBase uses \Drupal\Core\Test\FunctionalTestSetupTrait, \Drupal\Tests\UiHelperTrait, \Drupal\Core\Test\TestSetupTrait, \Drupal\Tests\block\Traits\BlockCreationTrait, \Drupal\FunctionalTests\AssertLegacyTrait, \Drupal\Tests\RandomGeneratorTrait, \Drupal\Tests\node\Traits\NodeCreationTrait, \Drupal\Tests\node\Traits\ContentTypeCreationTrait, \Drupal\Tests\ConfigTestTrait, \Drupal\Tests\TestRequirementsTrait, \Drupal\Tests\user\Traits\UserCreationTrait, \Drupal\Tests\XdebugRequestTrait, \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, \Drupal\Tests\ExtensionListTestTrait implements \PHPUnit\Framework\TestCase
- class \Drupal\Tests\views\Functional\ViewTestBase uses \Drupal\views\Tests\ViewResultAssertionTrait implements \Drupal\Tests\BrowserTestBase
- class \Drupal\Tests\comment\Functional\Views\DefaultViewRecentCommentsTest uses \Drupal\comment\Tests\CommentTestTrait implements \Drupal\Tests\views\Functional\ViewTestBase
- class \Drupal\Tests\views\Functional\ViewTestBase uses \Drupal\views\Tests\ViewResultAssertionTrait implements \Drupal\Tests\BrowserTestBase
Expanded class hierarchy of DefaultViewRecentCommentsTest
File
-
core/
modules/ comment/ tests/ src/ Functional/ Views/ DefaultViewRecentCommentsTest.php, line 17
Namespace
Drupal\Tests\comment\Functional\ViewsView source
class DefaultViewRecentCommentsTest extends ViewTestBase {
use CommentTestTrait;
/**
* Modules to install.
*
* @var array
*/
protected static $modules = [
'node',
'comment',
'block',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Number of results for the Default display.
*
* @var int
*/
protected $defaultDisplayResults = 5;
/**
* Number of results for the Block display.
*
* @var int
*/
protected $blockDisplayResults = 5;
/**
* Number of results for the Page display.
*
* @var int
*/
protected $pageDisplayResults = 5;
/**
* Will hold the comments created for testing.
*
* @var array
*/
protected $commentsCreated = [];
/**
* Contains the node object used for comments of this test.
*
* @var \Drupal\node\NodeInterface
*/
public $node;
/**
* {@inheritdoc}
*/
protected function setUp($import_test_views = TRUE, $modules = []) : void {
parent::setUp($import_test_views, $modules);
// Create a new content type
$content_type = $this->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->defaultDisplayResults; $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->defaultDisplayResults - $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->assertCount($this->blockDisplayResults, $view->result, new FormattableMarkup('There are exactly @results comments. Expected @expected', [
'@results' => count($view->result),
'@expected' => $this->blockDisplayResults,
]));
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.