More updates to stop using dev or alpha or beta versions.
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Mail / MailManagerTest.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Tests\Core\Mail\MailManagerTest.
6  */
7
8 namespace Drupal\Tests\Core\Mail;
9
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;
15
16 /**
17  * @coversDefaultClass \Drupal\Core\Mail\MailManager
18  * @group Mail
19  */
20 class MailManagerTest extends UnitTestCase {
21
22   /**
23    * The cache backend to use.
24    *
25    * @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit_Framework_MockObject_MockObject
26    */
27   protected $cache;
28
29   /**
30    * The module handler.
31    *
32    * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
33    */
34   protected $moduleHandler;
35
36   /**
37    * The configuration factory.
38    *
39    * @var \Drupal\Core\Config\ConfigFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
40    */
41   protected $configFactory;
42
43   /**
44    * The plugin discovery.
45    *
46    * @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface|\PHPUnit_Framework_MockObject_MockObject
47    */
48   protected $discovery;
49
50   /**
51    * The renderer.
52    *
53    * @var \Drupal\Core\Render\RendererInterface|\PHPUnit_Framework_MockObject_MockObject
54    */
55   protected $renderer;
56
57   /**
58    * The mail manager under test.
59    *
60    * @var \Drupal\Tests\Core\Mail\TestMailManager
61    */
62   protected $mailManager;
63
64   /**
65    * A list of mail plugin definitions.
66    *
67    * @var array
68    */
69   protected $definitions = [
70     'php_mail' => [
71       'id' => 'php_mail',
72       'class' => 'Drupal\Core\Mail\Plugin\Mail\PhpMail',
73     ],
74     'test_mail_collector' => [
75       'id' => 'test_mail_collector',
76       'class' => 'Drupal\Core\Mail\Plugin\Mail\TestMailCollector',
77     ],
78   ];
79
80   /**
81    * {@inheritdoc}
82    */
83   protected function setUp() {
84     parent::setUp();
85     // Prepare the default constructor arguments required by MailManager.
86     $this->cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
87
88     $this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
89
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));
95   }
96
97   /**
98    * Sets up the mail manager for testing.
99    */
100   protected function setUpMailManager($interface = []) {
101     // Use the provided config for system.mail.interface settings.
102     $this->configFactory = $this->getConfigFactoryStub([
103       'system.mail' => [
104         'interface' => $interface,
105       ],
106     ]);
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);
113   }
114
115   /**
116    * Tests the getInstance method.
117    *
118    * @covers ::getInstance
119    */
120   public function testGetInstance() {
121     $interface = [
122       'default' => 'php_mail',
123       'default' => 'test_mail_collector',
124     ];
125     $this->setUpMailManager($interface);
126
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);
131
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);
136   }
137
138
139   /**
140    * Tests that mails are sent in a separate render context.
141    *
142    * @covers ::mail
143    */
144   public function testMailInRenderContext() {
145     $interface = [
146       'default' => 'php_mail',
147       'example_testkey' => 'test_mail_collector',
148     ];
149     $this->setUpMailManager($interface);
150
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']);
156       });
157     $this->mailManager->mail('example', 'key', 'to@example.org', 'en');
158   }
159
160 }
161
162 /**
163  * Provides a testing version of MailManager with an empty constructor.
164  */
165 class TestMailManager extends MailManager {
166   /**
167    * Sets the discovery for the manager.
168    *
169    * @param \Drupal\Component\Plugin\Discovery\DiscoveryInterface $discovery
170    *   The discovery object.
171    */
172   public function setDiscovery(DiscoveryInterface $discovery) {
173     $this->discovery = $discovery;
174   }
175
176   /**
177    * {@inheritdoc}
178    */
179   public function doMail($module, $key, $to, $langcode, $params = [], $reply = NULL, $send = TRUE) {
180     // Build a simplified message array and return it.
181     $message = [
182       'id' => $module . '_' . $key,
183       'module' => $module,
184       'key' => $key,
185       'to' => $to,
186       'from' => 'from@example.org',
187       'reply-to' => $reply,
188       'langcode' => $langcode,
189       'params' => $params,
190       'send' => TRUE,
191       'subject' => '',
192       'body' => [],
193     ];
194
195     return $message;
196   }
197
198 }