class CommentDepthTest
Same name and namespace in other branches
- 11.x core/modules/comment/tests/src/Kernel/Views/CommentDepthTest.php \Drupal\Tests\comment\Kernel\Views\CommentDepthTest
Tests the depth of the comment field handler.
@group comment
Hierarchy
- class \Drupal\KernelTests\KernelTestBase extends \Drupal\Core\DependencyInjection\ServiceProviderInterface uses \Drupal\KernelTests\AssertLegacyTrait, \Drupal\KernelTests\AssertContentTrait, \Drupal\Tests\RandomGeneratorTrait, \Drupal\Tests\ConfigTestTrait, \Drupal\Tests\ExtensionListTestTrait, \Drupal\Tests\TestRequirementsTrait, \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait implements \PHPUnit\Framework\TestCase
- class \Drupal\Tests\views\Kernel\ViewsKernelTestBase uses \Drupal\views\Tests\ViewResultAssertionTrait implements \Drupal\KernelTests\KernelTestBase
- class \Drupal\Tests\comment\Kernel\Views\CommentViewsKernelTestBase implements \Drupal\Tests\views\Kernel\ViewsKernelTestBase
- class \Drupal\Tests\comment\Kernel\Views\CommentDepthTest implements \Drupal\Tests\comment\Kernel\Views\CommentViewsKernelTestBase
- class \Drupal\Tests\comment\Kernel\Views\CommentViewsKernelTestBase implements \Drupal\Tests\views\Kernel\ViewsKernelTestBase
- class \Drupal\Tests\views\Kernel\ViewsKernelTestBase uses \Drupal\views\Tests\ViewResultAssertionTrait implements \Drupal\KernelTests\KernelTestBase
Expanded class hierarchy of CommentDepthTest
File
-
core/
modules/ comment/ tests/ src/ Kernel/ Views/ CommentDepthTest.php, line 16
Namespace
Drupal\Tests\comment\Kernel\ViewsView source
class CommentDepthTest extends CommentViewsKernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'entity_test',
];
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = [
'test_comment',
];
/**
* {@inheritdoc}
*/
protected function setUp($import_test_views = TRUE) : void {
parent::setUp($import_test_views);
$this->installEntitySchema('entity_test');
}
/**
* Test the comment depth.
*/
public function testCommentDepth() {
$this->enableModules([
'field',
]);
$this->installConfig([
'field',
]);
// Create a comment field storage.
$field_storage_comment = FieldStorageConfig::create([
'field_name' => 'comment',
'type' => 'comment',
'entity_type' => 'entity_test',
]);
$field_storage_comment->save();
// Create a comment field which allows threading.
$field_comment = FieldConfig::create([
'field_name' => 'comment',
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
'settings' => [
'default_mode' => CommentManagerInterface::COMMENT_MODE_THREADED,
],
]);
$field_comment->save();
// Create a test entity.
$host = EntityTest::create([
'name' => $this->randomString(),
]);
$host->save();
// Create the thread of comments.
$comment1 = $this->commentStorage
->create([
'uid' => $this->adminUser
->id(),
'entity_type' => 'entity_test',
'entity_id' => $host->id(),
'comment_type' => 'entity_test',
'field_name' => $field_storage_comment->getName(),
'status' => 1,
]);
$comment1->save();
$comment2 = $this->commentStorage
->create([
'uid' => $this->adminUser
->id(),
'entity_type' => 'entity_test',
'entity_id' => $host->id(),
'comment_type' => 'entity_test',
'field_name' => $field_storage_comment->getName(),
'status' => 1,
'pid' => $comment1->id(),
]);
$comment2->save();
$comment3 = $this->commentStorage
->create([
'uid' => $this->adminUser
->id(),
'entity_type' => 'entity_test',
'entity_id' => $host->id(),
'comment_type' => 'entity_test',
'field_name' => $field_storage_comment->getName(),
'status' => 1,
'pid' => $comment2->id(),
]);
$comment3->save();
$view = Views::getView('test_comment');
$view->setDisplay();
$view->displayHandlers
->get('default')
->overrideOption('fields', [
'thread' => [
'table' => 'comment_field_data',
'field' => 'thread',
'id' => 'thread',
'plugin_id' => 'comment_depth',
'entity_type' => 'comment',
],
]);
$view->save();
$view->preview();
// Check if the depth of the first comment is 0.
$comment1_depth = $view->style_plugin
->getField(0, 'thread');
$this->assertEquals(0, (string) $comment1_depth, "The depth of the first comment is 0.");
// Check if the depth of the first comment is 1.
$comment2_depth = $view->style_plugin
->getField(1, 'thread');
$this->assertEquals(1, (string) $comment2_depth, "The depth of the second comment is 1.");
// Check if the depth of the first comment is 2.
$comment3_depth = $view->style_plugin
->getField(2, 'thread');
$this->assertEquals(2, (string) $comment3_depth, "The depth of the third comment is 2.");
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.