Commit 62105329 authored by gnuget's avatar gnuget Committed by greggles

Issue #2618192 by gnuget, DamienMcKenna, Arla: Increase basic test coverage

parent 685d0a3c
......@@ -2,29 +2,12 @@
namespace Drupal\Tests\comment_notify\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\comment\Tests\CommentTestTrait;
/**
* Tests for the comment_notify module.
*
* @group comment_notify
*/
class CommentNotifyConfigPageTest extends BrowserTestBase {
use CommentTestTrait;
/**
* Modules to enable.
*
* @var array
*/
public static $modules = [
'comment_notify',
'node',
'comment',
'token',
];
class CommentNotifyConfigPageTest extends CommentNotifyTestBase {
/**
* Test that the config page is working.
......@@ -32,9 +15,7 @@ class CommentNotifyConfigPageTest extends BrowserTestBase {
protected function setUp() {
parent::setUp();
// Create and login administrative user.
$admin_user = $this->drupalCreateUser(array('administer comment notify', 'administer permissions', 'administer comments'));
$this->drupalLogin($admin_user);
$this->drupalLogin($this->adminUser);
// Enable comment notify on this node.
$this->drupalCreateContentType([
......@@ -47,9 +28,117 @@ class CommentNotifyConfigPageTest extends BrowserTestBase {
* Test to all the options are saved correctly.
*/
public function testConfigPage() {
// Check the default values are working.
$this->drupalGet("admin/config/people/comment_notify");
// Test the default values are working.
$this->submitForm([], 'Save configuration');
$this->assertSession()->responseContains('The configuration options have been saved.');
// Test that the content types are saved correctly.
$this->getSession()->getPage()->checkField('node_types[article]');
$this->submitForm([], 'Save configuration');
$this->assertSession()->responseContains('The configuration options have been saved.');
$this->drupalGet("admin/config/people/comment_notify");
$this->assertTrue($this->getSession()->getPage()->hasCheckedField('node_types[article]'));
// Test that Available subscription modes are saved correctly.
$this->getSession()->getPage()->checkField('available_alerts[1]');
$this->getSession()->getPage()->checkField('available_alerts[2]');
$this->submitForm([], 'Save configuration');
$this->assertSession()->responseContains('The configuration options have been saved.');
$this->drupalGet("admin/config/people/comment_notify");
$this->assertTrue($this->getSession()->getPage()->hasCheckedField('available_alerts[1]'));
$this->assertTrue($this->getSession()->getPage()->hasCheckedField('available_alerts[2]'));
// Test that at least one subscription mode must be enabled.
$this->getSession()->getPage()->uncheckField('available_alerts[1]');
$this->getSession()->getPage()->uncheckField('available_alerts[2]');
$this->submitForm([], 'Save configuration');
$this->assertSession()->responseContains('You must enable at least one subscription mode.');
$this->getSession()->getPage()->uncheckField('available_alerts[1]');
$this->getSession()->getPage()->checkField('available_alerts[2]');
$this->submitForm([], 'Save configuration');
$this->assertSession()->responseContains('The configuration options have been saved.');
$this->drupalGet("admin/config/people/comment_notify");
$this->assertTrue($this->getSession()->getPage()->hasUncheckedField('available_alerts[1]'));
$this->assertTrue($this->getSession()->getPage()->hascheckedField('available_alerts[2]'));
// The default state select must hide the option as well.
$field = $this->getSession()->getPage()->findField('Default state for the notification selection box');
$this->assertFalse(strpos($field->getHtml(), 'All Comments'));
$this->assertTrue(strpos($field->getHtml(), 'Replies to my comment'));
$this->getSession()->getPage()->uncheckField('available_alerts[2]');
$this->getSession()->getPage()->checkField('available_alerts[1]');
$this->submitForm([], 'Save configuration');
$this->assertSession()->responseContains('The configuration options have been saved.');
$this->drupalGet("admin/config/people/comment_notify");
$this->assertTrue($this->getSession()->getPage()->hasUncheckedField('available_alerts[2]'));
$this->assertTrue($this->getSession()->getPage()->hascheckedField('available_alerts[1]'));
// The default state select must hide the option as well.
$field = $this->getSession()->getPage()->findField('Default state for the notification selection box');
$this->assertTrue(strpos($field->getHtml(), 'All comments'));
$this->assertFalse(strpos($field->getHtml(), 'Replies to my comment'));
$this->getSession()->getPage()->checkField('available_alerts[1]');
$this->getSession()->getPage()->checkField('available_alerts[2]');
$this->submitForm([], 'Save configuration');
$this->assertSession()->responseContains('The configuration options have been saved.');
$this->drupalGet("admin/config/people/comment_notify");
$this->assertTrue($this->getSession()->getPage()->hascheckedField('available_alerts[1]'));
$this->assertTrue($this->getSession()->getPage()->hascheckedField('available_alerts[2]'));
$field = $this->getSession()->getPage()->findField('Default state for the notification selection box');
$this->assertTrue(strpos($field->getHtml(), 'All comments'));
$this->assertTrue(strpos($field->getHtml(), 'Replies to my comment'));
$this->getSession()->getPage()->selectFieldOption('Default state for the notification selection box', "0");
$this->submitForm([], 'Save configuration');
$this->assertSession()->responseContains('The configuration options have been saved.');
$this->drupalGet("admin/config/people/comment_notify");
$field = $this->getSession()->getPage()->findField('Default state for the notification selection box');
$this->assertTrue($field->getValue() == "0");
$this->drupalGet("admin/config/people/comment_notify");
$this->getSession()->getPage()->selectFieldOption('Default state for the notification selection box', "1");
$this->submitForm([], 'Save configuration');
$this->assertSession()->responseContains('The configuration options have been saved.');
$this->drupalGet("admin/config/people/comment_notify");
$field = $this->getSession()->getPage()->findField('Default state for the notification selection box');
$this->assertTrue($field->getValue() == "1");
$this->getSession()->getPage()->selectFieldOption('Default state for the notification selection box', "2");
$this->submitForm([], 'Save configuration');
$this->assertSession()->responseContains('The configuration options have been saved.');
$this->drupalGet("admin/config/people/comment_notify");
$field = $this->getSession()->getPage()->findField('Default state for the notification selection box');
$this->assertTrue($field->getValue() == "2");
$this->getSession()->getPage()->checkField('Subscribe users to their node follow-up notification emails by default');
$this->submitForm([], 'Save configuration');
$this->assertSession()->responseContains('The configuration options have been saved.');
$this->drupalGet("admin/config/people/comment_notify");
$this->assertTrue($this->getSession()->getPage()->hasCheckedField('Subscribe users to their node follow-up notification emails by default'));
$this->getSession()->getPage()->uncheckField('Subscribe users to their node follow-up notification emails by default');
$this->submitForm([], 'Save configuration');
$this->assertSession()->responseContains('The configuration options have been saved.');
$this->drupalGet("admin/config/people/comment_notify");
$this->assertTrue($this->getSession()->getPage()->hasUncheckedField('Subscribe users to their node follow-up notification emails by default'));
$this->getSession()->getPage()->fillField('Default mail text for sending out notifications to commenters', 'Hello');
$this->submitForm([], 'Save configuration');
$this->assertSession()->responseContains('The configuration options have been saved.');
$this->drupalGet("admin/config/people/comment_notify");
$field = $this->getSession()->getPage()->findField('Default mail text for sending out notifications to commenters');
$this->assertTrue($field->getValue() == 'Hello');
$this->getSession()->getPage()->fillField('Default mail text for sending out the notifications to node authors', 'Hello');
$this->submitForm([], 'Save configuration');
$this->assertSession()->responseContains('The configuration options have been saved.');
$this->drupalGet("admin/config/people/comment_notify");
$field = $this->getSession()->getPage()->findField('Default mail text for sending out the notifications to node authors');
$this->assertTrue($field->getValue() == 'Hello');
}
}
<?php
namespace Drupal\comment_notify\Tests;
namespace Drupal\Tests\comment_notify\Functional;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Language\Language;
use Drupal\comment\CommentInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\node\NodeInterface;
use Drupal\simpletest\WebTestBase;
/**
* Comment notify general tests.
*
* @group comment_notify
*/
class CommentNotifyTest extends WebTestBase {
use CommentTestTrait;
/**
* Modules to enable.
*
* @var array
*/
public static $modules = [
'comment_notify',
'node',
'comment',
'token',
];
class CommentNotifyTest extends CommentNotifyTestBase {
/**
* Test various behaviors for anonymous users.
*/
function testCommentNotifyAnonymousUserFunctionalTest() {
// Code that does something to be tested.
// Create and login administrative user.
$admin_user = $this->drupalCreateUser(array('administer comment notify', 'administer permissions', 'administer comments'));
$this->drupalLogin($admin_user);
public function testCommentNotifyAnonymousUserFunctionalTest() {
$this->drupalLogin($this->adminUser);
// Enable comment notify on this node and allow anonymous information in comments.
// Enable comment notify on this node and allow anonymous information in
// comments.
$this->drupalCreateContentType([
'type' => 'article',
]);
$this->addDefaultCommentField('node', 'article');
$comment_field = FieldConfig::loadByName('node', 'article', 'comment');
$comment_field->setSetting('anonymous', COMMENT_ANONYMOUS_MAY_CONTACT);
$comment_field->setSetting('anonymous', CommentInterface::ANONYMOUS_MAY_CONTACT);
$comment_field->save();
// Create a node with comments allowed.
$node = $this->drupalCreateNode(array('type' => 'article', 'promote' => NODE_PROMOTED));
$node = $this->drupalCreateNode(array('type' => 'article', 'promote' => NodeInterface::PROMOTED));
// Allow anonymous users to post comments and get notifications.
user_role_grant_permissions(AccountInterface::ANONYMOUS_ROLE, array('access comments', 'access content', 'post comments', 'skip comment approval', 'subscribe to comments'));
user_role_grant_permissions(
AccountInterface::ANONYMOUS_ROLE,
[
'access comments',
'access content',
'post comments',
'skip comment approval',
'subscribe to comments',
]
);
$this->drupalLogout();
// Notify type 1 - All comments on the node.
// First confirm that we properly require an e-mail address.
$subscribe_1 = array('notify' => TRUE, 'notify_type' => COMMENT_NOTIFY_NODE);
$this->drupalGet($node->url());
$subscribe_1 = ['notify' => TRUE, 'notify_type' => COMMENT_NOTIFY_NODE];
$this->drupalGet($node->toUrl());
$this->postCommentNotifyComment($node, $this->randomMachineName(), $this->randomMachineName(), $subscribe_1);
$this->assertText(t('If you want to subscribe to comments you must supply a valid e-mail address.'));
$this->assertTrue($this->getSession()->getPage()->hasContent(t('If you want to subscribe to comments you must supply a valid e-mail address.')));
// Try again with an e-mail address.
$contact_1 = array('name' => $this->randomMachineName(), 'mail' => $this->getRandomEmailAddress());
......@@ -69,7 +60,7 @@ class CommentNotifyTest extends WebTestBase {
// Confirm that the notification is saved.
$result = comment_notify_get_notification_type($anonymous_comment_1['id']);
$this->assertEqual($result, $subscribe_1['notify_type'], 'Notify selection option 1 is saved properly.');
$this->assertEquals($result, $subscribe_1['notify_type'], 'Notify selection option 1 is saved properly.');
// Notify type 2 - replies to a comment.
$subscribe_2 = array('notify' => TRUE, 'notify_type' => COMMENT_NOTIFY_COMMENT);
......@@ -78,16 +69,17 @@ class CommentNotifyTest extends WebTestBase {
// Confirm that the notification is saved.
$result = comment_notify_get_notification_type($anonymous_comment_2['id']);
$this->assertEqual($result, $subscribe_2['notify_type'], 'Notify selection option 2 is saved properly.');
$this->assertEquals($result, $subscribe_2['notify_type'], 'Notify selection option 2 is saved properly.');
// Confirm that the original subscriber with all comments on this node got their mail.
// Confirm that the original subscriber with all comments on this node got
// their mail.
$this->assertMail('to', $contact_1['mail'], t('Message was sent to the proper anonymous user.'));
// Test the unsubscribe link.
$mails = $this->getMails();
preg_match("/\/comment_notify\/disable\/.+/", $mails[0]['body'], $output);
$this->drupalGet($output[0]);
$this->assertText("Your comment follow-up notification for this post was disabled. Thanks.");
$this->assertTrue($this->getSession()->getPage()->hasContent("Your comment follow-up notification for this post was disabled. Thanks."));
// Notify type 0 (i.e. only one mode is enabled).
\Drupal::configFactory()->getEditable('comment_notify.settings')->set('available_alerts', [1 => FALSE, 2 => TRUE])->save();
......@@ -97,82 +89,8 @@ class CommentNotifyTest extends WebTestBase {
// Confirm that the notification is saved.
$result = comment_notify_get_notification_type($anonymous_comment_0['id']);
$this->assertEqual($result, 2, 'Notify selection option 0 is saved properly.');
$this->assertEquals($result, 2, 'Notify selection option 0 is saved properly.');
// TODO yet more tests.
}
/**
* Post comment.
*
* @param object $node Node to post comment on.
* @param string $subject Comment subject.
* @param string $comment Comment body.
* @param boolean $preview Should preview be required.
* @param mixed $contact Set to NULL for no contact info, TRUE to ignore success checking, and array of values to set contact info.
*/
protected function postCommentNotifyComment(NodeInterface $node, $subject, $comment, $notify, $contact = NULL) {
$edit = array();
$edit['subject[0][value]'] = $subject;
$edit['comment_body[0][value]'] = $comment;
if ($notify !== NULL && is_array($notify)) {
$edit += $notify;
}
if ($contact !== NULL && is_array($contact)) {
$edit += $contact;
}
$this->drupalPostForm($node->url(), $edit, t('Save'));
$match = array();
// Get comment ID
preg_match('/#comment-([^"]+)/', $this->getURL(), $match);
// Get comment.
if (!empty($match[1])) { // 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])), t('Comment id found.'));
}
if (isset($match[1])) {
return array('id' => $match[1], 'subject' => $subject, 'comment' => $comment);
}
}
/**
* Checks current page for specified comment.
*
* @param object $comment Comment object.
* @param boolean $reply The comment is a reply to another comment.
* @return boolean Comment found.
*/
function commentExists($comment, $reply = FALSE) {
if ($comment && is_object($comment)) {
$regex = '/' . ($reply ? '<div class="indented">(.*?)' : '');
$regex .= '<a id="comment-' . $comment->id . '"(.*?)'; // Comment anchor.
$regex .= '<div(.*?)'; // Begin in comment div.
$regex .= $comment->subject . '(.*?)'; // Match subject.
$regex .= $comment->comment . '(.*?)'; // Match comment.
$regex .= '<\/div>/s'; // Dot matches newlines and ensure that match doesn't bleed outside comment div.
return (boolean)preg_match($regex, $this->getRawContent());
}
else {
return FALSE;
}
}
/**
* Returns a randomly generated valid email address.
*
* @return string.
*/
function getRandomEmailAddress() {
return $this->randomMachineName() . '@example.com';
}
}
<?php
namespace Drupal\Tests\comment_notify\Functional;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\Core\Test\AssertMailTrait;
use Drupal\node\NodeInterface;
use Drupal\Tests\BrowserTestBase;
/**
* Comment notify Base Test class.
*/
abstract class CommentNotifyTestBase extends BrowserTestBase {
use CommentTestTrait;
use AssertMailTrait;
/**
* Admin User.
*
* @var \Drupal\user\Entity\User $adminUser
*/
protected $adminUser;
/**
* Modules to enable.
*
* @var array
*/
public static $modules = [
'comment_notify',
'node',
'comment',
'token',
];
/**
* Test that the config page is working.
*/
protected function setUp() {
parent::setUp();
// Create and login administrative user.
$this->adminUser = $this->drupalCreateUser(
[
'administer comment notify',
'administer permissions',
'administer comments',
]
);
}
/**
* Post comment.
*
* @param \Drupal\node\NodeInterface $node
* Node to post comment on.
* @param string $subject
* Comment subject.
* @param string $comment
* Comment body.
* @param array $notify
* An array with the notify values.
* @param mixed $contact
* Set to NULL for no contact info, TRUE to ignore success checking, and
* array of values to set contact info.
*
* @return array|bool
* return an array with the comment or false if the post comment fails.
*/
protected function postCommentNotifyComment(NodeInterface $node, $subject, $comment, $notify, $contact = NULL) {
$edit = [];
$edit['subject[0][value]'] = $subject;
$edit['comment_body[0][value]'] = $comment;
if ($notify !== NULL && is_array($notify)) {
$edit += $notify;
}
if ($contact !== NULL && is_array($contact)) {
$edit += $contact;
}
$this->drupalPostForm($node->toUrl()->toString(), $edit, t('Save'));
$match = array();
// Get comment ID.
preg_match('/#comment-([^"]+)/', $this->getURL(), $match);
// Get comment.
// If true then attempting to find error message.
if (!empty($match[1])) {
if ($subject) {
$this->assertTrue($this->getSession()->getPage()->hasContent($subject), 'Comment subject posted.');
}
$this->assertTrue($this->getSession()->getPage()->hasContent($comment), 'Comment body posted.');
$this->assertTrue((!empty($match) && !empty($match[1])), t('Comment id found.'));
}
if (isset($match[1])) {
return ['id' => $match[1], 'subject' => $subject, 'comment' => $comment];
}
return FALSE;
}
/**
* Checks current page for specified comment.
*
* @param object $comment
* Comment object.
* @param bool $reply
* The comment is a reply to another comment.
*
* @return bool
* Comment found.
*/
protected function commentExists($comment, $reply = FALSE) {
if ($comment && is_object($comment)) {
$regex = '/' . ($reply ? '<div class="indented">(.*?)' : '');
// Comment anchor.
$regex .= '<a id="comment-' . $comment->id . '"(.*?)';
// Begin in comment div.
$regex .= '<div(.*?)';
// Match subject.
$regex .= $comment->subject . '(.*?)';
// Match comment.
$regex .= $comment->comment . '(.*?)';
// Dot matches newlines and ensure that match doesn't bleed outside
// comment div.
$regex .= '<\/div>/s';
return (boolean) preg_match($regex, $this->getSession()->getPage()->getContent());
}
else {
return FALSE;
}
}
/**
* Returns a randomly generated valid email address.
*
* @return string.
* A random email.
*/
public function getRandomEmailAddress() {
return $this->randomMachineName() . '@example.com';
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment