function CommentFieldNameTest::testCommentFieldName

Same name in other branches
  1. 8.9.x core/modules/comment/tests/src/Kernel/Views/CommentFieldNameTest.php \Drupal\Tests\comment\Kernel\Views\CommentFieldNameTest::testCommentFieldName()
  2. 10 core/modules/comment/tests/src/Kernel/Views/CommentFieldNameTest.php \Drupal\Tests\comment\Kernel\Views\CommentFieldNameTest::testCommentFieldName()
  3. 11.x core/modules/comment/tests/src/Kernel/Views/CommentFieldNameTest.php \Drupal\Tests\comment\Kernel\Views\CommentFieldNameTest::testCommentFieldName()

Tests comment field name.

File

core/modules/comment/tests/src/Kernel/Views/CommentFieldNameTest.php, line 54

Class

CommentFieldNameTest
Tests the comment field name field.

Namespace

Drupal\Tests\comment\Kernel\Views

Code

public function testCommentFieldName() {
    $renderer = $this->container
        ->get('renderer');
    $this->installEntitySchema('user');
    $this->installEntitySchema('node');
    $this->installEntitySchema('comment');
    $this->installSchema('system', [
        'sequences',
    ]);
    $this->installSchema('comment', [
        'comment_entity_statistics',
    ]);
    $this->installConfig([
        'filter',
    ]);
    NodeType::create([
        'type' => 'page',
    ])->save();
    FieldStorageConfig::create([
        'type' => 'text_long',
        'entity_type' => 'comment',
        'field_name' => 'comment_body',
    ])->save();
    $this->addDefaultCommentField('node', 'page', 'comment');
    $this->addDefaultCommentField('node', 'page', 'comment_custom');
    ViewTestData::createTestViews(static::class, [
        'comment_test_views',
    ]);
    $node = $this->createNode();
    $comment = Comment::create([
        'entity_id' => $node->id(),
        'entity_type' => 'node',
        'field_name' => 'comment',
    ]);
    $comment->save();
    $comment2 = Comment::create([
        'entity_id' => $node->id(),
        'entity_type' => 'node',
        'field_name' => 'comment_custom',
    ]);
    $comment2->save();
    $view = Views::getView('test_comment_field_name');
    $view->preview();
    $expected_result = [
        [
            'cid' => $comment->id(),
            'field_name' => $comment->getFieldName(),
        ],
        [
            'cid' => $comment2->id(),
            'field_name' => $comment2->getFieldName(),
        ],
    ];
    $column_map = [
        'cid' => 'cid',
        'comment_field_data_field_name' => 'field_name',
    ];
    $this->assertIdenticalResultset($view, $expected_result, $column_map);
    // Test that data rendered correctly.
    $expected_output = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) {
        return $view->field['field_name']
            ->advancedRender($view->result[0]);
    });
    $this->assertEquals($expected_output, $comment->getFieldName());
    $expected_output = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) {
        return $view->field['field_name']
            ->advancedRender($view->result[1]);
    });
    $this->assertEquals($expected_output, $comment2->getFieldName());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.