5 * Contains \Drupal\Tests\Core\Mail\MailManagerTest.
8 namespace Drupal\Tests\Core\Mail;
10 use Drupal\Core\Render\RenderContext;
11 use Drupal\Core\Render\RendererInterface;
12 use Drupal\Tests\UnitTestCase;
13 use Drupal\Core\Mail\MailManager;
14 use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
17 * @coversDefaultClass \Drupal\Core\Mail\MailManager
20 class MailManagerTest extends UnitTestCase {
23 * The cache backend to use.
25 * @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit_Framework_MockObject_MockObject
32 * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
34 protected $moduleHandler;
37 * The configuration factory.
39 * @var \Drupal\Core\Config\ConfigFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
41 protected $configFactory;
44 * The plugin discovery.
46 * @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface|\PHPUnit_Framework_MockObject_MockObject
53 * @var \Drupal\Core\Render\RendererInterface|\PHPUnit_Framework_MockObject_MockObject
58 * The mail manager under test.
60 * @var \Drupal\Tests\Core\Mail\TestMailManager
62 protected $mailManager;
65 * A list of mail plugin definitions.
69 protected $definitions = [
72 'class' => 'Drupal\Core\Mail\Plugin\Mail\PhpMail',
74 'test_mail_collector' => [
75 'id' => 'test_mail_collector',
76 'class' => 'Drupal\Core\Mail\Plugin\Mail\TestMailCollector',
83 protected function setUp() {
85 // Prepare the default constructor arguments required by MailManager.
86 $this->cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
88 $this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
90 // Mock a Discovery object to replace AnnotationClassDiscovery.
91 $this->discovery = $this->getMock('Drupal\Component\Plugin\Discovery\DiscoveryInterface');
92 $this->discovery->expects($this->any())
93 ->method('getDefinitions')
94 ->will($this->returnValue($this->definitions));
98 * Sets up the mail manager for testing.
100 protected function setUpMailManager($interface = []) {
101 // Use the provided config for system.mail.interface settings.
102 $this->configFactory = $this->getConfigFactoryStub([
104 'interface' => $interface,
107 $logger_factory = $this->getMock('\Drupal\Core\Logger\LoggerChannelFactoryInterface');
108 $string_translation = $this->getStringTranslationStub();
109 $this->renderer = $this->getMock(RendererInterface::class);
110 // Construct the manager object and override its discovery.
111 $this->mailManager = new TestMailManager(new \ArrayObject(), $this->cache, $this->moduleHandler, $this->configFactory, $logger_factory, $string_translation, $this->renderer);
112 $this->mailManager->setDiscovery($this->discovery);
116 * Tests the getInstance method.
118 * @covers ::getInstance
120 public function testGetInstance() {
122 'default' => 'php_mail',
123 'default' => 'test_mail_collector',
125 $this->setUpMailManager($interface);
127 // Test that an unmatched message_id returns the default plugin instance.
128 $options = ['module' => 'foo', 'key' => 'bar'];
129 $instance = $this->mailManager->getInstance($options);
130 $this->assertInstanceOf('Drupal\Core\Mail\Plugin\Mail\PhpMail', $instance);
132 // Test that a matching message_id returns the specified plugin instance.
133 $options = ['module' => 'example', 'key' => 'testkey'];
134 $instance = $this->mailManager->getInstance($options);
135 $this->assertInstanceOf('Drupal\Core\Mail\Plugin\Mail\TestMailCollector', $instance);
140 * Tests that mails are sent in a separate render context.
144 public function testMailInRenderContext() {
146 'default' => 'php_mail',
147 'example_testkey' => 'test_mail_collector',
149 $this->setUpMailManager($interface);
151 $this->renderer->expects($this->exactly(1))
152 ->method('executeInRenderContext')
153 ->willReturnCallback(function (RenderContext $render_context, $callback) {
154 $message = $callback();
155 $this->assertEquals('example', $message['module']);
157 $this->mailManager->mail('example', 'key', 'to@example.org', 'en');
163 * Provides a testing version of MailManager with an empty constructor.
165 class TestMailManager extends MailManager {
167 * Sets the discovery for the manager.
169 * @param \Drupal\Component\Plugin\Discovery\DiscoveryInterface $discovery
170 * The discovery object.
172 public function setDiscovery(DiscoveryInterface $discovery) {
173 $this->discovery = $discovery;
179 public function doMail($module, $key, $to, $langcode, $params = [], $reply = NULL, $send = TRUE) {
180 // Build a simplified message array and return it.
182 'id' => $module . '_' . $key,
186 'from' => 'from@example.org',
187 'reply-to' => $reply,
188 'langcode' => $langcode,