Drupal 8.0.0 beta 12. More info: https://www.drupal.org/node/2514176
This commit is contained in:
commit
9921556621
13277 changed files with 1459781 additions and 0 deletions
75
core/modules/comment/src/Tests/CommentActionsTest.php
Normal file
75
core/modules/comment/src/Tests/CommentActionsTest.php
Normal file
|
@ -0,0 +1,75 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\CommentActionsTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests;
|
||||
|
||||
use Drupal\comment\Entity\Comment;
|
||||
|
||||
/**
|
||||
* Tests actions provided by the Comment module.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class CommentActionsTest extends CommentTestBase {
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('dblog', 'action');
|
||||
|
||||
/**
|
||||
* Tests comment publish and unpublish actions.
|
||||
*/
|
||||
function testCommentPublishUnpublishActions() {
|
||||
$this->drupalLogin($this->webUser);
|
||||
$comment_text = $this->randomMachineName();
|
||||
$subject = $this->randomMachineName();
|
||||
$comment = $this->postComment($this->node, $comment_text, $subject);
|
||||
|
||||
// Unpublish a comment.
|
||||
$action = entity_load('action', 'comment_unpublish_action');
|
||||
$action->execute(array($comment));
|
||||
$this->assertTrue($comment->isPublished() === FALSE, 'Comment was unpublished');
|
||||
|
||||
// Publish a comment.
|
||||
$action = entity_load('action', 'comment_publish_action');
|
||||
$action->execute(array($comment));
|
||||
$this->assertTrue($comment->isPublished() === TRUE, 'Comment was published');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the unpublish comment by keyword action.
|
||||
*/
|
||||
function testCommentUnpublishByKeyword() {
|
||||
$this->drupalLogin($this->adminUser);
|
||||
$keyword_1 = $this->randomMachineName();
|
||||
$keyword_2 = $this->randomMachineName();
|
||||
$action = entity_create('action', array(
|
||||
'id' => 'comment_unpublish_by_keyword_action',
|
||||
'label' => $this->randomMachineName(),
|
||||
'type' => 'comment',
|
||||
'configuration' => array(
|
||||
'keywords' => array($keyword_1, $keyword_2),
|
||||
),
|
||||
'plugin' => 'comment_unpublish_by_keyword_action',
|
||||
));
|
||||
$action->save();
|
||||
|
||||
$comment = $this->postComment($this->node, $keyword_2, $this->randomMachineName());
|
||||
|
||||
// Load the full comment so that status is available.
|
||||
$comment = Comment::load($comment->id());
|
||||
|
||||
$this->assertTrue($comment->isPublished() === TRUE, 'The comment status was set to published.');
|
||||
|
||||
$action->execute(array($comment));
|
||||
$this->assertTrue($comment->isPublished() === FALSE, 'The comment status was set to not published.');
|
||||
}
|
||||
|
||||
}
|
214
core/modules/comment/src/Tests/CommentAdminTest.php
Normal file
214
core/modules/comment/src/Tests/CommentAdminTest.php
Normal file
|
@ -0,0 +1,214 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\CommentAdminTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests;
|
||||
|
||||
use Drupal\user\RoleInterface;
|
||||
|
||||
/**
|
||||
* Tests comment approval functionality.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class CommentAdminTest extends CommentTestBase {
|
||||
/**
|
||||
* Test comment approval functionality through admin/content/comment.
|
||||
*/
|
||||
function testApprovalAdminInterface() {
|
||||
// Set anonymous comments to require approval.
|
||||
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, array(
|
||||
'access comments' => TRUE,
|
||||
'post comments' => TRUE,
|
||||
'skip comment approval' => FALSE,
|
||||
));
|
||||
$this->drupalLogin($this->adminUser);
|
||||
$this->setCommentAnonymous('0'); // Ensure that doesn't require contact info.
|
||||
|
||||
// Test that the comments page loads correctly when there are no comments
|
||||
$this->drupalGet('admin/content/comment');
|
||||
$this->assertText(t('No comments available.'));
|
||||
|
||||
$this->drupalLogout();
|
||||
|
||||
// Post anonymous comment without contact info.
|
||||
$subject = $this->randomMachineName();
|
||||
$body = $this->randomMachineName();
|
||||
$this->postComment($this->node, $body, $subject, TRUE); // Set $contact to true so that it won't check for id and message.
|
||||
$this->assertText(t('Your comment has been queued for review by site administrators and will be published after approval.'), 'Comment requires approval.');
|
||||
|
||||
// Get unapproved comment id.
|
||||
$this->drupalLogin($this->adminUser);
|
||||
$anonymous_comment4 = $this->getUnapprovedComment($subject);
|
||||
$anonymous_comment4 = entity_create('comment', array(
|
||||
'cid' => $anonymous_comment4,
|
||||
'subject' => $subject,
|
||||
'comment_body' => $body,
|
||||
'entity_id' => $this->node->id(),
|
||||
'entity_type' => 'node',
|
||||
'field_name' => 'comment'
|
||||
));
|
||||
$this->drupalLogout();
|
||||
|
||||
$this->assertFalse($this->commentExists($anonymous_comment4), 'Anonymous comment was not published.');
|
||||
|
||||
// Approve comment.
|
||||
$this->drupalLogin($this->adminUser);
|
||||
$this->performCommentOperation($anonymous_comment4, 'publish', TRUE);
|
||||
$this->drupalLogout();
|
||||
|
||||
$this->drupalGet('node/' . $this->node->id());
|
||||
$this->assertTrue($this->commentExists($anonymous_comment4), 'Anonymous comment visible.');
|
||||
|
||||
// Post 2 anonymous comments without contact info.
|
||||
$comments[] = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
|
||||
$comments[] = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
|
||||
|
||||
// Publish multiple comments in one operation.
|
||||
$this->drupalLogin($this->adminUser);
|
||||
$this->drupalGet('admin/content/comment/approval');
|
||||
$this->assertText(t('Unapproved comments (@count)', array('@count' => 2)), 'Two unapproved comments waiting for approval.');
|
||||
$edit = array(
|
||||
"comments[{$comments[0]->id()}]" => 1,
|
||||
"comments[{$comments[1]->id()}]" => 1,
|
||||
);
|
||||
$this->drupalPostForm(NULL, $edit, t('Update'));
|
||||
$this->assertText(t('Unapproved comments (@count)', array('@count' => 0)), 'All comments were approved.');
|
||||
|
||||
// Delete multiple comments in one operation.
|
||||
$edit = array(
|
||||
'operation' => 'delete',
|
||||
"comments[{$comments[0]->id()}]" => 1,
|
||||
"comments[{$comments[1]->id()}]" => 1,
|
||||
"comments[{$anonymous_comment4->id()}]" => 1,
|
||||
);
|
||||
$this->drupalPostForm(NULL, $edit, t('Update'));
|
||||
$this->assertText(t('Are you sure you want to delete these comments and all their children?'), 'Confirmation required.');
|
||||
$this->drupalPostForm(NULL, $edit, t('Delete comments'));
|
||||
$this->assertText(t('No comments available.'), 'All comments were deleted.');
|
||||
// Test message when no comments selected.
|
||||
$edit = array(
|
||||
'operation' => 'delete',
|
||||
);
|
||||
$this->drupalPostForm(NULL, $edit, t('Update'));
|
||||
$this->assertText(t('Select one or more comments to perform the update on.'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests comment approval functionality through the node interface.
|
||||
*/
|
||||
function testApprovalNodeInterface() {
|
||||
// Set anonymous comments to require approval.
|
||||
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, array(
|
||||
'access comments' => TRUE,
|
||||
'post comments' => TRUE,
|
||||
'skip comment approval' => FALSE,
|
||||
));
|
||||
$this->drupalLogin($this->adminUser);
|
||||
$this->setCommentAnonymous('0'); // Ensure that doesn't require contact info.
|
||||
$this->drupalLogout();
|
||||
|
||||
// Post anonymous comment without contact info.
|
||||
$subject = $this->randomMachineName();
|
||||
$body = $this->randomMachineName();
|
||||
$this->postComment($this->node, $body, $subject, TRUE); // Set $contact to true so that it won't check for id and message.
|
||||
$this->assertText(t('Your comment has been queued for review by site administrators and will be published after approval.'), 'Comment requires approval.');
|
||||
|
||||
// Get unapproved comment id.
|
||||
$this->drupalLogin($this->adminUser);
|
||||
$anonymous_comment4 = $this->getUnapprovedComment($subject);
|
||||
$anonymous_comment4 = entity_create('comment', array(
|
||||
'cid' => $anonymous_comment4,
|
||||
'subject' => $subject,
|
||||
'comment_body' => $body,
|
||||
'entity_id' => $this->node->id(),
|
||||
'entity_type' => 'node',
|
||||
'field_name' => 'comment'
|
||||
));
|
||||
$this->drupalLogout();
|
||||
|
||||
$this->assertFalse($this->commentExists($anonymous_comment4), 'Anonymous comment was not published.');
|
||||
|
||||
// Approve comment.
|
||||
$this->drupalLogin($this->adminUser);
|
||||
$this->drupalGet('comment/1/approve');
|
||||
$this->assertResponse(403, 'Forged comment approval was denied.');
|
||||
$this->drupalGet('comment/1/approve', array('query' => array('token' => 'forged')));
|
||||
$this->assertResponse(403, 'Forged comment approval was denied.');
|
||||
$this->drupalGet('comment/1/edit');
|
||||
$this->assertFieldChecked('edit-status-0');
|
||||
$this->drupalGet('node/' . $this->node->id());
|
||||
$this->clickLink(t('Approve'));
|
||||
$this->drupalLogout();
|
||||
|
||||
$this->drupalGet('node/' . $this->node->id());
|
||||
$this->assertTrue($this->commentExists($anonymous_comment4), 'Anonymous comment visible.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests comment bundle admin.
|
||||
*/
|
||||
public function testCommentAdmin() {
|
||||
// Login.
|
||||
$this->drupalLogin($this->adminUser);
|
||||
// Browse to comment bundle overview.
|
||||
$this->drupalGet('admin/structure/comment');
|
||||
$this->assertResponse(200);
|
||||
// Make sure titles visible.
|
||||
$this->assertText('Comment type');
|
||||
$this->assertText('Description');
|
||||
// Make sure the description is present.
|
||||
$this->assertText('Default comment field');
|
||||
// Manage fields.
|
||||
$this->clickLink('Manage fields');
|
||||
$this->assertResponse(200);
|
||||
// Make sure comment_body field is shown.
|
||||
$this->assertText('comment_body');
|
||||
// Rest from here on in is field_ui.
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests editing a comment as an admin.
|
||||
*/
|
||||
public function testEditComment() {
|
||||
// Enable anonymous user comments.
|
||||
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, array(
|
||||
'access comments',
|
||||
'post comments',
|
||||
'skip comment approval',
|
||||
));
|
||||
|
||||
// Login as a web user.
|
||||
$this->drupalLogin($this->webUser);
|
||||
// Post a comment.
|
||||
$comment = $this->postComment($this->node, $this->randomMachineName());
|
||||
|
||||
$this->drupalLogout();
|
||||
|
||||
// Post anonymous comment.
|
||||
$this->drupalLogin($this->adminUser);
|
||||
$this->setCommentAnonymous('2'); // Ensure that we need email id before posting comment.
|
||||
$this->drupalLogout();
|
||||
|
||||
// Post comment with contact info (required).
|
||||
$author_name = $this->randomMachineName();
|
||||
$author_mail = $this->randomMachineName() . '@example.com';
|
||||
$anonymous_comment = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), array('name' => $author_name, 'mail' => $author_mail));
|
||||
|
||||
// Login as an admin user.
|
||||
$this->drupalLogin($this->adminUser);
|
||||
|
||||
// Make sure the comment field is not visible when
|
||||
// the comment was posted by an authenticated user.
|
||||
$this->drupalGet('comment/' . $comment->id() . '/edit');
|
||||
$this->assertNoFieldById('edit-mail', $comment->getAuthorEmail());
|
||||
|
||||
// Make sure the comment field is visible when
|
||||
// the comment was posted by an anonymous user.
|
||||
$this->drupalGet('comment/' . $anonymous_comment->id() . '/edit');
|
||||
$this->assertFieldById('edit-mail', $anonymous_comment->getAuthorEmail());
|
||||
}
|
||||
}
|
170
core/modules/comment/src/Tests/CommentAnonymousTest.php
Normal file
170
core/modules/comment/src/Tests/CommentAnonymousTest.php
Normal file
|
@ -0,0 +1,170 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\CommentAnonymousTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests;
|
||||
|
||||
use Drupal\user\RoleInterface;
|
||||
|
||||
/**
|
||||
* Tests anonymous commenting.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class CommentAnonymousTest extends CommentTestBase {
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
// Enable anonymous and authenticated user comments.
|
||||
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, array(
|
||||
'access comments',
|
||||
'post comments',
|
||||
'skip comment approval',
|
||||
));
|
||||
user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, array(
|
||||
'access comments',
|
||||
'post comments',
|
||||
'skip comment approval',
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests anonymous comment functionality.
|
||||
*/
|
||||
function testAnonymous() {
|
||||
$this->drupalLogin($this->adminUser);
|
||||
$this->setCommentAnonymous(COMMENT_ANONYMOUS_MAYNOT_CONTACT);
|
||||
$this->drupalLogout();
|
||||
|
||||
// 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.');
|
||||
|
||||
// Allow contact info.
|
||||
$this->drupalLogin($this->adminUser);
|
||||
$this->setCommentAnonymous(COMMENT_ANONYMOUS_MAY_CONTACT);
|
||||
|
||||
// Attempt to edit anonymous comment.
|
||||
$this->drupalGet('comment/' . $anonymous_comment1->id() . '/edit');
|
||||
$edited_comment = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName());
|
||||
$this->assertTrue($this->commentExists($edited_comment, FALSE), 'Modified reply found.');
|
||||
$this->drupalLogout();
|
||||
|
||||
// Post anonymous comment with contact info (optional).
|
||||
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
|
||||
$this->assertTrue($this->commentContactInfoAvailable(), 'Contact information available.');
|
||||
|
||||
// Check the presence of expected cache tags.
|
||||
$this->assertCacheTag('config:field.field.node.article.comment');
|
||||
$this->assertCacheTag('config:user.settings');
|
||||
|
||||
$anonymous_comment2 = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName());
|
||||
$this->assertTrue($this->commentExists($anonymous_comment2), 'Anonymous comment with contact info (optional) found.');
|
||||
|
||||
// Ensure anonymous users cannot post in the name of registered users.
|
||||
$edit = array(
|
||||
'name' => $this->adminUser->getUsername(),
|
||||
'mail' => $this->randomMachineName() . '@example.com',
|
||||
'subject[0][value]' => $this->randomMachineName(),
|
||||
'comment_body[0][value]' => $this->randomMachineName(),
|
||||
);
|
||||
$this->drupalPostForm('comment/reply/node/' . $this->node->id() . '/comment', $edit, t('Save'));
|
||||
$this->assertRaw(t('The name you used (%name) belongs to a registered user.', [
|
||||
'%name' => $this->adminUser->getUsername(),
|
||||
]));
|
||||
|
||||
// Require contact info.
|
||||
$this->drupalLogin($this->adminUser);
|
||||
$this->setCommentAnonymous(COMMENT_ANONYMOUS_MUST_CONTACT);
|
||||
$this->drupalLogout();
|
||||
|
||||
// Try to post comment with contact info (required).
|
||||
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
|
||||
$this->assertTrue($this->commentContactInfoAvailable(), 'Contact information available.');
|
||||
|
||||
$anonymous_comment3 = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
|
||||
// Name should have 'Anonymous' for value by default.
|
||||
$this->assertText(t('Email field is required.'), 'Email required.');
|
||||
$this->assertFalse($this->commentExists($anonymous_comment3), 'Anonymous comment with contact info (required) not found.');
|
||||
|
||||
// Post comment with contact info (required).
|
||||
$author_name = $this->randomMachineName();
|
||||
$author_mail = $this->randomMachineName() . '@example.com';
|
||||
$anonymous_comment3 = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), array('name' => $author_name, 'mail' => $author_mail));
|
||||
$this->assertTrue($this->commentExists($anonymous_comment3), 'Anonymous comment with contact info (required) found.');
|
||||
|
||||
// Make sure the user data appears correctly when editing the comment.
|
||||
$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->assertRaw($author_mail, "The anonymous user's email address is correct when editing the comment.");
|
||||
|
||||
// Unpublish comment.
|
||||
$this->performCommentOperation($anonymous_comment3, 'unpublish');
|
||||
|
||||
$this->drupalGet('admin/content/comment/approval');
|
||||
$this->assertRaw('comments[' . $anonymous_comment3->id() . ']', 'Comment was unpublished.');
|
||||
|
||||
// Publish comment.
|
||||
$this->performCommentOperation($anonymous_comment3, 'publish', TRUE);
|
||||
|
||||
$this->drupalGet('admin/content/comment');
|
||||
$this->assertRaw('comments[' . $anonymous_comment3->id() . ']', 'Comment was published.');
|
||||
|
||||
// Delete comment.
|
||||
$this->performCommentOperation($anonymous_comment3, 'delete');
|
||||
|
||||
$this->drupalGet('admin/content/comment');
|
||||
$this->assertNoRaw('comments[' . $anonymous_comment3->id() . ']', 'Comment was deleted.');
|
||||
$this->drupalLogout();
|
||||
|
||||
// Comment 3 was deleted.
|
||||
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $anonymous_comment3->id());
|
||||
$this->assertResponse(403);
|
||||
|
||||
// Reset.
|
||||
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, array(
|
||||
'access comments' => FALSE,
|
||||
'post comments' => FALSE,
|
||||
'skip comment approval' => FALSE,
|
||||
));
|
||||
|
||||
// Attempt to view comments while disallowed.
|
||||
// NOTE: if authenticated user has permission to post comments, then a
|
||||
// "Login or register to post comments" type link may be shown.
|
||||
$this->drupalGet('node/' . $this->node->id());
|
||||
$this->assertNoPattern('@<h2[^>]*>Comments</h2>@', 'Comments were not displayed.');
|
||||
$this->assertNoLink('Add new comment', 'Link to add comment was found.');
|
||||
|
||||
// Attempt to view node-comment form while disallowed.
|
||||
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
|
||||
$this->assertResponse(403);
|
||||
|
||||
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, array(
|
||||
'access comments' => TRUE,
|
||||
'post comments' => FALSE,
|
||||
'skip comment approval' => FALSE,
|
||||
));
|
||||
$this->drupalGet('node/' . $this->node->id());
|
||||
$this->assertPattern('@<h2[^>]*>Comments</h2>@', 'Comments were displayed.');
|
||||
$this->assertLink('Log in', 1, 'Link to log in was found.');
|
||||
$this->assertLink('register', 1, 'Link to register was found.');
|
||||
|
||||
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, array(
|
||||
'access comments' => FALSE,
|
||||
'post comments' => TRUE,
|
||||
'skip comment approval' => TRUE,
|
||||
));
|
||||
$this->drupalGet('node/' . $this->node->id());
|
||||
$this->assertNoPattern('@<h2[^>]*>Comments</h2>@', 'Comments were not displayed.');
|
||||
$this->assertFieldByName('subject[0][value]', '', 'Subject field found.');
|
||||
$this->assertFieldByName('comment_body[0][value]', '', 'Comment field found.');
|
||||
|
||||
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $anonymous_comment2->id());
|
||||
$this->assertResponse(403);
|
||||
}
|
||||
}
|
95
core/modules/comment/src/Tests/CommentBlockTest.php
Normal file
95
core/modules/comment/src/Tests/CommentBlockTest.php
Normal file
|
@ -0,0 +1,95 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\CommentBlockTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests;
|
||||
|
||||
use Drupal\Component\Utility\SafeMarkup;
|
||||
use Drupal\user\RoleInterface;
|
||||
|
||||
/**
|
||||
* Tests comment block functionality.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class CommentBlockTest extends CommentTestBase {
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('block', 'views');
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
// Update admin user to have the 'administer blocks' permission.
|
||||
$this->adminUser = $this->drupalCreateUser(array(
|
||||
'administer content types',
|
||||
'administer comments',
|
||||
'skip comment approval',
|
||||
'post comments',
|
||||
'access comments',
|
||||
'access content',
|
||||
'administer blocks',
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the recent comments block.
|
||||
*/
|
||||
function testRecentCommentBlock() {
|
||||
$this->drupalLogin($this->adminUser);
|
||||
$block = $this->drupalPlaceBlock('views_block:comments_recent-block_1');
|
||||
|
||||
// Add some test comments, with and without subjects. Because the 10 newest
|
||||
// comments should be shown by the block, we create 11 to test that behavior
|
||||
// below.
|
||||
$timestamp = REQUEST_TIME;
|
||||
for ($i = 0; $i < 11; ++$i) {
|
||||
$subject = ($i % 2) ? $this->randomMachineName() : '';
|
||||
$comments[$i] = $this->postComment($this->node, $this->randomMachineName(), $subject);
|
||||
$comments[$i]->created->value = $timestamp--;
|
||||
$comments[$i]->save();
|
||||
}
|
||||
|
||||
// Test that a user without the 'access comments' permission cannot see the
|
||||
// block.
|
||||
$this->drupalLogout();
|
||||
user_role_revoke_permissions(RoleInterface::ANONYMOUS_ID, array('access comments'));
|
||||
$this->drupalGet('');
|
||||
$this->assertNoText(t('Recent comments'));
|
||||
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, array('access comments'));
|
||||
|
||||
// Test that a user with the 'access comments' permission can see the
|
||||
// block.
|
||||
$this->drupalLogin($this->webUser);
|
||||
$this->drupalGet('');
|
||||
$this->assertText(t('Recent comments'));
|
||||
|
||||
// Test the only the 10 latest comments are shown and in the proper order.
|
||||
$this->assertNoText($comments[10]->getSubject(), 'Comment 11 not found in block.');
|
||||
for ($i = 0; $i < 10; $i++) {
|
||||
$this->assertText($comments[$i]->getSubject(), SafeMarkup::format('Comment @number found in block.', array('@number' => 10 - $i)));
|
||||
if ($i > 1) {
|
||||
$previous_position = $position;
|
||||
$position = strpos($this->getRawContent(), $comments[$i]->getSubject());
|
||||
$this->assertTrue($position > $previous_position, SafeMarkup::format('Comment @a appears after comment @b', array('@a' => 10 - $i, '@b' => 11 - $i)));
|
||||
}
|
||||
$position = strpos($this->getRawContent(), $comments[$i]->getSubject());
|
||||
}
|
||||
|
||||
// Test that links to comments work when comments are across pages.
|
||||
$this->setCommentsPerPage(1);
|
||||
|
||||
for ($i = 0; $i < 10; $i++) {
|
||||
$this->clickLink($comments[$i]->getSubject());
|
||||
$this->assertText($comments[$i]->getSubject(), 'Comment link goes to correct page.');
|
||||
$this->assertRaw('<link rel="canonical"', 'Canonical URL was found in the HTML head');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
80
core/modules/comment/src/Tests/CommentBookTest.php
Normal file
80
core/modules/comment/src/Tests/CommentBookTest.php
Normal file
|
@ -0,0 +1,80 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\CommentBookTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests;
|
||||
|
||||
use Drupal\comment\CommentInterface;
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
|
||||
/**
|
||||
* Tests visibility of comments on book pages.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class CommentBookTest extends WebTestBase {
|
||||
|
||||
use CommentTestTrait;
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('book', 'comment');
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
// Create comment field on book.
|
||||
$this->addDefaultCommentField('node', 'book');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests comments in book export.
|
||||
*/
|
||||
public function testBookCommentPrint() {
|
||||
$book_node = entity_create('node', array(
|
||||
'type' => 'book',
|
||||
'title' => 'Book title',
|
||||
'body' => 'Book body',
|
||||
));
|
||||
$book_node->book['bid'] = 'new';
|
||||
$book_node->save();
|
||||
|
||||
$comment_subject = $this->randomMachineName(8);
|
||||
$comment_body = $this->randomMachineName(8);
|
||||
$comment = entity_create('comment', array(
|
||||
'subject' => $comment_subject,
|
||||
'comment_body' => $comment_body,
|
||||
'entity_id' => $book_node->id(),
|
||||
'entity_type' => 'node',
|
||||
'field_name' => 'comment',
|
||||
'status' => CommentInterface::PUBLISHED,
|
||||
));
|
||||
$comment->save();
|
||||
|
||||
$commenting_user = $this->drupalCreateUser(array('access printer-friendly version', 'access comments', 'post comments'));
|
||||
$this->drupalLogin($commenting_user);
|
||||
|
||||
$this->drupalGet('node/' . $book_node->id());
|
||||
|
||||
$this->assertText($comment_subject, 'Comment subject found');
|
||||
$this->assertText($comment_body, 'Comment body found');
|
||||
$this->assertText(t('Add new comment'), 'Comment form found');
|
||||
$this->assertField('subject[0][value]', 'Comment form subject found');
|
||||
|
||||
$this->drupalGet('book/export/html/' . $book_node->id());
|
||||
|
||||
$this->assertText(t('Comments'), 'Comment thread found');
|
||||
$this->assertText($comment_subject, 'Comment subject found');
|
||||
$this->assertText($comment_body, 'Comment body found');
|
||||
|
||||
$this->assertNoText(t('Add new comment'), 'Comment form not found');
|
||||
$this->assertNoField('subject[0][value]', 'Comment form subject not found');
|
||||
}
|
||||
|
||||
}
|
138
core/modules/comment/src/Tests/CommentCSSTest.php
Normal file
138
core/modules/comment/src/Tests/CommentCSSTest.php
Normal file
|
@ -0,0 +1,138 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\CommentCSSTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests;
|
||||
|
||||
use Drupal\Core\Language\LanguageInterface;
|
||||
use Drupal\comment\CommentInterface;
|
||||
use Drupal\user\RoleInterface;
|
||||
|
||||
/**
|
||||
* Tests CSS classes on comments.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class CommentCSSTest extends CommentTestBase {
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
// Allow anonymous users to see comments.
|
||||
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, array(
|
||||
'access comments',
|
||||
'access content'
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests CSS classes on comments.
|
||||
*/
|
||||
function testCommentClasses() {
|
||||
// Create all permutations for comments, users, and nodes.
|
||||
$parameters = array(
|
||||
'node_uid' => array(0, $this->webUser->id()),
|
||||
'comment_uid' => array(0, $this->webUser->id(), $this->adminUser->id()),
|
||||
'comment_status' => array(CommentInterface::PUBLISHED, CommentInterface::NOT_PUBLISHED),
|
||||
'user' => array('anonymous', 'authenticated', 'admin'),
|
||||
);
|
||||
$permutations = $this->generatePermutations($parameters);
|
||||
|
||||
foreach ($permutations as $case) {
|
||||
// Create a new node.
|
||||
$node = $this->drupalCreateNode(array('type' => 'article', 'uid' => $case['node_uid']));
|
||||
|
||||
// Add a comment.
|
||||
/** @var \Drupal\comment\CommentInterface $comment */
|
||||
$comment = entity_create('comment', array(
|
||||
'entity_id' => $node->id(),
|
||||
'entity_type' => 'node',
|
||||
'field_name' => 'comment',
|
||||
'uid' => $case['comment_uid'],
|
||||
'status' => $case['comment_status'],
|
||||
'subject' => $this->randomMachineName(),
|
||||
'language' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
|
||||
'comment_body' => array(LanguageInterface::LANGCODE_NOT_SPECIFIED => array($this->randomMachineName())),
|
||||
));
|
||||
$comment->save();
|
||||
|
||||
// Adjust the current/viewing user.
|
||||
switch ($case['user']) {
|
||||
case 'anonymous':
|
||||
if ($this->loggedInUser) {
|
||||
$this->drupalLogout();
|
||||
}
|
||||
$case['user_uid'] = 0;
|
||||
break;
|
||||
|
||||
case 'authenticated':
|
||||
$this->drupalLogin($this->webUser);
|
||||
$case['user_uid'] = $this->webUser->id();
|
||||
break;
|
||||
|
||||
case 'admin':
|
||||
$this->drupalLogin($this->adminUser);
|
||||
$case['user_uid'] = $this->adminUser->id();
|
||||
break;
|
||||
}
|
||||
// Request the node with the comment.
|
||||
$this->drupalGet('node/' . $node->id());
|
||||
$settings = $this->getDrupalSettings();
|
||||
|
||||
// Verify the data-history-node-id attribute, which is necessary for the
|
||||
// by-viewer class and the "new" indicator, see below.
|
||||
$this->assertIdentical(1, count($this->xpath('//*[@data-history-node-id="' . $node->id() . '"]')), 'data-history-node-id attribute is set on node.');
|
||||
|
||||
// Verify classes if the comment is visible for the current user.
|
||||
if ($case['comment_status'] == CommentInterface::PUBLISHED || $case['user'] == 'admin') {
|
||||
// Verify the by-anonymous class.
|
||||
$comments = $this->xpath('//*[contains(@class, "comment") and contains(@class, "by-anonymous")]');
|
||||
if ($case['comment_uid'] == 0) {
|
||||
$this->assertTrue(count($comments) == 1, 'by-anonymous class found.');
|
||||
}
|
||||
else {
|
||||
$this->assertFalse(count($comments), 'by-anonymous class not found.');
|
||||
}
|
||||
|
||||
// Verify the by-node-author class.
|
||||
$comments = $this->xpath('//*[contains(@class, "comment") and contains(@class, "by-node-author")]');
|
||||
if ($case['comment_uid'] > 0 && $case['comment_uid'] == $case['node_uid']) {
|
||||
$this->assertTrue(count($comments) == 1, 'by-node-author class found.');
|
||||
}
|
||||
else {
|
||||
$this->assertFalse(count($comments), 'by-node-author class not found.');
|
||||
}
|
||||
|
||||
// Verify the data-comment-user-id attribute, which is used by the
|
||||
// drupal.comment-by-viewer library to add a by-viewer when the current
|
||||
// user (the viewer) was the author of the comment. We do this in Java-
|
||||
// Script to prevent breaking the render cache.
|
||||
$this->assertIdentical(1, count($this->xpath('//*[contains(@class, "comment") and @data-comment-user-id="' . $case['comment_uid'] . '"]')), 'data-comment-user-id attribute is set on comment.');
|
||||
$this->assertRaw(drupal_get_path('module', 'comment') . '/js/comment-by-viewer.js', 'drupal.comment-by-viewer library is present.');
|
||||
}
|
||||
|
||||
// Verify the unpublished class.
|
||||
$comments = $this->xpath('//*[contains(@class, "comment") and contains(@class, "unpublished")]');
|
||||
if ($case['comment_status'] == CommentInterface::NOT_PUBLISHED && $case['user'] == 'admin') {
|
||||
$this->assertTrue(count($comments) == 1, 'unpublished class found.');
|
||||
}
|
||||
else {
|
||||
$this->assertFalse(count($comments), 'unpublished class not found.');
|
||||
}
|
||||
|
||||
// Verify the data-comment-timestamp attribute, which is used by the
|
||||
// drupal.comment-new-indicator library to add a "new" indicator to each
|
||||
// comment that was created or changed after the last time the current
|
||||
// user read the corresponding node.
|
||||
if ($case['comment_status'] == CommentInterface::PUBLISHED || $case['user'] == 'admin') {
|
||||
$this->assertIdentical(1, count($this->xpath('//*[contains(@class, "comment")]/*[@data-comment-timestamp="' . $comment->getChangedTime() . '"]')), 'data-comment-timestamp attribute is set on comment');
|
||||
$expectedJS = ($case['user'] !== 'anonymous');
|
||||
$this->assertIdentical($expectedJS, isset($settings['ajaxPageState']['libraries']) && in_array('comment/drupal.comment-new-indicator', explode(',', $settings['ajaxPageState']['libraries'])), 'drupal.comment-new-indicator library is present.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
110
core/modules/comment/src/Tests/CommentCacheTagsTest.php
Normal file
110
core/modules/comment/src/Tests/CommentCacheTagsTest.php
Normal file
|
@ -0,0 +1,110 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\CommentCacheTagsTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests;
|
||||
|
||||
use Drupal\comment\CommentManagerInterface;
|
||||
use Drupal\Core\Entity\EntityInterface;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\system\Tests\Entity\EntityWithUriCacheTagsTestBase;
|
||||
use Drupal\user\Entity\Role;
|
||||
use Drupal\user\RoleInterface;
|
||||
|
||||
/**
|
||||
* Tests the Comment entity's cache tags.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class CommentCacheTagsTest extends EntityWithUriCacheTagsTestBase {
|
||||
|
||||
use CommentTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = array('comment');
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
// Give anonymous users permission to view comments, so that we can verify
|
||||
// the cache tags of cached versions of comment pages.
|
||||
$user_role = Role::load(RoleInterface::ANONYMOUS_ID);
|
||||
$user_role->grantPermission('access comments');
|
||||
$user_role->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function createEntity() {
|
||||
// Create a "bar" bundle for the "entity_test" entity type and create.
|
||||
$bundle = 'bar';
|
||||
entity_test_create_bundle($bundle, NULL, 'entity_test');
|
||||
|
||||
// Create a comment field on this bundle.
|
||||
$this->addDefaultCommentField('entity_test', 'bar', 'comment');
|
||||
|
||||
// Display comments in a flat list; threaded comments are not render cached.
|
||||
$field = FieldConfig::loadByName('entity_test', 'bar', 'comment');
|
||||
$field->setSetting('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT);
|
||||
$field->save();
|
||||
|
||||
// Create a "Camelids" test entity.
|
||||
$entity_test = entity_create('entity_test', array(
|
||||
'name' => 'Camelids',
|
||||
'type' => 'bar',
|
||||
));
|
||||
$entity_test->save();
|
||||
|
||||
// Create a "Llama" comment.
|
||||
$comment = entity_create('comment', array(
|
||||
'subject' => 'Llama',
|
||||
'comment_body' => array(
|
||||
'value' => 'The name "llama" was adopted by European settlers from native Peruvians.',
|
||||
'format' => 'plain_text',
|
||||
),
|
||||
'entity_id' => $entity_test->id(),
|
||||
'entity_type' => 'entity_test',
|
||||
'field_name' => 'comment',
|
||||
'status' => \Drupal\comment\CommentInterface::PUBLISHED,
|
||||
));
|
||||
$comment->save();
|
||||
|
||||
return $comment;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getAdditionalCacheContextsForEntity(EntityInterface $entity) {
|
||||
return [
|
||||
// Field access for the user picture rendered as part of the node that
|
||||
// this comment is created on.
|
||||
'user.permissions',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* Each comment must have a comment body, which always has a text format.
|
||||
*/
|
||||
protected function getAdditionalCacheTagsForEntity(EntityInterface $entity) {
|
||||
/** @var \Drupal\comment\CommentInterface $entity */
|
||||
return array(
|
||||
'config:filter.format.plain_text',
|
||||
'user:' . $entity->getOwnerId(),
|
||||
'user_view',
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,133 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\CommentDefaultFormatterCacheTagsTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests;
|
||||
|
||||
use Drupal\Core\Session\UserSession;
|
||||
use Drupal\comment\CommentInterface;
|
||||
use Drupal\system\Tests\Entity\EntityUnitTestBase;
|
||||
|
||||
/**
|
||||
* Tests the bubbling up of comment cache tags when using the Comment list
|
||||
* formatter on an entity.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class CommentDefaultFormatterCacheTagsTest extends EntityUnitTestBase {
|
||||
|
||||
use CommentTestTrait;
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('entity_test', 'comment');
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
// 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.
|
||||
$current_user = $this->container->get('current_user');
|
||||
$current_user->setAccount($this->createUser(array(), array('access comments')));
|
||||
|
||||
// Install tables and config needed to render comments.
|
||||
$this->installSchema('comment', array('comment_entity_statistics'));
|
||||
$this->installConfig(array('system', 'filter', 'comment'));
|
||||
|
||||
// Comment rendering generates links, so build the router.
|
||||
$this->installSchema('system', array('router'));
|
||||
$this->container->get('router.builder')->rebuild();
|
||||
|
||||
// Set up a field, so that the entity that'll be referenced bubbles up a
|
||||
// cache tag when rendering it entirely.
|
||||
$this->addDefaultCommentField('entity_test', 'entity_test');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the bubbling of cache tags.
|
||||
*/
|
||||
public function testCacheTags() {
|
||||
/** @var \Drupal\Core\Render\RendererInterface $renderer */
|
||||
$renderer = $this->container->get('renderer');
|
||||
|
||||
// Create the entity that will be commented upon.
|
||||
$commented_entity = entity_create('entity_test', array('name' => $this->randomMachineName()));
|
||||
$commented_entity->save();
|
||||
|
||||
// Verify cache tags on the rendered entity before it has comments.
|
||||
$build = \Drupal::entityManager()
|
||||
->getViewBuilder('entity_test')
|
||||
->view($commented_entity);
|
||||
$renderer->renderRoot($build);
|
||||
$expected_cache_tags = array(
|
||||
'entity_test_view',
|
||||
'entity_test:' . $commented_entity->id(),
|
||||
'comment_list',
|
||||
'config:core.entity_form_display.comment.comment.default',
|
||||
'config:field.field.comment.comment.comment_body',
|
||||
'config:field.field.entity_test.entity_test.comment',
|
||||
'config:field.storage.comment.comment_body',
|
||||
'config:user.settings',
|
||||
);
|
||||
sort($expected_cache_tags);
|
||||
$this->assertEqual($build['#cache']['tags'], $expected_cache_tags, 'The test entity has the expected cache tags before it has comments.');
|
||||
|
||||
// Create a comment on that entity. Comment loading requires that the uid
|
||||
// also exists in the {users} table.
|
||||
$user = $this->createUser();
|
||||
$user->save();
|
||||
$comment = entity_create('comment', array(
|
||||
'subject' => 'Llama',
|
||||
'comment_body' => array(
|
||||
'value' => 'Llamas are cool!',
|
||||
'format' => 'plain_text',
|
||||
),
|
||||
'entity_id' => $commented_entity->id(),
|
||||
'entity_type' => 'entity_test',
|
||||
'field_name' => 'comment',
|
||||
'comment_type' => 'comment',
|
||||
'status' => CommentInterface::PUBLISHED,
|
||||
'uid' => $user->id(),
|
||||
));
|
||||
$comment->save();
|
||||
|
||||
// Load commented entity so comment_count gets computed.
|
||||
// @todo Remove the $reset = TRUE parameter after
|
||||
// https://www.drupal.org/node/597236 lands. It's a temporary work-around.
|
||||
$commented_entity = entity_load('entity_test', $commented_entity->id(), TRUE);
|
||||
|
||||
// Verify cache tags on the rendered entity when it has comments.
|
||||
$build = \Drupal::entityManager()
|
||||
->getViewBuilder('entity_test')
|
||||
->view($commented_entity);
|
||||
$renderer->renderRoot($build);
|
||||
$expected_cache_tags = array(
|
||||
'entity_test_view',
|
||||
'entity_test:' . $commented_entity->id(),
|
||||
'comment_list',
|
||||
'comment_view',
|
||||
'comment:' . $comment->id(),
|
||||
'config:filter.format.plain_text',
|
||||
'user_view',
|
||||
'user:2',
|
||||
'config:core.entity_form_display.comment.comment.default',
|
||||
'config:field.field.comment.comment.comment_body',
|
||||
'config:field.field.entity_test.entity_test.comment',
|
||||
'config:field.storage.comment.comment_body',
|
||||
'config:user.settings',
|
||||
);
|
||||
sort($expected_cache_tags);
|
||||
$this->assertEqual($build['#cache']['tags'], $expected_cache_tags, 'The test entity has the expected cache tags when it has comments.');
|
||||
}
|
||||
|
||||
}
|
283
core/modules/comment/src/Tests/CommentFieldAccessTest.php
Normal file
283
core/modules/comment/src/Tests/CommentFieldAccessTest.php
Normal file
|
@ -0,0 +1,283 @@
|
|||
<?php
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\CommentFieldAccessTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests;
|
||||
|
||||
use Drupal\comment\Entity\Comment;
|
||||
use Drupal\comment\Entity\CommentType;
|
||||
use Drupal\Component\Utility\SafeMarkup;
|
||||
use Drupal\Core\Session\AnonymousUserSession;
|
||||
use Drupal\entity_test\Entity\EntityTest;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\simpletest\TestBase;
|
||||
use Drupal\system\Tests\Entity\EntityUnitTestBase;
|
||||
use Drupal\user\Entity\Role;
|
||||
use Drupal\user\RoleInterface;
|
||||
|
||||
/**
|
||||
* Tests comment field level access.
|
||||
*
|
||||
* @group comment
|
||||
* @group Access
|
||||
*/
|
||||
class CommentFieldAccessTest extends EntityUnitTestBase {
|
||||
|
||||
use CommentTestTrait;
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('comment', 'entity_test', 'user');
|
||||
|
||||
/**
|
||||
* Fields that only users with administer comments permissions can change.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $administrativeFields = array(
|
||||
'uid',
|
||||
'status',
|
||||
'created',
|
||||
);
|
||||
|
||||
/**
|
||||
* These fields are automatically managed and can not be changed by any user.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $readOnlyFields = array(
|
||||
'changed',
|
||||
'hostname',
|
||||
'uuid',
|
||||
'cid',
|
||||
'thread',
|
||||
'comment_type',
|
||||
'pid',
|
||||
'entity_id',
|
||||
'entity_type',
|
||||
'field_name',
|
||||
);
|
||||
|
||||
/**
|
||||
* These fields can only be edited by the admin or anonymous users if allowed.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $contactFields = array(
|
||||
'name',
|
||||
'mail',
|
||||
'homepage',
|
||||
);
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->installConfig(array('user', 'comment'));
|
||||
$this->installSchema('comment', array('comment_entity_statistics'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test permissions on comment fields.
|
||||
*/
|
||||
public function testAccessToAdministrativeFields() {
|
||||
// Create a comment type.
|
||||
$comment_type = CommentType::create([
|
||||
'id' => 'comment',
|
||||
'label' => 'Default comments',
|
||||
'description' => 'Default comment field',
|
||||
'target_entity_type_id' => 'entity_test',
|
||||
]);
|
||||
$comment_type->save();
|
||||
|
||||
// Create a comment against a test entity.
|
||||
$host = EntityTest::create();
|
||||
$host->save();
|
||||
|
||||
// An administrator user. No user exists yet, ensure that the first user
|
||||
// does not have UID 1.
|
||||
$comment_admin_user = $this->createUser(['uid' => 2, 'name' => 'admin'], [
|
||||
'administer comments',
|
||||
'access comments',
|
||||
]);
|
||||
|
||||
// Two comment enabled users, one with edit access.
|
||||
$comment_enabled_user = $this->createUser(['name' => 'enabled'], [
|
||||
'post comments',
|
||||
'skip comment approval',
|
||||
'edit own comments',
|
||||
'access comments',
|
||||
]);
|
||||
$comment_no_edit_user = $this->createUser(['name' => 'no edit'], [
|
||||
'post comments',
|
||||
'skip comment approval',
|
||||
'access comments',
|
||||
]);
|
||||
|
||||
// An unprivileged user.
|
||||
$comment_disabled_user = $this->createUser(['name' => 'disabled'], ['access content']);
|
||||
|
||||
$role = Role::load(RoleInterface::ANONYMOUS_ID);
|
||||
$role->grantPermission('post comments')
|
||||
->save();
|
||||
|
||||
$anonymous_user = new AnonymousUserSession();
|
||||
|
||||
// Add two fields.
|
||||
$this->addDefaultCommentField('entity_test', 'entity_test', 'comment');
|
||||
$this->addDefaultCommentField('entity_test', 'entity_test', 'comment_other');
|
||||
|
||||
// Change the second field's anonymous contact setting.
|
||||
$instance = FieldConfig::loadByName('entity_test', 'entity_test', 'comment_other');
|
||||
// Default is 'May not contact', for this field - they may contact.
|
||||
$instance->setSetting('anonymous', COMMENT_ANONYMOUS_MAY_CONTACT);
|
||||
$instance->save();
|
||||
|
||||
// Create three "Comments". One is owned by our edit-enabled user.
|
||||
$comment1 = Comment::create([
|
||||
'entity_type' => 'entity_test',
|
||||
'name' => 'Tony',
|
||||
'hostname' => 'magic.example.com',
|
||||
'mail' => 'tonythemagicalpony@example.com',
|
||||
'subject' => 'Bruce the Mesopotamian moose',
|
||||
'entity_id' => $host->id(),
|
||||
'comment_type' => 'comment',
|
||||
'field_name' => 'comment',
|
||||
'pid' => 0,
|
||||
'uid' => 0,
|
||||
'status' => 1,
|
||||
]);
|
||||
$comment1->save();
|
||||
$comment2 = Comment::create([
|
||||
'entity_type' => 'entity_test',
|
||||
'hostname' => 'magic.example.com',
|
||||
'subject' => 'Brian the messed up lion',
|
||||
'entity_id' => $host->id(),
|
||||
'comment_type' => 'comment',
|
||||
'field_name' => 'comment',
|
||||
'status' => 1,
|
||||
'pid' => 0,
|
||||
'uid' => $comment_enabled_user->id(),
|
||||
]);
|
||||
$comment2->save();
|
||||
$comment3 = Comment::create([
|
||||
'entity_type' => 'entity_test',
|
||||
'hostname' => 'magic.example.com',
|
||||
// Unpublished.
|
||||
'status' => 0,
|
||||
'subject' => 'Gail the minky whale',
|
||||
'entity_id' => $host->id(),
|
||||
'comment_type' => 'comment',
|
||||
'field_name' => 'comment_other',
|
||||
'pid' => $comment2->id(),
|
||||
'uid' => $comment_no_edit_user->id(),
|
||||
]);
|
||||
$comment3->save();
|
||||
// Note we intentionally don't save this comment so it remains 'new'.
|
||||
$comment4 = Comment::create([
|
||||
'entity_type' => 'entity_test',
|
||||
'hostname' => 'magic.example.com',
|
||||
// Unpublished.
|
||||
'status' => 0,
|
||||
'subject' => 'Daniel the Cocker-Spaniel',
|
||||
'entity_id' => $host->id(),
|
||||
'comment_type' => 'comment',
|
||||
'field_name' => 'comment_other',
|
||||
'pid' => 0,
|
||||
'uid' => $anonymous_user->id(),
|
||||
]);
|
||||
|
||||
// Generate permutations.
|
||||
$combinations = [
|
||||
'comment' => [$comment1, $comment2, $comment3, $comment4],
|
||||
'user' => [$comment_admin_user, $comment_enabled_user, $comment_no_edit_user, $comment_disabled_user, $anonymous_user]
|
||||
];
|
||||
$permutations = TestBase::generatePermutations($combinations);
|
||||
|
||||
// Check access to administrative fields.
|
||||
foreach ($this->administrativeFields as $field) {
|
||||
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', [
|
||||
'@user' => $set['user']->getUsername(),
|
||||
'!state' => $may_update ? 'can' : 'cannot',
|
||||
'@comment' => $set['comment']->getSubject(),
|
||||
'!field' => $field,
|
||||
]));
|
||||
$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',
|
||||
'@comment' => $set['comment']->getSubject(),
|
||||
'!field' => $field,
|
||||
]));
|
||||
}
|
||||
}
|
||||
|
||||
// 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', [
|
||||
'@user' => $set['user']->getUsername(),
|
||||
'!state' => $may_update ? 'can' : 'cannot',
|
||||
'@comment' => $set['comment']->getSubject(),
|
||||
]));
|
||||
}
|
||||
|
||||
// Check read-only fields.
|
||||
foreach ($this->readOnlyFields as $field) {
|
||||
// Check view operation.
|
||||
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', [
|
||||
'@user' => $set['user']->getUsername(),
|
||||
'!state' => $may_view ? 'can' : 'cannot',
|
||||
'@comment' => $set['comment']->getSubject(),
|
||||
'!field' => $field,
|
||||
]));
|
||||
$this->assertFalse($may_update, SafeMarkup::format('User @user !state update field !field on comment @comment', [
|
||||
'@user' => $set['user']->getUsername(),
|
||||
'!state' => $may_update ? 'can' : 'cannot',
|
||||
'@comment' => $set['comment']->getSubject(),
|
||||
'!field' => $field,
|
||||
]));
|
||||
}
|
||||
}
|
||||
|
||||
// Check contact fields.
|
||||
foreach ($this->contactFields as $field) {
|
||||
// Check view operation.
|
||||
foreach ($permutations as $set) {
|
||||
$may_update = $set['comment']->{$field}->access('edit', $set['user']);
|
||||
// To edit the 'mail' or 'name' field, either the user has the
|
||||
// "administer comments" permissions or the user is anonymous and
|
||||
// adding a new comment using a field that allows contact details.
|
||||
$this->assertEqual($may_update, $set['user']->hasPermission('administer comments') || (
|
||||
$set['user']->isAnonymous() &&
|
||||
$set['comment']->isNew() &&
|
||||
$set['user']->hasPermission('post comments') &&
|
||||
$set['comment']->getFieldName() == 'comment_other'
|
||||
), SafeMarkup::format('User @user !state update field !field on comment @comment', [
|
||||
'@user' => $set['user']->getUsername(),
|
||||
'!state' => $may_update ? 'can' : 'cannot',
|
||||
'@comment' => $set['comment']->getSubject(),
|
||||
'!field' => $field,
|
||||
]));
|
||||
}
|
||||
}
|
||||
foreach ($permutations as $set) {
|
||||
// Check no view-access to mail field for other than admin.
|
||||
$may_view = $set['comment']->mail->access('view', $set['user']);
|
||||
$this->assertEqual($may_view, $set['user']->hasPermission('administer comments'));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
191
core/modules/comment/src/Tests/CommentFieldsTest.php
Normal file
191
core/modules/comment/src/Tests/CommentFieldsTest.php
Normal file
|
@ -0,0 +1,191 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\CommentFieldsTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests;
|
||||
|
||||
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
|
||||
use Drupal\field\Entity\FieldStorageConfig;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\comment\Entity\CommentType;
|
||||
|
||||
/**
|
||||
* Tests fields on comments.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class CommentFieldsTest extends CommentTestBase {
|
||||
|
||||
/**
|
||||
* Install the field UI.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('field_ui');
|
||||
|
||||
/**
|
||||
* Tests that the default 'comment_body' field is correctly added.
|
||||
*/
|
||||
function testCommentDefaultFields() {
|
||||
// Do not make assumptions on default node types created by the test
|
||||
// installation profile, and create our own.
|
||||
$this->drupalCreateContentType(array('type' => 'test_node_type'));
|
||||
$this->addDefaultCommentField('node', 'test_node_type');
|
||||
|
||||
// Check that the 'comment_body' field is present on the comment bundle.
|
||||
$field = FieldConfig::loadByName('comment', 'comment', 'comment_body');
|
||||
$this->assertTrue(!empty($field), 'The comment_body field is added when a comment bundle is created');
|
||||
|
||||
$field->delete();
|
||||
|
||||
// Check that the 'comment_body' field is not deleted since it is persisted
|
||||
// even if it has no fields.
|
||||
$field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
|
||||
$this->assertTrue($field_storage, 'The comment_body field storage was not deleted');
|
||||
|
||||
// Create a new content type.
|
||||
$type_name = 'test_node_type_2';
|
||||
$this->drupalCreateContentType(array('type' => $type_name));
|
||||
$this->addDefaultCommentField('node', $type_name);
|
||||
|
||||
// Check that the 'comment_body' field exists and has an instance on the
|
||||
// new comment bundle.
|
||||
$field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
|
||||
$this->assertTrue($field_storage, 'The comment_body field exists');
|
||||
$field = FieldConfig::loadByName('comment', 'comment', 'comment_body');
|
||||
$this->assertTrue(isset($field), format_string('The comment_body field is present for comments on type @type', array('@type' => $type_name)));
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that you can remove a comment field.
|
||||
*/
|
||||
public function testCommentFieldDelete() {
|
||||
$this->drupalCreateContentType(array('type' => 'test_node_type'));
|
||||
$this->addDefaultCommentField('node', 'test_node_type');
|
||||
// We want to test the handling of removing the primary comment field, so we
|
||||
// ensure there is at least one other comment field attached to a node type
|
||||
// so that comment_entity_load() runs for nodes.
|
||||
$this->addDefaultCommentField('node', 'test_node_type', 'comment2');
|
||||
|
||||
// Create a sample node.
|
||||
$node = $this->drupalCreateNode(array(
|
||||
'title' => 'Baloney',
|
||||
'type' => 'test_node_type',
|
||||
));
|
||||
|
||||
$this->drupalLogin($this->webUser);
|
||||
|
||||
$this->drupalGet('node/' . $node->nid->value);
|
||||
$elements = $this->cssSelect('.field-type-comment');
|
||||
$this->assertEqual(2, count($elements), 'There are two comment fields on the node.');
|
||||
|
||||
// Delete the first comment field.
|
||||
FieldStorageConfig::loadByName('node', 'comment')->delete();
|
||||
$this->drupalGet('node/' . $node->nid->value);
|
||||
$elements = $this->cssSelect('.field-type-comment');
|
||||
$this->assertEqual(1, count($elements), 'There is one comment field on the node.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests creating a comment field through the interface.
|
||||
*/
|
||||
public function testCommentFieldCreate() {
|
||||
// Create user who can administer user fields.
|
||||
$user = $this->drupalCreateUser(array(
|
||||
'administer user fields',
|
||||
));
|
||||
$this->drupalLogin($user);
|
||||
|
||||
// Create comment field in account settings.
|
||||
$edit = array(
|
||||
'new_storage_type' => 'comment',
|
||||
'label' => 'User comment',
|
||||
'field_name' => 'user_comment',
|
||||
);
|
||||
$this->drupalPostForm('admin/config/people/accounts/fields/add-field', $edit, 'Save and continue');
|
||||
|
||||
// Try to save the comment field without selecting a comment type.
|
||||
$edit = array();
|
||||
$this->drupalPostForm('admin/config/people/accounts/fields/user.user.field_user_comment/storage', $edit, t('Save field settings'));
|
||||
// We should get an error message.
|
||||
$this->assertText(t('An illegal choice has been detected. Please contact the site administrator.'));
|
||||
|
||||
// Create a comment type for users.
|
||||
$bundle = CommentType::create(array(
|
||||
'id' => 'user_comment_type',
|
||||
'label' => 'user_comment_type',
|
||||
'description' => '',
|
||||
'target_entity_type_id' => 'user',
|
||||
));
|
||||
$bundle->save();
|
||||
|
||||
// Select a comment type and try to save again.
|
||||
$edit = array(
|
||||
'settings[comment_type]' => 'user_comment_type',
|
||||
);
|
||||
$this->drupalPostForm('admin/config/people/accounts/fields/user.user.field_user_comment/storage', $edit, t('Save field settings'));
|
||||
// We shouldn't get an error message.
|
||||
$this->assertNoText(t('An illegal choice has been detected. Please contact the site administrator.'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that comment module works when installed after a content module.
|
||||
*/
|
||||
function testCommentInstallAfterContentModule() {
|
||||
// Create a user to do module administration.
|
||||
$this->adminUser = $this->drupalCreateUser(array('access administration pages', 'administer modules'));
|
||||
$this->drupalLogin($this->adminUser);
|
||||
|
||||
// Drop default comment field added in CommentTestBase::setup().
|
||||
FieldStorageConfig::loadByName('node', 'comment')->delete();
|
||||
if ($field_storage = FieldStorageConfig::loadByName('node', 'comment_forum')) {
|
||||
$field_storage->delete();
|
||||
}
|
||||
|
||||
// Purge field data now to allow comment module to be uninstalled once the
|
||||
// field has been deleted.
|
||||
field_purge_batch(10);
|
||||
|
||||
// Uninstall the comment module.
|
||||
$edit = array();
|
||||
$edit['uninstall[comment]'] = TRUE;
|
||||
$this->drupalPostForm('admin/modules/uninstall', $edit, t('Uninstall'));
|
||||
$this->drupalPostForm(NULL, array(), t('Uninstall'));
|
||||
$this->rebuildContainer();
|
||||
$this->assertFalse($this->container->get('module_handler')->moduleExists('comment'), 'Comment module uninstalled.');
|
||||
|
||||
// Install core content type module (book).
|
||||
$edit = array();
|
||||
$edit['modules[Core][book][enable]'] = 'book';
|
||||
$this->drupalPostForm('admin/modules', $edit, t('Save configuration'));
|
||||
|
||||
// Now install the comment module.
|
||||
$edit = array();
|
||||
$edit['modules[Core][comment][enable]'] = 'comment';
|
||||
$this->drupalPostForm('admin/modules', $edit, t('Save configuration'));
|
||||
$this->rebuildContainer();
|
||||
$this->assertTrue($this->container->get('module_handler')->moduleExists('comment'), 'Comment module enabled.');
|
||||
|
||||
// Create nodes of each type.
|
||||
$this->addDefaultCommentField('node', 'book');
|
||||
$book_node = $this->drupalCreateNode(array('type' => 'book'));
|
||||
|
||||
$this->drupalLogout();
|
||||
|
||||
// Try to post a comment on each node. A failure will be triggered if the
|
||||
// comment body is missing on one of these forms, due to postComment()
|
||||
// asserting that the body is actually posted correctly.
|
||||
$this->webUser = $this->drupalCreateUser(array('access content', 'access comments', 'post comments', 'skip comment approval'));
|
||||
$this->drupalLogin($this->webUser);
|
||||
$this->postComment($book_node, $this->randomMachineName(), $this->randomMachineName());
|
||||
}
|
||||
|
||||
}
|
284
core/modules/comment/src/Tests/CommentInterfaceTest.php
Normal file
284
core/modules/comment/src/Tests/CommentInterfaceTest.php
Normal file
|
@ -0,0 +1,284 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\CommentInterfaceTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests;
|
||||
|
||||
use Drupal\comment\CommentManagerInterface;
|
||||
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
|
||||
use Drupal\comment\Entity\Comment;
|
||||
use Drupal\user\RoleInterface;
|
||||
use Drupal\filter\Entity\FilterFormat;
|
||||
|
||||
/**
|
||||
* Tests comment user interfaces.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class CommentInterfaceTest extends CommentTestBase {
|
||||
|
||||
/**
|
||||
* Set up comments to have subject and preview disabled.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
$this->drupalLogin($this->adminUser);
|
||||
$this->setCommentPreview(DRUPAL_DISABLED);
|
||||
$this->setCommentForm(TRUE);
|
||||
$this->setCommentSubject(FALSE);
|
||||
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
|
||||
$this->drupalLogout();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the comment interface.
|
||||
*/
|
||||
public function testCommentInterface() {
|
||||
|
||||
// Post comment #1 without subject or preview.
|
||||
$this->drupalLogin($this->webUser);
|
||||
$comment_text = $this->randomMachineName();
|
||||
$comment = $this->postComment($this->node, $comment_text);
|
||||
$this->assertTrue($this->commentExists($comment), 'Comment found.');
|
||||
|
||||
// Set comments to have subject and preview to required.
|
||||
$this->drupalLogout();
|
||||
$this->drupalLogin($this->adminUser);
|
||||
$this->setCommentSubject(TRUE);
|
||||
$this->setCommentPreview(DRUPAL_REQUIRED);
|
||||
$this->drupalLogout();
|
||||
|
||||
// Create comment #2 that allows subject and requires preview.
|
||||
$this->drupalLogin($this->webUser);
|
||||
$subject_text = $this->randomMachineName();
|
||||
$comment_text = $this->randomMachineName();
|
||||
$comment = $this->postComment($this->node, $comment_text, $subject_text, TRUE);
|
||||
$this->assertTrue($this->commentExists($comment), 'Comment found.');
|
||||
|
||||
// Comment as anonymous with preview required.
|
||||
$this->drupalLogout();
|
||||
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, array('access content', 'access comments', 'post comments', 'skip comment approval'));
|
||||
$anonymous_comment = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
|
||||
$this->assertTrue($this->commentExists($anonymous_comment), 'Comment found.');
|
||||
$anonymous_comment->delete();
|
||||
|
||||
// Check comment display.
|
||||
$this->drupalLogin($this->webUser);
|
||||
$this->drupalGet('node/' . $this->node->id());
|
||||
$this->assertText($subject_text, 'Individual comment subject found.');
|
||||
$this->assertText($comment_text, 'Individual comment body found.');
|
||||
|
||||
// Set comments to have subject and preview to optional.
|
||||
$this->drupalLogout();
|
||||
$this->drupalLogin($this->adminUser);
|
||||
$this->setCommentSubject(TRUE);
|
||||
$this->setCommentPreview(DRUPAL_OPTIONAL);
|
||||
|
||||
$this->drupalGet('comment/' . $comment->id() . '/edit');
|
||||
$this->assertTitle(t('Edit comment @title | Drupal', array(
|
||||
'@title' => $comment->getSubject(),
|
||||
)));
|
||||
|
||||
// Test changing the comment author to "Anonymous".
|
||||
$comment = $this->postComment(NULL, $comment->comment_body->value, $comment->getSubject(), array('name' => ''));
|
||||
$this->assertTrue($comment->getAuthorName() == t('Anonymous') && $comment->getOwnerId() == 0, 'Comment author successfully changed to anonymous.');
|
||||
|
||||
// Test changing the comment author to an unverified user.
|
||||
$random_name = $this->randomMachineName();
|
||||
$this->drupalGet('comment/' . $comment->id() . '/edit');
|
||||
$comment = $this->postComment(NULL, $comment->comment_body->value, $comment->getSubject(), array('name' => $random_name));
|
||||
$this->drupalGet('node/' . $this->node->id());
|
||||
$this->assertText($random_name . ' (' . t('not verified') . ')', 'Comment author successfully changed to an unverified user.');
|
||||
|
||||
// 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()));
|
||||
$this->assertTrue($comment->getAuthorName() == $this->webUser->getUsername() && $comment->getOwnerId() == $this->webUser->id(), 'Comment author successfully changed to a registered user.');
|
||||
|
||||
$this->drupalLogout();
|
||||
|
||||
// Reply to comment #2 creating comment #3 with optional preview and no
|
||||
// subject though field enabled.
|
||||
$this->drupalLogin($this->webUser);
|
||||
// Deliberately use the wrong url to test
|
||||
// \Drupal\comment\Controller\CommentController::redirectNode().
|
||||
$this->drupalGet('comment/' . $this->node->id() . '/reply');
|
||||
// Verify we were correctly redirected.
|
||||
$this->assertUrl(\Drupal::url('comment.reply', array('entity_type' => 'node', 'entity' => $this->node->id(), 'field_name' => 'comment'), array('absolute' => TRUE)));
|
||||
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment->id());
|
||||
$this->assertText($subject_text, 'Individual comment-reply subject found.');
|
||||
$this->assertText($comment_text, 'Individual comment-reply body found.');
|
||||
$reply = $this->postComment(NULL, $this->randomMachineName(), '', TRUE);
|
||||
$reply_loaded = Comment::load($reply->id());
|
||||
$this->assertTrue($this->commentExists($reply, TRUE), 'Reply found.');
|
||||
$this->assertEqual($comment->id(), $reply_loaded->getParentComment()->id(), 'Pid of a reply to a comment is set correctly.');
|
||||
// Check the thread of reply grows correctly.
|
||||
$this->assertEqual(rtrim($comment->getThread(), '/') . '.00/', $reply_loaded->getThread());
|
||||
|
||||
// Second reply to comment #2 creating comment #4.
|
||||
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment->id());
|
||||
$this->assertText($comment->getSubject(), 'Individual comment-reply subject found.');
|
||||
$this->assertText($comment->comment_body->value, 'Individual comment-reply body found.');
|
||||
$reply = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
|
||||
$reply_loaded = Comment::load($reply->id());
|
||||
$this->assertTrue($this->commentExists($reply, TRUE), 'Second reply found.');
|
||||
// Check the thread of second reply grows correctly.
|
||||
$this->assertEqual(rtrim($comment->getThread(), '/') . '.01/', $reply_loaded->getThread());
|
||||
|
||||
// Reply to comment #4 creating comment #5.
|
||||
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $reply_loaded->id());
|
||||
$this->assertText($reply_loaded->getSubject(), 'Individual comment-reply subject found.');
|
||||
$this->assertText($reply_loaded->comment_body->value, 'Individual comment-reply body found.');
|
||||
$reply = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
|
||||
$reply_loaded = Comment::load($reply->id());
|
||||
$this->assertTrue($this->commentExists($reply, TRUE), 'Second reply found.');
|
||||
// Check the thread of reply to second reply grows correctly.
|
||||
$this->assertEqual(rtrim($comment->getThread(), '/') . '.01.00/', $reply_loaded->getThread());
|
||||
|
||||
// Edit reply.
|
||||
$this->drupalGet('comment/' . $reply->id() . '/edit');
|
||||
$reply = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
|
||||
$this->assertTrue($this->commentExists($reply, TRUE), 'Modified reply found.');
|
||||
|
||||
// Confirm a new comment is posted to the correct page.
|
||||
$this->setCommentsPerPage(2);
|
||||
$comment_new_page = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
|
||||
$this->assertTrue($this->commentExists($comment_new_page), 'Page one exists. %s');
|
||||
$this->drupalGet('node/' . $this->node->id(), array('query' => array('page' => 2)));
|
||||
$this->assertTrue($this->commentExists($reply, TRUE), 'Page two exists. %s');
|
||||
$this->setCommentsPerPage(50);
|
||||
|
||||
// Attempt to reply to an unpublished comment.
|
||||
$reply_loaded->setPublished(FALSE);
|
||||
$reply_loaded->save();
|
||||
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $reply_loaded->id());
|
||||
$this->assertResponse(403);
|
||||
|
||||
// Attempt to post to node with comments disabled.
|
||||
$this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'comment' => array(array('status' => CommentItemInterface::HIDDEN))));
|
||||
$this->assertTrue($this->node, 'Article node created.');
|
||||
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
|
||||
$this->assertResponse(403);
|
||||
$this->assertNoField('edit-comment', 'Comment body field found.');
|
||||
|
||||
// Attempt to post to node with read-only comments.
|
||||
$this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'comment' => array(array('status' => CommentItemInterface::CLOSED))));
|
||||
$this->assertTrue($this->node, 'Article node created.');
|
||||
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
|
||||
$this->assertResponse(403);
|
||||
$this->assertNoField('edit-comment', 'Comment body field found.');
|
||||
|
||||
// Attempt to post to node with comments enabled (check field names etc).
|
||||
$this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'comment' => array(array('status' => CommentItemInterface::OPEN))));
|
||||
$this->assertTrue($this->node, 'Article node created.');
|
||||
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
|
||||
$this->assertNoText('This discussion is closed', 'Posting to node with comments enabled');
|
||||
$this->assertField('edit-comment-body-0-value', 'Comment body field found.');
|
||||
|
||||
// Delete comment and make sure that reply is also removed.
|
||||
$this->drupalLogout();
|
||||
$this->drupalLogin($this->adminUser);
|
||||
$this->deleteComment($comment);
|
||||
$this->deleteComment($comment_new_page);
|
||||
|
||||
$this->drupalGet('node/' . $this->node->id());
|
||||
$this->assertFalse($this->commentExists($comment), 'Comment not found.');
|
||||
$this->assertFalse($this->commentExists($reply, TRUE), 'Reply not found.');
|
||||
|
||||
// Enabled comment form on node page.
|
||||
$this->drupalLogin($this->adminUser);
|
||||
$this->setCommentForm(TRUE);
|
||||
$this->drupalLogout();
|
||||
|
||||
// Submit comment through node form.
|
||||
$this->drupalLogin($this->webUser);
|
||||
$this->drupalGet('node/' . $this->node->id());
|
||||
$form_comment = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
|
||||
$this->assertTrue($this->commentExists($form_comment), 'Form comment found.');
|
||||
|
||||
// Disable comment form on node page.
|
||||
$this->drupalLogout();
|
||||
$this->drupalLogin($this->adminUser);
|
||||
$this->setCommentForm(FALSE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the subject is automatically filled if disabled or left blank.
|
||||
*
|
||||
* When the subject field is blank or disabled, the first 29 characters of the
|
||||
* comment body are used for the subject. If this would break within a word,
|
||||
* then the break is put at the previous word boundary instead.
|
||||
*/
|
||||
public function testAutoFilledSubject() {
|
||||
$this->drupalLogin($this->webUser);
|
||||
$this->drupalGet('node/' . $this->node->id());
|
||||
|
||||
// Break when there is a word boundary before 29 characters.
|
||||
$body_text = 'Lorem ipsum Lorem ipsum Loreming ipsum Lorem ipsum';
|
||||
$comment1 = $this->postComment(NULL, $body_text, '', TRUE);
|
||||
$this->assertTrue($this->commentExists($comment1), 'Form comment found.');
|
||||
$this->assertEqual('Lorem ipsum Lorem ipsum…', $comment1->getSubject());
|
||||
|
||||
// Break at 29 characters where there's no boundary before that.
|
||||
$body_text2 = 'LoremipsumloremipsumLoremingipsumLoremipsum';
|
||||
$comment2 = $this->postComment(NULL, $body_text2, '', TRUE);
|
||||
$this->assertEqual('LoremipsumloremipsumLoreming…', $comment2->getSubject());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that automatic subject is correctly created from HTML comment text.
|
||||
*
|
||||
* This is the same test as in CommentInterfaceTest::testAutoFilledSubject()
|
||||
* with the additional check that HTML is stripped appropriately prior to
|
||||
* character-counting.
|
||||
*/
|
||||
public function testAutoFilledHtmlSubject() {
|
||||
// Set up two default (i.e. filtered HTML) input formats, because then we
|
||||
// can select one of them. Then create a user that can use these formats,
|
||||
// log the user in, and then GET the node page on which to test the
|
||||
// comments.
|
||||
$filtered_html_format = FilterFormat::create(array(
|
||||
'format' => 'filtered_html',
|
||||
'name' => 'Filtered HTML',
|
||||
));
|
||||
$filtered_html_format->save();
|
||||
$full_html_format = FilterFormat::create(array(
|
||||
'format' => 'full_html',
|
||||
'name' => 'Full HTML',
|
||||
));
|
||||
$full_html_format->save();
|
||||
$html_user = $this->drupalCreateUser(array(
|
||||
'access comments',
|
||||
'post comments',
|
||||
'edit own comments',
|
||||
'skip comment approval',
|
||||
'access content',
|
||||
$filtered_html_format->getPermissionName(),
|
||||
$full_html_format->getPermissionName(),
|
||||
));
|
||||
$this->drupalLogin($html_user);
|
||||
$this->drupalGet('node/' . $this->node->id());
|
||||
|
||||
// HTML should not be included in the character count.
|
||||
$body_text1 = '<span></span><strong> </strong><span> </span><strong></strong>Hello World<br />';
|
||||
$edit1 = array(
|
||||
'comment_body[0][value]' => $body_text1,
|
||||
'comment_body[0][format]' => 'filtered_html',
|
||||
);
|
||||
$this->drupalPostForm(NULL, $edit1, t('Save'));
|
||||
$this->assertEqual('Hello World', Comment::load(1)->getSubject());
|
||||
|
||||
// If there's nothing other than HTML, the subject should be '(No subject)'.
|
||||
$body_text2 = '<span></span><strong> </strong><span> </span><strong></strong> <br />';
|
||||
$edit2 = array(
|
||||
'comment_body[0][value]' => $body_text2,
|
||||
'comment_body[0][format]' => 'filtered_html',
|
||||
);
|
||||
$this->drupalPostForm(NULL, $edit2, t('Save'));
|
||||
$this->assertEqual('(No subject)', Comment::load(2)->getSubject());
|
||||
}
|
||||
|
||||
}
|
65
core/modules/comment/src/Tests/CommentItemTest.php
Normal file
65
core/modules/comment/src/Tests/CommentItemTest.php
Normal file
|
@ -0,0 +1,65 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\CommentItemTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests;
|
||||
|
||||
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\field\Tests\FieldUnitTestBase;
|
||||
|
||||
/**
|
||||
* Tests the new entity API for the comment field type.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class CommentItemTest extends FieldUnitTestBase {
|
||||
|
||||
use CommentTestTrait;
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = ['comment', 'entity_test', 'user'];
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->installSchema('comment', ['comment_entity_statistics']);
|
||||
$this->installConfig(['comment']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests using entity fields of the comment field type.
|
||||
*/
|
||||
public function testCommentItem() {
|
||||
$this->addDefaultCommentField('entity_test', 'entity_test', 'comment');
|
||||
|
||||
// Verify entity creation.
|
||||
$entity = entity_create('entity_test');
|
||||
$entity->name->value = $this->randomMachineName();
|
||||
$entity->save();
|
||||
|
||||
// Verify entity has been created properly.
|
||||
$id = $entity->id();
|
||||
$entity = entity_load('entity_test', $id, TRUE);
|
||||
$this->assertTrue($entity->comment instanceof FieldItemListInterface, 'Field implements interface.');
|
||||
$this->assertTrue($entity->comment[0] instanceof CommentItemInterface, 'Field item implements interface.');
|
||||
|
||||
// Test sample item generation.
|
||||
/** @var \Drupal\entity_test\Entity\EntityTest $entity */
|
||||
$entity = entity_create('entity_test');
|
||||
$entity->comment->generateSampleItems();
|
||||
$this->entityValidateAndSave($entity);
|
||||
$this->assertTrue(in_array($entity->get('comment')->status, [
|
||||
CommentItemInterface::HIDDEN,
|
||||
CommentItemInterface::CLOSED,
|
||||
CommentItemInterface::OPEN,
|
||||
]), 'Comment status value in defined range');
|
||||
}
|
||||
|
||||
}
|
140
core/modules/comment/src/Tests/CommentLanguageTest.php
Normal file
140
core/modules/comment/src/Tests/CommentLanguageTest.php
Normal file
|
@ -0,0 +1,140 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\CommentLanguageTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests;
|
||||
|
||||
use Drupal\comment\Entity\Comment;
|
||||
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
|
||||
use Drupal\field\Entity\FieldStorageConfig;
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
|
||||
/**
|
||||
* Tests for comment language.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class CommentLanguageTest extends WebTestBase {
|
||||
|
||||
use CommentTestTrait;
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* We also use the language_test module here to be able to turn on content
|
||||
* language negotiation. Drupal core does not provide a way in itself to do
|
||||
* that.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('node', 'language', 'language_test', 'comment_test');
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article'));
|
||||
|
||||
// Create and login user.
|
||||
$admin_user = $this->drupalCreateUser(array('administer site configuration', 'administer languages', 'access administration pages', 'administer content types', 'administer comments', 'create article content', 'access comments', 'post comments', 'skip comment approval'));
|
||||
$this->drupalLogin($admin_user);
|
||||
|
||||
// Add language.
|
||||
$edit = array('predefined_langcode' => 'fr');
|
||||
$this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
|
||||
|
||||
// Set "Article" content type to use multilingual support.
|
||||
$edit = array('language_configuration[language_alterable]' => TRUE);
|
||||
$this->drupalPostForm('admin/structure/types/manage/article', $edit, t('Save content type'));
|
||||
|
||||
// Enable content language negotiation UI.
|
||||
\Drupal::state()->set('language_test.content_language_type', TRUE);
|
||||
|
||||
// Set interface language detection to user and content language detection
|
||||
// to URL. Disable inheritance from interface language to ensure content
|
||||
// language will fall back to the default language if no URL language can be
|
||||
// detected.
|
||||
$edit = array(
|
||||
'language_interface[enabled][language-user]' => TRUE,
|
||||
'language_content[enabled][language-url]' => TRUE,
|
||||
'language_content[enabled][language-interface]' => FALSE,
|
||||
);
|
||||
$this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
|
||||
|
||||
// Change user language preference, this way interface language is always
|
||||
// French no matter what path prefix the URLs have.
|
||||
$edit = array('preferred_langcode' => 'fr');
|
||||
$this->drupalPostForm("user/" . $admin_user->id() . "/edit", $edit, t('Save'));
|
||||
|
||||
// Create comment field on article.
|
||||
$this->addDefaultCommentField('node', 'article');
|
||||
|
||||
// Make comment body translatable.
|
||||
$field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
|
||||
$field_storage->setTranslatable(TRUE);
|
||||
$field_storage->save();
|
||||
$this->assertTrue($field_storage->isTranslatable(), 'Comment body is translatable.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that comment language is properly set.
|
||||
*/
|
||||
function testCommentLanguage() {
|
||||
|
||||
// Create two nodes, one for english and one for french, and comment each
|
||||
// node using both english and french as content language by changing URL
|
||||
// language prefixes. Meanwhile interface language is always French, which
|
||||
// is the user language preference. This way we can ensure that node
|
||||
// language and interface language do not influence comment language, as
|
||||
// only content language has to.
|
||||
foreach ($this->container->get('language_manager')->getLanguages() as $node_langcode => $node_language) {
|
||||
// Create "Article" content.
|
||||
$title = $this->randomMachineName();
|
||||
$edit = array(
|
||||
'title[0][value]' => $title,
|
||||
'body[0][value]' => $this->randomMachineName(),
|
||||
'langcode[0][value]' => $node_langcode,
|
||||
'comment[0][status]' => CommentItemInterface::OPEN,
|
||||
);
|
||||
$this->drupalPostForm("node/add/article", $edit, t('Save'));
|
||||
$node = $this->drupalGetNodeByTitle($title);
|
||||
|
||||
$prefixes = language_negotiation_url_prefixes();
|
||||
foreach ($this->container->get('language_manager')->getLanguages() as $langcode => $language) {
|
||||
// Post a comment with content language $langcode.
|
||||
$prefix = empty($prefixes[$langcode]) ? '' : $prefixes[$langcode] . '/';
|
||||
$comment_values[$node_langcode][$langcode] = $this->randomMachineName();
|
||||
$edit = array(
|
||||
'subject[0][value]' => $this->randomMachineName(),
|
||||
'comment_body[0][value]' => $comment_values[$node_langcode][$langcode],
|
||||
);
|
||||
$this->drupalPostForm($prefix . 'node/' . $node->id(), $edit, t('Preview'));
|
||||
$this->drupalPostForm(NULL, $edit, t('Save'));
|
||||
|
||||
// Check that comment language matches the current content language.
|
||||
$cids = \Drupal::entityQuery('comment')
|
||||
->condition('entity_id', $node->id())
|
||||
->condition('entity_type', 'node')
|
||||
->condition('field_name', 'comment')
|
||||
->sort('cid', 'DESC')
|
||||
->range(0, 1)
|
||||
->execute();
|
||||
$comment = Comment::load(reset($cids));
|
||||
$args = array('%node_language' => $node_langcode, '%comment_language' => $comment->langcode->value, '%langcode' => $langcode);
|
||||
$this->assertEqual($comment->langcode->value, $langcode, format_string('The comment posted with content language %langcode and belonging to the node with language %node_language has language %comment_language', $args));
|
||||
$this->assertEqual($comment->comment_body->value, $comment_values[$node_langcode][$langcode], 'Comment body correctly stored.');
|
||||
}
|
||||
}
|
||||
|
||||
// Check that comment bodies appear in the administration UI.
|
||||
$this->drupalGet('admin/content/comment');
|
||||
foreach ($comment_values as $node_values) {
|
||||
foreach ($node_values as $value) {
|
||||
$this->assertRaw($value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
40
core/modules/comment/src/Tests/CommentLinksAlterTest.php
Normal file
40
core/modules/comment/src/Tests/CommentLinksAlterTest.php
Normal file
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\CommentLinksAlterTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests;
|
||||
|
||||
/**
|
||||
* Tests comment links altering.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class CommentLinksAlterTest extends CommentTestBase {
|
||||
|
||||
public static $modules = array('comment_test');
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
// Enable comment_test.module's hook_comment_links_alter() implementation.
|
||||
$this->container->get('state')->set('comment_test_links_alter_enabled', TRUE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests comment links altering.
|
||||
*/
|
||||
public function testCommentLinksAlter() {
|
||||
$this->drupalLogin($this->webUser);
|
||||
$comment_text = $this->randomMachineName();
|
||||
$subject = $this->randomMachineName();
|
||||
$comment = $this->postComment($this->node, $comment_text, $subject);
|
||||
|
||||
$this->drupalGet('node/' . $this->node->id());
|
||||
|
||||
$this->assertLink(t('Report'));
|
||||
}
|
||||
|
||||
}
|
130
core/modules/comment/src/Tests/CommentLinksTest.php
Normal file
130
core/modules/comment/src/Tests/CommentLinksTest.php
Normal file
|
@ -0,0 +1,130 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\CommentLinksTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests;
|
||||
|
||||
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
|
||||
use Drupal\Core\Language\LanguageInterface;
|
||||
use Drupal\comment\CommentInterface;
|
||||
use Drupal\user\RoleInterface;
|
||||
|
||||
/**
|
||||
* Basic comment links tests to ensure markup present.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class CommentLinksTest extends CommentTestBase {
|
||||
|
||||
/**
|
||||
* Comment being tested.
|
||||
*
|
||||
* @var \Drupal\comment\CommentInterface
|
||||
*/
|
||||
protected $comment;
|
||||
|
||||
/**
|
||||
* Seen comments, array of comment IDs.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $seen = array();
|
||||
|
||||
/**
|
||||
* Use the main node listing to test rendering on teasers.
|
||||
*
|
||||
* @var array
|
||||
*
|
||||
* @todo Remove this dependency.
|
||||
*/
|
||||
public static $modules = array('views');
|
||||
|
||||
/**
|
||||
* Tests that comment links are output and can be hidden.
|
||||
*/
|
||||
public function testCommentLinks() {
|
||||
// Bartik theme alters comment links, so use a different theme.
|
||||
\Drupal::service('theme_handler')->install(array('stark'));
|
||||
$this->config('system.theme')
|
||||
->set('default', 'stark')
|
||||
->save();
|
||||
|
||||
// Remove additional user permissions from $this->webUser added by setUp(),
|
||||
// since this test is limited to anonymous and authenticated roles only.
|
||||
$roles = $this->webUser->getRoles();
|
||||
entity_delete_multiple('user_role', array(reset($roles)));
|
||||
|
||||
// Create a comment via CRUD API functionality, since
|
||||
// $this->postComment() relies on actual user permissions.
|
||||
$comment = entity_create('comment', array(
|
||||
'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' => array(LanguageInterface::LANGCODE_NOT_SPECIFIED => array($this->randomMachineName())),
|
||||
));
|
||||
$comment->save();
|
||||
$this->comment = $comment;
|
||||
|
||||
// Change comment settings.
|
||||
$this->setCommentSettings('form_location', CommentItemInterface::FORM_BELOW, 'Set comment form location');
|
||||
$this->setCommentAnonymous(TRUE);
|
||||
$this->node->comment = CommentItemInterface::OPEN;
|
||||
$this->node->save();
|
||||
|
||||
// Change user permissions.
|
||||
$perms = array(
|
||||
'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();
|
||||
|
||||
// Assert basic link is output, actual functionality is unit-tested in
|
||||
// \Drupal\comment\Tests\CommentLinkBuilderTest.
|
||||
foreach (array('node', "node/$nid") as $path) {
|
||||
$this->drupalGet($path);
|
||||
|
||||
// In teaser view, a link containing the comment count is always
|
||||
// expected.
|
||||
if ($path == 'node') {
|
||||
$this->assertLink(t('1 comment'));
|
||||
}
|
||||
$this->assertLink('Add new comment');
|
||||
}
|
||||
|
||||
// Make sure we can hide node links.
|
||||
entity_get_display('node', $this->node->bundle(), 'default')
|
||||
->removeComponent('links')
|
||||
->save();
|
||||
$this->drupalGet($this->node->urlInfo());
|
||||
$this->assertNoLink('1 comment');
|
||||
$this->assertNoLink('Add new comment');
|
||||
|
||||
// Visit the full node, make sure there are links for the comment.
|
||||
$this->drupalGet('node/' . $this->node->id());
|
||||
$this->assertText($comment->getSubject());
|
||||
$this->assertLink('Reply');
|
||||
|
||||
// Make sure we can hide comment links.
|
||||
entity_get_display('comment', 'comment', 'default')
|
||||
->removeComponent('links')
|
||||
->save();
|
||||
$this->drupalGet('node/' . $this->node->id());
|
||||
$this->assertText($comment->getSubject());
|
||||
$this->assertNoLink('Reply');
|
||||
}
|
||||
|
||||
}
|
154
core/modules/comment/src/Tests/CommentNewIndicatorTest.php
Normal file
154
core/modules/comment/src/Tests/CommentNewIndicatorTest.php
Normal file
|
@ -0,0 +1,154 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\CommentNewIndicatorTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests;
|
||||
|
||||
use Drupal\Component\Serialization\Json;
|
||||
use Drupal\Core\Language\LanguageInterface;
|
||||
use Drupal\comment\CommentInterface;
|
||||
use Drupal\Core\Url;
|
||||
|
||||
/**
|
||||
* Tests the 'new' indicator posted on comments.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class CommentNewIndicatorTest extends CommentTestBase {
|
||||
|
||||
/**
|
||||
* Use the main node listing to test rendering on teasers.
|
||||
*
|
||||
* @var array
|
||||
*
|
||||
* @todo Remove this dependency.
|
||||
*/
|
||||
public static $modules = array('views');
|
||||
|
||||
/**
|
||||
* Get node "x new comments" metadata from the server for the current user.
|
||||
*
|
||||
* @param array $node_ids
|
||||
* An array of node IDs.
|
||||
*
|
||||
* @return string
|
||||
* The response body.
|
||||
*/
|
||||
protected function renderNewCommentsNodeLinks(array $node_ids) {
|
||||
// Build POST values.
|
||||
$post = array();
|
||||
for ($i = 0; $i < count($node_ids); $i++) {
|
||||
$post['node_ids[' . $i . ']'] = $node_ids[$i];
|
||||
}
|
||||
$post['field_name'] = 'comment';
|
||||
|
||||
// Serialize POST values.
|
||||
foreach ($post as $key => $value) {
|
||||
// Encode according to application/x-www-form-urlencoded
|
||||
// Both names and values needs to be urlencoded, according to
|
||||
// http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.1
|
||||
$post[$key] = urlencode($key) . '=' . urlencode($value);
|
||||
}
|
||||
$post = implode('&', $post);
|
||||
|
||||
// Perform HTTP request.
|
||||
return $this->curlExec(array(
|
||||
CURLOPT_URL => \Drupal::url('comment.new_comments_node_links', array(), array('absolute' => TRUE)),
|
||||
CURLOPT_POST => TRUE,
|
||||
CURLOPT_POSTFIELDS => $post,
|
||||
CURLOPT_HTTPHEADER => array(
|
||||
'Accept: application/json',
|
||||
'Content-Type: application/x-www-form-urlencoded',
|
||||
),
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests new comment marker.
|
||||
*/
|
||||
public function testCommentNewCommentsIndicator() {
|
||||
// Test if the right links are displayed when no comment is present for the
|
||||
// node.
|
||||
$this->drupalLogin($this->adminUser);
|
||||
$this->drupalGet('node');
|
||||
$this->assertNoLink(t('@count comments', array('@count' => 0)));
|
||||
$this->assertLink(t('Read more'));
|
||||
// Verify the data-history-node-last-comment-timestamp attribute, which is
|
||||
// used by the drupal.node-new-comments-link library to determine whether
|
||||
// a "x new comments" link might be necessary or not. We do this in
|
||||
// JavaScript to prevent breaking the render cache.
|
||||
$this->assertIdentical(0, count($this->xpath('//*[@data-history-node-last-comment-timestamp]')), 'data-history-node-last-comment-timestamp attribute is not set.');
|
||||
|
||||
// Create a new comment. This helper function may be run with different
|
||||
// comment settings so use $comment->save() to avoid complex setup.
|
||||
/** @var \Drupal\comment\CommentInterface $comment */
|
||||
$comment = entity_create('comment', array(
|
||||
'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' => array(LanguageInterface::LANGCODE_NOT_SPECIFIED => array($this->randomMachineName())),
|
||||
));
|
||||
$comment->save();
|
||||
$this->drupalLogout();
|
||||
|
||||
// Log in with 'web user' and check comment links.
|
||||
$this->drupalLogin($this->webUser);
|
||||
$this->drupalGet('node');
|
||||
// Verify the data-history-node-last-comment-timestamp attribute. Given its
|
||||
// value, the drupal.node-new-comments-link library would determine that the
|
||||
// node received a comment after the user last viewed it, and hence it would
|
||||
// perform an HTTP request to render the "new comments" node link.
|
||||
$this->assertIdentical(1, count($this->xpath('//*[@data-history-node-last-comment-timestamp="' . $comment->getChangedTime() . '"]')), 'data-history-node-last-comment-timestamp attribute is set to the correct value.');
|
||||
$this->assertIdentical(1, count($this->xpath('//*[@data-history-node-field-name="comment"]')), 'data-history-node-field-name attribute is set to the correct value.');
|
||||
// The data will be pre-seeded on this particular page in drupalSettings, to
|
||||
// avoid the need for the client to make a separate request to the server.
|
||||
$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(),
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
]);
|
||||
// Pretend the data was not present in drupalSettings, i.e. test the
|
||||
// separate request to the server.
|
||||
$response = $this->renderNewCommentsNodeLinks(array($this->node->id()));
|
||||
$this->assertResponse(200);
|
||||
$json = Json::decode($response);
|
||||
$expected = array($this->node->id() => array(
|
||||
'new_comment_count' => 1,
|
||||
'first_new_comment_link' => $this->node->url('canonical', array('fragment' => 'new')),
|
||||
));
|
||||
$this->assertIdentical($expected, $json);
|
||||
|
||||
// Failing to specify node IDs for the endpoint should return a 404.
|
||||
$this->renderNewCommentsNodeLinks(array());
|
||||
$this->assertResponse(404);
|
||||
|
||||
// Accessing the endpoint as the anonymous user should return a 403.
|
||||
$this->drupalLogout();
|
||||
$this->renderNewCommentsNodeLinks(array($this->node->id()));
|
||||
$this->assertResponse(403);
|
||||
$this->renderNewCommentsNodeLinks(array());
|
||||
$this->assertResponse(403);
|
||||
}
|
||||
|
||||
}
|
86
core/modules/comment/src/Tests/CommentNodeAccessTest.php
Normal file
86
core/modules/comment/src/Tests/CommentNodeAccessTest.php
Normal file
|
@ -0,0 +1,86 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\CommentNodeAccessTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests;
|
||||
|
||||
use Drupal\comment\CommentManagerInterface;
|
||||
|
||||
/**
|
||||
* Tests comments with node access.
|
||||
*
|
||||
* Verifies there is no PostgreSQL error when viewing a node with threaded
|
||||
* comments (a comment and a reply), if a node access module is in use.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class CommentNodeAccessTest extends CommentTestBase {
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('node_access_test');
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
node_access_rebuild();
|
||||
|
||||
// Re-create user.
|
||||
$this->webUser = $this->drupalCreateUser(array(
|
||||
'access comments',
|
||||
'post comments',
|
||||
'create article content',
|
||||
'edit own comments',
|
||||
'node test view',
|
||||
'skip comment approval',
|
||||
));
|
||||
|
||||
// Set the author of the created node to the web_user uid.
|
||||
$this->node->setOwnerId($this->webUser->id())->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that threaded comments can be viewed.
|
||||
*/
|
||||
function testThreadedCommentView() {
|
||||
// Set comments to have subject required and preview disabled.
|
||||
$this->drupalLogin($this->adminUser);
|
||||
$this->setCommentPreview(DRUPAL_DISABLED);
|
||||
$this->setCommentForm(TRUE);
|
||||
$this->setCommentSubject(TRUE);
|
||||
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
|
||||
$this->drupalLogout();
|
||||
|
||||
// Post comment.
|
||||
$this->drupalLogin($this->webUser);
|
||||
$comment_text = $this->randomMachineName();
|
||||
$comment_subject = $this->randomMachineName();
|
||||
$comment = $this->postComment($this->node, $comment_text, $comment_subject);
|
||||
$this->assertTrue($this->commentExists($comment), 'Comment found.');
|
||||
|
||||
// Check comment display.
|
||||
$this->drupalGet('node/' . $this->node->id());
|
||||
$this->assertText($comment_subject, 'Individual comment subject found.');
|
||||
$this->assertText($comment_text, 'Individual comment body found.');
|
||||
|
||||
// Reply to comment, creating second comment.
|
||||
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment->id());
|
||||
$reply_text = $this->randomMachineName();
|
||||
$reply_subject = $this->randomMachineName();
|
||||
$reply = $this->postComment(NULL, $reply_text, $reply_subject, TRUE);
|
||||
$this->assertTrue($this->commentExists($reply, TRUE), 'Reply found.');
|
||||
|
||||
// Go to the node page and verify comment and reply are visible.
|
||||
$this->drupalGet('node/' . $this->node->id());
|
||||
$this->assertText($comment_text);
|
||||
$this->assertText($comment_subject);
|
||||
$this->assertText($reply_text);
|
||||
$this->assertText($reply_subject);
|
||||
}
|
||||
}
|
39
core/modules/comment/src/Tests/CommentNodeChangesTest.php
Normal file
39
core/modules/comment/src/Tests/CommentNodeChangesTest.php
Normal file
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\CommentNodeChangesTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests;
|
||||
|
||||
use Drupal\comment\Entity\Comment;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\field\Entity\FieldStorageConfig;
|
||||
|
||||
/**
|
||||
* Tests that comments behave correctly when the node is changed.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class CommentNodeChangesTest extends CommentTestBase {
|
||||
|
||||
/**
|
||||
* Tests that comments are deleted with the node.
|
||||
*/
|
||||
function testNodeDeletion() {
|
||||
$this->drupalLogin($this->webUser);
|
||||
$comment = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName());
|
||||
$this->assertTrue($comment->id(), 'The comment could be loaded.');
|
||||
$this->node->delete();
|
||||
$this->assertFalse(Comment::load($comment->id()), 'The comment could not be loaded after the node was deleted.');
|
||||
// Make sure the comment field storage and all its fields are deleted when
|
||||
// the node type is deleted.
|
||||
$this->assertNotNull(FieldStorageConfig::load('node.comment'), 'Comment field storage exists');
|
||||
$this->assertNotNull(FieldConfig::load('node.article.comment'), 'Comment field exists');
|
||||
// Delete the node type.
|
||||
entity_delete_multiple('node_type', array($this->node->bundle()));
|
||||
$this->assertNull(FieldStorageConfig::load('node.comment'), 'Comment field storage deleted');
|
||||
$this->assertNull(FieldConfig::load('node.article.comment'), 'Comment field deleted');
|
||||
}
|
||||
}
|
496
core/modules/comment/src/Tests/CommentNonNodeTest.php
Normal file
496
core/modules/comment/src/Tests/CommentNonNodeTest.php
Normal file
|
@ -0,0 +1,496 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\CommentNonNodeTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests;
|
||||
|
||||
use Drupal\comment\CommentInterface;
|
||||
use Drupal\comment\Entity\Comment;
|
||||
use Drupal\comment\Entity\CommentType;
|
||||
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\field\Entity\FieldStorageConfig;
|
||||
use Drupal\field_ui\Tests\FieldUiTestTrait;
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
use Drupal\Core\Entity\EntityInterface;
|
||||
use Drupal\user\RoleInterface;
|
||||
|
||||
/**
|
||||
* Tests commenting on a test entity.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class CommentNonNodeTest extends WebTestBase {
|
||||
|
||||
use FieldUiTestTrait;
|
||||
use CommentTestTrait;
|
||||
|
||||
public static $modules = array('comment', 'user', 'field_ui', 'entity_test', 'block');
|
||||
|
||||
/**
|
||||
* An administrative user with permission to configure comment settings.
|
||||
*
|
||||
* @var \Drupal\user\UserInterface
|
||||
*/
|
||||
protected $adminUser;
|
||||
|
||||
/**
|
||||
* The entity to use within tests.
|
||||
*
|
||||
* @var \Drupal\entity_test\Entity\EntityTest
|
||||
*/
|
||||
protected $entity;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->drupalPlaceBlock('system_breadcrumb_block');
|
||||
|
||||
// Create a bundle for entity_test.
|
||||
entity_test_create_bundle('entity_test', 'Entity Test', 'entity_test');
|
||||
entity_create('comment_type', array(
|
||||
'id' => 'comment',
|
||||
'label' => 'Comment settings',
|
||||
'description' => 'Comment settings',
|
||||
'target_entity_type_id' => 'entity_test',
|
||||
))->save();
|
||||
// Create comment field on entity_test bundle.
|
||||
$this->addDefaultCommentField('entity_test', 'entity_test');
|
||||
|
||||
// Verify that bundles are defined correctly.
|
||||
$bundles = \Drupal::entityManager()->getBundleInfo('comment');
|
||||
$this->assertEqual($bundles['comment']['label'], 'Comment settings');
|
||||
|
||||
// Create test user.
|
||||
$this->adminUser = $this->drupalCreateUser(array(
|
||||
'administer comments',
|
||||
'skip comment approval',
|
||||
'post comments',
|
||||
'access comments',
|
||||
'view test entity',
|
||||
'administer entity_test content',
|
||||
));
|
||||
|
||||
// Enable anonymous and authenticated user comments.
|
||||
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, array(
|
||||
'access comments',
|
||||
'post comments',
|
||||
'skip comment approval',
|
||||
));
|
||||
user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, array(
|
||||
'access comments',
|
||||
'post comments',
|
||||
'skip comment approval',
|
||||
));
|
||||
|
||||
// Create a test entity.
|
||||
$random_label = $this->randomMachineName();
|
||||
$data = array('type' => 'entity_test', 'name' => $random_label);
|
||||
$this->entity = entity_create('entity_test', $data);
|
||||
$this->entity->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Posts a comment.
|
||||
*
|
||||
* @param \Drupal\Core\Entity\EntityInterface|null $entity
|
||||
* Entity to post comment on or NULL to post to the previously loaded page.
|
||||
* @param string $comment
|
||||
* Comment body.
|
||||
* @param string $subject
|
||||
* Comment subject.
|
||||
* @param mixed $contact
|
||||
* Set to NULL for no contact info, TRUE to ignore success checking, and
|
||||
* array of values to set contact info.
|
||||
*
|
||||
* @return \Drupal\comment\CommentInterface
|
||||
* The new comment entity.
|
||||
*/
|
||||
function postComment(EntityInterface $entity, $comment, $subject = '', $contact = NULL) {
|
||||
$edit = array();
|
||||
$edit['comment_body[0][value]'] = $comment;
|
||||
|
||||
$field = FieldConfig::loadByName('entity_test', 'entity_test', 'comment');
|
||||
$preview_mode = $field->getSetting('preview');
|
||||
|
||||
// Must get the page before we test for fields.
|
||||
if ($entity !== NULL) {
|
||||
$this->drupalGet('comment/reply/entity_test/' . $entity->id() . '/comment');
|
||||
}
|
||||
|
||||
// Determine the visibility of subject form field.
|
||||
if (entity_get_form_display('comment', 'comment', 'default')->getComponent('subject')) {
|
||||
// Subject input allowed.
|
||||
$edit['subject[0][value]'] = $subject;
|
||||
}
|
||||
else {
|
||||
$this->assertNoFieldByName('subject[0][value]', '', 'Subject field not found.');
|
||||
}
|
||||
|
||||
if ($contact !== NULL && is_array($contact)) {
|
||||
$edit += $contact;
|
||||
}
|
||||
switch ($preview_mode) {
|
||||
case DRUPAL_REQUIRED:
|
||||
// Preview required so no save button should be found.
|
||||
$this->assertNoFieldByName('op', t('Save'), 'Save button not found.');
|
||||
$this->drupalPostForm(NULL, $edit, t('Preview'));
|
||||
// Don't break here so that we can test post-preview field presence and
|
||||
// function below.
|
||||
case DRUPAL_OPTIONAL:
|
||||
$this->assertFieldByName('op', t('Preview'), 'Preview button found.');
|
||||
$this->assertFieldByName('op', t('Save'), 'Save button found.');
|
||||
$this->drupalPostForm(NULL, $edit, t('Save'));
|
||||
break;
|
||||
|
||||
case DRUPAL_DISABLED:
|
||||
$this->assertNoFieldByName('op', t('Preview'), 'Preview button not found.');
|
||||
$this->assertFieldByName('op', t('Save'), 'Save button found.');
|
||||
$this->drupalPostForm(NULL, $edit, t('Save'));
|
||||
break;
|
||||
}
|
||||
$match = array();
|
||||
// Get comment ID
|
||||
preg_match('/#comment-([0-9]+)/', $this->getURL(), $match);
|
||||
|
||||
// Get comment.
|
||||
if ($contact !== TRUE) { // If true then attempting to find error message.
|
||||
if ($subject) {
|
||||
$this->assertText($subject, 'Comment subject posted.');
|
||||
}
|
||||
$this->assertText($comment, 'Comment body posted.');
|
||||
$this->assertTrue((!empty($match) && !empty($match[1])), 'Comment ID found.');
|
||||
}
|
||||
|
||||
if (isset($match[1])) {
|
||||
return Comment::load($match[1]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks current page for specified comment.
|
||||
*
|
||||
* @param \Drupal\comment\CommentInterface $comment
|
||||
* The comment object.
|
||||
* @param bool $reply
|
||||
* Boolean indicating whether the comment is a reply to another comment.
|
||||
*
|
||||
* @return boolean
|
||||
* Boolean indicating whether the comment was found.
|
||||
*/
|
||||
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 .= '/s';
|
||||
|
||||
return (boolean) preg_match($regex, $this->getRawContent());
|
||||
}
|
||||
else {
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the commenter's contact information is displayed.
|
||||
*
|
||||
* @return boolean
|
||||
* Contact info is available.
|
||||
*/
|
||||
function commentContactInfoAvailable() {
|
||||
return preg_match('/(input).*?(name="name").*?(input).*?(name="mail").*?(input).*?(name="homepage")/s', $this->getRawContent());
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs the specified operation on the specified comment.
|
||||
*
|
||||
* @param object $comment
|
||||
* Comment to perform operation on.
|
||||
* @param string $operation
|
||||
* Operation to perform.
|
||||
* @param bool $approval
|
||||
* Operation is found on approval page.
|
||||
*/
|
||||
function performCommentOperation($comment, $operation, $approval = FALSE) {
|
||||
$edit = array();
|
||||
$edit['operation'] = $operation;
|
||||
$edit['comments[' . $comment->id() . ']'] = TRUE;
|
||||
$this->drupalPostForm('admin/content/comment' . ($approval ? '/approval' : ''), $edit, t('Update'));
|
||||
|
||||
if ($operation == 'delete') {
|
||||
$this->drupalPostForm(NULL, array(), t('Delete comments'));
|
||||
$this->assertRaw(\Drupal::translation()->formatPlural(1, 'Deleted 1 comment.', 'Deleted @count comments.'), format_string('Operation "@operation" was performed on comment.', array('@operation' => $operation)));
|
||||
}
|
||||
else {
|
||||
$this->assertText(t('The update has been performed.'), format_string('Operation "@operation" was performed on comment.', array('@operation' => $operation)));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the comment ID for an unapproved comment.
|
||||
*
|
||||
* @param string $subject
|
||||
* Comment subject to find.
|
||||
*
|
||||
* @return integer
|
||||
* Comment ID.
|
||||
*/
|
||||
function getUnapprovedComment($subject) {
|
||||
$this->drupalGet('admin/content/comment/approval');
|
||||
preg_match('/href="(.*?)#comment-([^"]+)"(.*?)>(' . $subject . ')/', $this->getRawContent(), $match);
|
||||
|
||||
return $match[2];
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests anonymous comment functionality.
|
||||
*/
|
||||
function testCommentFunctionality() {
|
||||
$limited_user = $this->drupalCreateUser(array(
|
||||
'administer entity_test fields'
|
||||
));
|
||||
$this->drupalLogin($limited_user);
|
||||
// Test that default field exists.
|
||||
$this->drupalGet('entity_test/structure/entity_test/fields');
|
||||
$this->assertText(t('Comments'));
|
||||
$this->assertLinkByHref('entity_test/structure/entity_test/fields/entity_test.entity_test.comment');
|
||||
// Test widget hidden option is not visible when there's no comments.
|
||||
$this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.comment');
|
||||
$this->assertResponse(200);
|
||||
$this->assertNoField('edit-default-value-input-comment-und-0-status-0');
|
||||
// Test that field to change cardinality is not available.
|
||||
$this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.comment/storage');
|
||||
$this->assertResponse(200);
|
||||
$this->assertNoField('cardinality_number');
|
||||
$this->assertNoField('cardinality');
|
||||
|
||||
$this->drupalLogin($this->adminUser);
|
||||
|
||||
// Test breadcrumb on comment add page.
|
||||
$this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment');
|
||||
$xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a';
|
||||
$this->assertEqual(current($this->xpath($xpath)), $this->entity->label(), 'Last breadcrumb item is equal to node title on comment reply page.');
|
||||
|
||||
// Post a comment.
|
||||
/** @var \Drupal\comment\CommentInterface $comment1 */
|
||||
$comment1 = $this->postComment($this->entity, $this->randomMachineName(), $this->randomMachineName());
|
||||
$this->assertTrue($this->commentExists($comment1), 'Comment on test entity exists.');
|
||||
|
||||
// Test breadcrumb on comment reply page.
|
||||
$this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment/' . $comment1->id());
|
||||
$xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a';
|
||||
$this->assertEqual(current($this->xpath($xpath)), $comment1->getSubject(), 'Last breadcrumb item is equal to comment title on comment reply page.');
|
||||
|
||||
// Test breadcrumb on comment edit page.
|
||||
$this->drupalGet('comment/' . $comment1->id() . '/edit');
|
||||
$xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a';
|
||||
$this->assertEqual(current($this->xpath($xpath)), $comment1->getSubject(), 'Last breadcrumb item is equal to comment subject on edit page.');
|
||||
|
||||
// Test breadcrumb on comment delete page.
|
||||
$this->drupalGet('comment/' . $comment1->id() . '/delete');
|
||||
$xpath = '//nav[@class="breadcrumb"]/ol/li[last()]/a';
|
||||
$this->assertEqual(current($this->xpath($xpath)), $comment1->getSubject(), 'Last breadcrumb item is equal to comment subject on delete confirm page.');
|
||||
|
||||
// Unpublish the comment.
|
||||
$this->performCommentOperation($comment1, 'unpublish');
|
||||
$this->drupalGet('admin/content/comment/approval');
|
||||
$this->assertRaw('comments[' . $comment1->id() . ']', 'Comment was unpublished.');
|
||||
|
||||
// Publish the comment.
|
||||
$this->performCommentOperation($comment1, 'publish', TRUE);
|
||||
$this->drupalGet('admin/content/comment');
|
||||
$this->assertRaw('comments[' . $comment1->id() . ']', 'Comment was published.');
|
||||
|
||||
// Delete the comment.
|
||||
$this->performCommentOperation($comment1, 'delete');
|
||||
$this->drupalGet('admin/content/comment');
|
||||
$this->assertNoRaw('comments[' . $comment1->id() . ']', 'Comment was deleted.');
|
||||
|
||||
// Post another comment.
|
||||
$comment1 = $this->postComment($this->entity, $this->randomMachineName(), $this->randomMachineName());
|
||||
$this->assertTrue($this->commentExists($comment1), 'Comment on test entity exists.');
|
||||
|
||||
// Check that the comment was found.
|
||||
$this->drupalGet('admin/content/comment');
|
||||
$this->assertRaw('comments[' . $comment1->id() . ']', 'Comment was published.');
|
||||
|
||||
// Check that entity access applies to administrative page.
|
||||
$this->assertText($this->entity->label(), 'Name of commented account found.');
|
||||
$limited_user = $this->drupalCreateUser(array(
|
||||
'administer comments',
|
||||
));
|
||||
$this->drupalLogin($limited_user);
|
||||
$this->drupalGet('admin/content/comment');
|
||||
$this->assertNoText($this->entity->label(), 'No commented account name found.');
|
||||
|
||||
$this->drupalLogout();
|
||||
|
||||
// Deny anonymous users access to comments.
|
||||
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, array(
|
||||
'access comments' => FALSE,
|
||||
'post comments' => FALSE,
|
||||
'skip comment approval' => FALSE,
|
||||
'view test entity' => TRUE,
|
||||
));
|
||||
|
||||
// Attempt to view comments while disallowed.
|
||||
$this->drupalGet('entity-test/' . $this->entity->id());
|
||||
$this->assertNoPattern('@<h2[^>]*>Comments</h2>@', 'Comments were not displayed.');
|
||||
$this->assertNoLink('Add new comment', 'Link to add comment was found.');
|
||||
|
||||
// Attempt to view test entity comment form while disallowed.
|
||||
$this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment');
|
||||
$this->assertResponse(403);
|
||||
$this->assertNoFieldByName('subject[0][value]', '', 'Subject field not found.');
|
||||
$this->assertNoFieldByName('comment_body[0][value]', '', 'Comment field not found.');
|
||||
|
||||
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, array(
|
||||
'access comments' => TRUE,
|
||||
'post comments' => FALSE,
|
||||
'view test entity' => TRUE,
|
||||
'skip comment approval' => FALSE,
|
||||
));
|
||||
$this->drupalGet('entity_test/' . $this->entity->id());
|
||||
$this->assertPattern('@<h2[^>]*>Comments</h2>@', 'Comments were displayed.');
|
||||
$this->assertLink('Log in', 0, 'Link to log in was found.');
|
||||
$this->assertLink('register', 0, 'Link to register was found.');
|
||||
$this->assertNoFieldByName('subject[0][value]', '', 'Subject field not found.');
|
||||
$this->assertNoFieldByName('comment_body[0][value]', '', 'Comment field not found.');
|
||||
|
||||
// Test the combination of anonymous users being able to post, but not view
|
||||
// comments, to ensure that access to post comments doesn't grant access to
|
||||
// view them.
|
||||
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, array(
|
||||
'access comments' => FALSE,
|
||||
'post comments' => TRUE,
|
||||
'skip comment approval' => TRUE,
|
||||
'view test entity' => TRUE,
|
||||
));
|
||||
$this->drupalGet('entity_test/' . $this->entity->id());
|
||||
$this->assertNoPattern('@<h2[^>]*>Comments</h2>@', 'Comments were not displayed.');
|
||||
$this->assertFieldByName('subject[0][value]', '', 'Subject field found.');
|
||||
$this->assertFieldByName('comment_body[0][value]', '', 'Comment field found.');
|
||||
|
||||
$this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment/' . $comment1->id());
|
||||
$this->assertResponse(403);
|
||||
$this->assertNoText($comment1->getSubject(), 'Comment not displayed.');
|
||||
|
||||
// Test comment field widget changes.
|
||||
$limited_user = $this->drupalCreateUser(array(
|
||||
'administer entity_test fields',
|
||||
'view test entity',
|
||||
'administer entity_test content',
|
||||
));
|
||||
$this->drupalLogin($limited_user);
|
||||
$this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.comment');
|
||||
$this->assertNoFieldChecked('edit-default-value-input-comment-0-status-0');
|
||||
$this->assertNoFieldChecked('edit-default-value-input-comment-0-status-1');
|
||||
$this->assertFieldChecked('edit-default-value-input-comment-0-status-2');
|
||||
// Test comment option change in field settings.
|
||||
$edit = array(
|
||||
'default_value_input[comment][0][status]' => CommentItemInterface::CLOSED,
|
||||
'settings[anonymous]' => COMMENT_ANONYMOUS_MAY_CONTACT,
|
||||
);
|
||||
$this->drupalPostForm(NULL, $edit, t('Save settings'));
|
||||
$this->drupalGet('entity_test/structure/entity_test/fields/entity_test.entity_test.comment');
|
||||
$this->assertNoFieldChecked('edit-default-value-input-comment-0-status-0');
|
||||
$this->assertFieldChecked('edit-default-value-input-comment-0-status-1');
|
||||
$this->assertNoFieldChecked('edit-default-value-input-comment-0-status-2');
|
||||
$this->assertFieldByName('settings[anonymous]', COMMENT_ANONYMOUS_MAY_CONTACT);
|
||||
|
||||
// Add a new comment-type.
|
||||
$bundle = CommentType::create(array(
|
||||
'id' => 'foobar',
|
||||
'label' => 'Foobar',
|
||||
'description' => '',
|
||||
'target_entity_type_id' => 'entity_test',
|
||||
));
|
||||
$bundle->save();
|
||||
|
||||
// Add a new comment field.
|
||||
$storage_edit = array(
|
||||
'settings[comment_type]' => 'foobar',
|
||||
);
|
||||
$this->fieldUIAddNewField('entity_test/structure/entity_test', 'foobar', 'Foobar', 'comment', $storage_edit);
|
||||
|
||||
// Add a third comment field.
|
||||
$this->fieldUIAddNewField('entity_test/structure/entity_test', 'barfoo', 'BarFoo', 'comment', $storage_edit);
|
||||
|
||||
// Check the field contains the correct comment type.
|
||||
$field_storage = FieldStorageConfig::load('entity_test.field_barfoo');
|
||||
$this->assertTrue($field_storage);
|
||||
$this->assertEqual($field_storage->getSetting('comment_type'), 'foobar');
|
||||
$this->assertEqual($field_storage->getCardinality(), 1);
|
||||
|
||||
// Test the new entity commenting inherits default.
|
||||
$random_label = $this->randomMachineName();
|
||||
$data = array('bundle' => 'entity_test', 'name' => $random_label);
|
||||
$new_entity = entity_create('entity_test', $data);
|
||||
$new_entity->save();
|
||||
$this->drupalGet('entity_test/manage/' . $new_entity->id());
|
||||
$this->assertNoFieldChecked('edit-field-foobar-0-status-1');
|
||||
$this->assertFieldChecked('edit-field-foobar-0-status-2');
|
||||
$this->assertNoField('edit-field-foobar-0-status-0');
|
||||
|
||||
// @todo Check proper url and form https://www.drupal.org/node/2458323
|
||||
$this->drupalGet('comment/reply/entity_test/comment/' . $new_entity->id());
|
||||
$this->assertNoFieldByName('subject[0][value]', '', 'Subject field found.');
|
||||
$this->assertNoFieldByName('comment_body[0][value]', '', 'Comment field found.');
|
||||
|
||||
// Test removal of comment_body field.
|
||||
$limited_user = $this->drupalCreateUser(array(
|
||||
'administer entity_test fields',
|
||||
'post comments',
|
||||
'administer comment fields',
|
||||
'administer comment types',
|
||||
));
|
||||
$this->drupalLogin($limited_user);
|
||||
|
||||
$this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment');
|
||||
$this->assertFieldByName('comment_body[0][value]', '', 'Comment body field found.');
|
||||
$this->fieldUIDeleteField('admin/structure/comment/manage/comment', 'comment.comment.comment_body', 'Comment', 'Comment settings');
|
||||
$this->drupalGet('comment/reply/entity_test/' . $this->entity->id() . '/comment');
|
||||
$this->assertNoFieldByName('comment_body[0][value]', '', 'Comment body field not found.');
|
||||
// Set subject field to autogenerate it.
|
||||
$edit = ['subject[0][value]' => ''];
|
||||
$this->drupalPostForm(NULL, $edit, t('Save'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests comment fields cannot be added to entity types without integer IDs.
|
||||
*/
|
||||
public function testsNonIntegerIdEntities() {
|
||||
// Create a bundle for entity_test_string_id.
|
||||
entity_test_create_bundle('entity_test', 'Entity Test', 'entity_test_string_id');
|
||||
$limited_user = $this->drupalCreateUser(array(
|
||||
'administer entity_test_string_id fields',
|
||||
));
|
||||
$this->drupalLogin($limited_user);
|
||||
// Visit the Field UI field add page.
|
||||
$this->drupalGet('entity_test_string_id/structure/entity_test/fields/add-field');
|
||||
// Ensure field isn't shown for string IDs.
|
||||
$this->assertNoOption('edit-new-storage-type', 'comment');
|
||||
// Ensure a core field type shown.
|
||||
$this->assertOption('edit-new-storage-type', 'boolean');
|
||||
|
||||
// Create a bundle for entity_test_no_id.
|
||||
entity_test_create_bundle('entity_test', 'Entity Test', 'entity_test_no_id');
|
||||
$this->drupalLogin($this->drupalCreateUser(array(
|
||||
'administer entity_test_no_id fields',
|
||||
)));
|
||||
// Visit the Field UI field add page.
|
||||
$this->drupalGet('entity_test_no_id/structure/entity_test/fields/add-field');
|
||||
// Ensure field isn't shown for empty IDs.
|
||||
$this->assertNoOption('edit-new-storage-type', 'comment');
|
||||
// Ensure a core field type shown.
|
||||
$this->assertOption('edit-new-storage-type', 'boolean');
|
||||
}
|
||||
|
||||
}
|
392
core/modules/comment/src/Tests/CommentPagerTest.php
Normal file
392
core/modules/comment/src/Tests/CommentPagerTest.php
Normal file
|
@ -0,0 +1,392 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\CommentPagerTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests;
|
||||
|
||||
use Drupal\comment\CommentManagerInterface;
|
||||
use Drupal\Component\Utility\SafeMarkup;
|
||||
use Drupal\node\Entity\Node;
|
||||
|
||||
/**
|
||||
* Tests paging of comments and their settings.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class CommentPagerTest extends CommentTestBase {
|
||||
/**
|
||||
* Confirms comment paging works correctly with flat and threaded comments.
|
||||
*/
|
||||
function testCommentPaging() {
|
||||
$this->drupalLogin($this->adminUser);
|
||||
|
||||
// Set comment variables.
|
||||
$this->setCommentForm(TRUE);
|
||||
$this->setCommentSubject(TRUE);
|
||||
$this->setCommentPreview(DRUPAL_DISABLED);
|
||||
|
||||
// Create a node and three comments.
|
||||
$node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));
|
||||
$comments = array();
|
||||
$comments[] = $this->postComment($node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
|
||||
$comments[] = $this->postComment($node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
|
||||
$comments[] = $this->postComment($node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
|
||||
|
||||
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT, 'Comment paging changed.');
|
||||
|
||||
// Set comments to one per page so that we are able to test paging without
|
||||
// needing to insert large numbers of comments.
|
||||
$this->setCommentsPerPage(1);
|
||||
|
||||
// Check the first page of the node, and confirm the correct comments are
|
||||
// shown.
|
||||
$this->drupalGet('node/' . $node->id());
|
||||
$this->assertRaw(t('next'), 'Paging links found.');
|
||||
$this->assertTrue($this->commentExists($comments[0]), 'Comment 1 appears on page 1.');
|
||||
$this->assertFalse($this->commentExists($comments[1]), 'Comment 2 does not appear on page 1.');
|
||||
$this->assertFalse($this->commentExists($comments[2]), 'Comment 3 does not appear on page 1.');
|
||||
|
||||
// Check the second page.
|
||||
$this->drupalGet('node/' . $node->id(), array('query' => array('page' => 1)));
|
||||
$this->assertTrue($this->commentExists($comments[1]), 'Comment 2 appears on page 2.');
|
||||
$this->assertFalse($this->commentExists($comments[0]), 'Comment 1 does not appear on page 2.');
|
||||
$this->assertFalse($this->commentExists($comments[2]), 'Comment 3 does not appear on page 2.');
|
||||
|
||||
// Check the third page.
|
||||
$this->drupalGet('node/' . $node->id(), array('query' => array('page' => 2)));
|
||||
$this->assertTrue($this->commentExists($comments[2]), 'Comment 3 appears on page 3.');
|
||||
$this->assertFalse($this->commentExists($comments[0]), 'Comment 1 does not appear on page 3.');
|
||||
$this->assertFalse($this->commentExists($comments[1]), 'Comment 2 does not appear on page 3.');
|
||||
|
||||
// Post a reply to the oldest comment and test again.
|
||||
$oldest_comment = reset($comments);
|
||||
$this->drupalGet('comment/reply/node/' . $node->id() . '/comment/' . $oldest_comment->id());
|
||||
$reply = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
|
||||
|
||||
$this->setCommentsPerPage(2);
|
||||
// We are still in flat view - the replies should not be on the first page,
|
||||
// even though they are replies to the oldest comment.
|
||||
$this->drupalGet('node/' . $node->id(), array('query' => array('page' => 0)));
|
||||
$this->assertFalse($this->commentExists($reply, TRUE), 'In flat mode, reply does not appear on page 1.');
|
||||
|
||||
// If we switch to threaded mode, the replies on the oldest comment
|
||||
// should be bumped to the first page and comment 6 should be bumped
|
||||
// to the second page.
|
||||
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Switched to threaded mode.');
|
||||
$this->drupalGet('node/' . $node->id(), array('query' => array('page' => 0)));
|
||||
$this->assertTrue($this->commentExists($reply, TRUE), 'In threaded mode, reply appears on page 1.');
|
||||
$this->assertFalse($this->commentExists($comments[1]), 'In threaded mode, comment 2 has been bumped off of page 1.');
|
||||
|
||||
// If (# replies > # comments per page) in threaded expanded view,
|
||||
// the overage should be bumped.
|
||||
$reply2 = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
|
||||
$this->drupalGet('node/' . $node->id(), array('query' => array('page' => 0)));
|
||||
$this->assertFalse($this->commentExists($reply2, TRUE), 'In threaded mode where # replies > # comments per page, the newest reply does not appear on page 1.');
|
||||
|
||||
// Test that the page build process does not somehow generate errors when
|
||||
// # comments per page is set to 0.
|
||||
$this->setCommentsPerPage(0);
|
||||
$this->drupalGet('node/' . $node->id(), array('query' => array('page' => 0)));
|
||||
$this->assertFalse($this->commentExists($reply2, TRUE), 'Threaded mode works correctly when comments per page is 0.');
|
||||
|
||||
$this->drupalLogout();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests comment ordering and threading.
|
||||
*/
|
||||
function testCommentOrderingThreading() {
|
||||
$this->drupalLogin($this->adminUser);
|
||||
|
||||
// Set comment variables.
|
||||
$this->setCommentForm(TRUE);
|
||||
$this->setCommentSubject(TRUE);
|
||||
$this->setCommentPreview(DRUPAL_DISABLED);
|
||||
|
||||
// Display all the comments on the same page.
|
||||
$this->setCommentsPerPage(1000);
|
||||
|
||||
// Create a node and three comments.
|
||||
$node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));
|
||||
$comments = array();
|
||||
$comments[] = $this->postComment($node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
|
||||
$comments[] = $this->postComment($node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
|
||||
$comments[] = $this->postComment($node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
|
||||
|
||||
// Post a reply to the second comment.
|
||||
$this->drupalGet('comment/reply/node/' . $node->id() . '/comment/' . $comments[1]->id());
|
||||
$comments[] = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
|
||||
|
||||
// Post a reply to the first comment.
|
||||
$this->drupalGet('comment/reply/node/' . $node->id() . '/comment/' . $comments[0]->id());
|
||||
$comments[] = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
|
||||
|
||||
// Post a reply to the last comment.
|
||||
$this->drupalGet('comment/reply/node/' . $node->id() . '/comment/' . $comments[2]->id());
|
||||
$comments[] = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
|
||||
|
||||
// Post a reply to the second comment.
|
||||
$this->drupalGet('comment/reply/node/' . $node->id() . '/comment/' . $comments[3]->id());
|
||||
$comments[] = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
|
||||
|
||||
// At this point, the comment tree is:
|
||||
// - 0
|
||||
// - 4
|
||||
// - 1
|
||||
// - 3
|
||||
// - 6
|
||||
// - 2
|
||||
// - 5
|
||||
|
||||
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT, 'Comment paging changed.');
|
||||
|
||||
$expected_order = array(
|
||||
0,
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
4,
|
||||
5,
|
||||
6,
|
||||
);
|
||||
$this->drupalGet('node/' . $node->id());
|
||||
$this->assertCommentOrder($comments, $expected_order);
|
||||
|
||||
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Switched to threaded mode.');
|
||||
|
||||
$expected_order = array(
|
||||
0,
|
||||
4,
|
||||
1,
|
||||
3,
|
||||
6,
|
||||
2,
|
||||
5,
|
||||
);
|
||||
$this->drupalGet('node/' . $node->id());
|
||||
$this->assertCommentOrder($comments, $expected_order);
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the comments are displayed in the correct order.
|
||||
*
|
||||
* @param $comments
|
||||
* And array of comments.
|
||||
* @param $expected_order
|
||||
* An array of keys from $comments describing the expected order.
|
||||
*/
|
||||
function assertCommentOrder(array $comments, array $expected_order) {
|
||||
$expected_cids = array();
|
||||
|
||||
// First, rekey the expected order by cid.
|
||||
foreach ($expected_order as $key) {
|
||||
$expected_cids[] = $comments[$key]->id();
|
||||
}
|
||||
|
||||
$comment_anchors = $this->xpath('//a[starts-with(@id,"comment-")]');
|
||||
$result_order = array();
|
||||
foreach ($comment_anchors as $anchor) {
|
||||
$result_order[] = substr($anchor['id'], 8);
|
||||
}
|
||||
return $this->assertEqual($expected_cids, $result_order, format_string('Comment order: expected @expected, returned @returned.', array('@expected' => implode(',', $expected_cids), '@returned' => implode(',', $result_order))));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests calculation of first page with new comment.
|
||||
*/
|
||||
function testCommentNewPageIndicator() {
|
||||
$this->drupalLogin($this->adminUser);
|
||||
|
||||
// Set comment variables.
|
||||
$this->setCommentForm(TRUE);
|
||||
$this->setCommentSubject(TRUE);
|
||||
$this->setCommentPreview(DRUPAL_DISABLED);
|
||||
|
||||
// Set comments to one per page so that we are able to test paging without
|
||||
// needing to insert large numbers of comments.
|
||||
$this->setCommentsPerPage(1);
|
||||
|
||||
// Create a node and three comments.
|
||||
$node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));
|
||||
$comments = array();
|
||||
$comments[] = $this->postComment($node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
|
||||
$comments[] = $this->postComment($node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
|
||||
$comments[] = $this->postComment($node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
|
||||
|
||||
// Post a reply to the second comment.
|
||||
$this->drupalGet('comment/reply/node/' . $node->id() . '/comment/' . $comments[1]->id());
|
||||
$comments[] = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
|
||||
|
||||
// Post a reply to the first comment.
|
||||
$this->drupalGet('comment/reply/node/' . $node->id() . '/comment/' . $comments[0]->id());
|
||||
$comments[] = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
|
||||
|
||||
// Post a reply to the last comment.
|
||||
$this->drupalGet('comment/reply/node/' . $node->id() . '/comment/' . $comments[2]->id());
|
||||
$comments[] = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
|
||||
|
||||
// At this point, the comment tree is:
|
||||
// - 0
|
||||
// - 4
|
||||
// - 1
|
||||
// - 3
|
||||
// - 2
|
||||
// - 5
|
||||
|
||||
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT, 'Comment paging changed.');
|
||||
|
||||
$expected_pages = array(
|
||||
1 => 5, // Page of comment 5
|
||||
2 => 4, // Page of comment 4
|
||||
3 => 3, // Page of comment 3
|
||||
4 => 2, // Page of comment 2
|
||||
5 => 1, // Page of comment 1
|
||||
6 => 0, // Page of comment 0
|
||||
);
|
||||
|
||||
$node = Node::load($node->id());
|
||||
foreach ($expected_pages as $new_replies => $expected_page) {
|
||||
$returned_page = \Drupal::entityManager()->getStorage('comment')
|
||||
->getNewCommentPageNumber($node->get('comment')->comment_count, $new_replies, $node);
|
||||
$this->assertIdentical($expected_page, $returned_page, format_string('Flat mode, @new replies: expected page @expected, returned page @returned.', array('@new' => $new_replies, '@expected' => $expected_page, '@returned' => $returned_page)));
|
||||
}
|
||||
|
||||
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Switched to threaded mode.');
|
||||
|
||||
$expected_pages = array(
|
||||
1 => 5, // Page of comment 5
|
||||
2 => 1, // Page of comment 4
|
||||
3 => 1, // Page of comment 4
|
||||
4 => 1, // Page of comment 4
|
||||
5 => 1, // Page of comment 4
|
||||
6 => 0, // Page of comment 0
|
||||
);
|
||||
|
||||
\Drupal::entityManager()->getStorage('node')->resetCache(array($node->id()));
|
||||
$node = Node::load($node->id());
|
||||
foreach ($expected_pages as $new_replies => $expected_page) {
|
||||
$returned_page = \Drupal::entityManager()->getStorage('comment')
|
||||
->getNewCommentPageNumber($node->get('comment')->comment_count, $new_replies, $node);
|
||||
$this->assertEqual($expected_page, $returned_page, format_string('Threaded mode, @new replies: expected page @expected, returned page @returned.', array('@new' => $new_replies, '@expected' => $expected_page, '@returned' => $returned_page)));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Confirms comment paging works correctly with two pagers.
|
||||
*/
|
||||
function testTwoPagers() {
|
||||
// Add another field to article content-type.
|
||||
$this->addDefaultCommentField('node', 'article', 'comment_2');
|
||||
// Set default to display comment list with unique pager id.
|
||||
entity_get_display('node', 'article', 'default')
|
||||
->setComponent('comment_2', array(
|
||||
'label' => 'hidden',
|
||||
'type' => 'comment_default',
|
||||
'weight' => 30,
|
||||
'settings' => array(
|
||||
'pager_id' => 1,
|
||||
)
|
||||
))
|
||||
->save();
|
||||
|
||||
// Make sure pager appears in formatter summary and settings form.
|
||||
$account = $this->drupalCreateUser(array('administer node display'));
|
||||
$this->drupalLogin($account);
|
||||
$this->drupalGet('admin/structure/types/manage/article/display');
|
||||
$this->assertNoText(t('Pager ID: @id', array('@id' => 0)), 'No summary for standard pager');
|
||||
$this->assertText(t('Pager ID: @id', array('@id' => 1)));
|
||||
$this->drupalPostAjaxForm(NULL, array(), 'comment_settings_edit');
|
||||
// Change default pager to 2.
|
||||
$this->drupalPostForm(NULL, array('fields[comment][settings_edit_form][settings][pager_id]' => 2), t('Save'));
|
||||
$this->assertText(t('Pager ID: @id', array('@id' => 2)));
|
||||
// Revert the changes.
|
||||
$this->drupalPostAjaxForm(NULL, array(), 'comment_settings_edit');
|
||||
$this->drupalPostForm(NULL, array('fields[comment][settings_edit_form][settings][pager_id]' => 0), t('Save'));
|
||||
$this->assertNoText(t('Pager ID: @id', array('@id' => 0)), 'No summary for standard pager');
|
||||
|
||||
$this->drupalLogin($this->adminUser);
|
||||
|
||||
// Add a new node with both comment fields open.
|
||||
$node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'uid' => $this->webUser->id()));
|
||||
// Set comment options.
|
||||
$comments = array();
|
||||
foreach (array('comment', 'comment_2') as $field_name) {
|
||||
$this->setCommentForm(TRUE, $field_name);
|
||||
$this->setCommentPreview(DRUPAL_OPTIONAL, $field_name);
|
||||
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT, 'Comment paging changed.', $field_name);
|
||||
|
||||
// Set comments to one per page so that we are able to test paging without
|
||||
// needing to insert large numbers of comments.
|
||||
$this->setCommentsPerPage(1, $field_name);
|
||||
for ($i = 0; $i < 3; $i++) {
|
||||
$comment = t('Comment @count on field @field', array(
|
||||
'@count' => $i + 1,
|
||||
'@field' => $field_name,
|
||||
));
|
||||
$comments[] = $this->postComment($node, $comment, $comment, TRUE, $field_name);
|
||||
}
|
||||
}
|
||||
|
||||
// Check the first page of the node, and confirm the correct comments are
|
||||
// shown.
|
||||
$this->drupalGet('node/' . $node->id());
|
||||
$this->assertRaw(t('next'), 'Paging links found.');
|
||||
$this->assertRaw('Comment 1 on field comment');
|
||||
$this->assertRaw('Comment 1 on field comment_2');
|
||||
// Navigate to next page of field 1.
|
||||
$this->clickLinkWithXPath('//h3/a[normalize-space(text())=:label]/ancestor::section[1]//a[@rel="next"]', array(':label' => 'Comment 1 on field comment'));
|
||||
// Check only one pager updated.
|
||||
$this->assertRaw('Comment 2 on field comment');
|
||||
$this->assertRaw('Comment 1 on field comment_2');
|
||||
// Return to page 1.
|
||||
$this->drupalGet('node/' . $node->id());
|
||||
// Navigate to next page of field 2.
|
||||
$this->clickLinkWithXPath('//h3/a[normalize-space(text())=:label]/ancestor::section[1]//a[@rel="next"]', array(':label' => 'Comment 1 on field comment_2'));
|
||||
// Check only one pager updated.
|
||||
$this->assertRaw('Comment 1 on field comment');
|
||||
$this->assertRaw('Comment 2 on field comment_2');
|
||||
// Navigate to next page of field 1.
|
||||
$this->clickLinkWithXPath('//h3/a[normalize-space(text())=:label]/ancestor::section[1]//a[@rel="next"]', array(':label' => 'Comment 1 on field comment'));
|
||||
// Check only one pager updated.
|
||||
$this->assertRaw('Comment 2 on field comment');
|
||||
$this->assertRaw('Comment 2 on field comment_2');
|
||||
}
|
||||
|
||||
/**
|
||||
* Follows a link found at a give xpath query.
|
||||
*
|
||||
* Will click the first link found with the given xpath query by default,
|
||||
* or a later one if an index is given.
|
||||
*
|
||||
* If the link is discovered and clicked, the test passes. Fail otherwise.
|
||||
*
|
||||
* @param string $xpath
|
||||
* Xpath query that targets an anchor tag, or set of anchor tags.
|
||||
* @param array $arguments
|
||||
* An array of arguments with keys in the form ':name' matching the
|
||||
* placeholders in the query. The values may be either strings or numeric
|
||||
* values.
|
||||
* @param int $index
|
||||
* Link position counting from zero.
|
||||
*
|
||||
* @return string|false
|
||||
* Page contents on success, or FALSE on failure.
|
||||
*
|
||||
* @see WebTestBase::clickLink()
|
||||
*/
|
||||
protected function clickLinkWithXPath($xpath, $arguments = array(), $index = 0) {
|
||||
$url_before = $this->getUrl();
|
||||
$urls = $this->xpath($xpath, $arguments);
|
||||
if (isset($urls[$index])) {
|
||||
$url_target = $this->getAbsoluteUrl($urls[$index]['href']);
|
||||
$this->pass(SafeMarkup::format('Clicked link %label (@url_target) from @url_before', array('%label' => $xpath, '@url_target' => $url_target, '@url_before' => $url_before)), 'Browser');
|
||||
return $this->drupalGet($url_target);
|
||||
}
|
||||
$this->fail(SafeMarkup::format('Link %label does not exist on @url_before', array('%label' => $xpath, '@url_before' => $url_before)), 'Browser');
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
}
|
195
core/modules/comment/src/Tests/CommentPreviewTest.php
Normal file
195
core/modules/comment/src/Tests/CommentPreviewTest.php
Normal file
|
@ -0,0 +1,195 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\CommentPreviewTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests;
|
||||
|
||||
use Drupal\comment\CommentManagerInterface;
|
||||
use Drupal\Core\Datetime\DrupalDateTime;
|
||||
use Drupal\comment\Entity\Comment;
|
||||
|
||||
/**
|
||||
* Tests comment preview.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class CommentPreviewTest extends CommentTestBase {
|
||||
|
||||
/**
|
||||
* The profile to install as a basis for testing.
|
||||
*
|
||||
* Using the standard profile to test user picture display in comments.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $profile = 'standard';
|
||||
|
||||
/**
|
||||
* Tests comment preview.
|
||||
*/
|
||||
function testCommentPreview() {
|
||||
// As admin user, configure comment settings.
|
||||
$this->drupalLogin($this->adminUser);
|
||||
$this->setCommentPreview(DRUPAL_OPTIONAL);
|
||||
$this->setCommentForm(TRUE);
|
||||
$this->setCommentSubject(TRUE);
|
||||
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
|
||||
$this->drupalLogout();
|
||||
|
||||
// Login as web user and add a user picture.
|
||||
$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.
|
||||
$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'));
|
||||
|
||||
// Check that the preview is displaying the title and body.
|
||||
$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.');
|
||||
|
||||
// Check that the title and body 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.');
|
||||
|
||||
// Check that the user picture is displayed.
|
||||
$this->assertFieldByXPath("//article[contains(@class, 'preview')]//div[contains(@class, 'user-picture')]//img", NULL, 'User picture displayed.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests comment preview.
|
||||
*/
|
||||
public function testCommentPreviewDuplicateSubmission() {
|
||||
// As admin user, configure comment settings.
|
||||
$this->drupalLogin($this->adminUser);
|
||||
$this->setCommentPreview(DRUPAL_OPTIONAL);
|
||||
$this->setCommentForm(TRUE);
|
||||
$this->setCommentSubject(TRUE);
|
||||
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
|
||||
$this->drupalLogout();
|
||||
|
||||
// Login as web user.
|
||||
$this->drupalLogin($this->webUser);
|
||||
|
||||
// As the web user, fill in the comment form and preview the comment.
|
||||
$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'));
|
||||
|
||||
// Check that the preview is displaying the title and body.
|
||||
$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.');
|
||||
|
||||
// Check that the title and body 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.');
|
||||
|
||||
// Store the content of this page.
|
||||
$content = $this->getRawContent();
|
||||
$this->drupalPostForm(NULL, [], 'Save');
|
||||
$this->assertText('Your comment has been posted.');
|
||||
$elements = $this->xpath('//section[contains(@class, "comment-wrapper")]/article');
|
||||
$this->assertEqual(1, count($elements));
|
||||
|
||||
// Reset the content of the page to simulate the browser's back button, and
|
||||
// re-submit the form.
|
||||
$this->setRawContent($content);
|
||||
$this->drupalPostForm(NULL, [], 'Save');
|
||||
$this->assertText('Your comment has been posted.');
|
||||
$elements = $this->xpath('//section[contains(@class, "comment-wrapper")]/article');
|
||||
$this->assertEqual(2, count($elements));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests comment edit, preview, and save.
|
||||
*/
|
||||
function testCommentEditPreviewSave() {
|
||||
$web_user = $this->drupalCreateUser(array('access comments', 'post comments', 'skip comment approval', 'edit own comments'));
|
||||
$this->drupalLogin($this->adminUser);
|
||||
$this->setCommentPreview(DRUPAL_OPTIONAL);
|
||||
$this->setCommentForm(TRUE);
|
||||
$this->setCommentSubject(TRUE);
|
||||
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
|
||||
|
||||
$edit = array();
|
||||
$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['date[date]'] = $date->format('Y-m-d');
|
||||
$edit['date[time]'] = $date->format('H:i:s');
|
||||
$raw_date = $date->getTimestamp();
|
||||
$expected_text_date = format_date($raw_date);
|
||||
$expected_form_date = $date->format('Y-m-d');
|
||||
$expected_form_time = $date->format('H:i:s');
|
||||
$comment = $this->postComment($this->node, $edit['subject[0][value]'], $edit['comment_body[0][value]'], TRUE);
|
||||
$this->drupalPostForm('comment/' . $comment->id() . '/edit', $edit, t('Preview'));
|
||||
|
||||
// Check that the preview is displaying the subject, comment, author and date correctly.
|
||||
$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($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('date[date]', $edit['date[date]'], 'Date field displayed.');
|
||||
$this->assertFieldByName('date[time]', $edit['date[time]'], 'Time field displayed.');
|
||||
|
||||
// Check that saving a comment produces a success message.
|
||||
$this->drupalPostForm('comment/' . $comment->id() . '/edit', $edit, t('Save'));
|
||||
$this->assertText(t('Your comment has been posted.'), 'Comment posted.');
|
||||
|
||||
// Check that the comment fields are correct after loading the saved comment.
|
||||
$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('date[date]', $expected_form_date, 'Date field displayed.');
|
||||
$this->assertFieldByName('date[time]', $expected_form_time, 'Time field displayed.');
|
||||
|
||||
// Submit the form using the displayed values.
|
||||
$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['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'));
|
||||
|
||||
// Check that the saved comment is still correct.
|
||||
$comment_storage = \Drupal::entityManager()->getStorage('comment');
|
||||
$comment_storage->resetCache(array($comment->id()));
|
||||
$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->getCreatedTime(), $raw_date, 'Date loaded.');
|
||||
$this->drupalLogout();
|
||||
|
||||
// Check that the date and time of the comment are correct when edited by
|
||||
// non-admin users.
|
||||
$user_edit = array();
|
||||
$expected_created_time = $comment_loaded->getCreatedTime();
|
||||
$this->drupalLogin($web_user);
|
||||
$this->drupalPostForm('comment/' . $comment->id() . '/edit', $user_edit, t('Save'));
|
||||
$comment_storage->resetCache(array($comment->id()));
|
||||
$comment_loaded = Comment::load($comment->id());
|
||||
$this->assertEqual($comment_loaded->getCreatedTime(), $expected_created_time, 'Expected date and time for comment edited.');
|
||||
$this->drupalLogout();
|
||||
}
|
||||
|
||||
}
|
75
core/modules/comment/src/Tests/CommentRssTest.php
Normal file
75
core/modules/comment/src/Tests/CommentRssTest.php
Normal file
|
@ -0,0 +1,75 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\CommentRssTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests;
|
||||
|
||||
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
|
||||
use Drupal\Core\Entity\Entity\EntityViewDisplay;
|
||||
use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait;
|
||||
|
||||
/**
|
||||
* Tests comments as part of an RSS feed.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class CommentRssTest extends CommentTestBase {
|
||||
|
||||
use AssertPageCacheContextsAndTagsTrait;
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('views');
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
// Setup the rss view display.
|
||||
EntityViewDisplay::create([
|
||||
'status' => TRUE,
|
||||
'targetEntityType' => 'node',
|
||||
'bundle' => 'article',
|
||||
'mode' => 'rss',
|
||||
'content' => ['links' => ['weight' => 100]],
|
||||
])->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests comments as part of an RSS feed.
|
||||
*/
|
||||
function testCommentRss() {
|
||||
// Find comment in RSS feed.
|
||||
$this->drupalLogin($this->webUser);
|
||||
$this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName());
|
||||
$this->drupalGet('rss.xml');
|
||||
|
||||
$this->assertCacheTags([
|
||||
'config:views.view.frontpage', 'node:1', 'node_list', 'node_view', 'user:3',
|
||||
]);
|
||||
$this->assertCacheContexts([
|
||||
'languages:language_interface',
|
||||
'theme',
|
||||
'user.node_grants:view',
|
||||
'user.permissions',
|
||||
'timezone',
|
||||
]);
|
||||
|
||||
$raw = '<comments>' . $this->node->url('canonical', array('fragment' => 'comments', 'absolute' => TRUE)) . '</comments>';
|
||||
$this->assertRaw($raw, 'Comments as part of RSS feed.');
|
||||
|
||||
// Hide comments from RSS feed and check presence.
|
||||
$this->node->set('comment', CommentItemInterface::HIDDEN);
|
||||
$this->node->save();
|
||||
$this->drupalGet('rss.xml');
|
||||
$this->assertNoRaw($raw, 'Hidden comments is not a part of RSS feed.');
|
||||
}
|
||||
}
|
123
core/modules/comment/src/Tests/CommentStatisticsTest.php
Normal file
123
core/modules/comment/src/Tests/CommentStatisticsTest.php
Normal file
|
@ -0,0 +1,123 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\CommentStatisticsTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests;
|
||||
|
||||
use Drupal\comment\CommentManagerInterface;
|
||||
use Drupal\comment\Entity\Comment;
|
||||
use Drupal\user\RoleInterface;
|
||||
|
||||
/**
|
||||
* Tests comment statistics on nodes.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class CommentStatisticsTest extends CommentTestBase {
|
||||
|
||||
/**
|
||||
* A secondary user for posting comments.
|
||||
*
|
||||
* @var \Drupal\user\UserInterface
|
||||
*/
|
||||
protected $webUser2;
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
// Create a second user to post comments.
|
||||
$this->webUser2 = $this->drupalCreateUser(array(
|
||||
'post comments',
|
||||
'create article content',
|
||||
'edit own comments',
|
||||
'post comments',
|
||||
'skip comment approval',
|
||||
'access comments',
|
||||
'access content',
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the node comment statistics.
|
||||
*/
|
||||
function testCommentNodeCommentStatistics() {
|
||||
$node_storage = $this->container->get('entity.manager')->getStorage('node');
|
||||
// Set comments to have subject and preview disabled.
|
||||
$this->drupalLogin($this->adminUser);
|
||||
$this->setCommentPreview(DRUPAL_DISABLED);
|
||||
$this->setCommentForm(TRUE);
|
||||
$this->setCommentSubject(FALSE);
|
||||
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
|
||||
$this->drupalLogout();
|
||||
|
||||
// Checks the initial values of node comment statistics with no comment.
|
||||
$node = $node_storage->load($this->node->id());
|
||||
$this->assertEqual($node->get('comment')->last_comment_timestamp, $this->node->getCreatedTime(), 'The initial value of node last_comment_timestamp is the node created date.');
|
||||
$this->assertEqual($node->get('comment')->last_comment_name, NULL, 'The initial value of node last_comment_name is NULL.');
|
||||
$this->assertEqual($node->get('comment')->last_comment_uid, $this->webUser->id(), 'The initial value of node last_comment_uid is the node uid.');
|
||||
$this->assertEqual($node->get('comment')->comment_count, 0, 'The initial value of node comment_count is zero.');
|
||||
|
||||
// Post comment #1 as web_user2.
|
||||
$this->drupalLogin($this->webUser2);
|
||||
$comment_text = $this->randomMachineName();
|
||||
$this->postComment($this->node, $comment_text);
|
||||
|
||||
// Checks the new values of node comment statistics with comment #1.
|
||||
// The node cache needs to be reset before reload.
|
||||
$node_storage->resetCache(array($this->node->id()));
|
||||
$node = $node_storage->load($this->node->id());
|
||||
$this->assertEqual($node->get('comment')->last_comment_name, NULL, 'The value of node last_comment_name is NULL.');
|
||||
$this->assertEqual($node->get('comment')->last_comment_uid, $this->webUser2->id(), 'The value of node last_comment_uid is the comment #1 uid.');
|
||||
$this->assertEqual($node->get('comment')->comment_count, 1, 'The value of node comment_count is 1.');
|
||||
|
||||
// Prepare for anonymous comment submission (comment approval enabled).
|
||||
$this->drupalLogin($this->adminUser);
|
||||
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, array(
|
||||
'access comments' => TRUE,
|
||||
'post comments' => TRUE,
|
||||
'skip comment approval' => FALSE,
|
||||
));
|
||||
// Ensure that the poster can leave some contact info.
|
||||
$this->setCommentAnonymous('1');
|
||||
$this->drupalLogout();
|
||||
|
||||
// Post comment #2 as anonymous (comment approval enabled).
|
||||
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
|
||||
$anonymous_comment = $this->postComment($this->node, $this->randomMachineName(), '', TRUE);
|
||||
|
||||
// Checks the new values of node comment statistics with comment #2 and
|
||||
// ensure they haven't changed since the comment has not been moderated.
|
||||
// The node needs to be reloaded with the cache reset.
|
||||
$node_storage->resetCache(array($this->node->id()));
|
||||
$node = $node_storage->load($this->node->id());
|
||||
$this->assertEqual($node->get('comment')->last_comment_name, NULL, 'The value of node last_comment_name is still NULL.');
|
||||
$this->assertEqual($node->get('comment')->last_comment_uid, $this->webUser2->id(), 'The value of node last_comment_uid is still the comment #1 uid.');
|
||||
$this->assertEqual($node->get('comment')->comment_count, 1, 'The value of node comment_count is still 1.');
|
||||
|
||||
// Prepare for anonymous comment submission (no approval required).
|
||||
$this->drupalLogin($this->adminUser);
|
||||
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, array(
|
||||
'access comments' => TRUE,
|
||||
'post comments' => TRUE,
|
||||
'skip comment approval' => TRUE,
|
||||
));
|
||||
$this->drupalLogout();
|
||||
|
||||
// Post comment #3 as anonymous.
|
||||
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
|
||||
$anonymous_comment = $this->postComment($this->node, $this->randomMachineName(), '', array('name' => $this->randomMachineName()));
|
||||
$comment_loaded = Comment::load($anonymous_comment->id());
|
||||
|
||||
// Checks the new values of node comment statistics with comment #3.
|
||||
// The node needs to be reloaded with the cache reset.
|
||||
$node_storage->resetCache(array($this->node->id()));
|
||||
$node = $node_storage->load($this->node->id());
|
||||
$this->assertEqual($node->get('comment')->last_comment_name, $comment_loaded->getAuthorName(), 'The value of node last_comment_name is the name of the anonymous user.');
|
||||
$this->assertEqual($node->get('comment')->last_comment_uid, 0, 'The value of node last_comment_uid is zero.');
|
||||
$this->assertEqual($node->get('comment')->comment_count, 2, 'The value of node comment_count is 2.');
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\CommentStringIdEntitiesTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests;
|
||||
|
||||
use Drupal\comment\Entity\CommentType;
|
||||
use Drupal\simpletest\KernelTestBase;
|
||||
|
||||
/**
|
||||
* Tests that comment fields cannot be added to entities with non-integer IDs.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class CommentStringIdEntitiesTest extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array(
|
||||
'comment',
|
||||
'user',
|
||||
'field',
|
||||
'field_ui',
|
||||
'entity_test',
|
||||
'text',
|
||||
);
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->installEntitySchema('comment');
|
||||
$this->installSchema('comment', array('comment_entity_statistics'));
|
||||
// Create the comment body field storage.
|
||||
$this->installConfig(array('field'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that comment fields cannot be added entities with non-integer IDs.
|
||||
*/
|
||||
public function testCommentFieldNonStringId() {
|
||||
try {
|
||||
$bundle = CommentType::create(array(
|
||||
'id' => 'foo',
|
||||
'label' => 'foo',
|
||||
'description' => '',
|
||||
'target_entity_type_id' => 'entity_test_string_id',
|
||||
));
|
||||
$bundle->save();
|
||||
$field_storage = entity_create('field_storage_config', array(
|
||||
'field_name' => 'foo',
|
||||
'entity_type' => 'entity_test_string_id',
|
||||
'settings' => array(
|
||||
'comment_type' => 'entity_test_string_id',
|
||||
),
|
||||
'type' => 'comment',
|
||||
));
|
||||
$field_storage->save();
|
||||
$this->fail('Did not throw an exception as expected.');
|
||||
}
|
||||
catch (\UnexpectedValueException $e) {
|
||||
$this->pass('Exception thrown when trying to create comment field on Entity Type with string ID.');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
394
core/modules/comment/src/Tests/CommentTestBase.php
Normal file
394
core/modules/comment/src/Tests/CommentTestBase.php
Normal file
|
@ -0,0 +1,394 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\CommentTestBase.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests;
|
||||
|
||||
use Drupal\comment\Entity\CommentType;
|
||||
use Drupal\comment\Entity\Comment;
|
||||
use Drupal\comment\CommentInterface;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
|
||||
use Drupal\node\Entity\NodeType;
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
|
||||
/**
|
||||
* Provides setup and helper methods for comment tests.
|
||||
*/
|
||||
abstract class CommentTestBase extends WebTestBase {
|
||||
|
||||
use CommentTestTrait;
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('comment', 'node', 'history', 'field_ui', 'datetime');
|
||||
|
||||
/**
|
||||
* An administrative user with permission to configure comment settings.
|
||||
*
|
||||
* @var \Drupal\user\UserInterface
|
||||
*/
|
||||
protected $adminUser;
|
||||
|
||||
/**
|
||||
* A normal user with permission to post comments.
|
||||
*
|
||||
* @var \Drupal\user\UserInterface
|
||||
*/
|
||||
protected $webUser;
|
||||
|
||||
/**
|
||||
* A test node to which comments will be posted.
|
||||
*
|
||||
* @var \Drupal\node\NodeInterface
|
||||
*/
|
||||
protected $node;
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
// Create an article content type only if it does not yet exist, so that
|
||||
// child classes may specify the standard profile.
|
||||
$types = NodeType::loadMultiple();
|
||||
if (empty($types['article'])) {
|
||||
$this->drupalCreateContentType(array('type' => 'article', 'name' => t('Article')));
|
||||
}
|
||||
|
||||
// Create two test users.
|
||||
$this->adminUser = $this->drupalCreateUser(array(
|
||||
'administer content types',
|
||||
'administer comments',
|
||||
'administer comment types',
|
||||
'administer comment fields',
|
||||
'administer comment display',
|
||||
'skip comment approval',
|
||||
'post comments',
|
||||
'access comments',
|
||||
'access content',
|
||||
));
|
||||
$this->webUser = $this->drupalCreateUser(array(
|
||||
'access comments',
|
||||
'post comments',
|
||||
'create article content',
|
||||
'edit own comments',
|
||||
'skip comment approval',
|
||||
'access content',
|
||||
));
|
||||
|
||||
// Create comment field on article.
|
||||
$this->addDefaultCommentField('node', 'article');
|
||||
|
||||
// Create a test node authored by the web user.
|
||||
$this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'uid' => $this->webUser->id()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Posts a comment.
|
||||
*
|
||||
* @param \Drupal\Core\Entity\EntityInterface|null $entity
|
||||
* Node to post comment on or NULL to post to the previously loaded page.
|
||||
* @param string $comment
|
||||
* Comment body.
|
||||
* @param string $subject
|
||||
* Comment subject.
|
||||
* @param string $contact
|
||||
* Set to NULL for no contact info, TRUE to ignore success checking, and
|
||||
* array of values to set contact info.
|
||||
* @param string $field_name
|
||||
* (optional) Field name through which the comment should be posted.
|
||||
* Defaults to 'comment'.
|
||||
*
|
||||
* @return \Drupal\comment\CommentInterface|null
|
||||
* The posted comment or NULL when posted comment was not found.
|
||||
*/
|
||||
public function postComment($entity, $comment, $subject = '', $contact = NULL, $field_name = 'comment') {
|
||||
$edit = array();
|
||||
$edit['comment_body[0][value]'] = $comment;
|
||||
|
||||
if ($entity !== NULL) {
|
||||
$field = FieldConfig::loadByName('node', $entity->bundle(), $field_name);
|
||||
}
|
||||
else {
|
||||
$field = FieldConfig::loadByName('node', 'article', $field_name);
|
||||
}
|
||||
$preview_mode = $field->getSetting('preview');
|
||||
|
||||
// Must get the page before we test for fields.
|
||||
if ($entity !== NULL) {
|
||||
$this->drupalGet('comment/reply/node/' . $entity->id() . '/' . $field_name);
|
||||
}
|
||||
|
||||
// Determine the visibility of subject form field.
|
||||
if (entity_get_form_display('comment', 'comment', 'default')->getComponent('subject')) {
|
||||
// Subject input allowed.
|
||||
$edit['subject[0][value]'] = $subject;
|
||||
}
|
||||
else {
|
||||
$this->assertNoFieldByName('subject[0][value]', '', 'Subject field not found.');
|
||||
}
|
||||
|
||||
if ($contact !== NULL && is_array($contact)) {
|
||||
$edit += $contact;
|
||||
}
|
||||
switch ($preview_mode) {
|
||||
case DRUPAL_REQUIRED:
|
||||
// Preview required so no save button should be found.
|
||||
$this->assertNoFieldByName('op', t('Save'), 'Save button not found.');
|
||||
$this->drupalPostForm(NULL, $edit, t('Preview'));
|
||||
// Don't break here so that we can test post-preview field presence and
|
||||
// function below.
|
||||
case DRUPAL_OPTIONAL:
|
||||
$this->assertFieldByName('op', t('Preview'), 'Preview button found.');
|
||||
$this->assertFieldByName('op', t('Save'), 'Save button found.');
|
||||
$this->drupalPostForm(NULL, $edit, t('Save'));
|
||||
break;
|
||||
|
||||
case DRUPAL_DISABLED:
|
||||
$this->assertNoFieldByName('op', t('Preview'), 'Preview button not found.');
|
||||
$this->assertFieldByName('op', t('Save'), 'Save button found.');
|
||||
$this->drupalPostForm(NULL, $edit, t('Save'));
|
||||
break;
|
||||
}
|
||||
$match = array();
|
||||
// Get comment ID
|
||||
preg_match('/#comment-([0-9]+)/', $this->getURL(), $match);
|
||||
|
||||
// Get comment.
|
||||
if ($contact !== TRUE) { // If true then attempting to find error message.
|
||||
if ($subject) {
|
||||
$this->assertText($subject, 'Comment subject posted.');
|
||||
}
|
||||
$this->assertText($comment, 'Comment body posted.');
|
||||
$this->assertTrue((!empty($match) && !empty($match[1])), 'Comment id found.');
|
||||
}
|
||||
|
||||
if (isset($match[1])) {
|
||||
\Drupal::entityManager()->getStorage('comment')->resetCache(array($match[1]));
|
||||
return Comment::load($match[1]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks current page for specified comment.
|
||||
*
|
||||
* @param \Drupal\comment\CommentInterface $comment
|
||||
* The comment object.
|
||||
* @param bool $reply
|
||||
* Boolean indicating whether the comment is a reply to another comment.
|
||||
*
|
||||
* @return boolean
|
||||
* Boolean indicating whether the comment was found.
|
||||
*/
|
||||
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';
|
||||
|
||||
return (boolean) preg_match($regex, $this->getRawContent());
|
||||
}
|
||||
else {
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a comment.
|
||||
*
|
||||
* @param \Drupal\comment\CommentInterface $comment
|
||||
* Comment to delete.
|
||||
*/
|
||||
function deleteComment(CommentInterface $comment) {
|
||||
$this->drupalPostForm('comment/' . $comment->id() . '/delete', array(), t('Delete'));
|
||||
$this->assertText(t('The comment and all its replies have been deleted.'), 'Comment deleted.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value governing whether the subject field should be enabled.
|
||||
*
|
||||
* @param bool $enabled
|
||||
* Boolean specifying whether the subject field should be enabled.
|
||||
*/
|
||||
public function setCommentSubject($enabled) {
|
||||
$form_display = entity_get_form_display('comment', 'comment', 'default');
|
||||
if ($enabled) {
|
||||
$form_display->setComponent('subject', array(
|
||||
'type' => 'string_textfield',
|
||||
));
|
||||
}
|
||||
else {
|
||||
$form_display->removeComponent('subject');
|
||||
}
|
||||
$form_display->save();
|
||||
// Display status message.
|
||||
$this->pass('Comment subject ' . ($enabled ? 'enabled' : 'disabled') . '.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value governing the previewing mode for the comment form.
|
||||
*
|
||||
* @param int $mode
|
||||
* The preview mode: DRUPAL_DISABLED, DRUPAL_OPTIONAL or DRUPAL_REQUIRED.
|
||||
* @param string $field_name
|
||||
* (optional) Field name through which the comment should be posted.
|
||||
* Defaults to 'comment'.
|
||||
*/
|
||||
public function setCommentPreview($mode, $field_name = 'comment') {
|
||||
switch ($mode) {
|
||||
case DRUPAL_DISABLED:
|
||||
$mode_text = 'disabled';
|
||||
break;
|
||||
|
||||
case DRUPAL_OPTIONAL:
|
||||
$mode_text = 'optional';
|
||||
break;
|
||||
|
||||
case DRUPAL_REQUIRED:
|
||||
$mode_text = 'required';
|
||||
break;
|
||||
}
|
||||
$this->setCommentSettings('preview', $mode, format_string('Comment preview @mode_text.', array('@mode_text' => $mode_text)), $field_name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value governing whether the comment form is on its own page.
|
||||
*
|
||||
* @param bool $enabled
|
||||
* TRUE if the comment form should be displayed on the same page as the
|
||||
* comments; FALSE if it should be displayed on its own page.
|
||||
* @param string $field_name
|
||||
* (optional) Field name through which the comment should be posted.
|
||||
* Defaults to 'comment'.
|
||||
*/
|
||||
public function setCommentForm($enabled, $field_name = 'comment') {
|
||||
$this->setCommentSettings('form_location', ($enabled ? CommentItemInterface::FORM_BELOW : CommentItemInterface::FORM_SEPARATE_PAGE), 'Comment controls ' . ($enabled ? 'enabled' : 'disabled') . '.', $field_name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value governing restrictions on anonymous comments.
|
||||
*
|
||||
* @param integer $level
|
||||
* The level of the contact information allowed for anonymous comments:
|
||||
* - 0: No contact information allowed.
|
||||
* - 1: Contact information allowed but not required.
|
||||
* - 2: Contact information required.
|
||||
*/
|
||||
function setCommentAnonymous($level) {
|
||||
$this->setCommentSettings('anonymous', $level, format_string('Anonymous commenting set to level @level.', array('@level' => $level)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value specifying the default number of comments per page.
|
||||
*
|
||||
* @param int $number
|
||||
* Comments per page value.
|
||||
* @param string $field_name
|
||||
* (optional) Field name through which the comment should be posted.
|
||||
* Defaults to 'comment'.
|
||||
*/
|
||||
public function setCommentsPerPage($number, $field_name = 'comment') {
|
||||
$this->setCommentSettings('per_page', $number, format_string('Number of comments per page set to @number.', array('@number' => $number)), $field_name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a comment settings variable for the article content type.
|
||||
*
|
||||
* @param string $name
|
||||
* Name of variable.
|
||||
* @param string $value
|
||||
* Value of variable.
|
||||
* @param string $message
|
||||
* Status message to display.
|
||||
* @param string $field_name
|
||||
* (optional) Field name through which the comment should be posted.
|
||||
* Defaults to 'comment'.
|
||||
*/
|
||||
public function setCommentSettings($name, $value, $message, $field_name = 'comment') {
|
||||
$field = FieldConfig::loadByName('node', 'article', $field_name);
|
||||
$field->setSetting($name, $value);
|
||||
$field->save();
|
||||
// Display status message.
|
||||
$this->pass($message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the commenter's contact information is displayed.
|
||||
*
|
||||
* @return boolean
|
||||
* Contact info is available.
|
||||
*/
|
||||
function commentContactInfoAvailable() {
|
||||
return preg_match('/(input).*?(name="name").*?(input).*?(name="mail").*?(input).*?(name="homepage")/s', $this->getRawContent());
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs the specified operation on the specified comment.
|
||||
*
|
||||
* @param \Drupal\comment\CommentInterface $comment
|
||||
* Comment to perform operation on.
|
||||
* @param string $operation
|
||||
* Operation to perform.
|
||||
* @param bool $approval
|
||||
* Operation is found on approval page.
|
||||
*/
|
||||
function performCommentOperation(CommentInterface $comment, $operation, $approval = FALSE) {
|
||||
$edit = array();
|
||||
$edit['operation'] = $operation;
|
||||
$edit['comments[' . $comment->id() . ']'] = TRUE;
|
||||
$this->drupalPostForm('admin/content/comment' . ($approval ? '/approval' : ''), $edit, t('Update'));
|
||||
|
||||
if ($operation == 'delete') {
|
||||
$this->drupalPostForm(NULL, array(), t('Delete comments'));
|
||||
$this->assertRaw(\Drupal::translation()->formatPlural(1, 'Deleted 1 comment.', 'Deleted @count comments.'), format_string('Operation "@operation" was performed on comment.', array('@operation' => $operation)));
|
||||
}
|
||||
else {
|
||||
$this->assertText(t('The update has been performed.'), format_string('Operation "@operation" was performed on comment.', array('@operation' => $operation)));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the comment ID for an unapproved comment.
|
||||
*
|
||||
* @param string $subject
|
||||
* Comment subject to find.
|
||||
*
|
||||
* @return integer
|
||||
* Comment id.
|
||||
*/
|
||||
function getUnapprovedComment($subject) {
|
||||
$this->drupalGet('admin/content/comment/approval');
|
||||
preg_match('/href="(.*?)#comment-([^"]+)"(.*?)>(' . $subject . ')/', $this->getRawContent(), $match);
|
||||
|
||||
return $match[2];
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a comment comment type (bundle).
|
||||
*
|
||||
* @param string $label
|
||||
* The comment type label.
|
||||
*
|
||||
* @return \Drupal\comment\Entity\CommentType
|
||||
* Created comment type.
|
||||
*/
|
||||
protected function createCommentType($label) {
|
||||
$bundle = CommentType::create(array(
|
||||
'id' => $label,
|
||||
'label' => $label,
|
||||
'description' => '',
|
||||
'target_entity_type_id' => 'node',
|
||||
));
|
||||
$bundle->save();
|
||||
return $bundle;
|
||||
}
|
||||
|
||||
}
|
130
core/modules/comment/src/Tests/CommentTestTrait.php
Normal file
130
core/modules/comment/src/Tests/CommentTestTrait.php
Normal file
|
@ -0,0 +1,130 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\CommentTestTrait.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests;
|
||||
|
||||
use Drupal\Component\Utility\SafeMarkup;
|
||||
use Drupal\Component\Utility\Unicode;
|
||||
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
|
||||
|
||||
/**
|
||||
* Provides common functionality for the Comment test classes.
|
||||
*/
|
||||
trait CommentTestTrait {
|
||||
|
||||
/**
|
||||
* Adds the default comment field to an entity.
|
||||
*
|
||||
* Attaches a comment field named 'comment' to the given entity type and
|
||||
* bundle. Largely replicates the default behavior in Drupal 7 and earlier.
|
||||
*
|
||||
* @param string $entity_type
|
||||
* The entity type to attach the default comment field to.
|
||||
* @param string $bundle
|
||||
* The bundle to attach the default comment field to.
|
||||
* @param string $field_name
|
||||
* (optional) Field name to use for the comment field. Defaults to
|
||||
* 'comment'.
|
||||
* @param int $default_value
|
||||
* (optional) Default value, one of CommentItemInterface::HIDDEN,
|
||||
* CommentItemInterface::OPEN, CommentItemInterface::CLOSED. Defaults to
|
||||
* CommentItemInterface::OPEN.
|
||||
* @param string $comment_type_id
|
||||
* (optional) ID of comment type to use. Defaults to 'comment'.
|
||||
*/
|
||||
public function addDefaultCommentField($entity_type, $bundle, $field_name = 'comment', $default_value = CommentItemInterface::OPEN, $comment_type_id = 'comment') {
|
||||
$entity_manager = \Drupal::entityManager();
|
||||
// Create the comment type if needed.
|
||||
$comment_type_storage = $entity_manager->getStorage('comment_type');
|
||||
if ($comment_type = $comment_type_storage->load($comment_type_id)) {
|
||||
if ($comment_type->getTargetEntityTypeId() !== $entity_type) {
|
||||
throw new \InvalidArgumentException(SafeMarkup::format('The given comment type id %id can only be used with the %entity_type entity type', array(
|
||||
'%id' => $comment_type_id,
|
||||
'%entity_type' => $entity_type,
|
||||
)));
|
||||
}
|
||||
}
|
||||
else {
|
||||
$comment_type_storage->create(array(
|
||||
'id' => $comment_type_id,
|
||||
'label' => Unicode::ucfirst($comment_type_id),
|
||||
'target_entity_type_id' => $entity_type,
|
||||
'description' => 'Default comment field',
|
||||
))->save();
|
||||
}
|
||||
// Add a body field to the comment type.
|
||||
\Drupal::service('comment.manager')->addBodyField($comment_type_id);
|
||||
|
||||
// Add a comment field to the host entity type. Create the field storage if
|
||||
// needed.
|
||||
if (!array_key_exists($field_name, $entity_manager->getFieldStorageDefinitions($entity_type))) {
|
||||
$entity_manager->getStorage('field_storage_config')->create(array(
|
||||
'entity_type' => $entity_type,
|
||||
'field_name' => $field_name,
|
||||
'type' => 'comment',
|
||||
'translatable' => TRUE,
|
||||
'settings' => array(
|
||||
'comment_type' => $comment_type_id,
|
||||
),
|
||||
))->save();
|
||||
}
|
||||
// Create the field if needed, and configure its form and view displays.
|
||||
if (!array_key_exists($field_name, $entity_manager->getFieldDefinitions($entity_type, $bundle))) {
|
||||
$entity_manager->getStorage('field_config')->create(array(
|
||||
'label' => 'Comments',
|
||||
'description' => '',
|
||||
'field_name' => $field_name,
|
||||
'entity_type' => $entity_type,
|
||||
'bundle' => $bundle,
|
||||
'required' => 1,
|
||||
'default_value' => array(
|
||||
array(
|
||||
'status' => $default_value,
|
||||
'cid' => 0,
|
||||
'last_comment_name' => '',
|
||||
'last_comment_timestamp' => 0,
|
||||
'last_comment_uid' => 0,
|
||||
),
|
||||
),
|
||||
))->save();
|
||||
|
||||
// Entity form displays: assign widget settings for the 'default' form
|
||||
// mode, and hide the field in all other form modes.
|
||||
entity_get_form_display($entity_type, $bundle, 'default')
|
||||
->setComponent($field_name, array(
|
||||
'type' => 'comment_default',
|
||||
'weight' => 20,
|
||||
))
|
||||
->save();
|
||||
foreach ($entity_manager->getFormModes($entity_type) as $id => $form_mode) {
|
||||
$display = entity_get_form_display($entity_type, $bundle, $id);
|
||||
// Only update existing displays.
|
||||
if ($display && !$display->isNew()) {
|
||||
$display->removeComponent($field_name)->save();
|
||||
}
|
||||
}
|
||||
|
||||
// Entity view displays: assign widget settings for the 'default' view
|
||||
// mode, and hide the field in all other view modes.
|
||||
entity_get_display($entity_type, $bundle, 'default')
|
||||
->setComponent($field_name, array(
|
||||
'label' => 'above',
|
||||
'type' => 'comment_default',
|
||||
'weight' => 20,
|
||||
))
|
||||
->save();
|
||||
foreach ($entity_manager->getViewModes($entity_type) as $id => $view_mode) {
|
||||
$display = entity_get_display($entity_type, $bundle, $id);
|
||||
// Only update existing displays.
|
||||
if ($display && !$display->isNew()) {
|
||||
$display->removeComponent($field_name)->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
176
core/modules/comment/src/Tests/CommentThreadingTest.php
Normal file
176
core/modules/comment/src/Tests/CommentThreadingTest.php
Normal file
|
@ -0,0 +1,176 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\CommentThreadingTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests;
|
||||
|
||||
use Drupal\comment\CommentManagerInterface;
|
||||
|
||||
/**
|
||||
* Tests to make sure the comment number increments properly.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class CommentThreadingTest extends CommentTestBase {
|
||||
/**
|
||||
* Tests the comment threading.
|
||||
*/
|
||||
function testCommentThreading() {
|
||||
// Set comments to have a subject with preview disabled.
|
||||
$this->drupalLogin($this->adminUser);
|
||||
$this->setCommentPreview(DRUPAL_DISABLED);
|
||||
$this->setCommentForm(TRUE);
|
||||
$this->setCommentSubject(TRUE);
|
||||
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
|
||||
$this->drupalLogout();
|
||||
|
||||
// Create a node.
|
||||
$this->drupalLogin($this->webUser);
|
||||
$this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'uid' => $this->webUser->id()));
|
||||
|
||||
// Post comment #1.
|
||||
$this->drupalLogin($this->webUser);
|
||||
$subject_text = $this->randomMachineName();
|
||||
$comment_text = $this->randomMachineName();
|
||||
|
||||
$comment1 = $this->postComment($this->node, $comment_text, $subject_text, TRUE);
|
||||
// Confirm that the comment was created and has the correct threading.
|
||||
$this->assertTrue($this->commentExists($comment1), 'Comment #1. Comment found.');
|
||||
$this->assertEqual($comment1->getThread(), '01/');
|
||||
// Confirm that there is no reference to a parent comment.
|
||||
$this->assertNoParentLink($comment1->id());
|
||||
|
||||
// Post comment #2 following the comment #1 to test if it correctly jumps
|
||||
// out the indentation in case there is a thread above.
|
||||
$subject_text = $this->randomMachineName();
|
||||
$comment_text = $this->randomMachineName();
|
||||
$this->postComment($this->node, $comment_text, $subject_text, TRUE);
|
||||
|
||||
// Reply to comment #1 creating comment #1_3.
|
||||
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment1->id());
|
||||
$comment1_3 = $this->postComment(NULL, $this->randomMachineName(), '', TRUE);
|
||||
|
||||
// Confirm that the comment was created and has the correct threading.
|
||||
$this->assertTrue($this->commentExists($comment1_3, TRUE), 'Comment #1_3. Reply found.');
|
||||
$this->assertEqual($comment1_3->getThread(), '01.00/');
|
||||
// Confirm that there is a link to the parent comment.
|
||||
$this->assertParentLink($comment1_3->id(), $comment1->id());
|
||||
|
||||
|
||||
// Reply to comment #1_3 creating comment #1_3_4.
|
||||
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment1_3->id());
|
||||
$comment1_3_4 = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
|
||||
|
||||
// Confirm that the comment was created and has the correct threading.
|
||||
$this->assertTrue($this->commentExists($comment1_3_4, TRUE), 'Comment #1_3_4. Second reply found.');
|
||||
$this->assertEqual($comment1_3_4->getThread(), '01.00.00/');
|
||||
// Confirm that there is a link to the parent comment.
|
||||
$this->assertParentLink($comment1_3_4->id(), $comment1_3->id());
|
||||
|
||||
// Reply to comment #1 creating comment #1_5.
|
||||
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment1->id());
|
||||
|
||||
$comment1_5 = $this->postComment(NULL, $this->randomMachineName(), '', TRUE);
|
||||
|
||||
// Confirm that the comment was created and has the correct threading.
|
||||
$this->assertTrue($this->commentExists($comment1_5), 'Comment #1_5. Third reply found.');
|
||||
$this->assertEqual($comment1_5->getThread(), '01.01/');
|
||||
// Confirm that there is a link to the parent comment.
|
||||
$this->assertParentLink($comment1_5->id(), $comment1->id());
|
||||
|
||||
// Post comment #3 overall comment #5.
|
||||
$this->drupalLogin($this->webUser);
|
||||
$subject_text = $this->randomMachineName();
|
||||
$comment_text = $this->randomMachineName();
|
||||
|
||||
$comment5 = $this->postComment($this->node, $comment_text, $subject_text, TRUE);
|
||||
// Confirm that the comment was created and has the correct threading.
|
||||
$this->assertTrue($this->commentExists($comment5), 'Comment #5. Second comment found.');
|
||||
$this->assertEqual($comment5->getThread(), '03/');
|
||||
// Confirm that there is no link to a parent comment.
|
||||
$this->assertNoParentLink($comment5->id());
|
||||
|
||||
// Reply to comment #5 creating comment #5_6.
|
||||
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment5->id());
|
||||
$comment5_6 = $this->postComment(NULL, $this->randomMachineName(), '', TRUE);
|
||||
|
||||
// Confirm that the comment was created and has the correct threading.
|
||||
$this->assertTrue($this->commentExists($comment5_6, TRUE), 'Comment #6. Reply found.');
|
||||
$this->assertEqual($comment5_6->getThread(), '03.00/');
|
||||
// Confirm that there is a link to the parent comment.
|
||||
$this->assertParentLink($comment5_6->id(), $comment5->id());
|
||||
|
||||
// Reply to comment #5_6 creating comment #5_6_7.
|
||||
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment5_6->id());
|
||||
$comment5_6_7 = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName(), TRUE);
|
||||
|
||||
// Confirm that the comment was created and has the correct threading.
|
||||
$this->assertTrue($this->commentExists($comment5_6_7, TRUE), 'Comment #5_6_7. Second reply found.');
|
||||
$this->assertEqual($comment5_6_7->getThread(), '03.00.00/');
|
||||
// Confirm that there is a link to the parent comment.
|
||||
$this->assertParentLink($comment5_6_7->id(), $comment5_6->id());
|
||||
|
||||
// Reply to comment #5 creating comment #5_8.
|
||||
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment/' . $comment5->id());
|
||||
$comment5_8 = $this->postComment(NULL, $this->randomMachineName(), '', TRUE);
|
||||
|
||||
// Confirm that the comment was created and has the correct threading.
|
||||
$this->assertTrue($this->commentExists($comment5_8), 'Comment #5_8. Third reply found.');
|
||||
$this->assertEqual($comment5_8->getThread(), '03.01/');
|
||||
// Confirm that there is a link to the parent comment.
|
||||
$this->assertParentLink($comment5_8->id(), $comment5->id());
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the link to the specified parent comment is present.
|
||||
*
|
||||
* @param int $cid
|
||||
* The comment ID to check.
|
||||
* @param int $pid
|
||||
* The expected parent comment ID.
|
||||
*/
|
||||
protected function assertParentLink($cid, $pid) {
|
||||
// This pattern matches a markup structure like:
|
||||
// <a id="comment-2"></a>
|
||||
// <article>
|
||||
// <p class="parent">
|
||||
// <a href="...comment-1"></a>
|
||||
// </p>
|
||||
// </article>
|
||||
$pattern = "//a[@id='comment-$cid']/following-sibling::article//p[contains(@class, 'parent')]//a[contains(@href, 'comment-$pid')]";
|
||||
|
||||
$this->assertFieldByXpath($pattern, NULL, format_string(
|
||||
'Comment %cid has a link to parent %pid.',
|
||||
array(
|
||||
'%cid' => $cid,
|
||||
'%pid' => $pid,
|
||||
)
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the specified comment does not have a link to a parent.
|
||||
*
|
||||
* @param int $cid
|
||||
* The comment ID to check.
|
||||
*/
|
||||
protected function assertNoParentLink($cid) {
|
||||
// This pattern matches a markup structure like:
|
||||
// <a id="comment-2"></a>
|
||||
// <article>
|
||||
// <p class="parent"></p>
|
||||
// </article>
|
||||
|
||||
$pattern = "//a[@id='comment-$cid']/following-sibling::article//p[contains(@class, 'parent')]";
|
||||
$this->assertNoFieldByXpath($pattern, NULL, format_string(
|
||||
'Comment %cid does not have a link to a parent.',
|
||||
array(
|
||||
'%cid' => $cid,
|
||||
)
|
||||
));
|
||||
}
|
||||
|
||||
}
|
68
core/modules/comment/src/Tests/CommentTitleTest.php
Normal file
68
core/modules/comment/src/Tests/CommentTitleTest.php
Normal file
|
@ -0,0 +1,68 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\CommentTitleTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests;
|
||||
|
||||
/**
|
||||
* Tests to ensure that appropriate and accessible markup is created for comment
|
||||
* titles.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class CommentTitleTest extends CommentTestBase {
|
||||
/**
|
||||
* Tests markup for comments with empty titles.
|
||||
*/
|
||||
public function testCommentEmptyTitles() {
|
||||
// Installs module that sets comments to an empty string.
|
||||
\Drupal::service('module_installer')->install(array('comment_empty_title_test'));
|
||||
|
||||
// Set comments to have a subject with preview disabled.
|
||||
$this->setCommentPreview(DRUPAL_DISABLED);
|
||||
$this->setCommentForm(TRUE);
|
||||
$this->setCommentSubject(TRUE);
|
||||
|
||||
// Create a node.
|
||||
$this->drupalLogin($this->webUser);
|
||||
$this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'uid' => $this->webUser->id()));
|
||||
|
||||
// Post comment #1 and verify that h3's are not rendered.
|
||||
$subject_text = $this->randomMachineName();
|
||||
$comment_text = $this->randomMachineName();
|
||||
$comment = $this->postComment($this->node, $comment_text, $subject_text, TRUE);
|
||||
// Confirm that the comment was created.
|
||||
$regex = '/<a id="comment-' . $comment->id() . '"(.*?)';
|
||||
$regex .= $comment->comment_body->value . '(.*?)';
|
||||
$regex .= '/s';
|
||||
$this->assertPattern($regex, 'Comment is created successfully');
|
||||
// Tests that markup is not generated for the comment without header.
|
||||
$this->assertNoPattern('|<h3[^>]*></h3>|', 'Comment title H3 element not found when title is an empty string.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests markup for comments with populated titles.
|
||||
*/
|
||||
public function testCommentPopulatedTitles() {
|
||||
// Set comments to have a subject with preview disabled.
|
||||
$this->setCommentPreview(DRUPAL_DISABLED);
|
||||
$this->setCommentForm(TRUE);
|
||||
$this->setCommentSubject(TRUE);
|
||||
|
||||
// Create a node.
|
||||
$this->drupalLogin($this->webUser);
|
||||
$this->node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1, 'uid' => $this->webUser->id()));
|
||||
|
||||
// Post comment #1 and verify that title is rendered in h3.
|
||||
$subject_text = $this->randomMachineName();
|
||||
$comment_text = $this->randomMachineName();
|
||||
$comment1 = $this->postComment($this->node, $comment_text, $subject_text, TRUE);
|
||||
// Confirm that the comment was created.
|
||||
$this->assertTrue($this->commentExists($comment1), 'Comment #1. Comment found.');
|
||||
// Tests that markup is created for comment with heading.
|
||||
$this->assertPattern('|<h3[^>]*><a[^>]*>' . $subject_text . '</a></h3>|', 'Comment title is rendered in h3 when title populated.');
|
||||
}
|
||||
}
|
122
core/modules/comment/src/Tests/CommentTokenReplaceTest.php
Normal file
122
core/modules/comment/src/Tests/CommentTokenReplaceTest.php
Normal file
|
@ -0,0 +1,122 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\CommentTokenReplaceTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests;
|
||||
|
||||
use Drupal\Component\Utility\SafeMarkup;
|
||||
use Drupal\Component\Utility\Xss;
|
||||
use Drupal\comment\Entity\Comment;
|
||||
use Drupal\node\Entity\Node;
|
||||
|
||||
/**
|
||||
* Generates text using placeholders for dummy content to check comment token
|
||||
* replacement.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class CommentTokenReplaceTest extends CommentTestBase {
|
||||
/**
|
||||
* Creates a comment, then tests the tokens generated from it.
|
||||
*/
|
||||
function testCommentTokenReplacement() {
|
||||
$token_service = \Drupal::token();
|
||||
$language_interface = \Drupal::languageManager()->getCurrentLanguage();
|
||||
$url_options = array(
|
||||
'absolute' => TRUE,
|
||||
'language' => $language_interface,
|
||||
);
|
||||
|
||||
$this->drupalLogin($this->adminUser);
|
||||
|
||||
// Set comment variables.
|
||||
$this->setCommentSubject(TRUE);
|
||||
|
||||
// Create a node and a comment.
|
||||
$node = $this->drupalCreateNode(array('type' => 'article'));
|
||||
$parent_comment = $this->postComment($node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
|
||||
|
||||
// Post a reply to the comment.
|
||||
$this->drupalGet('comment/reply/node/' . $node->id() . '/comment/' . $parent_comment->id());
|
||||
$child_comment = $this->postComment(NULL, $this->randomMachineName(), $this->randomMachineName());
|
||||
$comment = Comment::load($child_comment->id());
|
||||
$comment->setHomepage('http://example.org/');
|
||||
|
||||
// Add HTML to ensure that sanitation of some fields tested directly.
|
||||
$comment->setSubject('<blink>Blinking Comment</blink>');
|
||||
|
||||
// Generate and test sanitized tokens.
|
||||
$tests = array();
|
||||
$tests['[comment:cid]'] = $comment->id();
|
||||
$tests['[comment:hostname]'] = SafeMarkup::checkPlain($comment->getHostname());
|
||||
$tests['[comment:author]'] = Xss::filter($comment->getAuthorName());
|
||||
$tests['[comment:mail]'] = SafeMarkup::checkPlain($this->adminUser->getEmail());
|
||||
$tests['[comment:homepage]'] = check_url($comment->getHomepage());
|
||||
$tests['[comment:title]'] = Xss::filter($comment->getSubject());
|
||||
$tests['[comment:body]'] = $comment->comment_body->processed;
|
||||
$tests['[comment:langcode]'] = SafeMarkup::checkPlain($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: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]'] = SafeMarkup::checkPlain($parent_comment->getSubject());
|
||||
$tests['[comment:entity]'] = SafeMarkup::checkPlain($node->getTitle());
|
||||
// Test node specific tokens.
|
||||
$tests['[comment:entity:nid]'] = $comment->getCommentedEntityId();
|
||||
$tests['[comment:entity:title]'] = SafeMarkup::checkPlain($node->getTitle());
|
||||
$tests['[comment:author:uid]'] = $comment->getOwnerId();
|
||||
$tests['[comment:author:name]'] = SafeMarkup::checkPlain($this->adminUser->getUsername());
|
||||
|
||||
// Test to make sure that we generated something for each token.
|
||||
$this->assertFalse(in_array(0, array_map('strlen', $tests)), 'No empty tokens generated.');
|
||||
|
||||
foreach ($tests as $input => $expected) {
|
||||
$output = $token_service->replace($input, array('comment' => $comment), array('langcode' => $language_interface->getId()));
|
||||
$this->assertEqual($output, $expected, format_string('Sanitized comment token %token replaced.', array('%token' => $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();
|
||||
$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)));
|
||||
|
||||
// Load node so comment_count gets computed.
|
||||
$node = Node::load($node->id());
|
||||
|
||||
// Generate comment tokens for the node (it has 2 comments, both new).
|
||||
$tests = array();
|
||||
$tests['[entity:comment-count]'] = 2;
|
||||
$tests['[entity:comment-count-new]'] = 2;
|
||||
|
||||
foreach ($tests as $input => $expected) {
|
||||
$output = $token_service->replace($input, array('entity' => $node, 'node' => $node), array('langcode' => $language_interface->getId()));
|
||||
$this->assertEqual($output, $expected, format_string('Node comment token %token replaced.', array('%token' => $input)));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
209
core/modules/comment/src/Tests/CommentTranslationUITest.php
Normal file
209
core/modules/comment/src/Tests/CommentTranslationUITest.php
Normal file
|
@ -0,0 +1,209 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\CommentTranslationUITest.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests;
|
||||
|
||||
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
|
||||
use Drupal\content_translation\Tests\ContentTranslationUITestBase;
|
||||
use Drupal\language\Entity\ConfigurableLanguage;
|
||||
|
||||
/**
|
||||
* Tests the Comment Translation UI.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class CommentTranslationUITest extends ContentTranslationUITestBase {
|
||||
|
||||
use CommentTestTrait;
|
||||
|
||||
/**
|
||||
* The subject of the test comment.
|
||||
*/
|
||||
protected $subject;
|
||||
|
||||
/**
|
||||
* An administrative user with permission to administer comments.
|
||||
*
|
||||
* @var \Drupal\user\UserInterface
|
||||
*/
|
||||
protected $adminUser;
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('language', 'content_translation', 'node', 'comment');
|
||||
|
||||
protected function setUp() {
|
||||
$this->entityTypeId = 'comment';
|
||||
$this->nodeBundle = 'article';
|
||||
$this->bundle = 'comment_article';
|
||||
$this->testLanguageSelector = FALSE;
|
||||
$this->subject = $this->randomMachineName();
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
/**
|
||||
* Overrides \Drupal\content_translation\Tests\ContentTranslationUITestBase::setupBundle().
|
||||
*/
|
||||
function setupBundle() {
|
||||
parent::setupBundle();
|
||||
$this->drupalCreateContentType(array('type' => $this->nodeBundle, 'name' => $this->nodeBundle));
|
||||
// Add a comment field to the article content type.
|
||||
$this->addDefaultCommentField('node', 'article', 'comment_article', CommentItemInterface::OPEN, 'comment_article');
|
||||
// Create a page content type.
|
||||
$this->drupalCreateContentType(array('type' => 'page', 'name' => 'page'));
|
||||
// Add a comment field to the page content type - this one won't be
|
||||
// translatable.
|
||||
$this->addDefaultCommentField('node', 'page', 'comment');
|
||||
// Mark this bundle as translatable.
|
||||
$this->container->get('content_translation.manager')->setEnabled('comment', 'comment_article', TRUE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Overrides \Drupal\content_translation\Tests\ContentTranslationUITestBase::getTranslatorPermission().
|
||||
*/
|
||||
protected function getTranslatorPermissions() {
|
||||
return array_merge(parent::getTranslatorPermissions(), array('post comments', 'administer comments', 'access comments'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Overrides \Drupal\content_translation\Tests\ContentTranslationUITestBase::createEntity().
|
||||
*/
|
||||
protected function createEntity($values, $langcode, $comment_type = 'comment_article') {
|
||||
if ($comment_type == 'comment_article') {
|
||||
// This is the article node type, with the 'comment_article' field.
|
||||
$node_type = 'article';
|
||||
$field_name = 'comment_article';
|
||||
}
|
||||
else {
|
||||
// This is the page node type with the non-translatable 'comment' field.
|
||||
$node_type = 'page';
|
||||
$field_name = 'comment';
|
||||
}
|
||||
$node = $this->drupalCreateNode(array(
|
||||
'type' => $node_type,
|
||||
$field_name => array(
|
||||
array('status' => CommentItemInterface::OPEN)
|
||||
),
|
||||
));
|
||||
$values['entity_id'] = $node->id();
|
||||
$values['entity_type'] = 'node';
|
||||
$values['field_name'] = $field_name;
|
||||
$values['uid'] = $node->getOwnerId();
|
||||
return parent::createEntity($values, $langcode, $comment_type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Overrides \Drupal\content_translation\Tests\ContentTranslationUITestBase::getNewEntityValues().
|
||||
*/
|
||||
protected function getNewEntityValues($langcode) {
|
||||
// Comment subject is not translatable hence we use a fixed value.
|
||||
return array(
|
||||
'subject' => array(array('value' => $this->subject)),
|
||||
'comment_body' => array(array('value' => $this->randomMachineName(16))),
|
||||
) + parent::getNewEntityValues($langcode);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function doTestPublishedStatus() {
|
||||
$entity_manager = \Drupal::entityManager();
|
||||
$storage = $entity_manager->getStorage($this->entityTypeId);
|
||||
|
||||
$storage->resetCache();
|
||||
$entity = $storage->load($this->entityId);
|
||||
|
||||
// Unpublish translations.
|
||||
foreach ($this->langcodes as $index => $langcode) {
|
||||
if ($index > 0) {
|
||||
$edit = array('status' => 0);
|
||||
$url = $entity->urlInfo('edit-form', array('language' => ConfigurableLanguage::load($langcode)));
|
||||
$this->drupalPostForm($url, $edit, $this->getFormSubmitAction($entity, $langcode));
|
||||
$storage->resetCache();
|
||||
$entity = $storage->load($this->entityId);
|
||||
$this->assertFalse($this->manager->getTranslationMetadata($entity->getTranslation($langcode))->isPublished(), 'The translation has been correctly unpublished.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function doTestAuthoringInfo() {
|
||||
$entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
|
||||
$languages = $this->container->get('language_manager')->getLanguages();
|
||||
$values = array();
|
||||
|
||||
// Post different authoring information for each translation.
|
||||
foreach ($this->langcodes as $langcode) {
|
||||
$url = $entity->urlInfo('edit-form', ['language' => $languages[$langcode]]);
|
||||
$user = $this->drupalCreateUser();
|
||||
$values[$langcode] = array(
|
||||
'uid' => $user->id(),
|
||||
'created' => REQUEST_TIME - mt_rand(0, 1000),
|
||||
);
|
||||
$edit = array(
|
||||
'name' => $user->getUsername(),
|
||||
'date[date]' => format_date($values[$langcode]['created'], 'custom', 'Y-m-d'),
|
||||
'date[time]' => format_date($values[$langcode]['created'], 'custom', 'H:i:s'),
|
||||
);
|
||||
$this->drupalPostForm($url, $edit, $this->getFormSubmitAction($entity, $langcode));
|
||||
}
|
||||
|
||||
$entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
|
||||
foreach ($this->langcodes as $langcode) {
|
||||
$metadata = $this->manager->getTranslationMetadata($entity->getTranslation($langcode));
|
||||
$this->assertEqual($metadata->getAuthor()->id(), $values[$langcode]['uid'], 'Translation author correctly stored.');
|
||||
$this->assertEqual($metadata->getCreatedTime(), $values[$langcode]['created'], 'Translation date correctly stored.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests translate link on comment content admin page.
|
||||
*/
|
||||
function testTranslateLinkCommentAdminPage() {
|
||||
$this->adminUser = $this->drupalCreateUser(array_merge(parent::getTranslatorPermissions(), array('access administration pages', 'administer comments', 'skip comment approval')));
|
||||
$this->drupalLogin($this->adminUser);
|
||||
|
||||
$cid_translatable = $this->createEntity(array(), $this->langcodes[0]);
|
||||
$cid_untranslatable = $this->createEntity(array(), $this->langcodes[0], 'comment');
|
||||
|
||||
// Verify translation links.
|
||||
$this->drupalGet('admin/content/comment');
|
||||
$this->assertResponse(200);
|
||||
$this->assertLinkByHref('comment/' . $cid_translatable . '/translations');
|
||||
$this->assertNoLinkByHref('comment/' . $cid_untranslatable . '/translations');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function doTestTranslationEdit() {
|
||||
$entity = entity_load($this->entityTypeId, $this->entityId, TRUE);
|
||||
$languages = $this->container->get('language_manager')->getLanguages();
|
||||
|
||||
foreach ($this->langcodes as $langcode) {
|
||||
// We only want to test the title for non-english translations.
|
||||
if ($langcode != 'en') {
|
||||
$options = array('language' => $languages[$langcode]);
|
||||
$url = $entity->urlInfo('edit-form', $options);
|
||||
$this->drupalGet($url);
|
||||
|
||||
$title = t('Edit @type @title [%language translation]', array(
|
||||
'@type' => $this->entityTypeId,
|
||||
'@title' => $entity->getTranslation($langcode)->label(),
|
||||
'%language' => $languages[$langcode]->getName(),
|
||||
));
|
||||
$this->assertRaw($title);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
192
core/modules/comment/src/Tests/CommentTypeTest.php
Normal file
192
core/modules/comment/src/Tests/CommentTypeTest.php
Normal file
|
@ -0,0 +1,192 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\CommentTypeTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests;
|
||||
use Drupal\comment\Entity\Comment;
|
||||
use Drupal\comment\Entity\CommentType;
|
||||
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
|
||||
use Drupal\field\Entity\FieldStorageConfig;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\node\Entity\Node;
|
||||
|
||||
/**
|
||||
* Ensures that comment type functions work correctly.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class CommentTypeTest extends CommentTestBase {
|
||||
|
||||
/**
|
||||
* Admin user.
|
||||
*
|
||||
* @var \Drupal\Core\Session\AccountInterface
|
||||
*/
|
||||
protected $adminUser;
|
||||
|
||||
/**
|
||||
* Permissions to grant admin user.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $permissions = array(
|
||||
'administer comments',
|
||||
'administer comment fields',
|
||||
'administer comment types',
|
||||
);
|
||||
|
||||
/**
|
||||
* Sets the test up.
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->adminUser = $this->drupalCreateUser($this->permissions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests creating a comment type programmatically and via a form.
|
||||
*/
|
||||
public function testCommentTypeCreation() {
|
||||
// Create a comment type programmatically.
|
||||
$type = $this->createCommentType('other');
|
||||
|
||||
$comment_type = CommentType::load('other');
|
||||
$this->assertTrue($comment_type, 'The new comment type has been created.');
|
||||
|
||||
// Login a test user.
|
||||
$this->drupalLogin($this->adminUser);
|
||||
|
||||
$this->drupalGet('admin/structure/comment/manage/' . $type->id());
|
||||
$this->assertResponse(200, 'The new comment type can be accessed at the edit form.');
|
||||
|
||||
// Create a comment type via the user interface.
|
||||
$edit = array(
|
||||
'id' => 'foo',
|
||||
'label' => 'title for foo',
|
||||
'description' => '',
|
||||
'target_entity_type_id' => 'node',
|
||||
);
|
||||
$this->drupalPostForm('admin/structure/comment/types/add', $edit, t('Save'));
|
||||
$comment_type = CommentType::load('foo');
|
||||
$this->assertTrue($comment_type, 'The new comment type has been created.');
|
||||
|
||||
// Check that the comment type was created in site default language.
|
||||
$default_langcode = \Drupal::languageManager()->getDefaultLanguage()->getId();
|
||||
$this->assertEqual($comment_type->language()->getId(), $default_langcode);
|
||||
|
||||
// Edit the comment-type and ensure that we cannot change the entity-type.
|
||||
$this->drupalGet('admin/structure/comment/manage/foo');
|
||||
$this->assertNoField('target_entity_type_id', 'Entity type file not present');
|
||||
$this->assertText(t('Target entity type'));
|
||||
// Save the form and ensure the entity-type value is preserved even though
|
||||
// the field isn't present.
|
||||
$this->drupalPostForm(NULL, array(), t('Save'));
|
||||
\Drupal::entityManager()->getStorage('comment_type')->resetCache(array('foo'));
|
||||
$comment_type = CommentType::load('foo');
|
||||
$this->assertEqual($comment_type->getTargetEntityTypeId(), 'node');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests editing a comment type using the UI.
|
||||
*/
|
||||
public function testCommentTypeEditing() {
|
||||
$this->drupalLogin($this->adminUser);
|
||||
|
||||
$field = FieldConfig::loadByName('comment', 'comment', 'comment_body');
|
||||
$this->assertEqual($field->getLabel(), 'Comment', 'Comment body field was found.');
|
||||
|
||||
// Change the comment type name.
|
||||
$this->drupalGet('admin/structure/comment');
|
||||
$edit = array(
|
||||
'label' => 'Bar',
|
||||
);
|
||||
$this->drupalPostForm('admin/structure/comment/manage/comment', $edit, t('Save'));
|
||||
|
||||
$this->drupalGet('admin/structure/comment');
|
||||
$this->assertRaw('Bar', 'New name was displayed.');
|
||||
$this->clickLink('Manage fields');
|
||||
$this->assertUrl(\Drupal::url('entity.comment.field_ui_fields', ['comment_type' => 'comment'], ['absolute' => TRUE]), [], 'Original machine name was used in URL.');
|
||||
$this->assertTrue($this->cssSelect('tr#comment-body'), 'Body field exists.');
|
||||
|
||||
// Remove the body field.
|
||||
$this->drupalPostForm('admin/structure/comment/manage/comment/fields/comment.comment.comment_body/delete', array(), t('Delete'));
|
||||
// Resave the settings for this type.
|
||||
$this->drupalPostForm('admin/structure/comment/manage/comment', array(), t('Save'));
|
||||
// Check that the body field doesn't exist.
|
||||
$this->drupalGet('admin/structure/comment/manage/comment/fields');
|
||||
$this->assertFalse($this->cssSelect('tr#comment-body'), 'Body field does not exist.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests deleting a comment type that still has content.
|
||||
*/
|
||||
public function testCommentTypeDeletion() {
|
||||
// Create a comment type programmatically.
|
||||
$type = $this->createCommentType('foo');
|
||||
$this->drupalCreateContentType(array('type' => 'page'));
|
||||
$this->addDefaultCommentField('node', 'page', 'foo', CommentItemInterface::OPEN, 'foo');
|
||||
$field_storage = FieldStorageConfig::loadByName('node', 'foo');
|
||||
|
||||
$this->drupalLogin($this->adminUser);
|
||||
|
||||
// Create a node.
|
||||
$node = Node::create(array(
|
||||
'type' => 'page',
|
||||
'title' => 'foo',
|
||||
));
|
||||
$node->save();
|
||||
|
||||
// Add a new comment of this type.
|
||||
$comment = Comment::create(array(
|
||||
'comment_type' => 'foo',
|
||||
'entity_type' => 'node',
|
||||
'field_name' => 'foo',
|
||||
'entity_id' => $node->id(),
|
||||
));
|
||||
$comment->save();
|
||||
|
||||
// Attempt to delete the comment type, which should not be allowed.
|
||||
$this->drupalGet('admin/structure/comment/manage/' . $type->id() . '/delete');
|
||||
$this->assertRaw(
|
||||
t('%label is used by 1 comment on your site. You can not remove this comment type until you have removed all of the %label comments.', array('%label' => $type->label())),
|
||||
'The comment type will not be deleted until all comments of that type are removed.'
|
||||
);
|
||||
$this->assertRaw(
|
||||
t('%label is used by the %field field on your site. You can not remove this comment type until you have removed the field.', array(
|
||||
'%label' => 'foo',
|
||||
'%field' => 'node.foo',
|
||||
)),
|
||||
'The comment type will not be deleted until all fields of that type are removed.'
|
||||
);
|
||||
$this->assertNoText(t('This action cannot be undone.'), 'The comment type deletion confirmation form is not available.');
|
||||
|
||||
// Delete the comment and the field.
|
||||
$comment->delete();
|
||||
$field_storage->delete();
|
||||
// Attempt to delete the comment type, which should now be allowed.
|
||||
$this->drupalGet('admin/structure/comment/manage/' . $type->id() . '/delete');
|
||||
$this->assertRaw(
|
||||
t('Are you sure you want to delete the comment type %type?', array('%type' => $type->id())),
|
||||
'The comment type is available for deletion.'
|
||||
);
|
||||
$this->assertText(t('This action cannot be undone.'), 'The comment type deletion confirmation form is available.');
|
||||
|
||||
// Test exception thrown when re-using an existing comment type.
|
||||
try {
|
||||
$this->addDefaultCommentField('comment', 'comment', 'bar');
|
||||
$this->fail('Exception not thrown.');
|
||||
}
|
||||
catch (\InvalidArgumentException $e) {
|
||||
$this->pass('Exception thrown if attempting to re-use comment-type from another entity type.');
|
||||
}
|
||||
|
||||
// Delete the comment type.
|
||||
$this->drupalPostForm('admin/structure/comment/manage/' . $type->id() . '/delete', array(), t('Delete'));
|
||||
$this->assertNull(CommentType::load($type->id()), 'Comment type deleted.');
|
||||
$this->assertRaw(t('The comment type %label has been deleted.', array('%label' => $type->label())));
|
||||
}
|
||||
|
||||
}
|
88
core/modules/comment/src/Tests/CommentUninstallTest.php
Normal file
88
core/modules/comment/src/Tests/CommentUninstallTest.php
Normal file
|
@ -0,0 +1,88 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\CommentUninstallTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests;
|
||||
|
||||
use Drupal\field\Entity\FieldStorageConfig;
|
||||
use Drupal\Core\Extension\ModuleUninstallValidatorException;
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
|
||||
/**
|
||||
* Tests comment module uninstallation.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class CommentUninstallTest extends WebTestBase {
|
||||
|
||||
use CommentTestTrait;
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('comment', 'node');
|
||||
|
||||
protected function setUp() {
|
||||
parent::setup();
|
||||
|
||||
// Create an article content type.
|
||||
$this->drupalCreateContentType(array('type' => 'article', 'name' => t('Article')));
|
||||
// Create comment field on article so that adds 'comment_body' field.
|
||||
$this->addDefaultCommentField('node', 'article');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if comment module uninstallation fails if the field exists.
|
||||
*
|
||||
* @throws \Drupal\Core\Extension\ModuleUninstallValidatorException
|
||||
*/
|
||||
function testCommentUninstallWithField() {
|
||||
// Ensure that the field exists before uninstallation.
|
||||
$field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
|
||||
$this->assertNotNull($field_storage, 'The comment_body field exists.');
|
||||
|
||||
// Uninstall the comment module which should trigger an exception.
|
||||
try {
|
||||
$this->container->get('module_installer')->uninstall(array('comment'));
|
||||
$this->fail("Expected an exception when uninstall was attempted.");
|
||||
}
|
||||
catch (ModuleUninstallValidatorException $e) {
|
||||
$this->pass("Caught an exception when uninstall was attempted.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tests if uninstallation succeeds if the field has been deleted beforehand.
|
||||
*/
|
||||
function testCommentUninstallWithoutField() {
|
||||
// Manually delete the comment_body field before module uninstallation.
|
||||
$field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
|
||||
$this->assertNotNull($field_storage, 'The comment_body field exists.');
|
||||
$field_storage->delete();
|
||||
|
||||
// Check that the field is now deleted.
|
||||
$field_storage = FieldStorageConfig::loadByName('comment', 'comment_body');
|
||||
$this->assertNull($field_storage, 'The comment_body field has been deleted.');
|
||||
|
||||
// Manually delete the comment field on the node before module uninstallation.
|
||||
$field_storage = FieldStorageConfig::loadByName('node', 'comment');
|
||||
$this->assertNotNull($field_storage, 'The comment field exists.');
|
||||
$field_storage->delete();
|
||||
|
||||
// Check that the field is now deleted.
|
||||
$field_storage = FieldStorageConfig::loadByName('node', 'comment');
|
||||
$this->assertNull($field_storage, 'The comment field has been deleted.');
|
||||
|
||||
field_purge_batch(10);
|
||||
// Ensure that uninstallation succeeds even if the field has already been
|
||||
// deleted manually beforehand.
|
||||
$this->container->get('module_installer')->uninstall(array('comment'));
|
||||
}
|
||||
|
||||
}
|
206
core/modules/comment/src/Tests/CommentValidationTest.php
Normal file
206
core/modules/comment/src/Tests/CommentValidationTest.php
Normal file
|
@ -0,0 +1,206 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\CommentValidationTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests;
|
||||
|
||||
use Drupal\comment\CommentInterface;
|
||||
use Drupal\node\Entity\Node;
|
||||
use Drupal\system\Tests\Entity\EntityUnitTestBase;
|
||||
use Drupal\user\Entity\User;
|
||||
|
||||
/**
|
||||
* Tests comment validation constraints.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class CommentValidationTest extends EntityUnitTestBase {
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('comment', 'node');
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->installSchema('comment', array('comment_entity_statistics'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the comment validation constraints.
|
||||
*/
|
||||
public function testValidation() {
|
||||
// Add a user.
|
||||
$user = User::create(array('name' => 'test'));
|
||||
$user->save();
|
||||
|
||||
// Add comment type.
|
||||
$this->entityManager->getStorage('comment_type')->create(array(
|
||||
'id' => 'comment',
|
||||
'label' => 'comment',
|
||||
'target_entity_type_id' => 'node',
|
||||
))->save();
|
||||
|
||||
// Add comment field to content.
|
||||
$this->entityManager->getStorage('field_storage_config')->create(array(
|
||||
'entity_type' => 'node',
|
||||
'field_name' => 'comment',
|
||||
'type' => 'comment',
|
||||
'settings' => array(
|
||||
'comment_type' => 'comment',
|
||||
)
|
||||
))->save();
|
||||
|
||||
// Create a page node type.
|
||||
$this->entityManager->getStorage('node_type')->create(array(
|
||||
'type' => 'page',
|
||||
'name' => 'page',
|
||||
))->save();
|
||||
|
||||
// Add comment field to page content.
|
||||
/** @var \Drupal\field\FieldConfigInterface $field */
|
||||
$field = $this->entityManager->getStorage('field_config')->create(array(
|
||||
'field_name' => 'comment',
|
||||
'entity_type' => 'node',
|
||||
'bundle' => 'page',
|
||||
'label' => 'Comment settings',
|
||||
));
|
||||
$field->save();
|
||||
|
||||
$node = $this->entityManager->getStorage('node')->create(array(
|
||||
'type' => 'page',
|
||||
'title' => 'test',
|
||||
));
|
||||
$node->save();
|
||||
|
||||
$comment = $this->entityManager->getStorage('comment')->create(array(
|
||||
'entity_id' => $node->id(),
|
||||
'entity_type' => 'node',
|
||||
'field_name' => 'comment',
|
||||
'comment_body' => $this->randomMachineName(),
|
||||
));
|
||||
|
||||
$violations = $comment->validate();
|
||||
$this->assertEqual(count($violations), 0, 'No violations when validating a default comment.');
|
||||
|
||||
$comment->set('subject', $this->randomString(65));
|
||||
$this->assertLengthViolation($comment, 'subject', 64);
|
||||
|
||||
// Make the subject valid.
|
||||
$comment->set('subject', $this->randomString());
|
||||
$comment->set('name', $this->randomString(61));
|
||||
$this->assertLengthViolation($comment, 'name', 60);
|
||||
|
||||
// Validate a name collision between an anonymous comment author name and an
|
||||
// existing user account name.
|
||||
$comment->set('name', 'test');
|
||||
$comment->set('uid', 0);
|
||||
$violations = $comment->validate();
|
||||
$this->assertEqual(count($violations), 1, "Violation found on author name collision");
|
||||
$this->assertEqual($violations[0]->getPropertyPath(), "name");
|
||||
$this->assertEqual($violations[0]->getMessage(), t('The name you used (%name) belongs to a registered user.', array('%name' => 'test')));
|
||||
|
||||
// Make the name valid.
|
||||
$comment->set('name', 'valid unused name');
|
||||
$comment->set('mail', 'invalid');
|
||||
$violations = $comment->validate();
|
||||
$this->assertEqual(count($violations), 1, 'Violation found when email is invalid');
|
||||
$this->assertEqual($violations[0]->getPropertyPath(), 'mail.0.value');
|
||||
$this->assertEqual($violations[0]->getMessage(), t('This value is not a valid email address.'));
|
||||
|
||||
$comment->set('mail', NULL);
|
||||
$comment->set('homepage', 'http://example.com/' . $this->randomMachineName(237));
|
||||
$this->assertLengthViolation($comment, 'homepage', 255);
|
||||
|
||||
$comment->set('homepage', 'invalid');
|
||||
$violations = $comment->validate();
|
||||
$this->assertEqual(count($violations), 1, 'Violation found when homepage is invalid');
|
||||
$this->assertEqual($violations[0]->getPropertyPath(), 'homepage.0.value');
|
||||
|
||||
// @todo This message should be improved in
|
||||
// https://www.drupal.org/node/2012690.
|
||||
$this->assertEqual($violations[0]->getMessage(), t('This value should be of the correct primitive type.'));
|
||||
|
||||
$comment->set('homepage', NULL);
|
||||
$comment->set('hostname', $this->randomString(129));
|
||||
$this->assertLengthViolation($comment, 'hostname', 128);
|
||||
|
||||
$comment->set('hostname', NULL);
|
||||
$comment->set('thread', $this->randomString(256));
|
||||
$this->assertLengthViolation($comment, 'thread', 255);
|
||||
|
||||
$comment->set('thread', NULL);
|
||||
|
||||
// Force anonymous users to enter contact details.
|
||||
$field->setSetting('anonymous', COMMENT_ANONYMOUS_MUST_CONTACT);
|
||||
$field->save();
|
||||
// Reset the node entity.
|
||||
\Drupal::entityManager()->getStorage('node')->resetCache([$node->id()]);
|
||||
$node = Node::load($node->id());
|
||||
// Create a new comment with the new field.
|
||||
$comment = $this->entityManager->getStorage('comment')->create(array(
|
||||
'entity_id' => $node->id(),
|
||||
'entity_type' => 'node',
|
||||
'field_name' => 'comment',
|
||||
'comment_body' => $this->randomMachineName(),
|
||||
'uid' => 0,
|
||||
'name' => '',
|
||||
));
|
||||
$violations = $comment->validate();
|
||||
$this->assertEqual(count($violations), 1, 'Violation found when name is required, but empty and UID is anonymous.');
|
||||
$this->assertEqual($violations[0]->getPropertyPath(), 'name');
|
||||
$this->assertEqual($violations[0]->getMessage(), t('You have to specify a valid author.'));
|
||||
|
||||
// Test creating a default comment with a given user id works.
|
||||
$comment = $this->entityManager->getStorage('comment')->create(array(
|
||||
'entity_id' => $node->id(),
|
||||
'entity_type' => 'node',
|
||||
'field_name' => 'comment',
|
||||
'comment_body' => $this->randomMachineName(),
|
||||
'uid' => $user->id(),
|
||||
));
|
||||
$violations = $comment->validate();
|
||||
$this->assertEqual(count($violations), 0, 'No violations when validating a default comment with an author.');
|
||||
|
||||
// Test specifying a wrong author name does not work.
|
||||
$comment = $this->entityManager->getStorage('comment')->create(array(
|
||||
'entity_id' => $node->id(),
|
||||
'entity_type' => 'node',
|
||||
'field_name' => 'comment',
|
||||
'comment_body' => $this->randomMachineName(),
|
||||
'uid' => $user->id(),
|
||||
'name' => 'not-test',
|
||||
));
|
||||
$violations = $comment->validate();
|
||||
$this->assertEqual(count($violations), 1, 'Violation found when author name and comment author do not match.');
|
||||
$this->assertEqual($violations[0]->getPropertyPath(), 'name');
|
||||
$this->assertEqual($violations[0]->getMessage(), t('The specified author name does not match the comment author.'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that a length violation exists for the given field.
|
||||
*
|
||||
* @param \Drupal\comment\CommentInterface $comment
|
||||
* The comment object to validate.
|
||||
* @param string $field_name
|
||||
* The field that violates the maximum length.
|
||||
* @param int $length
|
||||
* Number of characters that was exceeded.
|
||||
*/
|
||||
protected function assertLengthViolation(CommentInterface $comment, $field_name, $length) {
|
||||
$violations = $comment->validate();
|
||||
$this->assertEqual(count($violations), 1, "Violation found when $field_name is too long.");
|
||||
$this->assertEqual($violations[0]->getPropertyPath(), "$field_name.0.value");
|
||||
$field_label = $comment->get($field_name)->getFieldDefinition()->getLabel();
|
||||
$this->assertEqual($violations[0]->getMessage(), t('%name: may not be longer than @max characters.', array('%name' => $field_label, '@max' => $length)));
|
||||
}
|
||||
|
||||
}
|
56
core/modules/comment/src/Tests/Views/ArgumentUserUIDTest.php
Normal file
56
core/modules/comment/src/Tests/Views/ArgumentUserUIDTest.php
Normal file
|
@ -0,0 +1,56 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\Views\ArgumentUserUIDTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests\Views;
|
||||
|
||||
use Drupal\comment\Entity\Comment;
|
||||
use Drupal\user\Entity\User;
|
||||
use Drupal\views\Views;
|
||||
|
||||
/**
|
||||
* Tests the user posted or commented argument handler.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class ArgumentUserUIDTest extends CommentTestBase {
|
||||
|
||||
/**
|
||||
* Views used by this test.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $testViews = array('test_comment_user_uid');
|
||||
|
||||
function testCommentUserUIDTest() {
|
||||
// Add an additional comment which is not created by the user.
|
||||
$new_user = User::create(['name' => 'new user']);
|
||||
$new_user->save();
|
||||
|
||||
$comment = Comment::create([
|
||||
'uid' => $new_user->uid->value,
|
||||
'entity_id' => $this->nodeUserCommented->id(),
|
||||
'entity_type' => 'node',
|
||||
'field_name' => 'comment',
|
||||
'subject' => 'if a woodchuck could chuck wood.',
|
||||
]);
|
||||
$comment->save();
|
||||
|
||||
$view = Views::getView('test_comment_user_uid');
|
||||
$this->executeView($view, array($this->account->id()));
|
||||
$result_set = array(
|
||||
array(
|
||||
'nid' => $this->nodeUserPosted->id(),
|
||||
),
|
||||
array(
|
||||
'nid' => $this->nodeUserCommented->id(),
|
||||
),
|
||||
);
|
||||
$column_map = array('nid' => 'nid');
|
||||
$this->assertIdenticalResultset($view, $result_set, $column_map);
|
||||
}
|
||||
|
||||
}
|
123
core/modules/comment/src/Tests/Views/CommentFieldFilterTest.php
Normal file
123
core/modules/comment/src/Tests/Views/CommentFieldFilterTest.php
Normal file
|
@ -0,0 +1,123 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\Views\CommentFieldFilterTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests\Views;
|
||||
|
||||
use Drupal\language\Entity\ConfigurableLanguage;
|
||||
|
||||
/**
|
||||
* Tests comment field filters with translations.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class CommentFieldFilterTest extends CommentTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = array('language');
|
||||
|
||||
/**
|
||||
* Views used by this test.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $testViews = array('test_field_filters');
|
||||
|
||||
/**
|
||||
* List of comment titles by language.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $commentTitles = array();
|
||||
|
||||
function setUp() {
|
||||
parent::setUp();
|
||||
$this->drupalLogin($this->drupalCreateUser(['access comments']));
|
||||
|
||||
// Add two new languages.
|
||||
ConfigurableLanguage::createFromLangcode('fr')->save();
|
||||
ConfigurableLanguage::createFromLangcode('es')->save();
|
||||
|
||||
// Set up comment titles.
|
||||
$this->commentTitles = array(
|
||||
'en' => 'Food in Paris',
|
||||
'es' => 'Comida en Paris',
|
||||
'fr' => 'Nouriture en Paris',
|
||||
);
|
||||
|
||||
// Create a new comment. Using the one created earlier will not work,
|
||||
// as it predates the language set-up.
|
||||
$comment = array(
|
||||
'uid' => $this->loggedInUser->id(),
|
||||
'entity_id' => $this->nodeUserCommented->id(),
|
||||
'entity_type' => 'node',
|
||||
'field_name' => 'comment',
|
||||
'cid' => '',
|
||||
'pid' => '',
|
||||
'node_type' => '',
|
||||
);
|
||||
$this->comment = entity_create('comment', $comment);
|
||||
|
||||
// Add field values and translate the comment.
|
||||
$this->comment->subject->value = $this->commentTitles['en'];
|
||||
$this->comment->comment_body->value = $this->commentTitles['en'];
|
||||
$this->comment->langcode = 'en';
|
||||
$this->comment->save();
|
||||
foreach (array('es', 'fr') as $langcode) {
|
||||
$translation = $this->comment->addTranslation($langcode, array());
|
||||
$translation->comment_body->value = $this->commentTitles[$langcode];
|
||||
$translation->subject->value = $this->commentTitles[$langcode];
|
||||
}
|
||||
$this->comment->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests body and title filters.
|
||||
*/
|
||||
public function testFilters() {
|
||||
// Test the title filter page, which filters for title contains 'Comida'.
|
||||
// Should show just the Spanish translation, once.
|
||||
$this->assertPageCounts('test-title-filter', array('es' => 1, 'fr' => 0, 'en' => 0), 'Comida title filter');
|
||||
|
||||
// Test the body filter page, which filters for body contains 'Comida'.
|
||||
// Should show just the Spanish translation, once.
|
||||
$this->assertPageCounts('test-body-filter', array('es' => 1, 'fr' => 0, 'en' => 0), 'Comida body filter');
|
||||
|
||||
// Test the title Paris filter page, which filters for title contains
|
||||
// 'Paris'. Should show each translation once.
|
||||
$this->assertPageCounts('test-title-paris', array('es' => 1, 'fr' => 1, 'en' => 1), 'Paris title filter');
|
||||
|
||||
// Test the body Paris filter page, which filters for body contains
|
||||
// 'Paris'. Should show each translation once.
|
||||
$this->assertPageCounts('test-body-paris', array('es' => 1, 'fr' => 1, 'en' => 1), 'Paris body filter');
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that the given comment translation counts are correct.
|
||||
*
|
||||
* @param string $path
|
||||
* Path of the page to test.
|
||||
* @param array $counts
|
||||
* Array whose keys are languages, and values are the number of times
|
||||
* that translation should be shown on the given page.
|
||||
* @param string $message
|
||||
* Message suffix to display.
|
||||
*/
|
||||
protected function assertPageCounts($path, $counts, $message) {
|
||||
// Get the text of the page.
|
||||
$this->drupalGet($path);
|
||||
$text = $this->getTextContent();
|
||||
|
||||
// Check the counts. Note that the title and body are both shown on the
|
||||
// page, and they are the same. So the title/body string should appear on
|
||||
// the page twice as many times as the input count.
|
||||
foreach ($counts as $langcode => $count) {
|
||||
$this->assertEqual(substr_count($text, $this->commentTitles[$langcode]), 2 * $count, 'Translation ' . $langcode . ' has count ' . $count . ' with ' . $message);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,92 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\Views\CommentFieldNameTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests\Views;
|
||||
|
||||
use Drupal\comment\Entity\Comment;
|
||||
use Drupal\Core\Session\AnonymousUserSession;
|
||||
use Drupal\user\RoleInterface;
|
||||
use Drupal\views\Views;
|
||||
|
||||
/**
|
||||
* Tests the comment field name field.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class CommentFieldNameTest extends CommentTestBase {
|
||||
|
||||
/**
|
||||
* Views used by this test.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $testViews = ['test_comment_field_name'];
|
||||
|
||||
/**
|
||||
* The second comment entity used by this test.
|
||||
*
|
||||
* @var \Drupal\comment\CommentInterface
|
||||
*/
|
||||
protected $customComment;
|
||||
|
||||
/**
|
||||
* The comment field name used by this test.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $fieldName = 'comment_custom';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
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();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test comment field name.
|
||||
*/
|
||||
public function testCommentFieldName() {
|
||||
$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);
|
||||
// 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.
|
||||
$this->assertIdentical($this->comment->getFieldName(), $view->field['field_name']->advancedRender($view->result[0]));
|
||||
$this->assertIdentical($this->customComment->getFieldName(), $view->field['field_name']->advancedRender($view->result[1]));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,71 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\Views\CommentRestExportTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests\Views;
|
||||
|
||||
use Drupal\Component\Serialization\Json;
|
||||
|
||||
/**
|
||||
* Tests a comment rest export view.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class CommentRestExportTest extends CommentTestBase {
|
||||
|
||||
/**
|
||||
* Views used by this test.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $testViews = ['test_comment_rest'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['node', 'comment', 'comment_test_views', 'rest', 'hal'];
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
// Add another anonymous comment.
|
||||
$comment = array(
|
||||
'uid' => 0,
|
||||
'entity_id' => $this->nodeUserCommented->id(),
|
||||
'entity_type' => 'node',
|
||||
'field_name' => 'comment',
|
||||
'subject' => 'A lot, apparently',
|
||||
'cid' => '',
|
||||
'pid' => $this->comment->id(),
|
||||
'mail' => 'someone@example.com',
|
||||
'name' => 'bobby tables',
|
||||
'hostname' => 'public.example.com',
|
||||
);
|
||||
$this->comment = entity_create('comment', $comment);
|
||||
$this->comment->save();
|
||||
|
||||
$user = $this->drupalCreateUser(['access comments']);
|
||||
$this->drupalLogin($user);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test comment row.
|
||||
*/
|
||||
public function testCommentRestExport() {
|
||||
$this->drupalGetWithFormat(sprintf('node/%d/comments', $this->nodeUserCommented->id()), 'hal_json');
|
||||
$this->assertResponse(200);
|
||||
$contents = Json::decode($this->getRawContent());
|
||||
$this->assertEqual($contents[0]['subject'], 'How much wood would a woodchuck chuck');
|
||||
$this->assertEqual($contents[1]['subject'], 'A lot, apparently');
|
||||
$this->assertEqual(count($contents), 2);
|
||||
|
||||
// Ensure field-level access is respected - user shouldn't be able to see
|
||||
// mail or hostname fields.
|
||||
$this->assertNoText('someone@example.com');
|
||||
$this->assertNoText('public.example.com');
|
||||
}
|
||||
|
||||
}
|
34
core/modules/comment/src/Tests/Views/CommentRowTest.php
Normal file
34
core/modules/comment/src/Tests/Views/CommentRowTest.php
Normal file
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\Views\CommentRowTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests\Views;
|
||||
|
||||
/**
|
||||
* Tests the comment row plugin.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class CommentRowTest extends CommentTestBase {
|
||||
|
||||
/**
|
||||
* Views used by this test.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $testViews = array('test_comment_row');
|
||||
|
||||
/**
|
||||
* Test comment row.
|
||||
*/
|
||||
public function testCommentRow() {
|
||||
$this->drupalGet('test-comment-row');
|
||||
|
||||
$result = $this->xpath('//article[contains(@class, "comment")]');
|
||||
$this->assertEqual(1, count($result), 'One rendered comment found.');
|
||||
}
|
||||
|
||||
}
|
94
core/modules/comment/src/Tests/Views/CommentTestBase.php
Normal file
94
core/modules/comment/src/Tests/Views/CommentTestBase.php
Normal file
|
@ -0,0 +1,94 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\Views\CommentTestBase.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests\Views;
|
||||
|
||||
use Drupal\comment\Tests\CommentTestTrait;
|
||||
use Drupal\views\Tests\ViewTestBase;
|
||||
use Drupal\views\Tests\ViewTestData;
|
||||
|
||||
/**
|
||||
* Tests the argument_comment_user_uid handler.
|
||||
*/
|
||||
abstract class CommentTestBase extends ViewTestBase {
|
||||
|
||||
use CommentTestTrait;
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('node', 'comment', 'comment_test_views');
|
||||
|
||||
/**
|
||||
* A normal user with permission to post comments (without approval).
|
||||
*
|
||||
* @var \Drupal\user\UserInterface
|
||||
*/
|
||||
protected $account;
|
||||
|
||||
/**
|
||||
* A second normal user that will author a node for $account to comment on.
|
||||
*
|
||||
* @var \Drupal\user\UserInterface
|
||||
*/
|
||||
protected $account2;
|
||||
|
||||
/**
|
||||
* Stores a node posted by the user created as $account.
|
||||
*
|
||||
* @var \Drupal\node\NodeInterface
|
||||
*/
|
||||
protected $nodeUserPosted;
|
||||
|
||||
/**
|
||||
* Stores a node posted by the user created as $account2.
|
||||
*
|
||||
* @var \Drupal\node\NodeInterface
|
||||
*/
|
||||
protected $nodeUserCommented;
|
||||
|
||||
/**
|
||||
* Stores a comment used by the tests.
|
||||
*
|
||||
* @var \Drupal\comment\Entity\Comment
|
||||
*/
|
||||
protected $comment;
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
ViewTestData::createTestViews(get_class($this), array('comment_test_views'));
|
||||
|
||||
// Add two users, create a node with the user1 as author and another node
|
||||
// with user2 as author. For the second node add a comment from user1.
|
||||
$this->account = $this->drupalCreateUser(array('skip comment approval'));
|
||||
$this->account2 = $this->drupalCreateUser();
|
||||
$this->drupalLogin($this->account);
|
||||
|
||||
$this->drupalCreateContentType(array('type' => 'page', 'name' => t('Basic page')));
|
||||
$this->addDefaultCommentField('node', 'page');
|
||||
|
||||
$this->nodeUserPosted = $this->drupalCreateNode();
|
||||
$this->nodeUserCommented = $this->drupalCreateNode(array('uid' => $this->account2->id()));
|
||||
|
||||
$comment = array(
|
||||
'uid' => $this->loggedInUser->id(),
|
||||
'entity_id' => $this->nodeUserCommented->id(),
|
||||
'entity_type' => 'node',
|
||||
'field_name' => 'comment',
|
||||
'subject' => 'How much wood would a woodchuck chuck',
|
||||
'cid' => '',
|
||||
'pid' => '',
|
||||
'mail' => 'someone@example.com',
|
||||
);
|
||||
$this->comment = entity_create('comment', $comment);
|
||||
$this->comment->save();
|
||||
}
|
||||
|
||||
}
|
169
core/modules/comment/src/Tests/Views/CommentUserNameTest.php
Normal file
169
core/modules/comment/src/Tests/Views/CommentUserNameTest.php
Normal file
|
@ -0,0 +1,169 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\Views\CommentUserNameTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests\Views;
|
||||
|
||||
use Drupal\comment\Entity\Comment;
|
||||
use Drupal\Core\Session\AnonymousUserSession;
|
||||
use Drupal\user\Entity\Role;
|
||||
use Drupal\user\Entity\User;
|
||||
use Drupal\views\Entity\View;
|
||||
use Drupal\views\Tests\ViewUnitTestBase;
|
||||
use Drupal\views\Views;
|
||||
|
||||
/**
|
||||
* Tests comment user name field
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class CommentUserNameTest extends ViewUnitTestBase {
|
||||
|
||||
/**
|
||||
* Admin user.
|
||||
*
|
||||
* @var \Drupal\user\UserInterface
|
||||
*/
|
||||
protected $adminUser;
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['user', 'comment', 'entity_test'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp($import_test_views = TRUE) {
|
||||
parent::setUp($import_test_views);
|
||||
|
||||
$this->installEntitySchema('user');
|
||||
$this->installEntitySchema('comment');
|
||||
// Create the anonymous role.
|
||||
$this->installConfig(['user']);
|
||||
|
||||
// Create an anonymous user.
|
||||
$storage = \Drupal::entityManager()->getStorage('user');
|
||||
// Insert a row for the anonymous user.
|
||||
$storage
|
||||
->create(array(
|
||||
'uid' => 0,
|
||||
'status' => 0,
|
||||
))
|
||||
->save();
|
||||
|
||||
$admin_role = Role::create([
|
||||
'id' => 'admin',
|
||||
'permissions' => ['administer comments', 'access user profiles'],
|
||||
]);
|
||||
$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 = User::create([
|
||||
'name' => $this->randomMachineName(),
|
||||
'roles' => [$admin_role->id()],
|
||||
]);
|
||||
$this->adminUser->save();
|
||||
|
||||
// Create some comments.
|
||||
$comment = Comment::create([
|
||||
'subject' => 'My comment title',
|
||||
'uid' => $this->adminUser->id(),
|
||||
'entity_type' => 'entity_test',
|
||||
'comment_type' => 'entity_test',
|
||||
'status' => 1,
|
||||
]);
|
||||
$comment->save();
|
||||
|
||||
$comment_anonymous = Comment::create([
|
||||
'subject' => 'Anonymous comment title',
|
||||
'uid' => 0,
|
||||
'name' => 'barry',
|
||||
'mail' => 'test@example.com',
|
||||
'homepage' => 'https://example.com',
|
||||
'entity_type' => 'entity_test',
|
||||
'comment_type' => 'entity_test',
|
||||
'created' => 123456,
|
||||
'status' => 1,
|
||||
]);
|
||||
$comment_anonymous->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the username formatter.
|
||||
*/
|
||||
public function testUsername() {
|
||||
$view_id = $this->randomMachineName();
|
||||
$view = View::create([
|
||||
'id' => $view_id,
|
||||
'base_table' => 'comment_field_data',
|
||||
'display' => [
|
||||
'default' => [
|
||||
'display_plugin' => 'default',
|
||||
'id' => 'default',
|
||||
'display_options' => [
|
||||
'fields' => [
|
||||
'name' => [
|
||||
'table' => 'comment_field_data',
|
||||
'field' => 'name',
|
||||
'id' => 'name',
|
||||
'plugin_id' => 'field',
|
||||
'type' => 'comment_username'
|
||||
],
|
||||
'subject' => [
|
||||
'table' => 'comment_field_data',
|
||||
'field' => 'subject',
|
||||
'id' => 'subject',
|
||||
'plugin_id' => 'field',
|
||||
'type' => 'string',
|
||||
'settings' => [
|
||||
'link_to_entity' => TRUE,
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
]);
|
||||
$view->save();
|
||||
|
||||
/* @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */
|
||||
$account_switcher = \Drupal::service('account_switcher');
|
||||
|
||||
/* @var \Drupal\Core\Render\RendererInterface $renderer */
|
||||
$renderer = \Drupal::service('renderer');
|
||||
|
||||
$account_switcher->switchTo($this->adminUser);
|
||||
$executable = Views::getView($view_id);
|
||||
$build = $executable->preview();
|
||||
$this->setRawContent($renderer->renderRoot($build));
|
||||
$this->verbose($this->getRawContent());
|
||||
|
||||
$this->assertLink('My comment title');
|
||||
$this->assertLink('Anonymous comment title');
|
||||
$this->assertLink($this->adminUser->label());
|
||||
$this->assertLink('barry (not verified)');
|
||||
|
||||
$account_switcher->switchTo(new AnonymousUserSession());
|
||||
$executable = Views::getView($view_id);
|
||||
$executable->storage->invalidateCaches();
|
||||
|
||||
$build = $executable->preview();
|
||||
$this->setRawContent($renderer->renderRoot($build));
|
||||
|
||||
// No access to user-profiles, so shouldn't be able to see links.
|
||||
$this->assertNoLink($this->adminUser->label());
|
||||
// Note: External users aren't pointing to drupal user profiles.
|
||||
$this->assertLink('barry (not verified)');
|
||||
$this->verbose($this->getRawContent());
|
||||
$this->assertLink('My comment title');
|
||||
$this->assertLink('Anonymous comment title');
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,81 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\Views\CommentViewsFieldAccessTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests\Views;
|
||||
|
||||
use Drupal\comment\Entity\Comment;
|
||||
use Drupal\user\Entity\User;
|
||||
use Drupal\views\Tests\Handler\FieldFieldAccessTestBase;
|
||||
|
||||
/**
|
||||
* Tests base field access in Views for the comment entity.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class CommentViewsFieldAccessTest extends FieldFieldAccessTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['comment', 'entity_test'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp($import_test_views = TRUE) {
|
||||
parent::setUp($import_test_views);
|
||||
|
||||
$this->installEntitySchema('comment');
|
||||
}
|
||||
|
||||
/**
|
||||
* Check access for comment fields.
|
||||
*/
|
||||
public function testCommentFields() {
|
||||
$user = User::create([
|
||||
'name' => 'test user',
|
||||
]);
|
||||
$user->save();
|
||||
|
||||
$comment = Comment::create([
|
||||
'subject' => 'My comment title',
|
||||
'uid' => $user->id(),
|
||||
'entity_type' => 'entity_test',
|
||||
'comment_type' => 'entity_test',
|
||||
]);
|
||||
$comment->save();
|
||||
|
||||
$comment_anonymous = Comment::create([
|
||||
'subject' => 'Anonymous comment title',
|
||||
'uid' => 0,
|
||||
'name' => 'anonymous',
|
||||
'mail' => 'test@example.com',
|
||||
'homepage' => 'https://example.com',
|
||||
'entity_type' => 'entity_test',
|
||||
'comment_type' => 'entity_test',
|
||||
'created' => 123456,
|
||||
'status' => 1,
|
||||
]);
|
||||
$comment_anonymous->save();
|
||||
|
||||
// @todo Expand the test coverage in https://www.drupal.org/node/2464635
|
||||
|
||||
$this->assertFieldAccess('comment', 'cid', $comment->id());
|
||||
$this->assertFieldAccess('comment', 'cid', $comment_anonymous->id());
|
||||
$this->assertFieldAccess('comment', 'uuid', $comment->uuid());
|
||||
$this->assertFieldAccess('comment', 'subject', 'My comment title');
|
||||
$this->assertFieldAccess('comment', 'subject', 'Anonymous comment title');
|
||||
$this->assertFieldAccess('comment', 'name', 'anonymous');
|
||||
$this->assertFieldAccess('comment', 'mail', 'test@example.com');
|
||||
$this->assertFieldAccess('comment', 'homepage', 'https://example.com');
|
||||
$this->assertFieldAccess('comment', 'uid', $user->getUsername());
|
||||
// $this->assertFieldAccess('comment', 'created', \Drupal::service('date.formatter')->format(123456));
|
||||
// $this->assertFieldAccess('comment', 'changed', \Drupal::service('date.formatter')->format(REQUEST_TIME));
|
||||
$this->assertFieldAccess('comment', 'status', 'On');
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,145 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\Views\DefaultViewRecentCommentsTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests\Views;
|
||||
|
||||
use Drupal\comment\CommentInterface;
|
||||
use Drupal\comment\Entity\Comment;
|
||||
use Drupal\comment\Tests\CommentTestTrait;
|
||||
use Drupal\views\Views;
|
||||
use Drupal\views\Tests\ViewTestBase;
|
||||
|
||||
/**
|
||||
* Tests results for the Recent Comments view shipped with the module.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class DefaultViewRecentCommentsTest extends ViewTestBase {
|
||||
|
||||
use CommentTestTrait;
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('node', 'comment', 'block');
|
||||
|
||||
/**
|
||||
* Number of results for the Master display.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $masterDisplayResults = 5;
|
||||
|
||||
/**
|
||||
* Number of results for the Block display.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $blockDisplayResults = 5;
|
||||
|
||||
/**
|
||||
* Number of results for the Page display.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $pageDisplayResults = 5;
|
||||
|
||||
/**
|
||||
* Will hold the comments created for testing.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $commentsCreated = array();
|
||||
|
||||
/**
|
||||
* Contains the node object used for comments of this test.
|
||||
*
|
||||
* @var \Drupal\node\Node
|
||||
*/
|
||||
public $node;
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
// Create a new content type
|
||||
$content_type = $this->drupalCreateContentType();
|
||||
|
||||
// Add a node of the new content type.
|
||||
$node_data = array(
|
||||
'type' => $content_type->id(),
|
||||
);
|
||||
|
||||
$this->addDefaultCommentField('node', $content_type->id());
|
||||
$this->node = $this->drupalCreateNode($node_data);
|
||||
|
||||
// Force a flush of the in-memory storage.
|
||||
$this->container->get('views.views_data')->clear();
|
||||
|
||||
// Create some comments and attach them to the created node.
|
||||
for ($i = 0; $i < $this->masterDisplayResults; $i++) {
|
||||
/** @var \Drupal\comment\CommentInterface $comment */
|
||||
$comment = entity_create('comment', array(
|
||||
'status' => CommentInterface::PUBLISHED,
|
||||
'field_name' => 'comment',
|
||||
'entity_type' => 'node',
|
||||
'entity_id' => $this->node->id(),
|
||||
));
|
||||
$comment->setOwnerId(0);
|
||||
$comment->setSubject('Test comment ' . $i);
|
||||
$comment->comment_body->value = 'Test body ' . $i;
|
||||
$comment->comment_body->format = 'full_html';
|
||||
|
||||
// Ensure comments are sorted in ascending order.
|
||||
$time = REQUEST_TIME + ($this->masterDisplayResults - $i);
|
||||
$comment->setCreatedTime($time);
|
||||
$comment->changed->value = $time;
|
||||
|
||||
$comment->save();
|
||||
}
|
||||
|
||||
// Store all the nodes just created to access their properties on the tests.
|
||||
$this->commentsCreated = Comment::loadMultiple();
|
||||
|
||||
// Sort created comments in descending order.
|
||||
ksort($this->commentsCreated, SORT_NUMERIC);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the block defined by the comments_recent view.
|
||||
*/
|
||||
public function testBlockDisplay() {
|
||||
$user = $this->drupalCreateUser(['access comments']);
|
||||
$this->drupalLogin($user);
|
||||
|
||||
$view = Views::getView('comments_recent');
|
||||
$view->setDisplay('block_1');
|
||||
$this->executeView($view);
|
||||
|
||||
$map = array(
|
||||
'subject' => 'subject',
|
||||
'cid' => 'cid',
|
||||
'comment_field_data_created' => 'created'
|
||||
);
|
||||
$expected_result = array();
|
||||
foreach (array_values($this->commentsCreated) as $key => $comment) {
|
||||
$expected_result[$key]['subject'] = $comment->getSubject();
|
||||
$expected_result[$key]['cid'] = $comment->id();
|
||||
$expected_result[$key]['created'] = $comment->getCreatedTime();
|
||||
}
|
||||
$this->assertIdenticalResultset($view, $expected_result, $map);
|
||||
|
||||
// Check the number of results given by the display is the expected.
|
||||
$this->assertEqual(sizeof($view->result), $this->blockDisplayResults,
|
||||
format_string('There are exactly @results comments. Expected @expected',
|
||||
array('@results' => count($view->result), '@expected' => $this->blockDisplayResults)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
68
core/modules/comment/src/Tests/Views/FilterUserUIDTest.php
Normal file
68
core/modules/comment/src/Tests/Views/FilterUserUIDTest.php
Normal file
|
@ -0,0 +1,68 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\Views\FilterUserUIDTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests\Views;
|
||||
|
||||
use Drupal\comment\Entity\Comment;
|
||||
use Drupal\user\Entity\User;
|
||||
use Drupal\views\Views;
|
||||
|
||||
/**
|
||||
* Tests the user posted or commented filter handler.
|
||||
*
|
||||
* The actual stuff is done in the parent class.
|
||||
*
|
||||
* @group comment
|
||||
*/
|
||||
class FilterUserUIDTest extends CommentTestBase {
|
||||
|
||||
/**
|
||||
* Views used by this test.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $testViews = array('test_comment_user_uid');
|
||||
|
||||
function testCommentUserUIDTest() {
|
||||
$view = Views::getView('test_comment_user_uid');
|
||||
$view->setDisplay();
|
||||
$view->removeHandler('default', 'argument', 'uid_touch');
|
||||
|
||||
// Add an additional comment which is not created by the user.
|
||||
$new_user = User::create(['name' => 'new user']);
|
||||
$new_user->save();
|
||||
|
||||
$comment = Comment::create([
|
||||
'uid' => $new_user->uid->value,
|
||||
'entity_id' => $this->nodeUserCommented->id(),
|
||||
'entity_type' => 'node',
|
||||
'field_name' => 'comment',
|
||||
'subject' => 'if a woodchuck could chuck wood.',
|
||||
]);
|
||||
$comment->save();
|
||||
|
||||
$options = array(
|
||||
'id' => 'uid_touch',
|
||||
'table' => 'node_field_data',
|
||||
'field' => 'uid_touch',
|
||||
'value' => array($this->loggedInUser->id()),
|
||||
);
|
||||
$view->addHandler('default', 'filter', 'node_field_data', 'uid_touch', $options);
|
||||
$this->executeView($view, array($this->account->id()));
|
||||
$result_set = array(
|
||||
array(
|
||||
'nid' => $this->nodeUserPosted->id(),
|
||||
),
|
||||
array(
|
||||
'nid' => $this->nodeUserCommented->id(),
|
||||
),
|
||||
);
|
||||
$column_map = array('nid' => 'nid');
|
||||
$this->assertIdenticalResultset($view, $result_set, $column_map);
|
||||
}
|
||||
|
||||
}
|
37
core/modules/comment/src/Tests/Views/RowRssTest.php
Normal file
37
core/modules/comment/src/Tests/Views/RowRssTest.php
Normal file
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\Views\RowRssTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests\Views;
|
||||
|
||||
/**
|
||||
* Tests the comment rss row plugin.
|
||||
*
|
||||
* @group comment
|
||||
* @see \Drupal\comment\Plugin\views\row\Rss
|
||||
*/
|
||||
class RowRssTest extends CommentTestBase {
|
||||
|
||||
/**
|
||||
* Views used by this test.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $testViews = array('test_comment_rss');
|
||||
|
||||
/**
|
||||
* Test comment rss output.
|
||||
*/
|
||||
public function testRssRow() {
|
||||
$this->drupalGet('test-comment-rss');
|
||||
|
||||
$result = $this->xpath('//item');
|
||||
$this->assertEqual(count($result), 1, 'Just one comment was found in the rss output.');
|
||||
|
||||
$this->assertEqual($result[0]->pubdate, gmdate('r', $this->comment->getCreatedTime()), 'The right pubDate appears in the rss output.');
|
||||
}
|
||||
|
||||
}
|
102
core/modules/comment/src/Tests/Views/WizardTest.php
Normal file
102
core/modules/comment/src/Tests/Views/WizardTest.php
Normal file
|
@ -0,0 +1,102 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\comment\Tests\Views\WizardTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\comment\Tests\Views;
|
||||
|
||||
use Drupal\comment\Tests\CommentTestTrait;
|
||||
use Drupal\views\Views;
|
||||
use Drupal\views\Tests\Wizard\WizardTestBase;
|
||||
|
||||
/**
|
||||
* Tests the comment module integration into the wizard.
|
||||
*
|
||||
* @group comment
|
||||
* @see \Drupal\comment\Plugin\views\wizard\Comment
|
||||
*/
|
||||
class WizardTest extends WizardTestBase {
|
||||
|
||||
use CommentTestTrait;
|
||||
|
||||
/**
|
||||
* Modules to install.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('node', 'comment');
|
||||
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->drupalCreateContentType(array('type' => 'page', 'name' => t('Basic page')));
|
||||
// Add comment field to page node type.
|
||||
$this->addDefaultCommentField('node', 'page');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests adding a view of comments.
|
||||
*/
|
||||
public function testCommentWizard() {
|
||||
$view = array();
|
||||
$view['label'] = $this->randomMachineName(16);
|
||||
$view['id'] = strtolower($this->randomMachineName(16));
|
||||
$view['show[wizard_key]'] = 'comment';
|
||||
$view['page[create]'] = TRUE;
|
||||
$view['page[path]'] = $this->randomMachineName(16);
|
||||
|
||||
// Just triggering the saving should automatically choose a proper row
|
||||
// plugin.
|
||||
$this->drupalPostForm('admin/structure/views/add', $view, t('Save and edit'));
|
||||
$this->assertUrl('admin/structure/views/view/' . $view['id'], array(), 'Make sure the view saving was successful and the browser got redirected to the edit page.');
|
||||
|
||||
// If we update the type first we should get a selection of comment valid
|
||||
// row plugins as the select field.
|
||||
|
||||
$this->drupalGet('admin/structure/views/add');
|
||||
$this->drupalPostForm('admin/structure/views/add', $view, t('Update "of type" choice'));
|
||||
|
||||
// Check for available options of the row plugin.
|
||||
$xpath = $this->constructFieldXpath('name', 'page[style][row_plugin]');
|
||||
$fields = $this->xpath($xpath);
|
||||
$options = array();
|
||||
foreach ($fields as $field) {
|
||||
$items = $this->getAllOptions($field);
|
||||
foreach ($items as $item) {
|
||||
$options[] = $item->attributes()->value;
|
||||
}
|
||||
}
|
||||
$expected_options = array('entity:comment', 'fields');
|
||||
$this->assertEqual($options, $expected_options);
|
||||
|
||||
$view['id'] = strtolower($this->randomMachineName(16));
|
||||
$this->drupalPostForm(NULL, $view, t('Save and edit'));
|
||||
$this->assertUrl('admin/structure/views/view/' . $view['id'], array(), 'Make sure the view saving was successful and the browser got redirected to the edit page.');
|
||||
|
||||
$user = $this->drupalCreateUser(['access comments']);
|
||||
$this->drupalLogin($user);
|
||||
|
||||
$view = Views::getView($view['id']);
|
||||
$view->initHandlers();
|
||||
$row = $view->display_handler->getOption('row');
|
||||
$this->assertEqual($row['type'], 'entity:comment');
|
||||
|
||||
// Check for the default filters.
|
||||
$this->assertEqual($view->filter['status']->table, 'comment_field_data');
|
||||
$this->assertEqual($view->filter['status']->field, 'status');
|
||||
$this->assertTrue($view->filter['status']->value);
|
||||
$this->assertEqual($view->filter['status_node']->table, 'node_field_data');
|
||||
$this->assertEqual($view->filter['status_node']->field, 'status');
|
||||
$this->assertTrue($view->filter['status_node']->value);
|
||||
|
||||
// Check for the default fields.
|
||||
$this->assertEqual($view->field['subject']->table, 'comment_field_data');
|
||||
$this->assertEqual($view->field['subject']->field, 'subject');
|
||||
}
|
||||
|
||||
}
|
Reference in a new issue