View source
<?php
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\Traits\TaxonomyTestTrait;
class CommentNotifyNotificationsTest extends CommentNotifyTestBase {
use TaxonomyTestTrait;
public static $modules = [
'comment_notify',
'node',
'comment',
'token',
'taxonomy',
];
protected $permissions = [
'access comments',
'access content',
'edit own comments',
'post comments',
'skip comment approval',
'subscribe to comments',
];
public function testCommentNotification() {
$user1 = $this
->drupalCreateUser($this->permissions);
$this->container
->get('comment_notify.user_settings')
->saveSettings($user1
->id(), COMMENT_NOTIFY_ENTITY, COMMENT_NOTIFY_COMMENT);
$user2 = $this
->drupalCreateUser($this->permissions);
$node = $this
->drupalCreateNode([
'type' => 'article',
'uid' => $user1,
]);
$this
->drupalLogin($user2);
$comment = $this
->postComment($node
->toUrl()
->toString(), $this
->randomMachineName(), $this
->randomMachineName(), [
'notify' => TRUE,
'notify_type' => COMMENT_NOTIFY_ENTITY,
]);
$this
->assertMail('to', $user1
->getEmail(), t('Message was sent to the user.'));
$this->container
->get('state')
->set('system.test_mail_collector', []);
$this
->drupalGet('comment/' . $comment['id'] . '/edit');
$this
->getSession()
->getPage()
->fillField(t('Comment'), $this
->randomMachineName());
$this
->getSession()
->getPage()
->pressButton(t('Save'));
$captured_emails = $this->container
->get('state')
->get('system.test_mail_collector');
$this
->assertEmpty($captured_emails, 'No notifications has been sent.');
$this
->drupalLogout();
}
public function testCommentTypeNotification() {
$this
->addDefaultCommentField('node', 'article', 'field_comment', CommentItemInterface::OPEN, 'comment_type_2');
$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);
$node = $this
->drupalCreateNode([
'type' => 'article',
'uid' => $this->adminUser,
]);
$comment1 = Comment::create([
'comment_type' => 'comment',
'langcode' => 'und',
'entity_id' => $node
->id(),
'entity_type' => $node
->getEntityTypeId(),
'uid' => $user1
->id(),
'subject' => $this
->randomMachineName(),
'status' => 1,
'field_name' => 'comment',
'comment_body' => [
'summary' => '',
'value' => $this
->randomMachineName(),
'format' => 'basic_html',
],
]);
$comment1
->save($comment1);
$notify_hash = \Drupal::csrfToken()
->get('127.0.0.1' . $comment1
->id());
comment_notify_add_notification($comment1
->id(), TRUE, $notify_hash, 1);
$comment2 = Comment::create([
'comment_type' => 'comment_type_2',
'langcode' => 'und',
'entity_id' => $node
->id(),
'entity_type' => $node
->getEntityTypeId(),
'uid' => $user2
->id(),
'subject' => $this
->randomMachineName(),
'status' => 1,
'field_name' => 'field_comment',
'comment_body' => [
'summary' => '',
'value' => $this
->randomMachineName(),
'format' => 'basic_html',
],
]);
$comment2
->save($comment2);
$notify_hash = \Drupal::csrfToken()
->get('127.0.0.1' . $comment1
->id());
comment_notify_add_notification($comment2
->id(), TRUE, $notify_hash, 1);
$this
->assertEmpty($this
->getMails(), 'No notifications has been sent.');
$this
->drupalLogin($user1);
$this
->postComment("/comment/reply/node/{$node->id()}/field_comment/{$comment2->id()}", $this
->randomMachineName(), $this
->randomMachineName(), [
'notify' => FALSE,
'notify_type' => COMMENT_NOTIFY_COMMENT,
]);
$this
->drupalLogout();
$this
->assertMail('to', $user2
->getEmail(), t('Message was sent to the user2 user.'));
$this->container
->get('state')
->set('system.test_mail_collector', []);
$this
->drupalLogin($user2);
$this
->postComment("/comment/reply/node/{$node->id()}/comment/{$comment1->id()}", $this
->randomMachineName(), $this
->randomMachineName(), [
'notify' => FALSE,
'notify_type' => COMMENT_NOTIFY_COMMENT,
]);
$this
->drupalLogout();
$this
->assertMail('to', $user1
->getEmail(), t('Message was sent to the user1 user.'));
$this->container
->get('state')
->set('system.test_mail_collector', []);
}
public function testEntityNotification() {
$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();
$config = $this->container
->get('config.factory')
->getEditable('comment_notify.settings');
$config
->set('bundle_types', [
'taxonomy_term--' . $vocabulary
->id() . '--field_comment_taxonomy',
]);
$config
->save();
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);
$this->container
->get('comment_notify.user_settings')
->saveSettings($user1
->id(), COMMENT_NOTIFY_DISABLED, COMMENT_NOTIFY_ENTITY);
$user2 = $this
->drupalCreateUser($permissions);
$this->container
->get('comment_notify.user_settings')
->saveSettings($user2
->id(), COMMENT_NOTIFY_DISABLED, COMMENT_NOTIFY_COMMENT);
$term = $this
->createTerm($vocabulary);
$term2 = $this
->createTerm($vocabulary);
$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', []);
$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']);
$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', []);
}
}