You are here

public function MailTest::testPluggableFramework in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/system/tests/src/Kernel/Mail/MailTest.php \Drupal\Tests\system\Kernel\Mail\MailTest::testPluggableFramework()
  2. 10 core/modules/system/tests/src/Kernel/Mail/MailTest.php \Drupal\Tests\system\Kernel\Mail\MailTest::testPluggableFramework()

Assert that the pluggable mail system is functional.

File

core/modules/system/tests/src/Kernel/Mail/MailTest.php, line 49

Class

MailTest
Performs tests on the pluggable mailing framework.

Namespace

Drupal\Tests\system\Kernel\Mail

Code

public function testPluggableFramework() {

  // Switch mail backends.
  $this
    ->configureDefaultMailInterface('test_php_mail_failure');

  // Get the default MailInterface class instance.
  $mail_backend = \Drupal::service('plugin.manager.mail')
    ->getInstance([
    'module' => 'default',
    'key' => 'default',
  ]);

  // Assert whether the default mail backend is an instance of the expected
  // class.
  // Default mail interface can be swapped.
  $this
    ->assertInstanceOf(TestPhpMailFailure::class, $mail_backend);

  // Add a module-specific mail backend.
  $this
    ->config('system.mail')
    ->set('interface.mymodule_testkey', 'test_mail_collector')
    ->save();

  // Get the added MailInterface class instance.
  $mail_backend = \Drupal::service('plugin.manager.mail')
    ->getInstance([
    'module' => 'mymodule',
    'key' => 'testkey',
  ]);

  // Assert whether the added mail backend is an instance of the expected
  // class.
  // Additional mail interfaces can be added.
  $this
    ->assertInstanceOf(TestMailCollector::class, $mail_backend);
}