View source
<?php
namespace Drupal\Tests\comment\Functional;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Language\LanguageInterface;
use Drupal\comment\CommentInterface;
use Drupal\Core\Url;
use Drupal\comment\Entity\Comment;
class CommentNewIndicatorTest extends CommentTestBase {
public static $modules = [
'views',
];
protected $defaultTheme = 'stark';
protected function renderNewCommentsNodeLinks(array $node_ids) {
$client = $this
->getHttpClient();
$url = Url::fromRoute('comment.new_comments_node_links');
return $client
->request('POST', $this
->buildUrl($url), [
'cookies' => $this
->getSessionCookies(),
'http_errors' => FALSE,
'form_params' => [
'node_ids' => $node_ids,
'field_name' => 'comment',
],
]);
}
public function testCommentNewCommentsIndicator() {
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('node');
$this
->assertSession()
->linkNotExists(t('@count comments', [
'@count' => 0,
]));
$this
->assertSession()
->linkExists(t('Read more'));
$this
->assertCount(0, $this
->xpath('//*[@data-history-node-last-comment-timestamp]'), 'data-history-node-last-comment-timestamp attribute is not set.');
$comment = Comment::create([
'cid' => NULL,
'entity_id' => $this->node
->id(),
'entity_type' => 'node',
'field_name' => 'comment',
'pid' => 0,
'uid' => $this->loggedInUser
->id(),
'status' => CommentInterface::PUBLISHED,
'subject' => $this
->randomMachineName(),
'hostname' => '127.0.0.1',
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
'comment_body' => [
LanguageInterface::LANGCODE_NOT_SPECIFIED => [
$this
->randomMachineName(),
],
],
]);
$comment
->save();
$this
->drupalLogout();
$this
->drupalLogin($this->webUser);
$this
->drupalGet('node');
$this
->assertCount(1, $this
->xpath('//*[@data-history-node-last-comment-timestamp="' . $comment
->getChangedTime() . '"]'), 'data-history-node-last-comment-timestamp attribute is set to the correct value.');
$this
->assertCount(1, $this
->xpath('//*[@data-history-node-field-name="comment"]'), 'data-history-node-field-name attribute is set to the correct value.');
$settings = $this
->getDrupalSettings();
$this
->assertEqual($settings['history'], [
'lastReadTimestamps' => [
1 => 0,
],
]);
$this
->assertEqual($settings['comment'], [
'newCommentsLinks' => [
'node' => [
'comment' => [
1 => [
'new_comment_count' => 1,
'first_new_comment_link' => Url::fromRoute('entity.node.canonical', [
'node' => 1,
])
->setOptions([
'fragment' => 'new',
])
->toString(),
],
],
],
],
]);
$response = $this
->renderNewCommentsNodeLinks([
$this->node
->id(),
]);
$this
->assertSame(200, $response
->getStatusCode());
$json = Json::decode($response
->getBody());
$expected = [
$this->node
->id() => [
'new_comment_count' => 1,
'first_new_comment_link' => $this->node
->toUrl('canonical', [
'fragment' => 'new',
])
->toString(),
],
];
$this
->assertIdentical($expected, $json);
$response = $this
->renderNewCommentsNodeLinks([]);
$this
->assertSame(404, $response
->getStatusCode());
$this
->drupalLogout();
$response = $this
->renderNewCommentsNodeLinks([
$this->node
->id(),
]);
$this
->assertSame(403, $response
->getStatusCode());
$response = $this
->renderNewCommentsNodeLinks([]);
$this
->assertSame(403, $response
->getStatusCode());
}
}