Commit 3a1d2047 authored by alexpott's avatar alexpott

Issue #3049604 by claudiu.cristea, Manuel Garcia: Convert CommentFieldNameTest into a Kernel test

parent b092eaa7
<?php
namespace Drupal\Tests\comment\Functional\Views;
use Drupal\comment\Entity\Comment;
use Drupal\Core\Render\RenderContext;
use Drupal\Core\Session\AnonymousUserSession;
use Drupal\user\RoleInterface;
use Drupal\views\Views;
/**
* Tests the comment field name field.
*
* @group comment
*/
class CommentFieldNameTest extends CommentTestBase {
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = ['test_comment_field_name'];
/**
* The second comment entity used by this test.
*
* @var \Drupal\comment\CommentInterface
*/
protected $customComment;
/**
* The comment field name used by this test.
*
* @var string
*/
protected $fieldName = 'comment_custom';
/**
* {@inheritdoc}
*/
protected function setUp($import_test_views = TRUE) {
parent::setUp($import_test_views);
$this->addDefaultCommentField('node', 'page', $this->fieldName);
$this->customComment = Comment::create([
'entity_id' => $this->nodeUserCommented->id(),
'entity_type' => 'node',
'field_name' => $this->fieldName,
]);
$this->customComment->save();
}
/**
* Test comment field name.
*/
public function testCommentFieldName() {
/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = \Drupal::service('renderer');
// Grant permission to properly check view access on render.
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['access comments']);
$this->container->get('account_switcher')->switchTo(new AnonymousUserSession());
$view = Views::getView('test_comment_field_name');
$this->executeView($view);
$expected_result = [
[
'cid' => $this->comment->id(),
'field_name' => $this->comment->getFieldName(),
],
[
'cid' => $this->customComment->id(),
'field_name' => $this->customComment->getFieldName(),
],
];
$column_map = [
'cid' => 'cid',
'comment_field_data_field_name' => 'field_name',
];
$this->assertIdenticalResultset($view, $expected_result, $column_map);
// Test that data rendered.
$output = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) {
return $view->field['field_name']->advancedRender($view->result[0]);
});
$this->assertEqual($this->comment->getFieldName(), $output);
$output = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) {
return $view->field['field_name']->advancedRender($view->result[1]);
});
$this->assertEqual($this->customComment->getFieldName(), $output);
}
}
<?php
namespace Drupal\Tests\comment\Kernel\Views;
use Drupal\comment\Entity\Comment;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\Core\Render\RenderContext;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\views\Tests\ViewResultAssertionTrait;
use Drupal\views\Tests\ViewTestData;
use Drupal\views\Views;
/**
* Tests the comment field name field.
*
* @group comment
*/
class CommentFieldNameTest extends KernelTestBase {
use CommentTestTrait;
use NodeCreationTrait;
use UserCreationTrait;
use ViewResultAssertionTrait;
/**
* {@inheritdoc}
*/
protected static $modules = [
'comment',
'comment_test_views',
'field',
'filter',
'node',
'system',
'text',
'user',
'views',
];
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = ['test_comment_field_name'];
/**
* Test comment field name.
*/
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());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment