You are here

class MailManagerTest in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Mail/MailManagerTest.php \Drupal\Tests\Core\Mail\MailManagerTest
  2. 10 core/tests/Drupal/Tests/Core/Mail/MailManagerTest.php \Drupal\Tests\Core\Mail\MailManagerTest

@coversDefaultClass \Drupal\Core\Mail\MailManager @group Mail

Hierarchy

Expanded class hierarchy of MailManagerTest

File

core/tests/Drupal/Tests/Core/Mail/MailManagerTest.php, line 21
Contains \Drupal\Tests\Core\Mail\MailManagerTest.

Namespace

Drupal\Tests\Core\Mail
View 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;

  /**
   * 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() {
    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')
      ->will($this
      ->returnValue($this->definitions));
  }

  /**
   * Sets up the mail manager for testing.
   */
  protected function setUpMailManager($interface = []) {

    // Use the provided config for system.mail.interface settings.
    $this->configFactory = $this
      ->getConfigFactoryStub([
      'system.mail' => [
        'interface' => $interface,
      ],
      '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);

    // @see \Drupal\Core\Plugin\Factory\ContainerFactory::createInstance()
    $container = new ContainerBuilder();
    $container
      ->set('config.factory', $this->configFactory);
    \Drupal::setContainer($container);
  }

  /**
   * Tests the getInstance method.
   *
   * @covers ::getInstance
   */
  public function testGetInstance() {
    $interface = [
      'default' => 'php_mail',
      'default' => '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' => 'testkey',
    ];
    $instance = $this->mailManager
      ->getInstance($options);
    $this
      ->assertInstanceOf('Drupal\\Core\\Mail\\Plugin\\Mail\\TestMailCollector', $instance);
  }

  /**
   * Tests that mails are sent in a separate render context.
   *
   * @covers ::mail
   */
  public function testMailInRenderContext() {
    $interface = [
      'default' => 'php_mail',
      'example_testkey' => 'test_mail_collector',
    ];
    $this
      ->setUpMailManager($interface);
    $this->renderer
      ->expects($this
      ->exactly(1))
      ->method('executeInRenderContext')
      ->willReturnCallback(function (RenderContext $render_context, $callback) {
      $message = $callback();
      $this
        ->assertEquals('example', $message['module']);
    });
    $this->mailManager
      ->mail('example', 'key', 'to@example.org', 'en');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MailManagerTest::$cache protected property The cache backend to use.
MailManagerTest::$configFactory protected property The configuration factory.
MailManagerTest::$definitions protected property A list of mail plugin definitions.
MailManagerTest::$discovery protected property The plugin discovery.
MailManagerTest::$mailManager protected property The mail manager under test.
MailManagerTest::$moduleHandler protected property The module handler.
MailManagerTest::$renderer protected property The renderer.
MailManagerTest::setUp protected function Overrides UnitTestCase::setUp
MailManagerTest::setUpMailManager protected function Sets up the mail manager for testing.
MailManagerTest::testGetInstance public function Tests the getInstance method.
MailManagerTest::testMailInRenderContext public function Tests that mails are sent in a separate render context.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.