MailManagerTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drupal\Tests\Core\Mail\MailManagerTest.
  5. */
  6. namespace Drupal\Tests\Core\Mail;
  7. use Drupal\Core\DependencyInjection\ContainerBuilder;
  8. use Drupal\Core\Render\RenderContext;
  9. use Drupal\Core\Render\RendererInterface;
  10. use Drupal\Tests\UnitTestCase;
  11. use Drupal\Core\Mail\MailManager;
  12. use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
  13. /**
  14. * @coversDefaultClass \Drupal\Core\Mail\MailManager
  15. * @group Mail
  16. */
  17. class MailManagerTest extends UnitTestCase {
  18. /**
  19. * The cache backend to use.
  20. *
  21. * @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. protected $cache;
  24. /**
  25. * The module handler.
  26. *
  27. * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $moduleHandler;
  30. /**
  31. * The configuration factory.
  32. *
  33. * @var \Drupal\Core\Config\ConfigFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. protected $configFactory;
  36. /**
  37. * The plugin discovery.
  38. *
  39. * @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $discovery;
  42. /**
  43. * The renderer.
  44. *
  45. * @var \Drupal\Core\Render\RendererInterface|\PHPUnit_Framework_MockObject_MockObject
  46. */
  47. protected $renderer;
  48. /**
  49. * The mail manager under test.
  50. *
  51. * @var \Drupal\Tests\Core\Mail\TestMailManager
  52. */
  53. protected $mailManager;
  54. /**
  55. * A list of mail plugin definitions.
  56. *
  57. * @var array
  58. */
  59. protected $definitions = [
  60. 'php_mail' => [
  61. 'id' => 'php_mail',
  62. 'class' => 'Drupal\Core\Mail\Plugin\Mail\PhpMail',
  63. ],
  64. 'test_mail_collector' => [
  65. 'id' => 'test_mail_collector',
  66. 'class' => 'Drupal\Core\Mail\Plugin\Mail\TestMailCollector',
  67. ],
  68. ];
  69. /**
  70. * {@inheritdoc}
  71. */
  72. protected function setUp() {
  73. parent::setUp();
  74. // Prepare the default constructor arguments required by MailManager.
  75. $this->cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
  76. $this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
  77. // Mock a Discovery object to replace AnnotationClassDiscovery.
  78. $this->discovery = $this->getMock('Drupal\Component\Plugin\Discovery\DiscoveryInterface');
  79. $this->discovery->expects($this->any())
  80. ->method('getDefinitions')
  81. ->will($this->returnValue($this->definitions));
  82. }
  83. /**
  84. * Sets up the mail manager for testing.
  85. */
  86. protected function setUpMailManager($interface = []) {
  87. // Use the provided config for system.mail.interface settings.
  88. $this->configFactory = $this->getConfigFactoryStub([
  89. 'system.mail' => [
  90. 'interface' => $interface,
  91. ],
  92. 'system.site' => [
  93. 'mail' => 'test@example.com',
  94. ],
  95. ]);
  96. $logger_factory = $this->getMock('\Drupal\Core\Logger\LoggerChannelFactoryInterface');
  97. $string_translation = $this->getStringTranslationStub();
  98. $this->renderer = $this->getMock(RendererInterface::class);
  99. // Construct the manager object and override its discovery.
  100. $this->mailManager = new TestMailManager(new \ArrayObject(), $this->cache, $this->moduleHandler, $this->configFactory, $logger_factory, $string_translation, $this->renderer);
  101. $this->mailManager->setDiscovery($this->discovery);
  102. // @see \Drupal\Core\Plugin\Factory\ContainerFactory::createInstance()
  103. $container = new ContainerBuilder();
  104. $container->set('config.factory', $this->configFactory);
  105. \Drupal::setContainer($container);
  106. }
  107. /**
  108. * Tests the getInstance method.
  109. *
  110. * @covers ::getInstance
  111. */
  112. public function testGetInstance() {
  113. $interface = [
  114. 'default' => 'php_mail',
  115. 'default' => 'test_mail_collector',
  116. ];
  117. $this->setUpMailManager($interface);
  118. // Test that an unmatched message_id returns the default plugin instance.
  119. $options = ['module' => 'foo', 'key' => 'bar'];
  120. $instance = $this->mailManager->getInstance($options);
  121. $this->assertInstanceOf('Drupal\Core\Mail\Plugin\Mail\PhpMail', $instance);
  122. // Test that a matching message_id returns the specified plugin instance.
  123. $options = ['module' => 'example', 'key' => 'testkey'];
  124. $instance = $this->mailManager->getInstance($options);
  125. $this->assertInstanceOf('Drupal\Core\Mail\Plugin\Mail\TestMailCollector', $instance);
  126. }
  127. /**
  128. * Tests that mails are sent in a separate render context.
  129. *
  130. * @covers ::mail
  131. */
  132. public function testMailInRenderContext() {
  133. $interface = [
  134. 'default' => 'php_mail',
  135. 'example_testkey' => 'test_mail_collector',
  136. ];
  137. $this->setUpMailManager($interface);
  138. $this->renderer->expects($this->exactly(1))
  139. ->method('executeInRenderContext')
  140. ->willReturnCallback(function (RenderContext $render_context, $callback) {
  141. $message = $callback();
  142. $this->assertEquals('example', $message['module']);
  143. });
  144. $this->mailManager->mail('example', 'key', 'to@example.org', 'en');
  145. }
  146. }
  147. /**
  148. * Provides a testing version of MailManager with an empty constructor.
  149. */
  150. class TestMailManager extends MailManager {
  151. /**
  152. * Sets the discovery for the manager.
  153. *
  154. * @param \Drupal\Component\Plugin\Discovery\DiscoveryInterface $discovery
  155. * The discovery object.
  156. */
  157. public function setDiscovery(DiscoveryInterface $discovery) {
  158. $this->discovery = $discovery;
  159. }
  160. /**
  161. * {@inheritdoc}
  162. */
  163. public function doMail($module, $key, $to, $langcode, $params = [], $reply = NULL, $send = TRUE) {
  164. // Build a simplified message array and return it.
  165. $message = [
  166. 'id' => $module . '_' . $key,
  167. 'module' => $module,
  168. 'key' => $key,
  169. 'to' => $to,
  170. 'from' => 'from@example.org',
  171. 'reply-to' => $reply,
  172. 'langcode' => $langcode,
  173. 'params' => $params,
  174. 'send' => TRUE,
  175. 'subject' => '',
  176. 'body' => [],
  177. ];
  178. return $message;
  179. }
  180. }