function CommentDepthTest::testCommentDepth

Same name in other branches
  1. 9 core/modules/comment/tests/src/Kernel/Views/CommentDepthTest.php \Drupal\Tests\comment\Kernel\Views\CommentDepthTest::testCommentDepth()
  2. 10 core/modules/comment/tests/src/Kernel/Views/CommentDepthTest.php \Drupal\Tests\comment\Kernel\Views\CommentDepthTest::testCommentDepth()

Test the comment depth.

File

core/modules/comment/tests/src/Kernel/Views/CommentDepthTest.php, line 44

Class

CommentDepthTest
Tests the depth of the comment field handler.

Namespace

Drupal\Tests\comment\Kernel\Views

Code

public function testCommentDepth() : void {
    $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.