Merge branch 'w41_MDL-42054_m26_phpmailertesting_i' of https://github.com/skodak...
authorDan Poltawski <dan@moodle.com>
Wed, 9 Oct 2013 10:16:56 +0000 (18:16 +0800)
committerDan Poltawski <dan@moodle.com>
Wed, 9 Oct 2013 10:16:56 +0000 (18:16 +0800)
lib/phpmailer/moodle_phpmailer.php
lib/tests/moodlelib_test.php

index 688c336..881b1f8 100644 (file)
@@ -145,12 +145,17 @@ class moodle_phpmailer extends PHPMailer {
      */
     public function postSend() {
         // Now ask phpunit if it wants to catch this message.
-        if (PHPUNIT_TEST && phpunit_util::is_redirecting_phpmailer()) {
+        if (PHPUNIT_TEST) {
+            if (!phpunit_util::is_redirecting_phpmailer()) {
+                debugging('Unit tests must not send real emails! Use $this->start_phpmailer_redirection()');
+                return true;
+            }
             $mail = new stdClass();
             $mail->header = $this->MIMEHeader;
             $mail->body = $this->MIMEBody;
             $mail->subject = $this->Subject;
             $mail->from = $this->From;
+            $mail->to = $this->to[0][0];
             phpunit_util::phpmailer_sent($mail);
             return true;
         } else {
index ed648c0..0400899 100644 (file)
@@ -2485,4 +2485,36 @@ class core_moodlelib_testcase extends advanced_testcase {
             $event->objectid);
         $this->assertEventLegacyLogData($expectedlogdata, $event);
     }
+
+    public function test_email_to_user() {
+        $this->resetAfterTest();
+
+        $user1 = $this->getDataGenerator()->create_user();
+        $user2 = $this->getDataGenerator()->create_user();
+
+        $subject = 'subject';
+        $messagetext = 'message text';
+        $subject2 = 'subject 2';
+        $messagetext2 = 'message text 2';
+
+        unset_config('noemailever');
+
+        $sink = $this->redirectEmails();
+        email_to_user($user1, $user2, $subject, $messagetext);
+        email_to_user($user2, $user1, $subject2, $messagetext2);
+        $this->assertSame(2, $sink->count());
+        $result = $sink->get_messages();
+        $this->assertCount(2, $result);
+        $sink->close();
+
+        $this->assertSame($subject, $result[0]->subject);
+        $this->assertSame($messagetext, trim($result[0]->body));
+        $this->assertSame($user1->email, $result[0]->to);
+        $this->assertSame($user2->email, $result[0]->from);
+
+        $this->assertSame($subject2, $result[1]->subject);
+        $this->assertSame($messagetext2, trim($result[1]->body));
+        $this->assertSame($user2->email, $result[1]->to);
+        $this->assertSame($user1->email, $result[1]->from);
+    }
 }