public function MassContactFormTest::testNormalAccess in Mass Contact 8
Tests basic form operation on an unprivileged user.
File
- tests/
src/ Functional/ Form/ MassContactFormTest.php, line 84
Class
- MassContactFormTest
- Tests for the Mass Contact form.
Namespace
Drupal\Tests\mass_contact\Functional\FormCode
public function testNormalAccess() {
$this
->drupalLogin($this->massContactUser);
// Ensure page loads successfully.
$this
->drupalGet(Url::fromRoute('entity.mass_contact_message.add_form'));
$this
->assertSession()
->statusCodeEquals(200);
// Test with queue system.
$this
->config('mass_contact.settings')
->set('send_with_cron', TRUE)
->save();
// Grant permission to one category only.
$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());
// Update some options.
$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');
// Do not respect opt-outs.
$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');
// Set category 2 to send to all authenticated users.
$recipients = [
'role' => [
'conjunction' => 'AND',
'categories' => [
$this->recipientRole
->id(),
],
],
];
$this->categories[2]
->setRecipients($recipients);
$this->categories[2]
->save();
// Send a message to category 2.
$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);
/** @var \Drupal\Core\Queue\QueueWorkerManagerInterface $manager */
/** @var \Drupal\Core\Queue\QueueWorkerInterface $message_queue_queue_worker */
/** @var \Drupal\Core\Queue\QueueWorkerInterface $send_message_queue_worker */
$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');
// Should be one item in the Queue messages queue.
$this
->verifyAndProcessQueueMessagesQueue($message_queue_queue_worker, 1);
// There should now be 9 items in the sending queue and 409 emails
// (409 non-blocked users with the recipient role).
// @see \Drupal\mass_contact\MassContact::MAX__QUEUE_RECIPIENTS
$this
->verifyAndProcessSendMessageQueue($send_message_queue_worker, 9, 409);
// Switch back to BCC mode and only 3 emails should be sent.
\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'));
// Send a message to category 2.
$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'));
// Should be one item in the Queue messages queue.
$this
->verifyAndProcessQueueMessagesQueue($message_queue_queue_worker, 1);
// There should now be 9 items in the sending queue and 9 emails
// (since BCC is used).
// @see \Drupal\mass_contact\MassContact::MAX__QUEUE_RECIPIENTS
$this
->verifyAndProcessSendMessageQueue($send_message_queue_worker, 9, 9);
// Verify message prefix/suffix are properly attached.
$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');
// Test opt out feature.
\Drupal::state()
->set('system.test_mail_collector', []);
$config
->set('optout_enabled', MassContactInterface::OPT_OUT_GLOBAL);
$config
->set('use_bcc', FALSE);
$config
->save();
// Get form again.
$this
->drupalGet(Url::fromRoute('entity.mass_contact_message.add_form'));
$this
->assertSession()
->statusCodeEquals(200);
// Send a message to category 2 with BCC disabled.
$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);
// Should be one item in the Queue messages queue.
$this
->verifyAndProcessQueueMessagesQueue($message_queue_queue_worker, 1);
// There should now be 9 items in the sending queue and 407 emails
// (since BCC is not used and 2 users have opted out).
// @see \Drupal\mass_contact\MassContact::MAX__QUEUE_RECIPIENTS
$this
->verifyAndProcessSendMessageQueue($send_message_queue_worker, 9, 407);
// Test send me a copy feature.
\Drupal::state()
->set('system.test_mail_collector', []);
// Use BCC for this test.
$config
->set('use_bcc', TRUE);
$config
->save();
// Get form again.
$this
->drupalGet(Url::fromRoute('entity.mass_contact_message.add_form'));
$this
->assertSession()
->statusCodeEquals(200);
// Test Send a message without any categories with 'Send me a copy'
// unchecked. Mail should not be sent since there are no recipients.
$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.');
// Test Sending a message without any categories with
// 'Send me a copy checked'. Mail should be sent since there is one
// recipient.
$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);
// Should be one item in the Queue messages queue.
$this
->verifyAndProcessQueueMessagesQueue($message_queue_queue_worker, 1);
// There should now be only 1 item in the sending queue for the current
// user and 1 email sent.
$this
->verifyAndProcessSendMessageQueue($send_message_queue_worker, 1, 1);
// Test sending a message to category 2 and also a copy to yourself with
// BCC option as false.
$config
->set('use_bcc', FALSE);
// Use the checkboxes to display the categories.
$config
->set('category_display', 'checkboxes');
$config
->save();
// Get form again.
$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);
// Should be one item in the Queue messages queue.
$this
->verifyAndProcessQueueMessagesQueue($message_queue_queue_worker, 1);
// There should now be 9 items in the sending queue for the current
// user and should be 408 emails (407 non-blocked users and non opted out
// users with the recipient role and 1 current user for copy).
$this
->verifyAndProcessSendMessageQueue($send_message_queue_worker, 9, 408);
// Test cancelling the message confirm form.
// No messages should be sent if the cancel button is pressed on the
// confirmation form.
\Drupal::state()
->set('system.test_mail_collector', []);
$config
->set('category_display', 'select');
$config
->save();
// Get form again.
$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);
// @todo Test with batch system.
// @see https://www.drupal.org/node/2855942
$this
->config('mass_contact.settings')
->set('send_with_cron', FALSE)
->save();
\Drupal::state()
->set('system.test_mail_collector', []);
}