View source
<?php
namespace Drupal\Tests\ajax_comments\FunctionalJavascript;
use Drupal\comment\Entity\Comment;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\editor\Entity\Editor;
use Drupal\filter\Entity\FilterFormat;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\user\Entity\Role;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
class AjaxCommentsFunctionalTest extends WebDriverTestBase {
use CommentTestTrait;
public static $modules = [
'system',
'ajax_comments',
'node',
'comment',
'editor',
'ckeditor',
'filter',
];
protected $defaultTheme = 'stark';
protected function setUp() {
parent::setUp();
$this->entityTypeManager = $this->container
->get('entity_type.manager');
$this
->drupalCreateContentType([
'type' => 'article',
]);
$this
->addDefaultCommentField('node', 'article');
$comment_field = $this->entityTypeManager
->getStorage('field_config')
->load('node.article.comment');
$comment_field
->setSetting('per_page', 10);
$comment_field
->save();
$entity_view_display = EntityViewDisplay::load('node.article.default');
$renderer = $entity_view_display
->getRenderer('comment');
$renderer
->setThirdPartySetting('ajax_comments', 'enable_ajax_comments', '1');
$entity_view_display
->save();
}
public function testCommentPosting() {
$format = $this
->randomMachineName();
FilterFormat::create([
'format' => $format,
'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();
$admin_user = $this
->drupalCreateUser([
'access content',
'access comments',
'access user profiles',
'administer comments',
'edit own comments',
'post comments',
'skip comment approval',
'use text format ' . $format,
]);
$this
->drupalLogin($admin_user);
$node = $this
->drupalCreateNode([
'type' => 'article',
'comment' => CommentItemInterface::OPEN,
]);
$this
->drupalGet($node
->toUrl());
$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();
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['{<span class="php-variable">$comment_body_id</span>}'].setData('New comment {<span class="php-variable">$count</span>}');
}());
JS;
$this
->getSession()
->executeScript($ckeditor_javascript);
$page
->pressButton('Save');
$this
->assertSession()
->assertWaitOnAjaxRequest(20000);
}
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');
$current_url = $this
->getSession()
->getCurrentUrl();
$parts = parse_url($current_url);
$path = empty($parts['path']) ? '/' : $parts['path'];
$current_path = preg_replace('/^\\/[^\\.\\/]+\\.php\\//', '/', $path);
$this
->assertSession()
->linkByHrefExists($current_path . '?page=1');
$javascript_assertion = <<<JS
(function () {
return window.jsErrors.length === 0;
}());
JS;
$this
->assertJsCondition($javascript_assertion);
$this
->prepareRequest();
$this
->refreshVariables();
$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['{<span class="php-variable">$comment_body_id</span>}'].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);
}
$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);
$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);
}
$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['{<span class="php-variable">$comment_body_id</span>}'].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);
$delete_link = $page
->findLink('Delete');
$this
->assertTrue(!empty($delete_link));
$delete_url = $delete_link
->getAttribute('href');
$this
->assertTrue(!empty($delete_url));
preg_match('/comment\\/(.+)\\/delete/i', $delete_url, $matches);
$this
->assertTrue(isset($matches[1]));
$comment_to_delete = Comment::load($matches[1]);
$comment_to_delete_body = $comment_to_delete
->get('comment_body')->value;
$delete_form = $this->container
->get('entity_type.manager')
->getFormObject($comment_to_delete
->getEntityTypeId(), 'delete');
$delete_form
->setEntity($comment_to_delete);
$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);
$delete_button = $page
->find('css', '.ui-dialog button.button--primary.js-form-submit');
$this
->assertTrue(!empty($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()
->pageTextNotContains($comment_to_delete_body);
$this
->assertJsCondition($javascript_assertion);
$roles = Role::loadMultiple($admin_user
->getRoles());
foreach ($roles as $role) {
$role
->revokePermission('post comments');
$role
->trustData()
->save();
}
$comment_body_id = $page
->findField('comment_body[0][value]')
->getAttribute('id');
$ckeditor_javascript = <<<JS
(function (){
CKEDITOR.instances['{<span class="php-variable">$comment_body_id</span>}'].setData('This should fail.');
}());
JS;
$this
->getSession()
->executeScript($ckeditor_javascript);
$page
->pressButton('Save');
$this
->assertSession()
->assertWaitOnAjaxRequest(20000);
$this
->assertSession()
->pageTextNotContains('This should fail.');
$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);
}
$roles = Role::loadMultiple($admin_user
->getRoles());
foreach ($roles as $role) {
$role
->grantPermission('post comments');
$role
->trustData()
->save();
}
$this
->drupalGet($node
->toUrl());
$roles = Role::loadMultiple($admin_user
->getRoles());
foreach ($roles as $role) {
$role
->revokePermission('post comments');
$role
->trustData()
->save();
}
$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);
}
$this
->assertSession()
->pageTextContains('You do not have permission to post a comment.');
$roles = Role::loadMultiple($admin_user
->getRoles());
foreach ($roles as $role) {
$role
->grantPermission('post comments');
$role
->trustData()
->save();
}
$this
->drupalGet($node
->toUrl());
$reply_link = $page
->findLink('Reply');
$reply_link
->click();
$this
->assertSession()
->assertWaitOnAjaxRequest(20000);
$comment_body_id = $page
->find('css', 'form.ajax-comments-form-reply textarea')
->getAttribute('id');
$ckeditor_javascript = <<<JS
(function (){
CKEDITOR.instances['{<span class="php-variable">$comment_body_id</span>}'].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);
}
$roles = Role::loadMultiple($admin_user
->getRoles());
foreach ($roles as $role) {
$role
->revokePermission('post comments');
$role
->trustData()
->save();
}
$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()
->pageTextNotContains('This reply should fail.');
$this
->assertSession()
->pageTextContains('You do not have permission to post a comment.');
}
}