class PhpMailTest
Same name and namespace in other branches
- 11.x core/tests/Drupal/Tests/Core/Mail/Plugin/Mail/PhpMailTest.php \Drupal\Tests\Core\Mail\Plugin\Mail\PhpMailTest
- 10 core/tests/Drupal/Tests/Core/Mail/Plugin/Mail/PhpMailTest.php \Drupal\Tests\Core\Mail\Plugin\Mail\PhpMailTest
@coversDefaultClass \Drupal\Core\Mail\Plugin\Mail\PhpMail
@group Mail
Hierarchy
- class \Drupal\Tests\UnitTestCase uses \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait implements \PHPUnit\Framework\TestCase
- class \Drupal\Tests\Core\Mail\Plugin\Mail\PhpMailTest implements \Drupal\Tests\UnitTestCase
Expanded class hierarchy of PhpMailTest
File
-
core/
tests/ Drupal/ Tests/ Core/ Mail/ Plugin/ Mail/ PhpMailTest.php, line 13
Namespace
Drupal\Tests\Core\Mail\Plugin\MailView source
class PhpMailTest extends UnitTestCase {
/**
* The configuration factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $configFactory;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
// Use the provided config for system.mail.interface settings.
$this->configFactory = $this->getConfigFactoryStub([
'system.mail' => [
'interface' => [],
],
'system.site' => [
'mail' => '[email protected]',
],
]);
$container = new ContainerBuilder();
$container->set('config.factory', $this->configFactory);
\Drupal::setContainer($container);
}
/**
* Creates a mocked PhpMail object.
*
* The method "doMail()" gets overridden to avoid a mail() call in tests.
*
* @return \Drupal\Core\Mail\Plugin\Mail\PhpMail|\PHPUnit\Framework\MockObject\MockObject
* A PhpMail instance.
*/
protected function createPhpMailInstance() : PhpMail {
$mailer = $this->getMockBuilder(PhpMail::class)
->onlyMethods([
'doMail',
])
->getMock();
return $mailer;
}
/**
* Tests sending a mail using a From address with a comma in it.
*
* @covers ::testMail
*/
public function testMail() {
// Setup a mail message.
$message = [
'id' => 'example_key',
'module' => 'example',
'key' => 'key',
'to' => '[email protected]',
'from' => '[email protected]',
'reply-to' => '[email protected]',
'langcode' => 'en',
'params' => [],
'send' => TRUE,
'subject' => "test\r\nsubject",
'body' => '',
'headers' => [
'MIME-Version' => '1.0',
'Content-Type' => 'text/plain; charset=UTF-8; format=flowed; delsp=yes',
'Content-Transfer-Encoding' => '8Bit',
'X-Mailer' => 'Drupal',
'From' => '"Foo, Bar, and Baz" <[email protected]>',
'Reply-to' => '[email protected]',
'Return-Path' => '[email protected]',
],
];
$mailer = $this->createPhpMailInstance();
// Verify we use line endings consistent with the PHP mail() function, which
// changed with PHP 8. See:
// - https://www.drupal.org/node/3270647
// - https://bugs.php.net/bug.php?id=81158
// Since Drupal 10+ does not support PHP < 8, the PHP version check in the next line can be removed in Drupal 10+.
$line_end = PHP_MAJOR_VERSION < 8 ? "\n" : "\r\n";
$expected_headers = "MIME-Version: 1.0{$line_end}";
$expected_headers .= "Content-Type: text/plain; charset=UTF-8; format=flowed; delsp=yes{$line_end}";
$expected_headers .= "Content-Transfer-Encoding: 8Bit{$line_end}";
$expected_headers .= "X-Mailer: Drupal{$line_end}";
$expected_headers .= "From: \"Foo, Bar, and Baz\" <[email protected]>{$line_end}";
$expected_headers .= "Reply-to: [email protected]{$line_end}";
$mailer->expects($this->once())
->method('doMail')
->with($this->equalTo('[email protected]'), $this->equalTo("=?utf-8?Q?test?={$line_end} =?utf-8?Q?subject?="), $this->equalTo(''), $this->stringStartsWith($expected_headers))
->willReturn(TRUE);
$this->assertTrue($mailer->mail($message));
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.