protected function PostSend() { // Now ask phpunit if it wants to catch this message. 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 { return parent::PostSend(); } }
public function test_message_attachment_send() { global $CFG; $this->preventResetByRollback(); $this->resetAfterTest(); // Set config setting to allow attachments. $CFG->allowattachments = true; unset_config('noemailever'); $user = $this->getDataGenerator()->create_user(); $context = context_user::instance($user->id); // Create a test file. $fs = get_file_storage(); $filerecord = array('contextid' => $context->id, 'component' => 'core', 'filearea' => 'unittest', 'itemid' => 99999, 'filepath' => '/', 'filename' => 'emailtest.txt'); $file = $fs->create_file_from_string($filerecord, 'Test content'); $message = new stdClass(); $message->component = 'moodle'; $message->name = 'instantmessage'; $message->userfrom = get_admin(); $message->userto = $user; $message->subject = 'message subject 1'; $message->fullmessage = 'message body'; $message->fullmessageformat = FORMAT_MARKDOWN; $message->fullmessagehtml = '<p>message body</p>'; $message->smallmessage = 'small message'; $message->attachment = $file; $message->attachname = 'emailtest.txt'; $message->notification = 0; // Make sure we are redirecting emails. $sink = $this->redirectEmails(); $this->assertTrue(phpunit_util::is_redirecting_phpmailer()); message_send($message); // Get the email that we just sent. $emails = $sink->get_messages(); $email = reset($emails); $this->assertTrue(strpos($email->body, 'Content-Disposition: attachment;') !== false); $this->assertTrue(strpos($email->body, 'emailtest.txt') !== false); // Check if the stored file still exists after remove the temporary attachment. $storedfileexists = $fs->file_exists($filerecord['contextid'], $filerecord['component'], $filerecord['filearea'], $filerecord['itemid'], $filerecord['filepath'], $filerecord['filename']); $this->assertTrue($storedfileexists); }