Update to drupal 8.0.0-rc1. For more information, see https://www.drupal.org/node/2582663

This commit is contained in:
Greg Anderson 2015-10-08 11:40:12 -07:00
parent eb34d130a8
commit f32e58e4b1
8476 changed files with 211648 additions and 170042 deletions

View file

@ -15,6 +15,13 @@ use Drupal\user\RoleInterface;
* @group comment
*/
class CommentAdminTest extends CommentTestBase {
protected function setUp() {
parent::setUp();
$this->drupalPlaceBlock('page_title_block');
}
/**
* Test comment approval functionality through admin/content/comment.
*/

View file

@ -40,6 +40,32 @@ class CommentAnonymousTest extends CommentTestBase {
$this->setCommentAnonymous(COMMENT_ANONYMOUS_MAYNOT_CONTACT);
$this->drupalLogout();
// Preview comments (with `skip comment approval` permission).
$edit = [];
$title = 'comment title with skip comment approval';
$body = 'comment body with skip comment approval';
$edit['subject[0][value]'] = $title;
$edit['comment_body[0][value]'] = $body;
$this->drupalPostForm($this->node->urlInfo(), $edit, t('Preview'));
// Cannot use assertRaw here since both title and body are in the form.
$preview = (string) $this->cssSelect('.preview')[0]->asXML();
$this->assertTrue(strpos($preview, $title) !== FALSE, 'Anonymous user can preview comment title.');
$this->assertTrue(strpos($preview, $body) !== FALSE, 'Anonymous user can preview comment body.');
// Preview comments (without `skip comment approval` permission).
user_role_revoke_permissions(RoleInterface::ANONYMOUS_ID, ['skip comment approval']);
$edit = [];
$title = 'comment title without skip comment approval';
$body = 'comment body without skip comment approval';
$edit['subject[0][value]'] = $title;
$edit['comment_body[0][value]'] = $body;
$this->drupalPostForm($this->node->urlInfo(), $edit, t('Preview'));
// Cannot use assertRaw here since both title and body are in the form.
$preview = (string) $this->cssSelect('.preview')[0]->asXML();
$this->assertTrue(strpos($preview, $title) !== FALSE, 'Anonymous user can preview comment title.');
$this->assertTrue(strpos($preview, $body) !== FALSE, 'Anonymous user can preview comment body.');
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['skip comment approval']);
// Post anonymous comment without contact info.
$anonymous_comment1 = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName());
$this->assertTrue($this->commentExists($anonymous_comment1), 'Anonymous comment without contact info found.');
@ -101,6 +127,7 @@ class CommentAnonymousTest extends CommentTestBase {
$this->drupalLogin($this->adminUser);
$this->drupalGet('comment/' . $anonymous_comment3->id() . '/edit');
$this->assertRaw($author_name, "The anonymous user's name is correct when editing the comment.");
$this->assertFieldByName('uid', '', 'The author field is empty (i.e. anonymous) when editing the comment.');
$this->assertRaw($author_mail, "The anonymous user's email address is correct when editing the comment.");
// Unpublish comment.
@ -167,4 +194,5 @@ class CommentAnonymousTest extends CommentTestBase {
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $anonymous_comment2->id());
$this->assertResponse(403);
}
}

View file

@ -11,6 +11,9 @@ use Drupal\Core\Cache\Cache;
use Drupal\Core\Session\UserSession;
use Drupal\comment\CommentInterface;
use Drupal\system\Tests\Entity\EntityUnitTestBase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
/**
* Tests the bubbling up of comment cache tags when using the Comment list
@ -35,6 +38,16 @@ class CommentDefaultFormatterCacheTagsTest extends EntityUnitTestBase {
protected function setUp() {
parent::setUp();
$session = new Session();
$request = Request::create('/');
$request->setSession($session);
/** @var RequestStack $stack */
$stack = $this->container->get('request_stack');
$stack->pop();
$stack->push($request);
// Set the current user to one that can access comments. Specifically, this
// user does not have access to the 'administer comments' permission, to
// ensure only published comments are visible to the end user.
@ -127,6 +140,24 @@ class CommentDefaultFormatterCacheTagsTest extends EntityUnitTestBase {
];
sort($expected_cache_tags);
$this->assertEqual($build['#cache']['tags'], $expected_cache_tags);
// Build a render array with the entity in a sub-element so that lazy
// builder elements bubble up outside of the entity and we can check that
// it got the correct cache max age.
$build = ['#type' => 'container'];
$build['entity'] = \Drupal::entityManager()
->getViewBuilder('entity_test')
->view($commented_entity);
$renderer->renderRoot($build);
// The entity itself was cached but the top-level element is max-age 0 due
// to the bubbled up max age due to the lazy-built comment form.
$this->assertIdentical(Cache::PERMANENT, $build['entity']['#cache']['max-age']);
$this->assertIdentical(0, $build['#cache']['max-age'], 'Top level render array has max-age 0');
// The children (fields) of the entity render array are only built in case
// of a cache miss.
$this->assertFalse(isset($build['entity']['comment']), 'Cache hit');
}
}

View file

@ -213,17 +213,16 @@ class CommentFieldAccessTest extends EntityUnitTestBase {
foreach ($permutations as $set) {
$may_view = $set['comment']->{$field}->access('view', $set['user']);
$may_update = $set['comment']->{$field}->access('edit', $set['user']);
$this->assertEqual($may_view, $set['user']->hasPermission('administer comments') || ($set['comment']->isPublished() && $set['user']->hasPermission('access comments')), SafeMarkup::format('User @user !state view field !field on comment @comment', [
$this->assertTrue($may_view, SafeMarkup::format('User @user can view field @field on comment @comment', [
'@user' => $set['user']->getUsername(),
'!state' => $may_update ? 'can' : 'cannot',
'@comment' => $set['comment']->getSubject(),
'!field' => $field,
'@field' => $field,
]));
$this->assertEqual($may_update, $set['user']->hasPermission('administer comments'), SafeMarkup::format('User @user !state update field !field on comment @comment', [
$this->assertEqual($may_update, $set['user']->hasPermission('administer comments'), SafeMarkup::format('User @user @state update field @field on comment @comment', [
'@user' => $set['user']->getUsername(),
'!state' => $may_update ? 'can' : 'cannot',
'@state' => $may_update ? 'can' : 'cannot',
'@comment' => $set['comment']->getSubject(),
'!field' => $field,
'@field' => $field,
]));
}
}
@ -231,9 +230,9 @@ class CommentFieldAccessTest extends EntityUnitTestBase {
// Check access to normal field.
foreach ($permutations as $set) {
$may_update = $set['comment']->access('update', $set['user']) && $set['comment']->subject->access('edit', $set['user']);
$this->assertEqual($may_update, $set['user']->hasPermission('administer comments') || ($set['user']->hasPermission('edit own comments') && $set['user']->id() == $set['comment']->getOwnerId()), SafeMarkup::format('User @user !state update field subject on comment @comment', [
$this->assertEqual($may_update, $set['user']->hasPermission('administer comments') || ($set['user']->hasPermission('edit own comments') && $set['user']->id() == $set['comment']->getOwnerId()), SafeMarkup::format('User @user @state update field subject on comment @comment', [
'@user' => $set['user']->getUsername(),
'!state' => $may_update ? 'can' : 'cannot',
'@state' => $may_update ? 'can' : 'cannot',
'@comment' => $set['comment']->getSubject(),
]));
}
@ -244,18 +243,26 @@ class CommentFieldAccessTest extends EntityUnitTestBase {
foreach ($permutations as $set) {
$may_view = $set['comment']->{$field}->access('view', $set['user']);
$may_update = $set['comment']->{$field}->access('edit', $set['user']);
$this->assertEqual($may_view, $field != 'hostname' && ($set['user']->hasPermission('administer comments') ||
($set['comment']->isPublished() && $set['user']->hasPermission('access comments'))), SafeMarkup::format('User @user !state view field !field on comment @comment', [
// Nobody has access to to view the hostname field.
if ($field === 'hostname') {
$view_access = FALSE;
$state = 'cannot';
}
else {
$view_access = TRUE;
$state = 'can';
}
$this->assertEqual($may_view, $view_access, SafeMarkup::format('User @user @state view field @field on comment @comment', [
'@user' => $set['user']->getUsername(),
'!state' => $may_view ? 'can' : 'cannot',
'@comment' => $set['comment']->getSubject(),
'!field' => $field,
'@field' => $field,
'@state' => $state,
]));
$this->assertFalse($may_update, SafeMarkup::format('User @user !state update field !field on comment @comment', [
$this->assertFalse($may_update, SafeMarkup::format('User @user @state update field @field on comment @comment', [
'@user' => $set['user']->getUsername(),
'!state' => $may_update ? 'can' : 'cannot',
'@state' => $may_update ? 'can' : 'cannot',
'@comment' => $set['comment']->getSubject(),
'!field' => $field,
'@field' => $field,
]));
}
}
@ -266,18 +273,16 @@ class CommentFieldAccessTest extends EntityUnitTestBase {
foreach ($permutations as $set) {
$may_view = $set['comment']->{$field}->access('view', $set['user']);
$may_update = $set['comment']->{$field}->access('edit', $set['user']);
$this->assertEqual($may_view, $field != 'hostname' && ($set['user']->hasPermission('administer comments') ||
($set['comment']->isPublished() && $set['user']->hasPermission('access comments'))), SafeMarkup::format('User @user !state view field !field on comment @comment', [
$this->assertEqual($may_view, TRUE, SafeMarkup::format('User @user can view field @field on comment @comment', [
'@user' => $set['user']->getUsername(),
'!state' => $may_view ? 'can' : 'cannot',
'@comment' => $set['comment']->getSubject(),
'!field' => $field,
'@field' => $field,
]));
$this->assertEqual($may_update, $set['user']->hasPermission('post comments') && $set['comment']->isNew(), SafeMarkup::format('User @user !state update field !field on comment @comment', [
$this->assertEqual($may_update, $set['user']->hasPermission('post comments') && $set['comment']->isNew(), SafeMarkup::format('User @user @state update field @field on comment @comment', [
'@user' => $set['user']->getUsername(),
'!state' => $may_update ? 'can' : 'cannot',
'@state' => $may_update ? 'can' : 'cannot',
'@comment' => $set['comment']->getSubject(),
'!field' => $field,
'@field' => $field,
]));
}
}
@ -295,11 +300,11 @@ class CommentFieldAccessTest extends EntityUnitTestBase {
$set['comment']->isNew() &&
$set['user']->hasPermission('post comments') &&
$set['comment']->getFieldName() == 'comment_other'
), SafeMarkup::format('User @user !state update field !field on comment @comment', [
), SafeMarkup::format('User @user @state update field @field on comment @comment', [
'@user' => $set['user']->getUsername(),
'!state' => $may_update ? 'can' : 'cannot',
'@state' => $may_update ? 'can' : 'cannot',
'@comment' => $set['comment']->getSubject(),
'!field' => $field,
'@field' => $field,
]));
}
}

View file

@ -60,8 +60,8 @@ class CommentFieldsTest extends CommentTestBase {
// Test adding a field that defaults to CommentItemInterface::CLOSED.
$this->addDefaultCommentField('node', 'test_node_type', 'who_likes_ponies', CommentItemInterface::CLOSED, 'who_likes_ponies');
$field = FieldConfig::load('node.test_node_type.who_likes_ponies');
$this->assertEqual($field->default_value[0]['status'], CommentItemInterface::CLOSED);
$field = FieldConfig::load('node.test_node_type.who_likes_ponies');;
$this->assertEqual($field->getDefaultValueLiteral()[0]['status'], CommentItemInterface::CLOSED);
}
/**

View file

@ -26,6 +26,12 @@ class CommentInterfaceTest extends CommentTestBase {
public function setUp() {
parent::setUp();
$this->drupalLogin($this->adminUser);
// Make sure that comment field title is not displayed when there's no
// comments posted.
$this->drupalGet($this->node->urlInfo());
$this->assertNoPattern('@<h2[^>]*>Comments</h2>@', 'Comments title is not displayed.');
// Set comments to have subject and preview disabled.
$this->setCommentPreview(DRUPAL_DISABLED);
$this->setCommentForm(TRUE);
$this->setCommentSubject(FALSE);
@ -44,6 +50,10 @@ class CommentInterfaceTest extends CommentTestBase {
$comment = $this->postComment($this->node, $comment_text);
$this->assertTrue($this->commentExists($comment), 'Comment found.');
// Test the comment field title is displayed when there's comments.
$this->drupalGet($this->node->urlInfo());
$this->assertPattern('@<h2[^>]*>Comments</h2>@', 'Comments title is displayed.');
// Set comments to have subject and preview to required.
$this->drupalLogout();
$this->drupalLogin($this->adminUser);
@ -83,7 +93,7 @@ class CommentInterfaceTest extends CommentTestBase {
)));
// Test changing the comment author to "Anonymous".
$comment = $this->postComment(NULL, $comment->comment_body->value, $comment->getSubject(), array('name' => ''));
$comment = $this->postComment(NULL, $comment->comment_body->value, $comment->getSubject(), array('uid' => ''));
$this->assertTrue($comment->getAuthorName() == t('Anonymous') && $comment->getOwnerId() == 0, 'Comment author successfully changed to anonymous.');
// Test changing the comment author to an unverified user.
@ -95,7 +105,7 @@ class CommentInterfaceTest extends CommentTestBase {
// Test changing the comment author to a verified user.
$this->drupalGet('comment/' . $comment->id() . '/edit');
$comment = $this->postComment(NULL, $comment->comment_body->value, $comment->getSubject(), array('name' => $this->webUser->getUsername()));
$comment = $this->postComment(NULL, $comment->comment_body->value, $comment->getSubject(), array('uid' => $this->webUser->getUsername() . ' (' . $this->webUser->id() . ')'));
$this->assertTrue($comment->getAuthorName() == $this->webUser->getUsername() && $comment->getOwnerId() == $this->webUser->id(), 'Comment author successfully changed to a registered user.');
$this->drupalLogout();

View file

@ -60,6 +60,9 @@ class CommentItemTest extends FieldUnitTestBase {
CommentItemInterface::CLOSED,
CommentItemInterface::OPEN,
]), 'Comment status value in defined range');
$mainProperty = $entity->comment[0]->mainPropertyName();
$this->assertEqual('status', $mainProperty);
}
}

View file

@ -50,6 +50,7 @@ class CommentNonNodeTest extends WebTestBase {
protected function setUp() {
parent::setUp();
$this->drupalPlaceBlock('system_breadcrumb_block');
$this->drupalPlaceBlock('page_title_block');
// Create a bundle for entity_test.
entity_test_create_bundle('entity_test', 'Entity Test', 'entity_test');

View file

@ -8,6 +8,7 @@
namespace Drupal\comment\Tests;
use Drupal\comment\CommentManagerInterface;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\comment\Entity\Comment;
@ -39,17 +40,31 @@ class CommentPreviewTest extends CommentTestBase {
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
$this->drupalLogout();
// Login as web user and add a user picture.
// Login as web user.
$this->drupalLogin($this->webUser);
$image = current($this->drupalGetTestFiles('image'));
$edit['files[user_picture_0]'] = drupal_realpath($image->uri);
$this->drupalPostForm('user/' . $this->webUser->id() . '/edit', $edit, t('Save'));
// As the web user, fill in the comment form and preview the comment.
// Test escaping of the username on the preview form.
\Drupal::service('module_installer')->install(['user_hooks_test']);
\Drupal::state()->set('user_hooks_test_user_format_name_alter', TRUE);
$edit = array();
$edit['subject[0][value]'] = $this->randomMachineName(8);
$edit['comment_body[0][value]'] = $this->randomMachineName(16);
$this->drupalPostForm('node/' . $this->node->id(), $edit, t('Preview'));
$this->assertEscaped('<em>' . $this->webUser->id() . '</em>');
\Drupal::state()->set('user_hooks_test_user_format_name_alter_safe', TRUE);
$this->drupalPostForm('node/' . $this->node->id(), $edit, t('Preview'));
$this->assertTrue(SafeMarkup::isSafe($this->webUser->getDisplayName()), 'Username is marked safe');
$this->assertNoEscaped('<em>' . $this->webUser->id() . '</em>');
$this->assertRaw('<em>' . $this->webUser->id() . '</em>');
// Add a user picture.
$image = current($this->drupalGetTestFiles('image'));
$user_edit['files[user_picture_0]'] = drupal_realpath($image->uri);
$this->drupalPostForm('user/' . $this->webUser->id() . '/edit', $user_edit, t('Save'));
// As the web user, fill in the comment form and preview the comment.
$this->drupalPostForm('node/' . $this->node->id(), $edit, t('Preview'));
// Check that the preview is displaying the title and body.
$this->assertTitle(t('Preview comment | Drupal'), 'Page title is "Preview comment".');
@ -125,7 +140,7 @@ class CommentPreviewTest extends CommentTestBase {
$date = new DrupalDateTime('2008-03-02 17:23');
$edit['subject[0][value]'] = $this->randomMachineName(8);
$edit['comment_body[0][value]'] = $this->randomMachineName(16);
$edit['name'] = $web_user->getUsername();
$edit['uid'] = $web_user->getUsername() . ' (' . $web_user->id() . ')';
$edit['date[date]'] = $date->format('Y-m-d');
$edit['date[time]'] = $date->format('H:i:s');
$raw_date = $date->getTimestamp();
@ -139,13 +154,13 @@ class CommentPreviewTest extends CommentTestBase {
$this->assertTitle(t('Preview comment | Drupal'), 'Page title is "Preview comment".');
$this->assertText($edit['subject[0][value]'], 'Subject displayed.');
$this->assertText($edit['comment_body[0][value]'], 'Comment displayed.');
$this->assertText($edit['name'], 'Author displayed.');
$this->assertText($web_user->getUsername(), 'Author displayed.');
$this->assertText($expected_text_date, 'Date displayed.');
// Check that the subject, comment, author and date fields are displayed with the correct values.
$this->assertFieldByName('subject[0][value]', $edit['subject[0][value]'], 'Subject field displayed.');
$this->assertFieldByName('comment_body[0][value]', $edit['comment_body[0][value]'], 'Comment field displayed.');
$this->assertFieldByName('name', $edit['name'], 'Author field displayed.');
$this->assertFieldByName('uid', $edit['uid'], 'Author field displayed.');
$this->assertFieldByName('date[date]', $edit['date[date]'], 'Date field displayed.');
$this->assertFieldByName('date[time]', $edit['date[time]'], 'Time field displayed.');
@ -157,7 +172,7 @@ class CommentPreviewTest extends CommentTestBase {
$this->drupalGet('comment/' . $comment->id() . '/edit');
$this->assertFieldByName('subject[0][value]', $edit['subject[0][value]'], 'Subject field displayed.');
$this->assertFieldByName('comment_body[0][value]', $edit['comment_body[0][value]'], 'Comment field displayed.');
$this->assertFieldByName('name', $edit['name'], 'Author field displayed.');
$this->assertFieldByName('uid', $edit['uid'], 'Author field displayed.');
$this->assertFieldByName('date[date]', $expected_form_date, 'Date field displayed.');
$this->assertFieldByName('date[time]', $expected_form_time, 'Time field displayed.');
@ -165,7 +180,7 @@ class CommentPreviewTest extends CommentTestBase {
$displayed = array();
$displayed['subject[0][value]'] = (string) current($this->xpath("//input[@id='edit-subject-0-value']/@value"));
$displayed['comment_body[0][value]'] = (string) current($this->xpath("//textarea[@id='edit-comment-body-0-value']"));
$displayed['name'] = (string) current($this->xpath("//input[@id='edit-name']/@value"));
$displayed['uid'] = (string) current($this->xpath("//input[@id='edit-uid']/@value"));
$displayed['date[date]'] = (string) current($this->xpath("//input[@id='edit-date-date']/@value"));
$displayed['date[time]'] = (string) current($this->xpath("//input[@id='edit-date-time']/@value"));
$this->drupalPostForm('comment/' . $comment->id() . '/edit', $displayed, t('Save'));
@ -173,10 +188,11 @@ class CommentPreviewTest extends CommentTestBase {
// Check that the saved comment is still correct.
$comment_storage = \Drupal::entityManager()->getStorage('comment');
$comment_storage->resetCache(array($comment->id()));
/** @var \Drupal\comment\CommentInterface $comment_loaded */
$comment_loaded = Comment::load($comment->id());
$this->assertEqual($comment_loaded->getSubject(), $edit['subject[0][value]'], 'Subject loaded.');
$this->assertEqual($comment_loaded->comment_body->value, $edit['comment_body[0][value]'], 'Comment body loaded.');
$this->assertEqual($comment_loaded->getAuthorName(), $edit['name'], 'Name loaded.');
$this->assertEqual($comment_loaded->getOwner()->id(), $web_user->id(), 'Name loaded.');
$this->assertEqual($comment_loaded->getCreatedTime(), $raw_date, 'Date loaded.');
$this->drupalLogout();
@ -185,6 +201,8 @@ class CommentPreviewTest extends CommentTestBase {
$user_edit = array();
$expected_created_time = $comment_loaded->getCreatedTime();
$this->drupalLogin($web_user);
// Web user cannot change the comment author.
unset($edit['uid']);
$this->drupalPostForm('comment/' . $comment->id() . '/edit', $user_edit, t('Save'));
$comment_storage->resetCache(array($comment->id()));
$comment_loaded = Comment::load($comment->id());

View file

@ -70,6 +70,9 @@ abstract class CommentTestBase extends WebTestBase {
'skip comment approval',
'post comments',
'access comments',
// Usernames aren't shown in comment edit form autocomplete unless this
// permission is granted.
'access user profiles',
'access content',
));
$this->webUser = $this->drupalCreateUser(array(
@ -188,14 +191,22 @@ abstract class CommentTestBase extends WebTestBase {
*/
function commentExists(CommentInterface $comment = NULL, $reply = FALSE) {
if ($comment) {
$regex = '!' . ($reply ? '<div class="indented">(.*?)' : '');
$regex .= '<a id="comment-' . $comment->id() . '"(.*?)';
$regex .= $comment->getSubject() . '(.*?)';
$regex .= $comment->comment_body->value . '(.*?)';
$regex .= ($reply ? '</article>\s</div>(.*?)' : '');
$regex .= '!s';
$comment_element = $this->cssSelect('.comment-wrapper ' . ($reply ? '.indented ' : '') . '#comment-' . $comment->id() . ' ~ article');
if (empty($comment_element)) {
return FALSE;
}
return (boolean) preg_match($regex, $this->getRawContent());
$comment_title = $comment_element[0]->xpath('div/h3/a');
if (empty($comment_title) || ((string)$comment_title[0]) !== $comment->getSubject()) {
return FALSE;
}
$comment_body = $comment_element[0]->xpath('div/div/p');
if (empty($comment_body) || ((string)$comment_body[0]) !== $comment->comment_body->value) {
return FALSE;
}
return TRUE;
}
else {
return FALSE;

View file

@ -7,6 +7,7 @@
namespace Drupal\comment\Tests;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Component\Utility\Xss;
@ -32,13 +33,16 @@ class CommentTokenReplaceTest extends CommentTestBase {
'language' => $language_interface,
);
// Change the title of the admin user.
$this->adminUser->name->value = 'This is a title with some special & > " stuff.';
$this->adminUser->save();
$this->drupalLogin($this->adminUser);
// Set comment variables.
$this->setCommentSubject(TRUE);
// Create a node and a comment.
$node = $this->drupalCreateNode(array('type' => 'article'));
$node = $this->drupalCreateNode(['type' => 'article', 'title' => '<script>alert("123")</script>']);
$parent_comment = $this->postComment($node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
// Post a reply to the comment.
@ -50,29 +54,29 @@ class CommentTokenReplaceTest extends CommentTestBase {
// Add HTML to ensure that sanitation of some fields tested directly.
$comment->setSubject('<blink>Blinking Comment</blink>');
// Generate and test sanitized tokens.
// Generate and test tokens.
$tests = array();
$tests['[comment:cid]'] = $comment->id();
$tests['[comment:hostname]'] = Html::escape($comment->getHostname());
$tests['[comment:author]'] = Xss::filter($comment->getAuthorName());
$tests['[comment:mail]'] = Html::escape($this->adminUser->getEmail());
$tests['[comment:hostname]'] = $comment->getHostname();
$tests['[comment:author]'] = Html::escape($comment->getAuthorName());
$tests['[comment:mail]'] = $this->adminUser->getEmail();
$tests['[comment:homepage]'] = UrlHelper::filterBadProtocol($comment->getHomepage());
$tests['[comment:title]'] = Xss::filter($comment->getSubject());
$tests['[comment:title]'] = Html::escape($comment->getSubject());
$tests['[comment:body]'] = $comment->comment_body->processed;
$tests['[comment:langcode]'] = Html::escape($comment->language()->getId());
$tests['[comment:langcode]'] = $comment->language()->getId();
$tests['[comment:url]'] = $comment->url('canonical', $url_options + array('fragment' => 'comment-' . $comment->id()));
$tests['[comment:edit-url]'] = $comment->url('edit-form', $url_options);
$tests['[comment:created]'] = \Drupal::service('date.formatter')->format($comment->getCreatedTime(), 'medium', array('langcode' => $language_interface->getId()));
$tests['[comment:created:since]'] = \Drupal::service('date.formatter')->formatTimeDiffSince($comment->getCreatedTime(), array('langcode' => $language_interface->getId()));
$tests['[comment:changed:since]'] = \Drupal::service('date.formatter')->formatTimeDiffSince($comment->getChangedTimeAcrossTranslations(), array('langcode' => $language_interface->getId()));
$tests['[comment:parent:cid]'] = $comment->hasParentComment() ? $comment->getParentComment()->id() : NULL;
$tests['[comment:parent:title]'] = Html::escape($parent_comment->getSubject());
$tests['[comment:parent:title]'] = $parent_comment->getSubject();
$tests['[comment:entity]'] = Html::escape($node->getTitle());
// Test node specific tokens.
$tests['[comment:entity:nid]'] = $comment->getCommentedEntityId();
$tests['[comment:entity:title]'] = Html::escape($node->getTitle());
$tests['[comment:author:uid]'] = $comment->getOwnerId();
$tests['[comment:author:name]'] = Html::escape($this->adminUser->getUsername());
$tests['[comment:author:name]'] = Html::escape($this->adminUser->getDisplayName());
$base_bubbleable_metadata = BubbleableMetadata::createFromObject($comment);
$metadata_tests = [];
@ -114,35 +118,16 @@ class CommentTokenReplaceTest extends CommentTestBase {
foreach ($tests as $input => $expected) {
$bubbleable_metadata = new BubbleableMetadata();
$output = $token_service->replace($input, array('comment' => $comment), array('langcode' => $language_interface->getId()), $bubbleable_metadata);
$this->assertEqual($output, $expected, format_string('Sanitized comment token %token replaced.', array('%token' => $input)));
$this->assertEqual($output, $expected, new FormattableMarkup('Comment token %token replaced.', ['%token' => $input]));
$this->assertEqual($bubbleable_metadata, $metadata_tests[$input]);
}
// Generate and test unsanitized tokens.
$tests['[comment:hostname]'] = $comment->getHostname();
$tests['[comment:author]'] = $comment->getAuthorName();
$tests['[comment:mail]'] = $this->adminUser->getEmail();
$tests['[comment:homepage]'] = $comment->getHomepage();
$tests['[comment:title]'] = $comment->getSubject();
$tests['[comment:body]'] = $comment->comment_body->value;
$tests['[comment:langcode]'] = $comment->language()->getId();
$tests['[comment:parent:title]'] = $parent_comment->getSubject();
$tests['[comment:entity]'] = $node->getTitle();
$tests['[comment:author:name]'] = $this->adminUser->getUsername();
foreach ($tests as $input => $expected) {
$output = $token_service->replace($input, array('comment' => $comment), array('langcode' => $language_interface->getId(), 'sanitize' => FALSE));
$this->assertEqual($output, $expected, format_string('Unsanitized comment token %token replaced.', array('%token' => $input)));
}
// Test anonymous comment author.
$author_name = $this->randomString();
$author_name = 'This is a random & " > string';
$comment->setOwnerId(0)->setAuthorName($author_name);
$input = '[comment:author]';
$output = $token_service->replace($input, array('comment' => $comment), array('langcode' => $language_interface->getId()));
$this->assertEqual($output, Xss::filter($author_name), format_string('Sanitized comment author token %token replaced.', array('%token' => $input)));
$output = $token_service->replace($input, array('comment' => $comment), array('langcode' => $language_interface->getId(), 'sanitize' => FALSE));
$this->assertEqual($output, $author_name, format_string('Unsanitized comment author token %token replaced.', array('%token' => $input)));
$this->assertEqual($output, Html::escape($author_name), format_string('Comment author token %token replaced.', array('%token' => $input)));
// Load node so comment_count gets computed.
$node = Node::load($node->id());

View file

@ -37,11 +37,13 @@ class CommentTranslationUITest extends ContentTranslationUITestBase {
*/
protected $defaultCacheContexts = [
'languages:language_interface',
'session',
'theme',
'timezone',
'url.query_args:_wrapper_format',
'url.query_args.pagers:0',
'user'
'user.permissions',
'user.roles',
];
/**
@ -162,7 +164,7 @@ class CommentTranslationUITest extends ContentTranslationUITestBase {
'created' => REQUEST_TIME - mt_rand(0, 1000),
);
$edit = array(
'name' => $user->getUsername(),
'uid' => $user->getUsername() . '(' . $user->id() . ')',
'date[date]' => format_date($values[$langcode]['created'], 'custom', 'Y-m-d'),
'date[time]' => format_date($values[$langcode]['created'], 'custom', 'H:i:s'),
);

View file

@ -43,6 +43,9 @@ class CommentTypeTest extends CommentTestBase {
*/
protected function setUp() {
parent::setUp();
$this->drupalPlaceBlock('page_title_block');
$this->adminUser = $this->drupalCreateUser($this->permissions);
}

View file

@ -19,7 +19,16 @@ class MigrateCommentTest extends MigrateDrupal6TestBase {
use CommentTestTrait;
static $modules = array('node', 'comment', 'text', 'filter');
/**
* {@inheritdoc}
*/
public static $modules = [
'comment',
// Directly testing that a stub comment's entity_id is populated upon
// importing is not straightforward, but RDF module serves as an implicit
// test - its hook_comment_storage_load() references a stubbed comment.
'rdf',
];
/**
* {@inheritdoc}
@ -30,35 +39,21 @@ class MigrateCommentTest extends MigrateDrupal6TestBase {
$this->installEntitySchema('node');
$this->installEntitySchema('comment');
$this->installSchema('comment', ['comment_entity_statistics']);
$this->installConfig(['node', 'comment']);
$this->installSchema('system', ['router']);
$this->installConfig(['comment']);
entity_create('node_type', array('type' => 'page'))->save();
entity_create('node_type', array('type' => 'story'))->save();
$this->addDefaultCommentField('node', 'story');
$this->container->get('entity.manager')->getStorage('comment_type')->create(array(
'id' => 'comment_no_subject',
'label' => 'comment_no_subject',
'target_entity_type_id' => 'node',
))->save();
\Drupal::service('comment.manager')->addBodyField('comment_no_subject');
// The entity.node.canonical route must exist when the RDF hook is called.
$this->container->get('router.builder')->rebuild();
$node = entity_create('node', array(
'type' => 'story',
'nid' => 1,
'title' => $this->randomString(),
));
$node->enforceIsNew();
$node->save();
$id_mappings = array(
'd6_filter_format' => array(array(array(1), array('filtered_html'))),
'd6_node:*' => array(array(array(1), array(1))),
'd6_user' => array(array(array(0), array(0))),
'd6_comment_type' => array(array(array('comment'), array('comment_no_subject'))),
'd6_comment_entity_display' => array(array(array('story'), array('node', 'story', 'default', 'comment'))),
'd6_comment_entity_form_display' => array(array(array('story'), array('node', 'story', 'default', 'comment'))),
);
$this->prepareMigrations($id_mappings);
$this->executeMigration('d6_comment');
$this->migrateContent();
$this->executeMigrations([
'd6_comment_type',
'd6_comment_field',
'd6_comment_field_instance',
'd6_comment_entity_display',
'd6_comment_entity_form_display',
'd6_comment',
]);
}
/**

View file

@ -17,7 +17,10 @@ use Drupal\migrate_drupal\Tests\d6\MigrateDrupal6TestBase;
*/
class MigrateCommentTypeTest extends MigrateDrupal6TestBase {
static $modules = array('node', 'comment', 'text', 'filter');
/**
* {@inheritdoc}
*/
public static $modules = ['comment'];
/**
* {@inheritdoc}

View file

@ -15,57 +15,22 @@ use Drupal\migrate_drupal\Tests\d6\MigrateDrupal6TestBase;
abstract class MigrateCommentVariableDisplayBase extends MigrateDrupal6TestBase {
/**
* The ID of migration to run.
*
* This constant needs to be set in the concrete class in order for the test
* to work.
* {@inheritdoc}
*/
const MIGRATION = '';
/**
* Modules to enable.
*
* @var array
*/
static $modules = array('comment', 'node');
/**
* The node types being tested.
*
* @var array
*/
protected $types = array('page', 'story', 'article');
public static $modules = ['comment'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
entity_create('field_storage_config', array(
'entity_type' => 'node',
'field_name' => 'comment',
'type' => 'comment',
'translatable' => '0',
))->save();
foreach ($this->types as $type) {
entity_create('node_type', array('type' => $type))->save();
entity_create('field_config', array(
'label' => 'Comments',
'description' => '',
'field_name' => 'comment',
'entity_type' => 'node',
'bundle' => $type,
'required' => 1,
))->save();
}
$id_mappings = array(
'd6_comment_field_instance' => array(
array(array('page'), array('node', 'comment', 'page')),
),
);
$this->prepareMigrations($id_mappings);
$this->executeMigration(static::MIGRATION);
$this->installConfig(['comment']);
$this->migrateContentTypes();
$this->executeMigrations([
'd6_comment_type',
'd6_comment_field',
'd6_comment_field_instance',
]);
}
}

View file

@ -7,6 +7,8 @@
namespace Drupal\comment\Tests\Migrate\d6;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
/**
* Upgrade comment variables to entity.display.node.*.default.yml.
*
@ -15,21 +17,19 @@ namespace Drupal\comment\Tests\Migrate\d6;
class MigrateCommentVariableEntityDisplayTest extends MigrateCommentVariableDisplayBase {
/**
* The migration to run.
* {@inheritdoc}
*/
const MIGRATION = 'd6_comment_entity_display';
/**
* The node types being used.
*/
protected $types = array('page', 'story', 'article');
protected function setUp() {
parent::setUp();
$this->executeMigration('d6_comment_entity_display');
}
/**
* Tests comment variables migrated into an entity display.
*/
public function testCommentEntityDisplay() {
foreach ($this->types as $type) {
$component = entity_get_display('node', $type, 'default')->getComponent('comment');
foreach (['page', 'story', 'article'] as $type) {
$component = EntityViewDisplay::load('node.' . $type . '.default')->getComponent('comment');
$this->assertIdentical('hidden', $component['label']);
$this->assertIdentical('comment_default', $component['type']);
$this->assertIdentical(20, $component['weight']);

View file

@ -7,6 +7,7 @@
namespace Drupal\comment\Tests\Migrate\d6;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\migrate_drupal\Tests\d6\MigrateDrupal6TestBase;
/**
@ -17,43 +18,31 @@ use Drupal\migrate_drupal\Tests\d6\MigrateDrupal6TestBase;
class MigrateCommentVariableEntityFormDisplaySubjectTest extends MigrateDrupal6TestBase {
/**
* Modules to enable.
*
* @var array
* {@inheritdoc}
*/
public static $modules = array('comment', 'node');
public static $modules = ['comment'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
foreach (['comment', 'comment_no_subject'] as $comment_type) {
entity_create('comment_type', array(
'id' => $comment_type,
'target_entity_type_id' => 'node',
))
->save();
}
// Add some id mappings for the dependant migrations.
$id_mappings = array(
'd6_comment_type' => array(
array(array('comment'), array('comment_no_subject')),
),
);
$this->prepareMigrations($id_mappings);
$this->executeMigration('d6_comment_entity_form_display_subject');
$this->installConfig(['comment']);
$this->executeMigrations([
'd6_comment_type',
'd6_comment_entity_form_display_subject',
]);
}
/**
* Tests comment subject variable migrated into an entity display.
*/
public function testCommentEntityFormDisplay() {
$component = entity_get_form_display('comment', 'comment', 'default')
$component = EntityFormDisplay::load('comment.comment.default')
->getComponent('subject');
$this->assertIdentical('string_textfield', $component['type']);
$this->assertIdentical(10, $component['weight']);
$component = entity_get_form_display('comment', 'comment_no_subject', 'default')
$component = EntityFormDisplay::load('comment.comment_no_subject.default')
->getComponent('subject');
$this->assertNull($component);
}

View file

@ -7,6 +7,8 @@
namespace Drupal\comment\Tests\Migrate\d6;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
/**
* Upgrade comment variables to core.entity_form_display.node.*.default.yml.
*
@ -15,16 +17,20 @@ namespace Drupal\comment\Tests\Migrate\d6;
class MigrateCommentVariableEntityFormDisplayTest extends MigrateCommentVariableDisplayBase {
/**
* The migration to run.
* {@inheritdoc}
*/
const MIGRATION = 'd6_comment_entity_form_display';
protected function setUp() {
parent::setUp();
$this->executeMigration('d6_comment_entity_form_display');
}
/**
* Tests comment variables migrated into an entity display.
*/
public function testCommentEntityFormDisplay() {
foreach ($this->types as $type) {
$component = entity_get_form_display('node', $type, 'default')->getComponent('comment');
foreach (['page', 'article', 'story'] as $type) {
$component = EntityFormDisplay::load('node.' . $type . '.default')
->getComponent('comment');
$this->assertIdentical('comment_default', $component['type']);
$this->assertIdentical(20, $component['weight']);
}

View file

@ -17,31 +17,19 @@ use Drupal\migrate_drupal\Tests\d6\MigrateDrupal6TestBase;
*/
class MigrateCommentVariableFieldTest extends MigrateDrupal6TestBase {
static $modules = array('comment', 'node');
/**
* {@inheritdoc}
*/
public static $modules = ['comment'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
foreach (array('page', 'story', 'test') as $type) {
entity_create('node_type', array('type' => $type))->save();
}
foreach (['comment', 'comment_no_subject'] as $comment_type) {
entity_create('comment_type', array(
'id' => $comment_type,
'target_entity_type_id' => 'node',
))
->save();
}
// Add some id mappings for the dependant migrations.
$id_mappings = array(
'd6_comment_type' => array(
array(array('comment'), array('comment_no_subject')),
),
);
$this->prepareMigrations($id_mappings);
$this->executeMigration('d6_comment_field');
$this->installConfig(['comment']);
$this->migrateContentTypes();
$this->executeMigrations(['d6_comment_type', 'd6_comment_field']);
}
/**

View file

@ -8,6 +8,7 @@
namespace Drupal\comment\Tests\Migrate\d6;
use Drupal\migrate_drupal\Tests\d6\MigrateDrupal6TestBase;
use Drupal\node\Entity\Node;
/**
* Upgrade comment variables to field.instance.node.*.comment.yml.
@ -16,47 +17,30 @@ use Drupal\migrate_drupal\Tests\d6\MigrateDrupal6TestBase;
*/
class MigrateCommentVariableInstanceTest extends MigrateDrupal6TestBase {
static $modules = array('comment', 'node');
/**
* {@inheritdoc}
*/
public static $modules = ['comment'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
// Add some id mappings for the dependant migrations.
$id_mappings = array(
'd6_comment_field' => array(
array(array('page'), array('node', 'page')),
),
'd6_node_type' => array(
array(array('page'), array('page')),
),
);
$this->prepareMigrations($id_mappings);
foreach (array('page', 'story', 'article') as $type) {
entity_create('node_type', array('type' => $type))->save();
}
entity_create('field_storage_config', array(
'entity_type' => 'node',
'field_name' => 'comment',
'type' => 'comment',
'translatable' => '0',
))->save();
entity_create('field_storage_config', array(
'entity_type' => 'node',
'field_name' => 'comment_no_subject',
'type' => 'comment',
'translatable' => '0',
))->save();
$this->executeMigration('d6_comment_field_instance');
$this->installConfig(['comment']);
$this->migrateContentTypes();
$this->executeMigrations([
'd6_comment_type',
'd6_comment_field',
'd6_comment_field_instance',
]);
}
/**
* Test the migrated field instance values.
*/
public function testCommentFieldInstance() {
$node = entity_create('node', array('type' => 'page'));
$node = Node::create(['type' => 'page']);
$this->assertIdentical(0, $node->comment->status);
$this->assertIdentical('comment', $node->comment->getFieldDefinition()->getName());
$settings = $node->comment->getFieldDefinition()->getSettings();
@ -66,7 +50,7 @@ class MigrateCommentVariableInstanceTest extends MigrateDrupal6TestBase {
$this->assertIdentical(FALSE, $settings['form_location']);
$this->assertIdentical(1, $settings['preview']);
$node = entity_create('node', array('type' => 'story'));
$node = Node::create(['type' => 'story']);
$this->assertIdentical(2, $node->comment_no_subject->status);
$this->assertIdentical('comment_no_subject', $node->comment_no_subject->getFieldDefinition()->getName());
$settings = $node->comment_no_subject->getFieldDefinition()->getSettings();

View file

@ -25,11 +25,13 @@ class MigrateCommentEntityDisplayTest extends MigrateDrupal7TestBase {
protected function setUp() {
parent::setUp();
$this->installConfig(static::$modules);
$this->executeMigration('d7_node_type');
$this->executeMigration('d7_comment_type');
$this->executeMigration('d7_comment_field');
$this->executeMigration('d7_comment_field_instance');
$this->executeMigration('d7_comment_entity_display');
$this->executeMigrations([
'd7_node_type',
'd7_comment_type',
'd7_comment_field',
'd7_comment_field_instance',
'd7_comment_entity_display',
]);
}
/**

View file

@ -25,9 +25,11 @@ class MigrateCommentEntityFormDisplaySubjectTest extends MigrateDrupal7TestBase
protected function setUp() {
parent::setUp();
$this->installConfig(static::$modules);
$this->executeMigration('d7_node_type');
$this->executeMigration('d7_comment_type');
$this->executeMigration('d7_comment_entity_form_display_subject');
$this->executeMigrations([
'd7_node_type',
'd7_comment_type',
'd7_comment_entity_form_display_subject',
]);
}
/**

View file

@ -25,11 +25,13 @@ class MigrateCommentEntityFormDisplayTest extends MigrateDrupal7TestBase {
protected function setUp() {
parent::setUp();
$this->installConfig(static::$modules);
$this->executeMigration('d7_node_type');
$this->executeMigration('d7_comment_type');
$this->executeMigration('d7_comment_field');
$this->executeMigration('d7_comment_field_instance');
$this->executeMigration('d7_comment_entity_form_display');
$this->executeMigrations([
'd7_node_type',
'd7_comment_type',
'd7_comment_field',
'd7_comment_field_instance',
'd7_comment_entity_form_display',
]);
}
/**

View file

@ -28,10 +28,12 @@ class MigrateCommentFieldInstanceTest extends MigrateDrupal7TestBase {
protected function setUp() {
parent::setUp();
$this->installConfig(static::$modules);
$this->executeMigration('d7_node_type');
$this->executeMigration('d7_comment_type');
$this->executeMigration('d7_comment_field');
$this->executeMigration('d7_comment_field_instance');
$this->executeMigrations([
'd7_node_type',
'd7_comment_type',
'd7_comment_field',
'd7_comment_field_instance',
]);
}
/**

View file

@ -27,9 +27,11 @@ class MigrateCommentFieldTest extends MigrateDrupal7TestBase {
protected function setUp() {
parent::setUp();
$this->installConfig(static::$modules);
$this->executeMigration('d7_node_type');
$this->executeMigration('d7_comment_type');
$this->executeMigration('d7_comment_field');
$this->executeMigrations([
'd7_node_type',
'd7_comment_type',
'd7_comment_field',
]);
}
/**

View file

@ -32,9 +32,11 @@ class MigrateCommentTest extends MigrateDrupal7TestBase {
$this->installEntitySchema('node');
$this->installEntitySchema('comment');
$this->executeMigration('d7_filter_format');
$this->executeMigration('d7_user_role');
$this->executeMigration('d7_user');
$this->executeMigrations([
'd7_filter_format',
'd7_user_role',
'd7_user',
]);
// The test database doesn't include uid 1, so we'll need to create it.
User::create(array(
'uid' => 1,
@ -49,9 +51,11 @@ class MigrateCommentTest extends MigrateDrupal7TestBase {
array(array(0), array(0)),
),
));
$this->executeMigration('d7_node__test_content_type');
$this->executeMigration('d7_comment_type');
$this->executeMigration('d7_comment');
$this->executeMigrations([
'd7_node__test_content_type',
'd7_comment_type',
'd7_comment',
]);
}
/**

View file

@ -9,6 +9,7 @@ namespace Drupal\comment\Tests\Migrate\d7;
use Drupal\comment\CommentTypeInterface;
use Drupal\comment\Entity\CommentType;
use Drupal\migrate\Entity\Migration;
use Drupal\migrate_drupal\Tests\d7\MigrateDrupal7TestBase;
/**
@ -26,8 +27,10 @@ class MigrateCommentTypeTest extends MigrateDrupal7TestBase {
protected function setUp() {
parent::setUp();
$this->installConfig(static::$modules);
$this->executeMigration('d7_node_type');
$this->executeMigration('d7_comment_type');
$this->executeMigrations([
'd7_node_type',
'd7_comment_type',
]);
}
/**
@ -56,6 +59,11 @@ class MigrateCommentTypeTest extends MigrateDrupal7TestBase {
$this->assertEntity('comment_node_book', 'Book page comment');
$this->assertEntity('comment_node_forum', 'Forum topic comment');
$this->assertEntity('comment_node_test_content_type', 'Test content type comment');
// Validate that the source count and processed count match up.
/** @var \Drupal\migrate\Entity\MigrationInterface $migration */
$migration = Migration::load('d7_comment_type');
$this->assertIdentical($migration->getSourcePlugin()->count(), $migration->getIdMap()->processedCount());
}
}

View file

@ -61,6 +61,9 @@ class CommentFieldNameTest extends CommentTestBase {
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);
@ -79,14 +82,7 @@ class CommentFieldNameTest extends CommentTestBase {
'comment_field_data_field_name' => 'field_name',
];
$this->assertIdenticalResultset($view, $expected_result, $column_map);
// Test that no data can be rendered.
$this->assertIdentical(FALSE, isset($view->field['field_name']));
// 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);
// Test that data rendered.
$output = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) {
return $view->field['field_name']->advancedRender($view->result[0]);

View file

@ -0,0 +1,180 @@
<?php
/**
* @file
* Contains \Drupal\comment\Tests\Views\CommentLinksTest.
*/
namespace Drupal\comment\Tests\Views;
use Drupal\comment\CommentInterface;
use Drupal\comment\CommentManagerInterface;
use Drupal\Core\Session\AnonymousUserSession;
use Drupal\Core\Url;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\views\Views;
/**
* Tests the comment link field handlers.
*
* @group comment
*/
class CommentLinksTest extends CommentViewKernelTestBase {
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = ['test_comment'];
/**
* Test the comment approve link.
*/
public function testLinkApprove() {
// Create an unapproved comment.
$comment = $this->commentStorage->create([
'uid' => $this->adminUser->id(),
'entity_type' => 'entity_test',
'comment_type' => 'entity_test',
'status' => 0,
]);
$comment->save();
$view = Views::getView('test_comment');
$view->setDisplay();
$view->displayHandlers->get('default')->overrideOption('fields', [
'approve_comment' => [
'table' => 'comment',
'field' => 'approve_comment',
'id' => 'approve_comment',
'plugin_id' => 'comment_link_approve',
],
]);
$view->save();
/* @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */
$account_switcher = \Drupal::service('account_switcher');
$account_switcher->switchTo($this->adminUser);
$view->preview();
// Check if I can see the comment approve link on an unapproved comment.
$approve_comment = $view->style_plugin->getField(0, 'approve_comment');
$options = ['query' => ['destination' => '/']];
$url = Url::fromRoute('comment.approve', ['comment' => $comment->id()], $options);
$this->assertEqual(\Drupal::l('Approve', $url), (string) $approve_comment, 'Found a comment approve link for an unapproved comment.');
// Approve the comment.
$comment->setPublished(CommentInterface::PUBLISHED);
$comment->save();
$view = Views::getView('test_comment');
$view->preview();
// Check if I can see the comment approve link on an approved comment.
$approve_comment = $view->style_plugin->getField(1, 'approve_comment');
$this->assertFalse((string) $approve_comment, "Didn't find a comment approve link for an already approved comment.");
// Check if I can see the comment approve link on an approved comment as an
// anonymous user.
$account_switcher->switchTo(new AnonymousUserSession());
// Set the comment as unpublished again.
$comment->setPublished(CommentInterface::NOT_PUBLISHED);
$comment->save();
$view = Views::getView('test_comment');
$view->preview();
$replyto_comment = $view->style_plugin->getField(0, 'approve_comment');
$this->assertFalse((string) $replyto_comment, "I can't approve the comment as an anonymous user.");
}
/**
* Test the comment reply link.
*/
public function testLinkReply() {
$this->enableModules(['field', 'entity_test']);
$this->installEntitySchema('entity_test');
$this->installSchema('comment', ['comment_entity_statistics']);
$this->installConfig(['field']);
$field_storage_comment = FieldStorageConfig::create([
'field_name' => 'comment',
'type' => 'comment',
'entity_type' => 'entity_test',
]);
$field_storage_comment->save();
// Create a comment field which allows threading.
$field_comment = FieldConfig::create([
'field_name' => 'comment',
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
'settings' => [
'default_mode' => CommentManagerInterface::COMMENT_MODE_THREADED,
],
]);
$field_comment->save();
$host = EntityTest::create(['name' => $this->randomString()]);
$host->save();
// Attach an unapproved comment to the test entity.
$comment = $this->commentStorage->create([
'uid' => $this->adminUser->id(),
'entity_type' => 'entity_test',
'entity_id' => $host->id(),
'comment_type' => 'entity_test',
'field_name' => $field_storage_comment->getName(),
'status' => 0,
]);
$comment->save();
$view = Views::getView('test_comment');
$view->setDisplay();
$view->displayHandlers->get('default')->overrideOption('fields', [
'replyto_comment' => [
'table' => 'comment',
'field' => 'replyto_comment',
'id' => 'replyto_comment',
'plugin_id' => 'comment_link_reply',
'entity_type' => 'comment',
],
]);
$view->save();
/* @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */
$account_switcher = \Drupal::service('account_switcher');
$account_switcher->switchTo($this->adminUser);
$view->preview();
// Check if I can see the reply link on an unapproved comment.
$replyto_comment = $view->style_plugin->getField(0, 'replyto_comment');
$this->assertFalse((string) $replyto_comment, "I can't reply to an unapproved comment.");
// Approve the comment.
$comment->setPublished(CommentInterface::PUBLISHED);
$comment->save();
$view = Views::getView('test_comment');
$view->preview();
// Check if I can see the reply link on an approved comment.
$replyto_comment = $view->style_plugin->getField(0, 'replyto_comment');
$url = Url::fromRoute('comment.reply', [
'entity_type' => 'entity_test',
'entity' => $host->id(),
'field_name' => 'comment',
'pid' => $comment->id(),
]);
$this->assertEqual(\Drupal::l('Reply', $url), (string) $replyto_comment, 'Found the comment reply link as an admin user.');
// Check if I can see the reply link as an anonymous user.
$account_switcher->switchTo(new AnonymousUserSession());
$view = Views::getView('test_comment');
$view->preview();
$replyto_comment = $view->style_plugin->getField(0, 'replyto_comment');
$this->assertFalse((string) $replyto_comment, "Didn't find the comment reply link as an anonymous user.");
}
}

View file

@ -0,0 +1,83 @@
<?php
/**
* @file
* Contains \Drupal\comment\Tests\Views\CommentViewKernelTestBase.
*/
namespace Drupal\comment\Tests\Views;
use Drupal\user\Entity\Role;
use Drupal\views\Tests\ViewKernelTestBase;
use Drupal\views\Tests\ViewTestData;
/**
* Provides a common test base for comment views tests.
*/
abstract class CommentViewKernelTestBase extends ViewKernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['comment_test_views', 'user', 'comment'];
/**
* Admin user.
*
* @var \Drupal\user\UserInterface
*/
protected $adminUser;
/**
* The entity storage for comments.
*
* @var \Drupal\comment\CommentStorageInterface
*/
protected $commentStorage;
/**
* The entity storage for users.
*
* @var \Drupal\user\UserStorageInterface
*/
protected $userStorage;
protected function setUp($import_test_views = TRUE) {
parent::setUp($import_test_views);
ViewTestData::createTestViews(get_class($this), ['comment_test_views']);
$this->installEntitySchema('user');
$this->installEntitySchema('comment');
$this->installConfig(['user']);
$entity_manager = $this->container->get('entity.manager');
$this->commentStorage = $entity_manager->getStorage('comment');
$this->userStorage = $entity_manager->getStorage('user');
// Insert a row for the anonymous user.
$this->userStorage
->create([
'uid' => 0,
'name' => '',
'status' => 0,
])
->save();
$admin_role = Role::create(['id' => 'admin']);
$admin_role->grantPermission('administer comments');
$admin_role->save();
/* @var \Drupal\user\RoleInterface $anonymous_role */
$anonymous_role = Role::load(Role::ANONYMOUS_ID);
$anonymous_role->grantPermission('access comments');
$anonymous_role->save();
$this->adminUser = $this->userStorage->create(['name' => $this->randomMachineName()]);
$this->adminUser->addRole('admin');
$this->adminUser->save();
}
}