diff --git a/core/tests/Drupal/Tests/Core/Mail/MailManagerTest.php b/core/tests/Drupal/Tests/Core/Mail/MailManagerTest.php new file mode 100644 index 0000000..3ff41e8 --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Mail/MailManagerTest.php @@ -0,0 +1,158 @@ + array( + 'id' => 'php_mail', + 'class' => 'Drupal\Core\Mail\Plugin\Mail\PhpMail', + ), + 'test_mail_collector' => array( + 'id' => 'test_mail_collector', + 'class' => 'Drupal\Core\Mail\Plugin\Mail\TestMailCollector', + ), + ); + + /** + * {@inheritdoc} + */ + public static function getInfo() { + return array( + 'name' => 'Mail manager test', + 'description' => 'Tests the mail plugin manager.', + 'group' => 'Mail', + ); + } + + /** + * {@inheritdoc} + */ + protected function setUp() { + parent::setUp(); + // Prepare the default constructor arguments required by MailManager. + $this->cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface'); + $this->languageManager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface'); + $this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface'); + + // Mock a CachedDiscovery object to replace AnnotationClassDiscovery. + $this->discovery = $this->getMock('Drupal\Component\Plugin\Discovery\CachedDiscoveryInterface'); + foreach ($this->definitions as $id => $definition) { + $definitions_map[] = array($id, $definition); + } + $this->discovery->expects($this->any()) + ->method('getDefinition') + ->will($this->returnValueMap($definitions_map)); + $this->discovery->expects($this->any()) + ->method('getDefinitions') + ->will($this->returnValue($this->definitions)); + } + + /** + * Sets up the mail manager for testing. + */ + protected function setUpMailManager($interface = array()) { + // Use the provided config for system.mail.interface settings. + $this->configFactory = $this->getConfigFactoryStub(array('system.mail' => array( + 'interface' => $interface, + ))); + // Construct the manager object and override its discovery. + $this->mailManager = new TestMailManager(new \ArrayObject(), $this->cache, $this->languageManager, $this->moduleHandler, $this->configFactory); + $this->mailManager->setDiscovery($this->discovery); + } + + /** + * Tests the getInstance method. + * + * @covers \Drupal\Core\Mail\MailManager::getInstance() + */ + public function testGetInstance() { + $interface = array( + 'default' => 'php_mail', + 'mailmanagertest_getinstance' => 'test_mail_collector', + ); + $this->setUpMailManager($interface); + + // Test that an unmatched message_id returns the default plugin instance. + $options = array('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 = array('module' => 'mailmanagertest', 'key' => 'getinstance'); + $instance = $this->mailManager->getInstance($options); + $this->assertInstanceOf('Drupal\Core\Mail\Plugin\Mail\TestMailCollector', $instance); + } +} + +/** + * Provides a testing version of MailManager with an empty constructor. + */ +class TestMailManager extends MailManager { + /** + * Sets the discovery for the manager. + * + * @param \Drupal\Component\Plugin\Discovery\DiscoveryInterface $discovery + * The discovery object. + */ + public function setDiscovery(DiscoveryInterface $discovery) { + $this->discovery = $discovery; + } +}