class MailManagerTest
Same name and namespace in other branches
- 11.x core/tests/Drupal/Tests/Core/Mail/MailManagerTest.php \Drupal\Tests\Core\Mail\MailManagerTest
- 10 core/tests/Drupal/Tests/Core/Mail/MailManagerTest.php \Drupal\Tests\Core\Mail\MailManagerTest
- 9 core/tests/Drupal/Tests/Core/Mail/MailManagerTest.php \Drupal\Tests\Core\Mail\MailManagerTest
- 8.9.x core/tests/Drupal/Tests/Core/Mail/MailManagerTest.php \Drupal\Tests\Core\Mail\MailManagerTest
Tests Drupal\Core\Mail\MailManager.
Attributes
#[CoversClass(MailManager::class)]
#[Group('Mail')]
Hierarchy
- class \Drupal\Tests\UnitTestCase uses \Drupal\Tests\DrupalTestCaseTrait, \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Drupal\TestTools\Extension\DeprecationBridge\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\Tests\Core\Mail\MailManagerTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of MailManagerTest
File
-
core/
tests/ Drupal/ Tests/ Core/ Mail/ MailManagerTest.php, line 21
Namespace
Drupal\Tests\Core\MailView source
class MailManagerTest extends UnitTestCase {
/**
* The cache backend to use.
*
* @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $cache;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $moduleHandler;
/**
* The configuration factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $configFactory;
/**
* The plugin discovery.
*
* @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $discovery;
/**
* The renderer.
*
* @var \Drupal\Core\Render\RendererInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $renderer;
/**
* The mail manager under test.
*
* @var \Drupal\Tests\Core\Mail\TestMailManager
*/
protected $mailManager;
/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack|\Prophecy\Prophecy\ProphecyInterface
*/
protected $requestStack;
/**
* The current request.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $request;
/**
* A list of mail plugin definitions.
*
* @var array
*/
protected $definitions = [
'php_mail' => [
'id' => 'php_mail',
'class' => 'Drupal\\Core\\Mail\\Plugin\\Mail\\PhpMail',
],
'test_mail_collector' => [
'id' => 'test_mail_collector',
'class' => 'Drupal\\Core\\Mail\\Plugin\\Mail\\TestMailCollector',
],
];
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
// Prepare the default constructor arguments required by MailManager.
$this->cache = $this->createMock('Drupal\\Core\\Cache\\CacheBackendInterface');
$this->moduleHandler = $this->createMock('Drupal\\Core\\Extension\\ModuleHandlerInterface');
// Mock a Discovery object to replace AnnotationClassDiscovery.
$this->discovery = $this->createMock('Drupal\\Component\\Plugin\\Discovery\\DiscoveryInterface');
$this->discovery
->expects($this->any())
->method('getDefinitions')
->willReturn($this->definitions);
}
/**
* Sets up the mail manager for testing.
*/
protected function setUpMailManager($interface = []) : void {
// Use the provided config for system.mail.interface settings.
$this->configFactory = $this->getConfigFactoryStub([
'system.mail' => [
'interface' => $interface,
'mailer_dsn' => [
'scheme' => 'null',
'host' => 'null',
'user' => NULL,
'password' => NULL,
'port' => NULL,
'options' => [],
],
],
'system.site' => [
'mail' => 'test@example.com',
],
]);
$logger_factory = $this->createMock('\\Drupal\\Core\\Logger\\LoggerChannelFactoryInterface');
$string_translation = $this->getStringTranslationStub();
$this->renderer = $this->createMock(RendererInterface::class);
// Construct the manager object and override its discovery.
$this->mailManager = new TestMailManager(new \ArrayObject(), $this->cache, $this->moduleHandler, $this->configFactory, $logger_factory, $string_translation, $this->renderer);
$this->mailManager
->setDiscovery($this->discovery);
$this->request = new Request();
$this->requestStack = $this->prophesize(RequestStack::class);
$this->requestStack
->getCurrentRequest()
->willReturn($this->request);
// @see \Drupal\Core\Plugin\Factory\ContainerFactory::createInstance()
$container = new ContainerBuilder();
$container->set('config.factory', $this->configFactory);
$container->set('request_stack', $this->requestStack
->reveal());
\Drupal::setContainer($container);
}
/**
* Tests the getInstance method.
*/
public function testGetInstance() : void {
$interface = [
'default' => 'php_mail',
'example_test_key' => 'test_mail_collector',
];
$this->setUpMailManager($interface);
// Test that an unmatched message_id returns the default plugin instance.
$options = [
'module' => 'foo',
'key' => 'bar',
];
$instance = $this->mailManager
->getInstance($options);
$this->assertInstanceOf('Drupal\\Core\\Mail\\Plugin\\Mail\\PhpMail', $instance);
// Test that a matching message_id returns the specified plugin instance.
$options = [
'module' => 'example',
'key' => 'test_key',
];
$instance = $this->mailManager
->getInstance($options);
$this->assertInstanceOf('Drupal\\Core\\Mail\\Plugin\\Mail\\TestMailCollector', $instance);
}
/**
* Tests that mails are sent in a separate render context.
*/
public function testMailInRenderContext() : void {
$interface = [
'default' => 'php_mail',
'example_test_key' => 'test_mail_collector',
];
$this->setUpMailManager($interface);
$this->renderer
->expects($this->exactly(1))
->method('executeInRenderContext')
->willReturnCallback(function (RenderContext $render_context, $callback) : void {
$message = $callback();
$this->assertEquals('example', $message['module']);
});
$this->mailManager
->mail('example', 'key', 'to@example.org', 'en');
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.