View source
<?php
namespace Drupal\Tests\comment\Unit {
use Drupal\comment\CommentLinkBuilder;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\Core\Url;
use Drupal\node\NodeInterface;
use Drupal\simpletest\TestBase;
use Drupal\Tests\UnitTestCase;
class CommentLinkBuilderTest extends UnitTestCase {
protected $commentManager;
protected $stringTranslation;
protected $entityManager;
protected $moduleHandler;
protected $currentUser;
protected $timestamp;
protected $commentLinkBuilder;
protected function setUp() {
$this->commentManager = $this
->getMock('\\Drupal\\comment\\CommentManagerInterface');
$this->stringTranslation = $this
->getStringTranslationStub();
$this->entityManager = $this
->getMock('\\Drupal\\Core\\Entity\\EntityManagerInterface');
$this->moduleHandler = $this
->getMock('\\Drupal\\Core\\Extension\\ModuleHandlerInterface');
$this->currentUser = $this
->getMock('\\Drupal\\Core\\Session\\AccountProxyInterface');
$this->commentLinkBuilder = new CommentLinkBuilder($this->currentUser, $this->commentManager, $this->moduleHandler, $this->stringTranslation, $this->entityManager);
$this->commentManager
->expects($this
->any())
->method('getFields')
->with('node')
->willReturn(array(
'comment' => array(),
));
$this->commentManager
->expects($this
->any())
->method('forbiddenMessage')
->willReturn("Can't let you do that Dave.");
$this->stringTranslation
->expects($this
->any())
->method('formatPlural')
->willReturnArgument(1);
}
public function testCommentLinkBuilder(NodeInterface $node, $context, $has_access_comments, $history_exists, $has_post_comments, $is_anonymous, $expected) {
$this->moduleHandler
->expects($this
->any())
->method('moduleExists')
->with('history')
->willReturn($history_exists);
$this->currentUser
->expects($this
->any())
->method('hasPermission')
->willReturnMap(array(
array(
'access comments',
$has_access_comments,
),
array(
'post comments',
$has_post_comments,
),
));
$this->currentUser
->expects($this
->any())
->method('isAuthenticated')
->willReturn(!$is_anonymous);
$this->currentUser
->expects($this
->any())
->method('isAnonymous')
->willReturn($is_anonymous);
$links = $this->commentLinkBuilder
->buildCommentedEntityLinks($node, $context);
if (!empty($expected)) {
if (!empty($links)) {
foreach ($expected as $link => $detail) {
if (is_array($detail)) {
foreach ($detail as $key => $value) {
$this
->assertEquals($value, $links['comment__comment']['#links'][$link][$key]);
}
}
else {
$this
->assertEquals($detail, $links['comment__comment']['#links'][$link]['title']);
}
}
}
else {
$this
->fail('Expected links but found none.');
}
}
else {
$this
->assertSame($links, $expected);
}
}
public function getLinkCombinations() {
$cases = array();
$cases[] = array(
$this
->getMockNode(FALSE, CommentItemInterface::OPEN, CommentItemInterface::FORM_BELOW, 1),
array(
'view_mode' => 'teaser',
),
TRUE,
TRUE,
TRUE,
TRUE,
array(),
);
foreach (array(
'search_result',
'search_index',
'print',
) as $view_mode) {
$cases[] = array(
$this
->getMockNode(TRUE, CommentItemInterface::OPEN, CommentItemInterface::FORM_BELOW, 1),
array(
'view_mode' => $view_mode,
),
TRUE,
TRUE,
TRUE,
TRUE,
array(),
);
}
$combinations = array(
'is_anonymous' => array(
FALSE,
TRUE,
),
'comment_count' => array(
0,
1,
),
'has_access_comments' => array(
0,
1,
),
'history_exists' => array(
FALSE,
TRUE,
),
'has_post_comments' => array(
0,
1,
),
'form_location' => array(
CommentItemInterface::FORM_BELOW,
CommentItemInterface::FORM_SEPARATE_PAGE,
),
'comments' => array(
CommentItemInterface::OPEN,
CommentItemInterface::CLOSED,
CommentItemInterface::HIDDEN,
),
'view_mode' => array(
'teaser',
'rss',
'full',
),
);
$permutations = TestBase::generatePermutations($combinations);
foreach ($permutations as $combination) {
$case = array(
$this
->getMockNode(TRUE, $combination['comments'], $combination['form_location'], $combination['comment_count']),
array(
'view_mode' => $combination['view_mode'],
),
$combination['has_access_comments'],
$combination['history_exists'],
$combination['has_post_comments'],
$combination['is_anonymous'],
);
$expected = array();
if ($combination['comments'] && $combination['view_mode'] == 'teaser' && $combination['comment_count'] && $combination['has_access_comments']) {
$expected['comment-comments'] = '1 comment';
if ($combination['history_exists']) {
$expected['comment-new-comments'] = '';
}
}
if ($combination['view_mode'] != 'rss') {
if ($combination['comments'] == CommentItemInterface::OPEN) {
if ($combination['has_post_comments']) {
if ($combination['view_mode'] == 'teaser' || $combination['has_access_comments'] && $combination['comment_count'] || $combination['form_location'] == CommentItemInterface::FORM_SEPARATE_PAGE) {
$expected['comment-add'] = array(
'title' => 'Add new comment',
);
if ($combination['form_location'] == CommentItemInterface::FORM_BELOW) {
$expected['comment-add']['url'] = Url::fromRoute('node.view');
}
else {
$expected['comment-add']['url'] = Url::fromRoute('comment.reply', [
'entity_type' => 'node',
'entity' => 1,
'field_name' => 'comment',
]);
}
}
}
elseif ($combination['is_anonymous']) {
$expected['comment-forbidden'] = "Can't let you do that Dave.";
}
}
}
$case[] = $expected;
$cases[] = $case;
}
return $cases;
}
protected function getMockNode($has_field, $comment_status, $form_location, $comment_count) {
$node = $this
->getMock('\\Drupal\\node\\NodeInterface');
$node
->expects($this
->once())
->method('hasField')
->willReturn($has_field);
if (empty($this->timestamp)) {
$this->timestamp = time();
}
$field_item = (object) array(
'status' => $comment_status,
'comment_count' => $comment_count,
'last_comment_timestamp' => $this->timestamp,
);
$node
->expects($this
->any())
->method('get')
->with('comment')
->willReturn($field_item);
$field_definition = $this
->getMock('\\Drupal\\Core\\Field\\FieldDefinitionInterface');
$field_definition
->expects($this
->any())
->method('getSetting')
->with('form_location')
->willReturn($form_location);
$node
->expects($this
->any())
->method('getFieldDefinition')
->with('comment')
->willReturn($field_definition);
$node
->expects($this
->any())
->method('language')
->willReturn('und');
$node
->expects($this
->any())
->method('getEntityTypeId')
->willReturn('node');
$node
->expects($this
->any())
->method('id')
->willReturn(1);
$url = Url::fromRoute('node.view');
$node
->expects($this
->any())
->method('urlInfo')
->willReturn($url);
$node
->expects($this
->any())
->method('url')
->willReturn(array(
'route_name' => 'node.view',
));
return $node;
}
}
}
namespace {
if (!function_exists('history_read')) {
function history_read() {
return 0;
}
}
}