View source
<?php
namespace Drupal\Tests\comment\Kernel\Views;
use Drupal\comment\CommentManagerInterface;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\views\Views;
class CommentDepthTest extends CommentViewsKernelTestBase {
protected static $modules = [
'entity_test',
];
public static $testViews = [
'test_comment',
];
protected function setUp($import_test_views = TRUE) : void {
parent::setUp($import_test_views);
$this
->installEntitySchema('entity_test');
}
public function testCommentDepth() {
$this
->enableModules([
'field',
]);
$this
->installConfig([
'field',
]);
$field_storage_comment = FieldStorageConfig::create([
'field_name' => 'comment',
'type' => 'comment',
'entity_type' => 'entity_test',
]);
$field_storage_comment
->save();
$field_comment = FieldConfig::create([
'field_name' => 'comment',
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
'settings' => [
'default_mode' => CommentManagerInterface::COMMENT_MODE_THREADED,
],
]);
$field_comment
->save();
$host = EntityTest::create([
'name' => $this
->randomString(),
]);
$host
->save();
$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();
$comment1_depth = $view->style_plugin
->getField(0, 'thread');
$this
->assertEquals(0, (string) $comment1_depth, "The depth of the first comment is 0.");
$comment2_depth = $view->style_plugin
->getField(1, 'thread');
$this
->assertEquals(1, (string) $comment2_depth, "The depth of the second comment is 1.");
$comment3_depth = $view->style_plugin
->getField(2, 'thread');
$this
->assertEquals(2, (string) $comment3_depth, "The depth of the third comment is 2.");
}
}