Skip to content
Snippets Groups Projects
Commit 46b3b18f authored by Ide Braakman's avatar Ide Braakman
Browse files

Issue #3463304: Fix failing PHPUnit test

parent cc86f4b8
No related branches found
No related tags found
1 merge request!18Issue #3463304: Fix failing PHPUnit test
Pipeline #232262 passed with warnings
......@@ -8,3 +8,4 @@ commands:
- core/drupal
- core/drupal.ajax
- core/drupal.form
- core/drupal.dialog.ajax
......@@ -6,7 +6,6 @@ use Drupal\comment\Entity\Comment;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\editor\Entity\Editor;
use Drupal\filter\Entity\FilterFormat;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\user\Entity\Role;
......@@ -28,8 +27,6 @@ class AjaxCommentsFunctionalTest extends WebDriverTestBase {
'ajax_comments',
'node',
'comment',
'editor',
'ckeditor',
'filter',
];
......@@ -64,31 +61,13 @@ class AjaxCommentsFunctionalTest extends WebDriverTestBase {
* Tests that comments can be posted and edited over ajax without errors.
*/
public function testCommentPosting() {
// Enable CKEditor.
$format = $this->randomMachineName();
FilterFormat::create([
'format' => $format,
$format = FilterFormat::create([
'format' => $this->randomMachineName(),
'name' => $this->randomString(),
'weight' => 1,
'filters' => [],
])->save();
$settings['toolbar']['rows'] = [
[
[
'name' => 'Links',
'items' => [
'DrupalLink',
'DrupalUnlink',
],
],
],
];
$editor = Editor::create([
'format' => $format,
'editor' => 'ckeditor',
]);
$editor->setSettings($settings);
$editor->save();
$format->save();
$admin_user = $this->drupalCreateUser([
'access content',
......@@ -100,7 +79,7 @@ class AjaxCommentsFunctionalTest extends WebDriverTestBase {
'edit own comments',
'post comments',
'skip comment approval',
'use text format ' . $format,
$format->getPermissionName(),
]);
$this->drupalLogin($admin_user);
......@@ -110,40 +89,16 @@ class AjaxCommentsFunctionalTest extends WebDriverTestBase {
]);
$this->drupalGet($node->toUrl());
// Set up JavaScript to track errors.
$onerror_javascript = <<<JS
(function (){
window.jsErrors = [];
window.onerror = function (message, source, lineno, colno, error) {
window.jsErrors.push(error);
}
}());
JS;
$this->getSession()->executeScript($onerror_javascript);
$page = $this->getSession()->getPage();
// Post comments through ajax.
for ($i = 0; $i < 11; $i++) {
$comment_body_id = $page
->findField('comment_body[0][value]')
->getAttribute('id');
$count = $i + 1;
$ckeditor_javascript = <<<JS
(function (){
CKEDITOR.instances['$comment_body_id'].setData('New comment $count');
}());
JS;
$this->getSession()->executeScript($ckeditor_javascript);
$page->pressButton('Save');
$this->assertSession()->assertWaitOnAjaxRequest(20000);
for ($i = 1; $i < 12; $i++) {
$comment_form = $this->assertSession()->waitForElement('css', 'form.comment-form');
$comment_form->findField('comment_body[0][value]')->setValue("New comment $i");
$comment_form->pressButton('Save');
$this->assertSession()->waitForElement('css', "#comment-$i");
}
// Export the updated content of the page.
if ($this->htmlOutputEnabled) {
$out = $page->getContent();
$this->htmlOutput($out);
}
$this->assertSession()->pageTextContains('Your comment has been posted.');
$this->assertSession()->pageTextContains('New comment 1');
$this->assertSession()->pageTextContains('New comment 2');
......@@ -155,13 +110,6 @@ JS;
$this->assertSession()->linkByHrefExists($current_path . '?page=1');
$javascript_assertion = <<<JS
(function () {
return window.jsErrors.length === 0;
}());
JS;
$this->assertJsCondition($javascript_assertion);
// Using prepareRequest() followed by refreshVariables() seems to help
// refresh the route permissions for the ajax_comments.update route.
$this->prepareRequest();
......@@ -169,86 +117,28 @@ JS;
// Test updating a comment through ajax.
$this->clickLink('Edit');
$this->assertSession()->assertWaitOnAjaxRequest(20000);
$comment_body_id = $page->find('css', 'form.ajax-comments-form-edit textarea')->getAttribute('id');
$ckeditor_javascript = <<<JS
(function (){
CKEDITOR.instances['$comment_body_id'].setData('Updated comment');
}());
JS;
$this->getSession()->executeScript($ckeditor_javascript);
if ($this->htmlOutputEnabled) {
$out = $page->getContent();
$html_output = $out . '<hr />' . $this->getHtmlOutputHeaders();
$this->htmlOutput($html_output);
}
// Save the edits to the comment.
$this->refreshVariables();
$save_button = $page->find('css', 'form.ajax-comments-form-edit input[value=Save]');
$this->assertTrue(!empty($save_button));
$save_button->press();
$this->assertSession()->assertWaitOnAjaxRequest(20000);
if ($this->htmlOutputEnabled) {
$out = $page->getContent();
$html_output = $out . '<hr />' . $this->getHtmlOutputHeaders();
$this->htmlOutput($html_output);
}
$this->assertSession()->pageTextContains('Updated comment');
$this->assertJsCondition($javascript_assertion);
$edit_form = $this->assertSession()->waitForElementVisible('css', 'form.ajax-comments-form-edit');
$edit_form->fillField('comment_body[0][value]', 'Updated comment');
$edit_form->pressButton('Save');
$this->assertSession()->waitForText('Updated comment');
// Test the cancel button.
$this->clickLink('Edit');
$this->assertSession()->assertWaitOnAjaxRequest(20000);
$this->assertSession()->elementExists('css', 'form.ajax-comments-form-edit');
if ($this->htmlOutputEnabled) {
$out = $page->getContent();
$html_output = $out . '<hr />' . $this->getHtmlOutputHeaders();
$this->htmlOutput($html_output);
}
$cancel_button = $page->find('css', 'form.ajax-comments-form-edit input[value=Cancel]');
$this->assertTrue(!empty($cancel_button));
$cancel_button->press();
$this->assertSession()->assertWaitOnAjaxRequest(20000);
if ($this->htmlOutputEnabled) {
$out = $page->getContent();
$html_output = $out . '<hr />' . $this->getHtmlOutputHeaders();
$this->htmlOutput($html_output);
}
$edit_form = $this->assertSession()->waitForElementVisible('css', 'form.ajax-comments-form-edit');
$edit_form->pressButton('Cancel');
// Test replying to a comment.
$this->clickLink('Reply');
$this->assertSession()->assertWaitOnAjaxRequest(20000);
$comment_body_id = $page->find('css', 'form.ajax-comments-form-reply textarea')->getAttribute('id');
$ckeditor_javascript = <<<JS
(function (){
CKEDITOR.instances['$comment_body_id'].setData('Comment reply');
}());
JS;
$this->getSession()->executeScript($ckeditor_javascript);
if ($this->htmlOutputEnabled) {
$out = $page->getContent();
$html_output = $out . '<hr />' . $this->getHtmlOutputHeaders();
$this->htmlOutput($html_output);
}
$save_button = $page->find('css', 'form.ajax-comments-form-reply input[value=Save]');
$this->assertTrue(!empty($save_button));
$save_button->press();
$this->assertSession()->assertWaitOnAjaxRequest(20000);
if ($this->htmlOutputEnabled) {
$out = $page->getContent();
$html_output = $out . '<hr />' . $this->getHtmlOutputHeaders();
$this->htmlOutput($html_output);
}
$this->assertSession()->pageTextContains('Comment reply');
$this->assertJsCondition($javascript_assertion);
$reply_form = $this->assertSession()->waitForElementVisible('css', 'form.ajax-comments-form-reply');
$reply_form->fillField('comment_body[0][value]', 'Comment reply');
$reply_form->pressButton('Save');
$this->assertSession()->waitForText('Comment reply');
// Test deleting a comment.
$delete_link = $page->findLink('Delete');
$this->assertTrue(!empty($delete_link));
$this->assertNotNull($delete_link);
$delete_url = $delete_link->getAttribute('href');
$this->assertTrue(!empty($delete_url));
$this->assertNotNull($delete_url);
// Get the comment ID (in $matches[1]).
preg_match('/comment\/(.+)\/delete/i', $delete_url, $matches);
......@@ -267,148 +157,97 @@ JS;
$confirm_question = substr(strip_tags($delete_form->getQuestion()), 0, 50);
$delete_link->click();
$this->assertSession()->assertWaitOnAjaxRequest(20000);
if ($this->htmlOutputEnabled) {
$out = $page->getContent();
$html_output = $out . '<hr />' . $this->getHtmlOutputHeaders();
$this->htmlOutput($html_output);
}
$this->assertSession()->pageTextContains($confirm_question);
$this->assertSession()->waitForText($confirm_question);
$delete_button = $page->find('css', '.ui-dialog button.button--primary.js-form-submit');
$this->assertTrue(!empty($delete_button));
$this->assertNotNull($delete_button);
$delete_button->click();
$this->assertSession()->assertWaitOnAjaxRequest(20000);
if ($this->htmlOutputEnabled) {
$out = $page->getContent();
$html_output = $out . '<hr />' . $this->getHtmlOutputHeaders();
$this->htmlOutput($html_output);
}
$this->assertSession()->waitForText('The comment and all its replies have been deleted.');
$this->assertSession()->pageTextNotContains($comment_to_delete_body);
$this->assertJsCondition($javascript_assertion);
// Test removing the role's permission to post comments.
/** @var \Drupal\user\RoleInterface[] $roles */
$roles = Role::loadMultiple($admin_user->getRoles());
foreach ($roles as $role) {
$role->revokePermission('post comments');
$role->trustData()->save();
$role->revokePermission('post comments')->save();
}
$this->refreshVariables();
// Now try to submit a new comment. We haven't reloaded the page after
// changing permissions, so the comment field should still be visible.
$comment_body_id = $page
->findField('comment_body[0][value]')
->getAttribute('id');
$ckeditor_javascript = <<<JS
(function (){
CKEDITOR.instances['$comment_body_id'].setData('This should fail.');
}());
JS;
$this->getSession()->executeScript($ckeditor_javascript);
$page->pressButton('Save');
$this->assertSession()->assertWaitOnAjaxRequest(20000);
$comment_form = $this->assertSession()->waitForElement('css', 'form.comment-form');
$comment_form->findField('comment_body[0][value]')->setValue('This should fail.');
$comment_form->pressButton('Save');
// Confirm that the new comment does not appear.
$this->assertSession()->pageTextNotContains('This should fail.');
// Confirm that the error message DOES appear.
$this->assertSession()->pageTextContains('You do not have permission to post a comment.');
if ($this->htmlOutputEnabled) {
$out = $page->getContent();
$html_output = $out . '<hr />' . $this->getHtmlOutputHeaders();
$this->htmlOutput($html_output);
}
$this->assertSession()->waitForText('You do not have permission to post a comment.');
// Restore the user's permission to post comments, and reload the page
// so that the reply links are visible.
/** @var \Drupal\user\RoleInterface[] $roles */
$roles = Role::loadMultiple($admin_user->getRoles());
foreach ($roles as $role) {
$role->grantPermission('post comments');
$role->trustData()->save();
$role->grantPermission('post comments')->save();
}
$this->refreshVariables();
// Reload the page.
$this->drupalGet($node->toUrl());
$reply_link = $page->findLink('Reply');
$this->assertNotNull($reply_link);
// Revoke the user's permission to post comments, again.
/** @var \Drupal\user\RoleInterface[] $roles */
$roles = Role::loadMultiple($admin_user->getRoles());
foreach ($roles as $role) {
$role->revokePermission('post comments');
$role->trustData()->save();
$role->revokePermission('post comments')->save();
}
$this->refreshVariables();
// Click the link to reply to a comment. The link should still be present,
// because we haven't reloaded the page since revoking the user's
// permission.
$reply_link = $page->findLink('Reply');
$reply_link->click();
$this->assertSession()->assertWaitOnAjaxRequest(20000);
if ($this->htmlOutputEnabled) {
$out = $page->getContent();
$html_output = $out . '<hr />' . $this->getHtmlOutputHeaders();
$this->htmlOutput($html_output);
}
// Confirm that the error message appears.
$this->assertSession()->pageTextContains('You do not have permission to post a comment.');
$this->assertSession()->waitForText('You do not have permission to post a comment.');
// Again, restore the user's permission to post comments, and
// reload the page so that the reply links are visible.
/** @var \Drupal\user\RoleInterface[] $roles */
$roles = Role::loadMultiple($admin_user->getRoles());
foreach ($roles as $role) {
$role->grantPermission('post comments');
$role->trustData()->save();
$role->grantPermission('post comments')->save();
}
$this->refreshVariables();
// Reload the page.
$this->drupalGet($node->toUrl());
// Click the link to reply to a comment.
$reply_link = $page->findLink('Reply');
$reply_link->click();
$this->assertSession()->assertWaitOnAjaxRequest(20000);
$this->clickLink('Reply');
// The reply form should load. Enter a comment in the reply field.
$comment_body_id = $page->find('css', 'form.ajax-comments-form-reply textarea')->getAttribute('id');
$ckeditor_javascript = <<<JS
(function (){
CKEDITOR.instances['$comment_body_id'].setData('This reply should fail.');
}());
JS;
$this->getSession()->executeScript($ckeditor_javascript);
if ($this->htmlOutputEnabled) {
$out = $page->getContent();
$html_output = $out . '<hr />' . $this->getHtmlOutputHeaders();
$this->htmlOutput($html_output);
}
$reply_form = $this->assertSession()->waitForElementVisible('css', 'form.ajax-comments-form-reply');
$reply_form->fillField('comment_body[0][value]', 'This reply should fail.');
// Revoke the user's permission to post comments without reloading the page.
/** @var \Drupal\user\RoleInterface[] $roles */
$roles = Role::loadMultiple($admin_user->getRoles());
foreach ($roles as $role) {
$role->revokePermission('post comments');
$role->trustData()->save();
$role->revokePermission('post comments')->save();
}
$this->refreshVariables();
// Now try to click the 'Save' button on the reply form.
$save_button = $page->find('css', 'form.ajax-comments-form-reply input[value=Save]');
$this->assertTrue(!empty($save_button));
$save_button->press();
$this->assertSession()->assertWaitOnAjaxRequest(20000);
if ($this->htmlOutputEnabled) {
$out = $page->getContent();
$html_output = $out . '<hr />' . $this->getHtmlOutputHeaders();
$this->htmlOutput($html_output);
}
$reply_form->pressButton('Save');
// Confirm that the new comment does not appear.
$this->assertSession()->pageTextNotContains('This reply should fail.');
// Confirm that the error message DOES appear.
$this->assertSession()->pageTextContains('You do not have permission to post a comment.');
$this->assertSession()->waitForText('You do not have permission to post a comment.');
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment