From 45d02d07a08247f25273267eb4fa34cd71b01a33 Mon Sep 17 00:00:00 2001 From: Klimov Paul Date: Tue, 5 Nov 2013 15:45:27 +0200 Subject: [PATCH] Method 'yii\mail\MessageInterface::body()' added. --- framework/yii/mail/BaseMessage.php | 16 ++++++++++++++++ framework/yii/mail/MessageInterface.php | 11 +++++++++++ tests/unit/framework/mail/BaseMessageTest.php | 26 +++++++++++++++++++++++++- 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/framework/yii/mail/BaseMessage.php b/framework/yii/mail/BaseMessage.php index b3dade7..f81dfdc 100644 --- a/framework/yii/mail/BaseMessage.php +++ b/framework/yii/mail/BaseMessage.php @@ -61,4 +61,20 @@ abstract class BaseMessage extends Object implements MessageInterface $this->text($this->getMailer()->render($view, $params, $this->getMailer()->textLayout)); return $this; } + + /** + * @inheritdoc + */ + public function body($view, $params = []) + { + if (is_array($view)) { + $this->renderHtml($view['html'], $params); + $this->renderText($view['text'], $params); + } else { + $html = $this->getMailer()->render($view, $params, $this->getMailer()->htmlLayout); + $this->html($html); + $this->text(strip_tags($html)); + } + return $this; + } } \ No newline at end of file diff --git a/framework/yii/mail/MessageInterface.php b/framework/yii/mail/MessageInterface.php index d0e4180..8f7a3b2 100644 --- a/framework/yii/mail/MessageInterface.php +++ b/framework/yii/mail/MessageInterface.php @@ -167,6 +167,17 @@ interface MessageInterface public function renderText($view, $params = []); /** + * Composes the message HTML and plain text body. + * @param string|array $view varies method behavior depending on type: + * - string - the view name or the path alias of the HTML body view file, in this case + * text body will be composed from html one using [[strip_tags()]] function. + * - array - list of views for each body type in format: ['html' => 'htmlView', 'text' => 'textView'] + * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file. + * @return static self reference. + */ + public function body($view, $params = []); + + /** * String output. * This is PHP magic method that returns string representation of an object. * @return string the string representation of the object diff --git a/tests/unit/framework/mail/BaseMessageTest.php b/tests/unit/framework/mail/BaseMessageTest.php index 9409ed2..93d94f9 100644 --- a/tests/unit/framework/mail/BaseMessageTest.php +++ b/tests/unit/framework/mail/BaseMessageTest.php @@ -53,7 +53,31 @@ class BaseMessageTest extends TestCase $viewName = 'test/html/view'; $message->renderHtml($viewName); $expectedHtml = 'view=' . $viewName . ' layout=' . $mailer->htmlLayout; - $this->assertEquals($expectedHtml, $message->html, 'Unable to render text!'); + $this->assertEquals($expectedHtml, $message->html, 'Unable to render html!'); + } + + /** + * @depends testRender + */ + public function testComposeBody() + { + $mailer = $this->getMailer(); + $message = $mailer->compose(); + + $viewName = 'test/html/view'; + $message->body($viewName); + $expectedHtml = 'view=' . $viewName . ' layout=' . $mailer->htmlLayout; + $this->assertEquals($expectedHtml, $message->html, 'Unable to compose html!'); + $expectedText = strip_tags($expectedHtml); + $this->assertEquals($expectedText, $message->text, 'Unable to compose text from html!'); + + $textViewName = 'test/text/view'; + $htmlViewName = 'test/html/view'; + $message->body(['text' => $textViewName, 'html' => $htmlViewName]); + $expectedHtml = 'view=' . $htmlViewName . ' layout=' . $mailer->htmlLayout; + $this->assertEquals($expectedHtml, $message->html, 'Unable to compose html from separated view!'); + $expectedText = 'view=' . $textViewName . ' layout=' . $mailer->textLayout; + $this->assertEquals($expectedText, $message->text, 'Unable to compose text from separated view!'); } public function testSend()