class PhpMailTest
Same name in other branches
- 9 core/tests/Drupal/Tests/Core/Mail/Plugin/Mail/PhpMailTest.php \Drupal\Tests\Core\Mail\Plugin\Mail\PhpMailTest
- 11.x 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 extends \PHPUnit\Framework\TestCase uses \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait
- class \Drupal\Tests\Core\Mail\Plugin\Mail\PhpMailTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of PhpMailTest
File
-
core/
tests/ Drupal/ Tests/ Core/ Mail/ Plugin/ Mail/ PhpMailTest.php, line 18
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;
/**
* The current request.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $request;
/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack|\Prophecy\Prophecy\ProphecyInterface
*/
protected $requestStack;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
// Use the provided config for system.mail.interface settings.
$this->configFactory = $this->getConfigFactoryStub([
'system.mail' => [
'interface' => [],
'mailer_dsn' => [
'scheme' => 'null',
'host' => 'null',
'user' => NULL,
'password' => NULL,
'port' => NULL,
'options' => [],
],
],
'system.site' => [
'mail' => 'test@example.com',
],
]);
$this->request = new Request();
$this->requestStack = $this->prophesize(RequestStack::class);
$this->requestStack
->getCurrentRequest()
->willReturn($this->request);
$container = new ContainerBuilder();
$container->set('config.factory', $this->configFactory);
$container->set('request_stack', $this->requestStack
->reveal());
\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();
$request = $this->getMockBuilder(Request::class)
->disableOriginalConstructor()
->getMock();
$request->server = $this->getMockBuilder(ServerBag::class)
->onlyMethods([
'has',
'get',
])
->getMock();
$request->server
->method('has')
->willReturn(FALSE);
$request->server
->method('get')
->willReturn(FALSE);
$reflection = new \ReflectionClass($mailer);
$reflection_property = $reflection->getProperty('request');
$reflection_property->setValue($mailer, $request);
return $mailer;
}
/**
* Tests sending a mail using a From address with a comma in it.
*
* @covers ::mail
*/
public function testMail() : void {
// Setup a mail message.
$message = [
'id' => 'example_key',
'module' => 'example',
'key' => 'key',
'to' => 'to@example.org',
'from' => 'from@example.org',
'reply-to' => 'from@example.org',
'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" <from@example.org>',
'Reply-to' => 'from@example.org',
'Return-Path' => 'from@example.org',
],
];
$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
$line_end = "\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\" <from@example.org>{$line_end}";
$expected_headers .= "Reply-to: from@example.org{$line_end}";
$mailer->expects($this->once())
->method('doMail')
->with($this->equalTo('to@example.org'), $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));
}
}
Members
Title Sort descending | Deprecated | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|---|
PhpMailTest::$configFactory | protected | property | The configuration factory. | |||
PhpMailTest::$request | protected | property | The current request. | |||
PhpMailTest::$requestStack | protected | property | The request stack. | |||
PhpMailTest::createPhpMailInstance | protected | function | Creates a mocked PhpMail object. | |||
PhpMailTest::setUp | protected | function | Overrides UnitTestCase::setUp | |||
PhpMailTest::testMail | public | function | Tests sending a mail using a From address with a comma in it. | |||
PhpUnitWarnings::$deprecationWarnings | private static | property | Deprecation warnings from PHPUnit to raise with @trigger_error(). | |||
PhpUnitWarnings::addWarning | public | function | Converts PHPUnit deprecation warnings to E_USER_DEPRECATED. | |||
RandomGeneratorTrait::getRandomGenerator | protected | function | Gets the random generator for the utility methods. | |||
RandomGeneratorTrait::randomMachineName | protected | function | Generates a unique random string containing letters and numbers. | |||
RandomGeneratorTrait::randomObject | public | function | Generates a random PHP object. | |||
RandomGeneratorTrait::randomString | public | function | Generates a pseudo-random string of ASCII characters of codes 32 to 126. | |||
RandomGeneratorTrait::randomStringValidate | Deprecated | public | function | Callback for random string validation. | ||
UnitTestCase::$root | protected | property | The app root. | 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::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | |||
UnitTestCase::setUpBeforeClass | public static | function | ||||
UnitTestCase::__get | public | function |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.