Commit 3597274c authored by gnuget's avatar gnuget Committed by gnuget

Issue #2618182 by DamienMcKenna, gnuget, ashrafabed, Silvan Wakker, toamit,...

Issue #2618182 by DamienMcKenna, gnuget, ashrafabed, Silvan Wakker, toamit, Arla, peezy, nevergone: Support all entity types as commentable, not only nodes
parent 0546cd5f
......@@ -50,7 +50,7 @@ function comment_notify_get_user_notification_setting($uid) {
function comment_notify_get_default_notification_setting() {
return (object) [
'comment_notify' => \Drupal::config('comment_notify.settings')->get('enable_default.watcher'),
'node_notify' => \Drupal::config('comment_notify.settings')->get('enable_default.entity_author'),
'entity_notify' => \Drupal::config('comment_notify.settings')->get('enable_default.entity_author'),
];
}
......@@ -87,9 +87,9 @@ function comment_notify_get_user_comment_notify_preference($uid) {
}
/**
* Get a user's default preference for node update notification.
* Get a user's default preference for entity update notification.
*
* This is notification on nodes where the user is the author.
* This is notification on entity where the user is the author.
*
* @param int $uid
* The User ID.
......@@ -97,12 +97,12 @@ function comment_notify_get_user_comment_notify_preference($uid) {
* @return int
* Return the node_notify value.
*/
function comment_notify_get_user_node_notify_preference($uid) {
function comment_notify_get_user_entity_notify_preference($uid) {
$setting = comment_notify_get_user_notification_setting($uid);
if (!$setting) {
$setting = comment_notify_get_default_notification_setting();
}
return $setting->node_notify;
return $setting->entity_notify;
}
/**
......@@ -110,22 +110,22 @@ function comment_notify_get_user_node_notify_preference($uid) {
*
* @param int $uid
* The User ID.
* @param int $node_notification
* The node notification value.
* @param int $entity_notification
* The entity notification value.
* @param int $comment_notification
* The comment notification value.
*
* @return bool
* TRUE if the notification was set correctly.
*/
function comment_notify_set_user_notification_setting($uid, $node_notification = NULL, $comment_notification = NULL) {
function comment_notify_set_user_notification_setting($uid, $entity_notification = NULL, $comment_notification = NULL) {
if (!$uid) {
throw new Exception('Cannot set user preference, uid missing');
}
$fields = ['uid' => $uid];
if (!is_null($node_notification)) {
$fields['node_notify'] = $node_notification;
if (!is_null($entity_notification)) {
$fields['entity_notify'] = $entity_notification;
}
if (!is_null($comment_notification)) {
$fields['comment_notify'] = $comment_notification;
......@@ -243,19 +243,19 @@ function comment_notify_get_notification_type($cid) {
}
/**
* Get a list of mails which need to be contacted for a node.
* Get a list of mails which need to be contacted for an entity.
*
* @param int $nid
* The node id.
* @param int $entity_id
* The entity id.
* @param string $comment_type
* The comment type.
*
* @return \Drupal\comment\CommentInterface[]
* A list of comment entities.
*/
function comment_notify_get_watchers($nid, $comment_type) {
$cids = db_query("SELECT c.cid FROM {comment_field_data} c INNER JOIN {comment_notify} cn ON c.cid = cn.cid LEFT JOIN {users_field_data} u ON c.uid = u.uid WHERE c.entity_id = :nid AND c.comment_type = :comment_type AND c.status = :status AND cn.notify <> :notify AND (u.uid = 0 OR u.status = 1)", [
':nid' => $nid,
function comment_notify_get_watchers($entity_id, $comment_type) {
$cids = db_query("SELECT c.cid FROM {comment_field_data} c INNER JOIN {comment_notify} cn ON c.cid = cn.cid LEFT JOIN {users_field_data} u ON c.uid = u.uid WHERE c.entity_id = :entity_id AND c.comment_type = :comment_type AND c.status = :status AND cn.notify <> :notify AND (u.uid = 0 OR u.status = 1)", [
':entity_id' => $entity_id,
':comment_type' => $comment_type,
':status' => CommentInterface::PUBLISHED,
':notify' => COMMENT_NOTIFY_DISABLED,
......@@ -331,7 +331,7 @@ function comment_notify_unsubscribe_by_hash($hash) {
$query->condition('cn.notify_hash', $hash)
->condition('cn.notify', COMMENT_NOTIFY_DISABLED, '!=')
->fields('cn', ['cid', 'notify', 'notified'])
->fields('cf', ['entity_id', 'uid'])
->fields('cf', ['entity_id', 'entity_type', 'uid'])
->execute()->fetchObject();
$notification = $query->execute()->fetchObject();
......@@ -339,10 +339,14 @@ function comment_notify_unsubscribe_by_hash($hash) {
return FALSE;
}
// If this notification is at the node level and the commenter has a Drupal
// account, delete all notifications for this node.
if (COMMENT_NOTIFY_NODE == $notification->notify && $notification->uid) {
$result = db_query("SELECT cid FROM {comment_field_data} WHERE entity_id = :entity_id AND uid = :uid", [':entity_id' => $notification->entity_id, ':uid' => $notification->uid]);
// If this notification is at the entity level and the commenter has a Drupal
// account, delete all notifications for this entity.
if (COMMENT_NOTIFY_ENTITY == $notification->notify && $notification->uid) {
$result = db_query("SELECT cid FROM {comment_field_data} WHERE entity_id = :entity_id AND entity_type = :entity_type AND uid = :uid", [
':entity_id' => $notification->entity_id,
':entity_type' => $notification->entity_type,
':uid' => $notification->uid,
]);
$cids = $result->fetchCol();
// Update all comment notifications to be disabled.
......
......@@ -79,7 +79,7 @@ function comment_notify_schema() {
'not null' => TRUE,
'disp-width' => '11',
],
'node_notify' => [
'entity_notify' => [
'type' => 'int',
'description' => 'An integer indicating the default type of subscription: 0 means not subscribed, 1 means subscribed to all comments, and 2 means only subscribed to replies of this comment.',
'size' => 'tiny',
......@@ -101,3 +101,77 @@ function comment_notify_schema() {
return $schema;
}
/**
* Implementations of hook_update().
*/
/**
* Rename db column name from node_notify to entity_notify.
*/
function comment_notify_update_8001() {
$spec = [
'type' => 'int',
'description' => 'An integer indicating the default type of subscription: 0 means not subscribed, 1 means subscribed to all comments, and 2 means only subscribed to replies of this comment.',
'size' => 'tiny',
'not null' => TRUE,
'default' => 0,
'disp-width' => '11',
];
db_change_field('comment_notify_user_settings', 'node_notify', 'entity_notify', $spec);
}
/**
* Preserve node notification settings.
*/
function comment_notify_update_8002() {
$node_values = \Drupal::config('comment_notify.settings')->get('node_types');
$bundle_types = [];
$field_manager = \Drupal::service('entity_field.manager');
$bundles_with_comment_fields = [];
$comment_field_map = $field_manager->getFieldMapByFieldType('comment');
foreach ($comment_field_map as $entity_type => $comment_fields) {
foreach ($comment_fields as $field_name => $field_info) {
foreach ($field_info['bundles'] as $field_bundle) {
$bundles_with_comment_fields[$field_bundle][] = $field_name;
}
}
}
foreach ($bundles_with_comment_fields as $bundle => $fields) {
if (!in_array($bundle, $node_values)) {
continue;
}
foreach ($fields as $field_name) {
$bundle_types[] = 'node--' . $bundle . '--' . $field_name;
}
}
\Drupal::configFactory()
->getEditable('comment_notify.settings')
->set('bundle_types', $bundle_types)
->save();
}
/**
* Update settings for generic entity support.
*/
function comment_notify_update_8003() {
$config = \Drupal::service('config.factory')
->getEditable('comment_notify.settings');
// Update the email settings.
$vars = [
'mail_templates.watcher.subject' => 'mail_templates.watcher.node.subject',
'mail_templates.watcher.body' => 'mail_templates.watcher.node.body',
'mail_templates.entity_author.subject' => 'mail_templates.entity_author.node.subject',
'mail_templates.entity_author.body' => 'mail_templates.entity_author.node.body',
];
// Update the settings.
foreach ($vars as $old => $new) {
$value = $config->get($old);
$config->set($new, $value);
$config->clear($old);
}
$config->save();
}
This diff is collapsed.
node_types:
- article
bundle_types:
- node--article--comment
available_alerts:
1: true
2: true
......@@ -8,46 +8,48 @@ enable_default:
entity_author: false
mail_templates:
watcher:
subject: '[site:name] :: new comment on [node:title]'
body: |
Hi [comment-subscribed:author],
node:
subject: '[site:name] :: new comment on [node:title]'
body: |
Hi [comment-subscribed:author],
[comment:author] has commented on: "[node:title]"
[comment:author] has commented on: "[node:title]"
----
[comment:title]
[comment:body]
----
----
[comment:title]
[comment:body]
----
You can view the comment at the following url
[comment:url]
You can view the comment at the following url
[comment:url]
You can stop receiving emails when someone replies to this post,
by going to [comment-subscribed:unsubscribe-url]
You can stop receiving emails when someone replies to this post,
by going to [comment-subscribed:unsubscribe-url]
You can set up auto-following feature for all future posts
by creating your own user with a few clicks here [site:login-url]
You can set up auto-following feature for all future posts
by creating your own user with a few clicks here [site:login-url]
-- [site:name] team
[site:url]
-- [site:name] team
[site:url]
entity_author:
subject: '[site:name] :: new comment for your post'
body: |
Hi [node:author],
node:
subject: '[site:name] :: new comment for your post'
body: |
Hi [node:author],
You have received a comment on: "[node:title]"
You have received a comment on: "[node:title]"
----
[comment:title]
[comment:body]
----
----
[comment:title]
[comment:body]
----
You can view the comment at the following url
[comment:url]
You can view the comment at the following url
[comment:url]
You will receive emails like this for all replies to your posts. You can
disable this by logging in and changing the settings on your user account at
[node:author:edit-url].
You will receive emails like this for all replies to your posts. You can
disable this by logging in and changing the settings on your user account at
[node:author:edit-url].
-- [site:name] team
[site:url]
-- [site:name] team
[site:url]
comment_notify.settings:
type: config_object
mapping:
node_types:
bundle_types:
type: sequence
label: Content types to enable for comment notification
label: Bundle types to enable for comment notification
sequence:
type: string
available_alerts:
type: mapping
label: Available subscription modes
mapping:
# COMMENT_NOTIFY_NODE
# COMMENT_NOTIFY_ENTITY
1:
type: boolean
# COMMENT_NOTIFY_COMMENT
......@@ -24,7 +24,7 @@ comment_notify.settings:
label: Default state for the notification selection box
entity_author:
type: boolean
label: Subscribe users to their node follow-up notification emails by default
label: Subscribe users to their entity follow-up notification emails by default
mail_templates:
type: mapping
label: Default mail text for sending out notifications
......@@ -33,19 +33,27 @@ comment_notify.settings:
type: mapping
label: Notifications to commenters
mapping:
subject:
type: label
label: Subject
body:
type: text
label: Body
node:
type: mapping
label: Default mail text for sending out notifications for nodes
mapping:
subject:
type: label
label: Subject
body:
type: text
label: Body
entity_author:
type: mapping
label: Notifications to owner of commented entity
mapping:
subject:
type: label
label: Subject
body:
type: text
label: Body
node:
type: mapping
label: Default mail text for sending out notifications for nodes
mapping:
subject:
type: label
label: Subject
body:
type: text
label: Body
This diff is collapsed.
......@@ -37,7 +37,7 @@ class CommentNotifyAnonymousTest extends CommentNotifyTestBase {
/** @var \Drupal\node\Entity\Node $node */
$node = $this->drupalCreateNode(['type' => 'article']);
$subscribe = ['notify' => TRUE, 'notify_type' => COMMENT_NOTIFY_NODE];
$subscribe = ['notify' => TRUE, 'notify_type' => COMMENT_NOTIFY_ENTITY];
$this->drupalGet($node->toUrl());
$this->postComment($node->toUrl()->toString(), $this->randomMachineName(), $this->randomMachineName(), $subscribe);
$this->assertTrue($this->getSession()->getPage()->hasContent(t('If you want to subscribe to comments you must supply a valid e-mail address.')));
......@@ -49,7 +49,7 @@ class CommentNotifyAnonymousTest extends CommentNotifyTestBase {
public function testAnonymousAllCommentsTest() {
/** @var \Drupal\node\Entity\Node $node */
$node = $this->drupalCreateNode(['type' => 'article']);
$subscribe = ['notify' => TRUE, 'notify_type' => COMMENT_NOTIFY_NODE];
$subscribe = ['notify' => TRUE, 'notify_type' => COMMENT_NOTIFY_ENTITY];
$contact = ['name' => $this->randomMachineName(), 'mail' => $this->getRandomEmailAddress()];
$comment = $this->postComment(
$node->toUrl()->toString(),
......@@ -68,7 +68,7 @@ class CommentNotifyAnonymousTest extends CommentNotifyTestBase {
$node->toUrl()->toString(),
$this->randomMachineName(),
$this->randomMachineName(),
['notify' => FALSE, 'notify_type' => COMMENT_NOTIFY_NODE],
['notify' => FALSE, 'notify_type' => COMMENT_NOTIFY_ENTITY],
['name' => $this->randomMachineName(), 'mail' => $this->getRandomEmailAddress()]
);
$this->assertMail('to', $contact['mail'], t('Message was sent to the anonymous user.'));
......@@ -87,7 +87,7 @@ class CommentNotifyAnonymousTest extends CommentNotifyTestBase {
$node->toUrl()->toString(),
$this->randomMachineName(),
$this->randomMachineName(),
['notify' => FALSE, 'notify_type' => COMMENT_NOTIFY_NODE],
['notify' => FALSE, 'notify_type' => COMMENT_NOTIFY_ENTITY],
['name' => $this->randomMachineName(), 'mail' => $this->getRandomEmailAddress()]
);
$captured_emails = $this->container->get('state')->get('system.test_mail_collector');
......@@ -113,7 +113,7 @@ class CommentNotifyAnonymousTest extends CommentNotifyTestBase {
$node->toUrl()->toString(),
$this->randomMachineName(),
$this->randomMachineName(),
['notify' => FALSE, 'notify_type' => COMMENT_NOTIFY_NODE],
['notify' => FALSE, 'notify_type' => COMMENT_NOTIFY_ENTITY],
['name' => $this->randomMachineName(), 'mail' => $this->getRandomEmailAddress()]
);
$captured_emails = $this->container->get('state')->get('system.test_mail_collector');
......@@ -124,7 +124,7 @@ class CommentNotifyAnonymousTest extends CommentNotifyTestBase {
"/comment/reply/node/{$node->id()}/comment/{$comment['id']}",
$this->randomMachineName(),
$this->randomMachineName(),
['notify' => FALSE, 'notify_type' => COMMENT_NOTIFY_NODE],
['notify' => FALSE, 'notify_type' => COMMENT_NOTIFY_ENTITY],
['name' => $this->randomMachineName(), 'mail' => $this->getRandomEmailAddress()]
);
$this->assertMail('to', $contact['mail'], t('Message was sent to the anonymous user.'));
......@@ -143,7 +143,7 @@ class CommentNotifyAnonymousTest extends CommentNotifyTestBase {
"/comment/reply/node/{$node->id()}/comment/{$comment['id']}",
$this->randomMachineName(),
$this->randomMachineName(),
['notify' => FALSE, 'notify_type' => COMMENT_NOTIFY_NODE],
['notify' => FALSE, 'notify_type' => COMMENT_NOTIFY_ENTITY],
['name' => $this->randomMachineName(), 'mail' => $this->getRandomEmailAddress()]
);
$captured_emails = $this->container->get('state')->get('system.test_mail_collector');
......
......@@ -2,6 +2,10 @@
namespace Drupal\Tests\comment_notify\Functional;
use Drupal\comment\CommentInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\field\Entity\FieldConfig;
/**
* Tests for the comment_notify module.
*
......@@ -21,11 +25,11 @@ class CommentNotifyConfigPageTest extends CommentNotifyTestBase {
$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->getSession()->getPage()->checkField('bundle_types[node--article--comment]');
$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]'));
$this->assertTrue($this->getSession()->getPage()->hasCheckedField('bundle_types[node--article--comment]'));
// Test that Available subscription modes are saved correctly.
$this->getSession()->getPage()->checkField('available_alerts[1]');
......@@ -99,17 +103,17 @@ class CommentNotifyConfigPageTest extends CommentNotifyTestBase {
$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->getSession()->getPage()->checkField('Subscribe users to their entity 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->assertTrue($this->getSession()->getPage()->hasCheckedField('Subscribe users to their entity follow-up notification emails by default'));
$this->getSession()->getPage()->uncheckField('Subscribe users to their node follow-up notification emails by default');
$this->getSession()->getPage()->uncheckField('Subscribe users to their entity 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->assertTrue($this->getSession()->getPage()->hasUncheckedField('Subscribe users to their entity follow-up notification emails by default'));
$this->getSession()->getPage()->fillField('Default mail text for sending out notifications to commenters', 'Hello');
$this->submitForm([], 'Save configuration');
......@@ -118,14 +122,69 @@ class CommentNotifyConfigPageTest extends CommentNotifyTestBase {
$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->getSession()->getPage()->fillField('Default mail text for sending out the notifications to entity 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');
$field = $this->getSession()->getPage()->findField('Default mail text for sending out the notifications to entity authors');
$this->assertTrue($field->getValue() == 'Hello');
$this->drupalLogout();
}
/**
* Tests the warning message when anonymous users have configuration problems.
*/
public function testsAnonymousProblemsAreReported() {
// Tests that the anonymous users have the permission to use comment notify
// but aren't allowed to leave posts.
user_role_grant_permissions(
AccountInterface::ANONYMOUS_ROLE,
[
'access comments',
'access content',
'subscribe to comments',
]
);
$this->drupalLogin($this->adminUser);
$this->drupalGet("admin/config/people/comment_notify");
// Test that a warning error is displayed when anonymous users have the
// permission to use comment notify but cannot post posts.
$this->assertSession()->responseContains('Anonymous commenters have the permission to subscribe to comments but they need to be allowed to:');
$this->assertSession()->responseContains('Post comments');
user_role_grant_permissions(
AccountInterface::ANONYMOUS_ROLE,
[
'post comments',
]
);
$this->drupalGet("admin/config/people/comment_notify");
$this->assertSession()->responseNotContains('Anonymous commenters have the permission to subscribe to comments but they need to be allowed to:');
$this->assertSession()->responseNotContains('Post comments');
// Tests that a warning error is displayed when anonymous users haven't
// permission to leave their contact information.
$comment_field = FieldConfig::loadByName('node', 'article', 'comment');
$comment_field->setSetting('anonymous', CommentInterface::ANONYMOUS_MAYNOT_CONTACT);
$comment_field->save();
$this->drupalGet("admin/config/people/comment_notify");
$this->assertSession()->responseContains('Anonymous commenters have the permission to subscribe to comments but they need to be allowed to:');
$this->assertSession()->responseContains('Leave their contact information on the following fields:');
$this->assertSession()->responseContains('node--article--comment');
// If the field_ui module is installed then the field with the problem must
// be a link.
$this->container->get('module_installer')->install(['field_ui'], TRUE);
$this->rebuildContainer();
$this->drupalGet("admin/config/people/comment_notify");
$this->assertSession()->responseContains('Anonymous commenters have the permission to subscribe to comments but they need to be allowed to:');
$this->assertSession()->responseContains('Leave their contact information on the following fields:');
$this->assertSession()->responseContains('node--article--comment');
$this->assertSession()->linkByHrefExists('/admin/structure/types/manage/article/fields/node.article.comment');
}
}
......@@ -2,8 +2,12 @@
namespace Drupal\Tests\comment_notify\Functional;
use Drupal\comment\CommentInterface;
use Drupal\comment\Entity\Comment;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\Tests\taxonomy\Functional\TaxonomyTestTrait;
/**
* Tests that all the notifications are sent as expected.
......@@ -12,6 +16,21 @@ use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
*/
class CommentNotifyNotificationsTest extends CommentNotifyTestBase {
use TaxonomyTestTrait;
/**
* Modules to enable.
*
* @var array
*/
public static $modules = [
'comment_notify',
'node',
'comment',
'token',
'taxonomy',
];
/**
* The permissions required by the tests.
*
......@@ -31,7 +50,7 @@ class CommentNotifyNotificationsTest extends CommentNotifyTestBase {
*/
public function testCommentNotification() {
$user1 = $this->drupalCreateUser($this->permissions);
comment_notify_set_user_notification_setting($user1->id(), COMMENT_NOTIFY_NODE, COMMENT_NOTIFY_COMMENT);
comment_notify_set_user_notification_setting($user1->id(), COMMENT_NOTIFY_ENTITY, COMMENT_NOTIFY_COMMENT);
$user2 = $this->drupalCreateUser($this->permissions);
$node = $this->drupalCreateNode(
[
......@@ -44,7 +63,7 @@ class CommentNotifyNotificationsTest extends CommentNotifyTestBase {
$node->toUrl()->toString(),
$this->randomMachineName(),
$this->randomMachineName(),
['notify' => TRUE, 'notify_type' => COMMENT_NOTIFY_NODE]
['notify' => TRUE, 'notify_type' => COMMENT_NOTIFY_ENTITY]
);
// Test that the notification was sent.
$this->assertMail('to', $user1->getEmail(), t('Message was sent to the user.'));
......@@ -66,6 +85,10 @@ class CommentNotifyNotificationsTest extends CommentNotifyTestBase {
public function testCommentTypeNotification() {
// Add a second comment type.
$this->addDefaultCommentField('node', 'article', 'field_comment', CommentItemInterface::OPEN, 'comment_type_2');
/** @var \Drupal\Core\Config\Config $config */
$config = $this->container->get('config.factory')->getEditable('comment_notify.settings');
$config->set('bundle_types', ['node--article--comment', 'node--article--field_comment']);
$config->save();
$user1 = $this->drupalCreateUser($this->permissions);
$user2 = $this->drupalCreateUser($this->permissions);
......@@ -144,4 +167,90 @@ class CommentNotifyNotificationsTest extends CommentNotifyTestBase {
}
/**
* Tests that the notifications are working on a different entity than a node.
*/
public function testEntityNotification() {
/** @var \Drupal\taxonomy\Entity\Vocabulary $vocabulary */
$vocabulary = $this->createVocabulary();
$this->addDefaultCommentField('taxonomy_term', $vocabulary->id(), 'field_comment_taxonomy', CommentItemInterface::OPEN, 'comment_type_2');
$comment_field = FieldConfig::loadByName('taxonomy_term', $vocabulary->id(), 'field_comment_taxonomy');
$comment_field->setSetting('anonymous', CommentInterface::ANONYMOUS_MAY_CONTACT);
$comment_field->save();
/** @var \Drupal\Core\Config\Config $config */
$config = $this->container->get('config.factory')->getEditable('comment_notify.settings');
$config->set('bundle_types', ['taxonomy_term--' . $vocabulary->id() . '--field_comment_taxonomy']);
$config->save();
// Allow anonymous users to post comments and get notifications.
user_role_grant_permissions(
AccountInterface::ANONYMOUS_ROLE,
[
'access comments',
'access content',
'post comments',
'skip comment approval',
'subscribe to comments',
]
);
$permissions = array_merge($this->permissions, ['access user profiles']);
$user1 = $this->drupalCreateUser($permissions);
comment_notify_set_user_notification_setting($user1->id(), COMMENT_NOTIFY_DISABLED, COMMENT_NOTIFY_ENTITY);
$user2 = $this->drupalCreateUser($permissions);
comment_notify_set_user_notification_setting($user2->id(), COMMENT_NOTIFY_DISABLED, COMMENT_NOTIFY_COMMENT);
$term = $this->createTerm($vocabulary);
$term2 = $this->createTerm($vocabulary);
// User1 Should get notification for any new comment on the entity.
$this->drupalLogin($user1);
$this->postComment(
$term->toUrl()->toString(),
$this->randomMachineName(),
$this->randomMachineName(),
['notify' => TRUE, 'notify_type' => COMMENT_NOTIFY_ENTITY]
);
$this->drupalLogout();
$this->postComment(
$term->toUrl()->toString(),
$this->randomMachineName(),
$this->randomMachineName(),
['notify' => FALSE, 'notify_type' => COMMENT_NOTIFY_ENTITY],
['name' => $this->randomMachineName(), 'mail' => $this->getRandomEmailAddress()]
);
$this->assertMail('to', $user1->getEmail(), t('Message was sent to the user1 user.'));
$this->container->get('state')->set('system.test_mail_collector', []);
// User 2 should get a notification only when someone reply to its comment.
$this->drupalLogin($user2);
$comment = $this->postComment(
$term2->toUrl()->toString(),
$this->randomMachineName(),
$this->randomMachineName(),
['notify' => TRUE, 'notify_type' => COMMENT_NOTIFY_COMMENT]
);
$this->drupalLogout();
$this->postComment(
$term2->toUrl()->toString(),
$this->randomMachineName(),
$this->randomMachineName(),
['notify' => FALSE, 'notify_type' => COMMENT_NOTIFY_ENTITY],
['name' => $this->randomMachineName(), 'mail' => $this->getRandomEmailAddress()]
);
$this->assertEmpty($this->getMails(), 'No notification was sent');
$this->drupalGet('/comment/reply/taxonomy_term/' . $term2->id() . '/field_comment_taxonomy/' . $comment['id']);
// /comment/reply/taxonomy_term/1/field_comment_taxonomy/2.
$this->postComment(
'/comment/reply/taxonomy_term/' . $term2->id() . '/field_comment_taxonomy/' . $comment['id'],
$this->randomMachineName(),
$this->randomMachineName(),
['notify' => FALSE, 'notify_type' => COMMENT_NOTIFY_ENTITY],
['name' => $this->randomMachineName(), 'mail' => $this->getRandomEmailAddress()]
);
$this->assertMail('to', $user2->getEmail(), t('Message was sent to the user1 user.'));
$this->container->get('state')->set('system.test_mail_collector', []);
}
}
......@@ -148,7 +148,7 @@ class CommentNotifyUserPreferencesTest extends CommentNotifyTestBase {
// Test the "All comments" option.
$this->drupalGet($this->authenticatedUser->toUrl('edit-form')->toString());
$this->getSession()->getPage()->selectFieldOption('comment_notify', COMMENT_NOTIFY_NODE);
$this->getSession()->getPage()->selectFieldOption('comment_notify', COMMENT_NOTIFY_ENTITY);
$this->getSession()->getPage()->pressButton(t('Save'));
$this->drupalGet($node->toUrl()->toString());
$this->assertTrue($this->getSession()->getPage()->hascheckedField('Notify me when new comments are posted'));
......@@ -187,8 +187,8 @@ class CommentNotifyUserPreferencesTest extends CommentNotifyTestBase {
$this->assertTrue($this->getSession()->getPage()->hasContent(t('Receive content follow-up notification e-mails')));
$this->getSession()->getPage()->checkField('Receive content follow-up notification e-mails');
$this->getSession()->getPage()->pressButton(t('Save'));
$node_notify_preference = comment_notify_get_user_node_notify_preference($this->authenticatedUser->id());
$this->assertEquals(COMMENT_NOTIFY_NODE, $node_notify_preference);
$node_notify_preference = comment_notify_get_user_entity_notify_preference($this->authenticatedUser->id());
$this->assertEquals(COMMENT_NOTIFY_ENTITY, $node_notify_preference);
$this->drupalLogout();
// Tests that the notification is sent when the content created by the user
......@@ -214,7 +214,7 @@ class CommentNotifyUserPreferencesTest extends CommentNotifyTestBase {