View source
<?php
namespace Drupal\Tests\comment_notify\Functional;
use Drupal\Core\Session\AccountInterface;
class CommentNotifyAnonymousTest extends CommentNotifyTestBase {
protected function setUp() {
parent::setUp();
user_role_grant_permissions(AccountInterface::ANONYMOUS_ROLE, [
'access comments',
'access content',
'post comments',
'skip comment approval',
'subscribe to comments',
]);
}
public function testMail() {
$node = $this
->drupalCreateNode([
'type' => 'article',
]);
$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.')));
}
public function testAnonymousAllCommentsTest() {
$node = $this
->drupalCreateNode([
'type' => 'article',
]);
$subscribe = [
'notify' => TRUE,
'notify_type' => COMMENT_NOTIFY_ENTITY,
];
$contact = [
'name' => $this
->randomMachineName(),
'mail' => $this
->getRandomEmailAddress(),
];
$comment = $this
->postComment($node
->toUrl()
->toString(), $this
->randomMachineName(), $this
->randomMachineName(), $subscribe, $contact);
$result = comment_notify_get_notification_type($comment['id']);
$this
->assertEquals($result, $subscribe['notify_type'], 'All Comments option was saved properly.');
$this
->postComment($node
->toUrl()
->toString(), $this
->randomMachineName(), $this
->randomMachineName(), [
'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.'));
$mails = $this
->getMails();
preg_match("/\\/comment_notify\\/disable\\/.+/", $mails[0]['body'], $output);
$this
->drupalGet($output[0]);
$this
->assertTrue($this
->getSession()
->getPage()
->hasContent("Your comment follow-up notification for this post was disabled. Thanks."));
$result = comment_notify_get_notification_type($comment['id']);
$this
->assertEquals($result, COMMENT_NOTIFY_DISABLED, 'The notification has been disabled');
$this->container
->get('state')
->set('system.test_mail_collector', []);
$this
->postComment($node
->toUrl()
->toString(), $this
->randomMachineName(), $this
->randomMachineName(), [
'notify' => FALSE,
'notify_type' => COMMENT_NOTIFY_ENTITY,
], [
'name' => $this
->randomMachineName(),
'mail' => $this
->getRandomEmailAddress(),
]);
$captured_emails = $this->container
->get('state')
->get('system.test_mail_collector');
$this
->assertEmpty($captured_emails, 'No notifications has been sent.');
}
public function testAnonymousRepliesTest() {
$node = $this
->drupalCreateNode([
'type' => 'article',
]);
$subscribe = [
'notify' => TRUE,
'notify_type' => COMMENT_NOTIFY_COMMENT,
];
$contact = [
'name' => $this
->randomMachineName(),
'mail' => $this
->getRandomEmailAddress(),
];
$comment = $this
->postComment($node
->toUrl()
->toString(), $this
->randomMachineName(), $this
->randomMachineName(), $subscribe, $contact);
$result = comment_notify_get_notification_type($comment['id']);
$this
->assertEquals($result, $subscribe['notify_type'], 'Replies to my comment option was saved properly.');
$this
->postComment($node
->toUrl()
->toString(), $this
->randomMachineName(), $this
->randomMachineName(), [
'notify' => FALSE,
'notify_type' => COMMENT_NOTIFY_ENTITY,
], [
'name' => $this
->randomMachineName(),
'mail' => $this
->getRandomEmailAddress(),
]);
$captured_emails = $this->container
->get('state')
->get('system.test_mail_collector');
$this
->assertEmpty($captured_emails, 'No notifications has been sent.');
$this
->postComment("/comment/reply/node/{$node->id()}/comment/{$comment['id']}", $this
->randomMachineName(), $this
->randomMachineName(), [
'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.'));
$mails = $this
->getMails();
preg_match("/\\/comment_notify\\/disable\\/.+/", $mails[0]['body'], $output);
$this
->drupalGet($output[0]);
$this
->assertTrue($this
->getSession()
->getPage()
->hasContent("Your comment follow-up notification for this post was disabled. Thanks."));
$result = comment_notify_get_notification_type($comment['id']);
$this
->assertEquals($result, COMMENT_NOTIFY_DISABLED, 'The notification has been disabled');
$this->container
->get('state')
->set('system.test_mail_collector', []);
$this
->postComment("/comment/reply/node/{$node->id()}/comment/{$comment['id']}", $this
->randomMachineName(), $this
->randomMachineName(), [
'notify' => FALSE,
'notify_type' => COMMENT_NOTIFY_ENTITY,
], [
'name' => $this
->randomMachineName(),
'mail' => $this
->getRandomEmailAddress(),
]);
$captured_emails = $this->container
->get('state')
->get('system.test_mail_collector');
$this
->assertEmpty($captured_emails, 'No notifications has been sent.');
}
}