View source
<?php
namespace Drupal\Tests\mass_contact\Functional\Form;
use Drupal\Core\Queue\QueueWorkerInterface;
use Drupal\Core\Test\AssertMailTrait;
use Drupal\Core\Url;
use Drupal\Tests\mass_contact\Functional\MassContactTestBase;
use Drupal\user\Entity\Role;
use Drupal\mass_contact\MassContactInterface;
class MassContactFormTest extends MassContactTestBase {
use AssertMailTrait;
protected $massContactUser;
protected $massContactRole;
protected $recipientRole;
protected $categories;
protected function setUp() {
parent::setUp();
$this->massContactUser = $this
->createUser([
'mass contact send messages',
'mass contact view archived messages',
]);
$role_id = $this->massContactUser
->getRoles(TRUE);
$this->massContactRole = Role::load(reset($role_id));
foreach (range(1, 6) as $i) {
$this->categories[$i] = $this
->createCategory();
}
$this->recipientRole = Role::load($this
->createRole([]));
foreach (range(1, 410) as $i) {
$account = $this
->createUser();
if ($i == 5) {
$account
->block();
}
if ($i == 20 || $i == 50) {
$account->mass_contact_opt_out->value = 1;
}
$account
->addRole($this->recipientRole
->id());
$account
->save();
}
}
public function testNormalAccess() {
$this
->drupalLogin($this->massContactUser);
$this
->drupalGet(Url::fromRoute('entity.mass_contact_message.add_form'));
$this
->assertSession()
->statusCodeEquals(200);
$this
->config('mass_contact.settings')
->set('send_with_cron', TRUE)
->save();
$this->massContactRole
->grantPermission('mass contact send to users in the ' . $this->categories[2]
->id() . ' category')
->save();
$this
->drupalGet(Url::fromRoute('entity.mass_contact_message.add_form'));
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains('A copy of this message will be archived on this website.');
$this
->assertSession()
->pageTextContains('Recipients will be hidden from each other.');
$this
->assertSession()
->fieldExists('sender_mail');
$this
->assertSession()
->fieldValueEquals('sender_mail', $this->massContactUser
->getEmail());
$this
->assertSession()
->fieldExists('sender_name');
$this
->assertSession()
->fieldValueEquals('sender_name', $this->massContactUser
->getDisplayName());
$config = $this
->config('mass_contact.settings');
$config
->set('use_bcc', FALSE);
$config
->set('create_archive_copy', FALSE);
$config
->set('default_sender_email', 'foo@bar.com');
$config
->set('default_sender_name', 'Foo Bar');
$config
->set('optout_enabled', MassContactInterface::OPT_OUT_DISABLED);
$config
->set('message_prefix', [
'value' => $this
->randomString(),
'format' => filter_default_format(),
]);
$config
->set('message_suffix', [
'value' => $this
->randomString(),
'format' => filter_default_format(),
]);
$config
->save();
$this->massContactRole
->grantPermission('mass contact send to users in the ' . $this->categories[3]
->id() . ' category')
->save();
$this
->drupalGet(Url::fromRoute('entity.mass_contact_message.add_form'));
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains('Recipients will NOT be hidden from each other.');
$this
->assertSession()
->pageTextContains(' A copy of this message will NOT be archived on this website.');
$this
->assertSession()
->fieldNotExists('sender_mail');
$this
->assertSession()
->fieldNotExists('sender_name');
$recipients = [
'role' => [
'conjunction' => 'AND',
'categories' => [
$this->recipientRole
->id(),
],
],
];
$this->categories[2]
->setRecipients($recipients);
$this->categories[2]
->save();
$edit = [
'subject' => $this
->randomString(),
'body[value]' => $this
->randomString(),
'categories[]' => [
$this->categories[2]
->id(),
],
];
$this
->drupalPostForm(NULL, $edit, t('Send email'));
$message_configs['optout'] = FALSE;
$message_configs['copy'] = FALSE;
$message_configs['bcc'] = FALSE;
$message_configs['create_archive_copy'] = FALSE;
$message_configs['user_count'] = 409;
$this
->verifyConfirmFormAndConfirmSendEmail($message_configs);
$manager = $this->container
->get('plugin.manager.queue_worker');
$message_queue_queue_worker = $manager
->createInstance('mass_contact_queue_messages');
$send_message_queue_worker = $manager
->createInstance('mass_contact_send_message');
$this
->verifyAndProcessQueueMessagesQueue($message_queue_queue_worker, 1);
$this
->verifyAndProcessSendMessageQueue($send_message_queue_worker, 9, 409);
\Drupal::state()
->set('system.test_mail_collector', []);
$config
->set('create_archive_copy', TRUE);
$config
->set('use_bcc', TRUE);
$config
->save();
$this
->drupalGet(Url::fromRoute('entity.mass_contact_message.add_form'));
$edit = [
'subject' => $this
->randomString(),
'body[value]' => $this
->randomString(),
'categories[]' => [
$this->categories[2]
->id(),
],
];
$this
->drupalPostForm(NULL, $edit, t('Send email'));
$message_configs['optout'] = FALSE;
$message_configs['copy'] = FALSE;
$message_configs['bcc'] = TRUE;
$message_configs['create_archive_copy'] = TRUE;
$message_configs['user_count'] = 409;
$this
->verifyConfirmFormAndConfirmSendEmail($message_configs);
$this
->assertSession()
->pageTextContains(t('A copy has been archived'));
$this
->clickLink('here');
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet(Url::fromRoute('entity.mass_contact_message.add_form'));
$this
->verifyAndProcessQueueMessagesQueue($message_queue_queue_worker, 1);
$this
->verifyAndProcessSendMessageQueue($send_message_queue_worker, 9, 9);
$expected = implode("\n\n", [
$config
->get('message_prefix.value'),
$edit['body[value]'],
$config
->get('message_suffix.value'),
]) . "\n\n";
$this
->assertMail('body', $expected);
$this
->assertMail('to', 'foo@bar.com');
\Drupal::state()
->set('system.test_mail_collector', []);
$config
->set('optout_enabled', MassContactInterface::OPT_OUT_GLOBAL);
$config
->set('use_bcc', FALSE);
$config
->save();
$this
->drupalGet(Url::fromRoute('entity.mass_contact_message.add_form'));
$this
->assertSession()
->statusCodeEquals(200);
$edit = [
'subject' => $this
->randomString(),
'body[value]' => $this
->randomString(),
'categories[]' => [
$this->categories[2]
->id(),
],
];
$this
->drupalPostForm(NULL, $edit, t('Send email'));
$message_configs['optout'] = TRUE;
$message_configs['copy'] = FALSE;
$message_configs['bcc'] = FALSE;
$message_configs['create_archive_copy'] = TRUE;
$message_configs['user_count'] = 407;
$this
->verifyConfirmFormAndConfirmSendEmail($message_configs);
$this
->verifyAndProcessQueueMessagesQueue($message_queue_queue_worker, 1);
$this
->verifyAndProcessSendMessageQueue($send_message_queue_worker, 9, 407);
\Drupal::state()
->set('system.test_mail_collector', []);
$config
->set('use_bcc', TRUE);
$config
->save();
$this
->drupalGet(Url::fromRoute('entity.mass_contact_message.add_form'));
$this
->assertSession()
->statusCodeEquals(200);
$edit = [
'subject' => $this
->randomString(),
'body[value]' => $this
->randomString(),
];
$this
->drupalPostForm(NULL, $edit, t('Send email'));
$this
->assertSession()
->pageTextContains('There are no recipients chosen for this mass contact message.');
$edit = [
'subject' => $this
->randomString(),
'body[value]' => $this
->randomString(),
'copy' => TRUE,
];
$this
->drupalPostForm(NULL, $edit, t('Send email'));
$message_configs['optout'] = TRUE;
$message_configs['copy'] = TRUE;
$message_configs['bcc'] = TRUE;
$message_configs['create_archive_copy'] = TRUE;
$message_configs['user_count'] = 1;
$this
->verifyConfirmFormAndConfirmSendEmail($message_configs);
$this
->verifyAndProcessQueueMessagesQueue($message_queue_queue_worker, 1);
$this
->verifyAndProcessSendMessageQueue($send_message_queue_worker, 1, 1);
$config
->set('use_bcc', FALSE);
$config
->set('category_display', 'checkboxes');
$config
->save();
$this
->drupalGet(Url::fromRoute('entity.mass_contact_message.add_form'));
\Drupal::state()
->set('system.test_mail_collector', []);
$edit = [
'subject' => $this
->randomString(),
'body[value]' => $this
->randomString(),
'categories[' . $this->categories[2]
->id() . ']' => TRUE,
'categories[' . $this->categories[3]
->id() . ']' => FALSE,
'copy' => TRUE,
];
$this
->drupalPostForm(NULL, $edit, t('Send email'));
$message_configs['optout'] = TRUE;
$message_configs['copy'] = TRUE;
$message_configs['bcc'] = FALSE;
$message_configs['create_archive_copy'] = TRUE;
$message_configs['user_count'] = 408;
$this
->verifyConfirmFormAndConfirmSendEmail($message_configs);
$this
->verifyAndProcessQueueMessagesQueue($message_queue_queue_worker, 1);
$this
->verifyAndProcessSendMessageQueue($send_message_queue_worker, 9, 408);
\Drupal::state()
->set('system.test_mail_collector', []);
$config
->set('category_display', 'select');
$config
->save();
$this
->drupalGet(Url::fromRoute('entity.mass_contact_message.add_form'));
$edit = [
'subject' => $this
->randomString(),
'body[value]' => $this
->randomString(),
'categories[]' => [
$this->categories[2]
->id(),
],
'copy' => TRUE,
];
$this
->drupalPostForm(NULL, $edit, t('Send email'));
$message_configs['optout'] = TRUE;
$message_configs['copy'] = TRUE;
$message_configs['bcc'] = FALSE;
$message_configs['create_archive_copy'] = TRUE;
$message_configs['user_count'] = 408;
$message_configs['cancel'] = TRUE;
$this
->verifyConfirmFormAndConfirmSendEmail($message_configs);
$this
->verifyAndProcessQueueMessagesQueue($message_queue_queue_worker, 0);
$this
->config('mass_contact.settings')
->set('send_with_cron', FALSE)
->save();
\Drupal::state()
->set('system.test_mail_collector', []);
}
protected function verifyAndProcessQueueMessagesQueue(QueueWorkerInterface $queue_worker, $expected_queue_items) {
$queue = \Drupal::queue('mass_contact_queue_messages');
$this
->assertEquals($expected_queue_items, $queue
->numberOfItems());
while ($item = $queue
->claimItem()) {
$queue_worker
->processItem($item->data);
$queue
->deleteItem($item);
}
}
protected function verifyAndProcessSendMessageQueue(QueueWorkerInterface $queue_worker, $expected_queue_items, $expected_mails) {
$queue = \Drupal::queue('mass_contact_send_message');
$this
->assertEquals($expected_queue_items, $queue
->numberOfItems());
while ($item = $queue
->claimItem()) {
$queue_worker
->processItem($item->data);
$queue
->deleteItem($item);
}
$emails = $this
->getMails();
$this
->assertEquals($expected_mails, count($emails));
}
protected function verifyConfirmFormAndConfirmSendEmail(array $message_configs) {
$this
->assertSession()
->pageTextContains('Are you sure you want to send this message to ' . $message_configs['user_count'] . ' user(s)?');
if ($this->massContactUser
->hasPermission('mass contact administer')) {
if ($message_configs['optout']) {
$this
->assertSession()
->pageTextContains('You have selected to respect user opt-outs. If a user has opted out of emails they will not receive this mass contact message.');
}
else {
$this
->assertSession()
->pageTextContains('You have selected to NOT respect user opt-outs. Emails will be sent to all users even if they have elected not to receive a mass contact message.');
}
}
if ($this->massContactUser
->hasPermission('mass contact override bcc')) {
if ($message_configs['bcc']) {
$this
->assertSession()
->pageTextContains('Recipients of this message will be HIDDEN on the email');
}
else {
$this
->assertSession()
->pageTextContains('Recipients of this message will NOT be HIDDEN on the email');
}
}
if ($message_configs['copy']) {
$this
->assertSession()
->pageTextContains('A copy of this message will be sent to you.');
}
else {
$this
->assertSession()
->pageTextContains('A copy of this message will NOT be sent to you.');
}
if ($this->massContactUser
->hasPermission('mass contact override archiving')) {
if ($message_configs['create_archive_copy']) {
$this
->assertSession()
->pageTextContains('A copy of this message will be archived on this site.');
}
else {
$this
->assertSession()
->pageTextContains('A copy of this message will NOT be archived on this site.');
}
}
if (empty($message_configs['cancel'])) {
$this
->drupalPostForm(NULL, [], t('Confirm'));
$this
->assertSession()
->pageTextContains('Mass Contact message sent successfully.');
}
else {
$this
->clickLink('Cancel');
}
}
}