PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/framework/mail/BaseMailerTest.php

https://gitlab.com/brucealdridge/yii2
PHP | 449 lines | 333 code | 78 blank | 38 comment | 2 complexity | 32e53b9160200962687e54c05abce3d2 MD5 | raw file
  1. <?php
  2. namespace yiiunit\framework\mail;
  3. use Yii;
  4. use yii\base\View;
  5. use yii\mail\BaseMailer;
  6. use yii\mail\BaseMessage;
  7. use yii\helpers\FileHelper;
  8. use yiiunit\TestCase;
  9. /**
  10. * @group mail
  11. */
  12. class BaseMailerTest extends TestCase
  13. {
  14. public function setUp()
  15. {
  16. $this->mockApplication([
  17. 'components' => [
  18. 'mailer' => $this->createTestMailComponent(),
  19. ]
  20. ]);
  21. $filePath = $this->getTestFilePath();
  22. if (!file_exists($filePath)) {
  23. FileHelper::createDirectory($filePath);
  24. }
  25. }
  26. public function tearDown()
  27. {
  28. $filePath = $this->getTestFilePath();
  29. if (file_exists($filePath)) {
  30. FileHelper::removeDirectory($filePath);
  31. }
  32. }
  33. /**
  34. * @return string test file path.
  35. */
  36. protected function getTestFilePath()
  37. {
  38. return Yii::getAlias('@yiiunit/runtime') . DIRECTORY_SEPARATOR . basename(get_class($this)) . '_' . getmypid();
  39. }
  40. /**
  41. * @return Mailer test email component instance.
  42. */
  43. protected function createTestMailComponent()
  44. {
  45. $component = new Mailer();
  46. $component->viewPath = $this->getTestFilePath();
  47. return $component;
  48. }
  49. /**
  50. * @return Mailer mailer instance
  51. */
  52. protected function getTestMailComponent()
  53. {
  54. return Yii::$app->get('mailer');
  55. }
  56. // Tests :
  57. public function testSetupView()
  58. {
  59. $mailer = new Mailer();
  60. $view = new View();
  61. $mailer->setView($view);
  62. $this->assertEquals($view, $mailer->getView(), 'Unable to setup view!');
  63. $viewConfig = [
  64. 'params' => [
  65. 'param1' => 'value1',
  66. 'param2' => 'value2',
  67. ]
  68. ];
  69. $mailer->setView($viewConfig);
  70. $view = $mailer->getView();
  71. $this->assertTrue(is_object($view), 'Unable to setup view via config!');
  72. $this->assertEquals($viewConfig['params'], $view->params, 'Unable to configure view via config array!');
  73. }
  74. /**
  75. * @depends testSetupView
  76. */
  77. public function testGetDefaultView()
  78. {
  79. $mailer = new Mailer();
  80. $view = $mailer->getView();
  81. $this->assertTrue(is_object($view), 'Unable to get default view!');
  82. }
  83. public function testCreateMessage()
  84. {
  85. $mailer = new Mailer();
  86. $message = $mailer->compose();
  87. $this->assertTrue(is_object($message), 'Unable to create message instance!');
  88. $this->assertEquals($mailer->messageClass, get_class($message), 'Invalid message class!');
  89. }
  90. /**
  91. * @depends testCreateMessage
  92. */
  93. public function testDefaultMessageConfig()
  94. {
  95. $mailer = new Mailer();
  96. $notPropertyConfig = [
  97. 'charset' => 'utf-16',
  98. 'from' => 'from@domain.com',
  99. 'to' => 'to@domain.com',
  100. 'cc' => 'cc@domain.com',
  101. 'bcc' => 'bcc@domain.com',
  102. 'subject' => 'Test subject',
  103. 'textBody' => 'Test text body',
  104. 'htmlBody' => 'Test HTML body',
  105. ];
  106. $propertyConfig = [
  107. 'id' => 'test-id',
  108. 'encoding' => 'test-encoding',
  109. ];
  110. $messageConfig = array_merge($notPropertyConfig, $propertyConfig);
  111. $mailer->messageConfig = $messageConfig;
  112. $message = $mailer->compose();
  113. foreach ($notPropertyConfig as $name => $value) {
  114. $this->assertEquals($value, $message->{'_' . $name});
  115. }
  116. foreach ($propertyConfig as $name => $value) {
  117. $this->assertEquals($value, $message->$name);
  118. }
  119. }
  120. /**
  121. * @depends testGetDefaultView
  122. */
  123. public function testRender()
  124. {
  125. $mailer = $this->getTestMailComponent();
  126. $viewName = 'test_view';
  127. $viewFileName = $this->getTestFilePath() . DIRECTORY_SEPARATOR . $viewName . '.php';
  128. $viewFileContent = '<?php echo $testParam; ?>';
  129. file_put_contents($viewFileName, $viewFileContent);
  130. $params = [
  131. 'testParam' => 'test output'
  132. ];
  133. $renderResult = $mailer->render($viewName, $params);
  134. $this->assertEquals($params['testParam'], $renderResult);
  135. }
  136. /**
  137. * @depends testRender
  138. */
  139. public function testRenderLayout()
  140. {
  141. $mailer = $this->getTestMailComponent();
  142. $filePath = $this->getTestFilePath();
  143. $viewName = 'test_view2';
  144. $viewFileName = $filePath . DIRECTORY_SEPARATOR . $viewName . '.php';
  145. $viewFileContent = 'view file content';
  146. file_put_contents($viewFileName, $viewFileContent);
  147. $layoutName = 'test_layout';
  148. $layoutFileName = $filePath . DIRECTORY_SEPARATOR . $layoutName . '.php';
  149. $layoutFileContent = 'Begin Layout <?php echo $content; ?> End Layout';
  150. file_put_contents($layoutFileName, $layoutFileContent);
  151. $renderResult = $mailer->render($viewName, [], $layoutName);
  152. $this->assertEquals('Begin Layout ' . $viewFileContent . ' End Layout', $renderResult);
  153. }
  154. /**
  155. * @depends testCreateMessage
  156. * @depends testRender
  157. */
  158. public function testCompose()
  159. {
  160. $mailer = $this->getTestMailComponent();
  161. $mailer->htmlLayout = false;
  162. $mailer->textLayout = false;
  163. $htmlViewName = 'test_html_view';
  164. $htmlViewFileName = $this->getTestFilePath() . DIRECTORY_SEPARATOR . $htmlViewName . '.php';
  165. $htmlViewFileContent = 'HTML <b>view file</b> content';
  166. file_put_contents($htmlViewFileName, $htmlViewFileContent);
  167. $textViewName = 'test_text_view';
  168. $textViewFileName = $this->getTestFilePath() . DIRECTORY_SEPARATOR . $textViewName . '.php';
  169. $textViewFileContent = 'Plain text view file content';
  170. file_put_contents($textViewFileName, $textViewFileContent);
  171. $message = $mailer->compose([
  172. 'html' => $htmlViewName,
  173. 'text' => $textViewName,
  174. ]);
  175. $this->assertEquals($htmlViewFileContent, $message->_htmlBody, 'Unable to render html!');
  176. $this->assertEquals($textViewFileContent, $message->_textBody, 'Unable to render text!');
  177. $message = $mailer->compose($htmlViewName);
  178. $this->assertEquals($htmlViewFileContent, $message->_htmlBody, 'Unable to render html by direct view!');
  179. $this->assertEquals(strip_tags($htmlViewFileContent), $message->_textBody, 'Unable to render text by direct view!');
  180. }
  181. public function htmlAndPlainProvider()
  182. {
  183. return [
  184. [
  185. 1,
  186. 'HTML <b>view file</b> content <a href="http://yiifresh.com/index.php?r=site%2Freset-password&amp;token=abcdef">http://yiifresh.com/index.php?r=site%2Freset-password&amp;token=abcdef</a>',
  187. 'HTML view file content http://yiifresh.com/index.php?r=site%2Freset-password&token=abcdef',
  188. ],
  189. [
  190. 2, <<<HTML
  191. <html><head><style type="text/css">.content{color: #112345;}</style><title>TEST</title></head>
  192. <body>
  193. <style type="text/css">.content{color: #112345;}</style>
  194. <p> First paragraph
  195. second line
  196. <a href="http://yiifresh.com/index.php?r=site%2Freset-password&amp;token=abcdef">http://yiifresh.com/index.php?r=site%2Freset-password&amp;token=abcdef</a>
  197. </p><script type="text/javascript">alert("hi")</script>
  198. <p>Test Lorem ipsum...</p>
  199. </body>
  200. </html>
  201. HTML
  202. , <<<TEXT
  203. First paragraph
  204. second line
  205. http://yiifresh.com/index.php?r=site%2Freset-password&token=abcdef
  206. Test Lorem ipsum...
  207. TEXT
  208. ],
  209. ];
  210. }
  211. /**
  212. * @dataProvider htmlAndPlainProvider
  213. */
  214. public function testComposePlainTextFallback($i, $htmlViewFileContent, $expectedTextRendering)
  215. {
  216. $mailer = $this->getTestMailComponent();
  217. $mailer->htmlLayout = false;
  218. $mailer->textLayout = false;
  219. $htmlViewName = 'test_html_view' . $i; // $i is needed to generate different view files to ensure it works on HHVM
  220. $htmlViewFileName = $this->getTestFilePath() . DIRECTORY_SEPARATOR . $htmlViewName . '.php';
  221. file_put_contents($htmlViewFileName, $htmlViewFileContent);
  222. $message = $mailer->compose([
  223. 'html' => $htmlViewName,
  224. ]);
  225. $this->assertEqualsWithoutLE($htmlViewFileContent, $message->_htmlBody, 'Unable to render html!');
  226. $this->assertEqualsWithoutLE($expectedTextRendering, $message->_textBody, 'Unable to render text!');
  227. }
  228. public function testUseFileTransport()
  229. {
  230. $mailer = new Mailer();
  231. $this->assertFalse($mailer->useFileTransport);
  232. $this->assertEquals('@runtime/mail', $mailer->fileTransportPath);
  233. $mailer->fileTransportPath = '@yiiunit/runtime/mail';
  234. $mailer->useFileTransport = true;
  235. $mailer->fileTransportCallback = function () {
  236. return 'message.txt';
  237. };
  238. $message = $mailer->compose()
  239. ->setTo('to@example.com')
  240. ->setFrom('from@example.com')
  241. ->setSubject('test subject')
  242. ->setTextBody('text body' . microtime(true));
  243. $this->assertTrue($mailer->send($message));
  244. $file = Yii::getAlias($mailer->fileTransportPath) . '/message.txt';
  245. $this->assertTrue(is_file($file));
  246. $this->assertEquals($message->toString(), file_get_contents($file));
  247. }
  248. public function testBeforeSendEvent()
  249. {
  250. $message = new Message();
  251. $mailerMock = $this->getMockBuilder('yiiunit\framework\mail\Mailer')->setMethods(['beforeSend', 'afterSend'])->getMock();
  252. $mailerMock->expects($this->once())->method('beforeSend')->with($message)->will($this->returnValue(true));
  253. $mailerMock->expects($this->once())->method('afterSend')->with($message, true);
  254. $mailerMock->send($message);
  255. }
  256. }
  257. /**
  258. * Test Mailer class
  259. */
  260. class Mailer extends BaseMailer
  261. {
  262. public $messageClass = 'yiiunit\framework\mail\Message';
  263. public $sentMessages = [];
  264. protected function sendMessage($message)
  265. {
  266. $this->sentMessages[] = $message;
  267. return true;
  268. }
  269. }
  270. /**
  271. * Test Message class
  272. */
  273. class Message extends BaseMessage
  274. {
  275. public $id;
  276. public $encoding;
  277. public $_charset;
  278. public $_from;
  279. public $_replyTo;
  280. public $_to;
  281. public $_cc;
  282. public $_bcc;
  283. public $_subject;
  284. public $_textBody;
  285. public $_htmlBody;
  286. public function getCharset()
  287. {
  288. return $this->_charset;
  289. }
  290. public function setCharset($charset)
  291. {
  292. $this->_charset = $charset;
  293. return $this;
  294. }
  295. public function getFrom()
  296. {
  297. return $this->_from;
  298. }
  299. public function setFrom($from)
  300. {
  301. $this->_from = $from;
  302. return $this;
  303. }
  304. public function getTo()
  305. {
  306. return $this->_to;
  307. }
  308. public function setTo($to)
  309. {
  310. $this->_to = $to;
  311. return $this;
  312. }
  313. public function getCc()
  314. {
  315. return $this->_cc;
  316. }
  317. public function setCc($cc)
  318. {
  319. $this->_cc = $cc;
  320. return $this;
  321. }
  322. public function getBcc()
  323. {
  324. return $this->_bcc;
  325. }
  326. public function setBcc($bcc)
  327. {
  328. $this->_bcc = $bcc;
  329. return $this;
  330. }
  331. public function getSubject()
  332. {
  333. return $this->_subject;
  334. }
  335. public function setSubject($subject)
  336. {
  337. $this->_subject = $subject;
  338. return $this;
  339. }
  340. public function getReplyTo()
  341. {
  342. return $this->_replyTo;
  343. }
  344. public function setReplyTo($replyTo)
  345. {
  346. $this->_replyTo = $replyTo;
  347. return $this;
  348. }
  349. public function setTextBody($text)
  350. {
  351. $this->_textBody = $text;
  352. return $this;
  353. }
  354. public function setHtmlBody($html)
  355. {
  356. $this->_htmlBody = $html;
  357. return $this;
  358. }
  359. public function attachContent($content, array $options = []) {}
  360. public function attach($fileName, array $options = []) {}
  361. public function embed($fileName, array $options = []) {}
  362. public function embedContent($content, array $options = []) {}
  363. public function toString()
  364. {
  365. $mailer = $this->mailer;
  366. $this->mailer = null;
  367. $s = var_export($this, true);
  368. $this->mailer = $mailer;
  369. return $s;
  370. }
  371. }