CommentFieldNameTest.php in Zircon Profile 8.0
File
core/modules/comment/src/Tests/Views/CommentFieldNameTest.php
View source
<?php
namespace Drupal\comment\Tests\Views;
use Drupal\comment\Entity\Comment;
use Drupal\Core\Render\RenderContext;
use Drupal\Core\Session\AnonymousUserSession;
use Drupal\user\RoleInterface;
use Drupal\views\Views;
class CommentFieldNameTest extends CommentTestBase {
public static $testViews = [
'test_comment_field_name',
];
protected $customComment;
protected $fieldName = 'comment_custom';
protected function setUp() {
parent::setUp();
$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();
}
public function testCommentFieldName() {
$renderer = \Drupal::service('renderer');
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);
$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);
}
}