View source
<?php
namespace Drupal\Tests\comment\Functional;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\comment\CommentInterface;
use Drupal\user\RoleInterface;
use Drupal\comment\Entity\Comment;
class CommentLinksTest extends CommentTestBase {
protected $comment;
protected $seen = [];
public static $modules = [
'views',
];
protected $defaultTheme = 'stark';
public function testCommentLinks() {
\Drupal::service('theme_installer')
->install([
'stark',
]);
$this
->config('system.theme')
->set('default', 'stark')
->save();
$roles = $this->webUser
->getRoles();
\Drupal::entityTypeManager()
->getStorage('user_role')
->load(reset($roles))
->delete();
$comment = Comment::create([
'cid' => NULL,
'entity_id' => $this->node
->id(),
'entity_type' => 'node',
'field_name' => 'comment',
'pid' => 0,
'uid' => 0,
'status' => CommentInterface::PUBLISHED,
'subject' => $this
->randomMachineName(),
'hostname' => '127.0.0.1',
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
'comment_body' => [
[
'value' => $this
->randomMachineName(),
],
],
]);
$comment
->save();
$this->comment = $comment;
$this
->setCommentSettings('form_location', CommentItemInterface::FORM_BELOW, 'Set comment form location');
$this
->setCommentAnonymous(TRUE);
$this->node->comment = CommentItemInterface::OPEN;
$this->node
->save();
$perms = [
'access comments' => 1,
'post comments' => 1,
'skip comment approval' => 1,
'edit own comments' => 1,
];
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, $perms);
$nid = $this->node
->id();
foreach ([
'node',
"node/{$nid}",
] as $path) {
$this
->drupalGet($path);
if ($path == 'node') {
$this
->assertSession()
->linkExists(t('1 comment'));
}
$this
->assertSession()
->linkExists('Add new comment');
}
$display_repository = $this->container
->get('entity_display.repository');
$display_repository
->getViewDisplay('comment', 'comment')
->setComponent('links', [
'weight' => -100,
])
->save();
$this
->drupalGet($this->node
->toUrl());
$element = $this
->cssSelect('article.js-comment > div');
$element = end($element);
$this
->assertIdentical($element
->getTagName(), 'div', 'Last element is comment body.');
$display_repository
->getViewDisplay('comment', 'comment')
->setComponent('links', [
'weight' => 100,
])
->save();
$this
->drupalGet($this->node
->toUrl());
$element = $this
->cssSelect('article.js-comment > div');
$element = end($element);
$this
->assertNotEmpty($element
->find('css', 'ul.links'), 'Last element is comment links.');
$display_repository
->getViewDisplay('node', $this->node
->bundle())
->removeComponent('links')
->save();
$this
->drupalGet($this->node
->toUrl());
$this
->assertSession()
->linkNotExists('1 comment');
$this
->assertSession()
->linkNotExists('Add new comment');
$this
->drupalGet('node/' . $this->node
->id());
$this
->assertText($comment
->getSubject());
$this
->assertSession()
->linkExists('Reply');
$display_repository
->getViewDisplay('comment', 'comment')
->removeComponent('links')
->save();
$this
->drupalGet('node/' . $this->node
->id());
$this
->assertText($comment
->getSubject());
$this
->assertSession()
->linkNotExists('Reply');
}
}