function CourierTest::testCourier in Courier 8
Same name and namespace in other branches
- 2.x src/Tests/CourierTest.php \Drupal\courier\Tests\CourierTest::testCourier()
General API test.
Creates a template collection and checks if user receives a message.
File
- src/
Tests/ CourierTest.php, line 71 - Contains \Drupal\courier\Tests\CourierTest.
Class
- CourierTest
- Courier test.
Namespace
Drupal\courier\TestsCode
function testCourier() {
// Owner is auto saved by entity reference field.
$owner_entity = EntityTest::create();
$template_collection = TemplateCollection::create()
->setOwner($owner_entity);
$this->courierManager
->addTemplates($template_collection);
// Saving collection should auto save templates (via entity_reference).
// See DynamicEntityReferenceItem::preSave().
$templates = $template_collection
->getTemplates();
$this
->assertTrue($templates[0]
->isNew(), 'Template is not saved.');
$this
->assertEqual($template_collection
->save(), SAVED_NEW, 'Saved template collection');
$templates = $template_collection
->getTemplates();
/** @var \Drupal\courier\Entity\Email $courier_email */
$courier_email = $templates[0];
$this
->assertFalse($courier_email
->isNew(), 'Template is saved.');
$this
->assertTrue($courier_email instanceof Email, 'Template 0 is a courier_email.');
// Message will not be added to queue if ->isEmpty()
$courier_email
->setSubject($this
->randomMachineName());
$courier_email
->setBody('Greetings, [identity:label]');
$courier_email
->save();
// MQI.
$this
->assertEqual(count(MessageQueueItem::loadMultiple()), 0, 'There are no message queue items.');
$options = [];
$this->courierManager
->sendMessage($template_collection, $this->identity, $options);
/** @var \Drupal\courier\MessageQueueItemInterface[] $mqi */
$mqi = MessageQueueItem::loadMultiple();
$this
->assertEqual(count($mqi), 1, 'There is one message queue item.');
/** @var \Drupal\courier\ChannelInterface[] $messages */
$this
->assertTrue($mqi[1]
->getIdentity()
->id() == $this->identity
->id(), 'Identity is identical.');
$this
->assertTrue($mqi[1]
->getOptions() == $options, 'Options are identical.');
$messages = $mqi[1]
->getMessages();
$courier_email = $messages[0];
$this
->assertTrue($courier_email instanceof Email, 'Message 0 is a courier_email.');
// Token replacement.
$this
->assertEqual($courier_email
->getBody(), 'Greetings, ' . $this->identity
->label());
$this
->assertTrue(empty(\Drupal::state()
->get('system.test_mail_collector')), 'There are no mails.');
/** @var \Drupal\Core\Cron $cron */
$cron = \Drupal::service('cron');
$cron
->run();
$mail_collector = \Drupal::state()
->get('system.test_mail_collector');
$this
->assertEqual(count($mail_collector), 1, 'There is a mail.');
$this
->assertEqual($mail_collector[0]['to'], $this->identity
->label() . ' <' . $this->identity
->getEmail() . '>');
$this
->assertEqual($mail_collector[0]['subject'], $courier_email
->getSubject());
$this
->assertEqual(trim($mail_collector[0]['body']), $courier_email
->getBody());
$this
->assertEqual(count(MessageQueueItem::loadMultiple()), 0, 'There are no message queue items.');
$this
->assertFalse(entity_load($courier_email
->getEntityTypeId(), $courier_email
->id()), 'courier_email is deleted.');
// Deleting owner entity deletes template collections.
$owner_entity
->delete();
$this
->assertEqual(count(TemplateCollection::loadMultiple()), 0, 'Deleted template collection.');
}