Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

MDL-41196 Unit Tests: Add a phpmailer message sink

  • Loading branch information...
commit 3822616ba664195a8c4cdceadf861b73f075ba0d 1 parent 14e8af6
@andrewnicols andrewnicols authored
View
15 lib/phpmailer/moodle_phpmailer.php
@@ -125,4 +125,19 @@ public function EncodeQP($string, $line_max = 76, $space_conv = false) {
fclose($fp);
return $out;
}
+
+ protected function PostSend() {
+ // Now ask phpunit if it wants to catch this message.
+ if (phpunit_util::is_redirecting_messages()) {
+ $mail = new stdClass();
+ $mail->header = $this->MIMEHeader;
+ $mail->body = $this->MIMEBody;
+ $mail->subject = $this->Subject;
+ $mail->from = $this->From;
+ phpunit_util::phpmailer_sent($mail);
+ return true;
+ } else {
+ return parent::PostSend();
+ }
+ }
}
View
13 lib/phpunit/classes/advanced_testcase.php
@@ -320,6 +320,19 @@ public function redirectMessages() {
}
/**
+ * Starts email redirection.
+ *
+ * You can verify if email were sent or not by inspecting the email
+ * array in the returned phpmailer sink instance. The redirection
+ * can be stopped by calling $sink->close();
+ *
+ * @return phpunit_message_sink
+ */
+ public function redirectEmails() {
+ return phpunit_util::start_phpmailer_redirection();
+ }
+
+ /**
* Cleanup after all tests are executed.
*
* Note: do not forget to call this if overridden...
View
87 lib/phpunit/classes/phpmailer_sink.php
@@ -0,0 +1,87 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * phpmailer message sink.
+ *
+ * @package core
+ * @category phpunit
+ * @copyright 2013 Andrew Nicols
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+
+/**
+ * phpmailer message sink.
+ *
+ * @package core
+ * @category phpunit
+ * @copyright 2013 Andrew Nicols
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class phpunit_phpmailer_sink {
+ /**
+ * @var array of records which would have been sent by phpmailer.
+ */
+ protected $messages = array();
+
+ /**
+ * Stop message redirection.
+ *
+ * Use if you do not want message redirected any more.
+ */
+ public function close() {
+ phpunit_util::stop_phpmailer_redirection();
+ }
+
+ /**
+ * To be called from phpunit_util only!
+ *
+ * @param stdClass $message record from message_read table
+ */
+ public function add_message($message) {
+ /* Number messages from 0. */
+ $this->messages[] = $message;
+ }
+
+ /**
+ * Returns all redirected messages.
+ *
+ * The instances are records form the message_read table.
+ * The array indexes are numbered from 0 and the order is matching
+ * the creation of events.
+ *
+ * @return array
+ */
+ public function get_messages() {
+ return $this->messages;
+ }
+
+ /**
+ * Return number of messages redirected to this sink.
+ * @return int
+ */
+ public function count() {
+ return count($this->messages);
+ }
+
+ /**
+ * Removes all previously stored messages.
+ */
+ public function clear() {
+ $this->messages = array();
+ }
+}
View
55 lib/phpunit/classes/util.php
@@ -63,6 +63,9 @@ class phpunit_util {
/** @var phpunit_message_sink alternative target for moodle messaging */
protected static $messagesink = null;
+ /** @var phpunit_phpmailer_sink alternative target for phpmailer messaging */
+ protected static $phpmailersink = null;
+
/**
* Prevent parallel test execution - this can not work in Moodle because we modify database and dataroot.
*
@@ -559,6 +562,9 @@ public static function reset_all_data($logchanges = false) {
// Stop any message redirection.
phpunit_util::stop_message_redirection();
+ // Stop any message redirection.
+ phpunit_util::stop_phpmailer_redirection();
+
// Release memory and indirectly call destroy() methods to release resource handles, etc.
gc_collect_cycles();
@@ -1330,4 +1336,53 @@ public static function message_sent($message) {
self::$messagesink->add_message($message);
}
}
+
+ /**
+ * Start phpmailer redirection.
+ *
+ * Note: Do not call directly from tests,
+ * use $sink = $this->redirectEmails() instead.
+ *
+ * @return phpunit_phpmailer_sink
+ */
+ public static function start_phpmailer_redirection() {
+ if (self::$phpmailersink) {
+ self::stop_phpmailer_redirection();
+ }
+ self::$phpmailersink = new phpunit_phpmailer_sink();
+ return self::$phpmailersink;
+ }
+
+ /**
+ * End phpmailer redirection.
+ *
+ * Note: Do not call directly from tests,
+ * use $sink->close() instead.
+ */
+ public static function stop_phpmailer_redirection() {
+ self::$phpmailersink = null;
+ }
+
+ /**
+ * Are messages for phpmailer redirected to some sink?
+ *
+ * Note: to be called from moodle_phpmailer.php only!
+ *
+ * @return bool
+ */
+ public static function is_redirecting_phpmailer() {
+ return !empty(self::$phpmailersink);
+ }
+
+ /**
+ * To be called from messagelib.php only!
+ *
+ * @param stdClass $message record from message_read table
+ * @return bool true means send message, false means message "sent" to sink.
+ */
+ public static function phpmailer_sent($message) {
+ if (self::$phpmailersink) {
+ self::$phpmailersink->add_message($message);
+ }
+ }
}
View
1  lib/phpunit/lib.php
@@ -30,6 +30,7 @@
require_once(__DIR__.'/classes/util.php');
require_once(__DIR__.'/classes/message_sink.php');
+require_once(__DIR__.'/classes/phpmailer_sink.php');
require_once(__DIR__.'/classes/basic_testcase.php');
require_once(__DIR__.'/classes/database_driver_testcase.php');
require_once(__DIR__.'/classes/arraydataset.php');
Please sign in to comment.
Something went wrong with that request. Please try again.