MDL-53674 event monitor: Do not send HTML as plain text formatted emails
authorDavid Mudrák <david@moodle.com>
Thu, 31 Mar 2016 13:42:06 +0000 (15:42 +0200)
committerDavid Mudrák <david@moodle.com>
Fri, 8 Apr 2016 13:14:23 +0000 (15:14 +0200)
There were two problems here.

* The $msgdata->fullmessage was always formatted as HTML even if the
  $msgdata->fullmessageformat was set to the original template's format.
* The plain text part of the emailed message contained HTML code.

The patch makes sure that the original template is always formatted into
HTML and then converted to plain text before setting the text/plain
multipart of the sent email.

admin/tool/monitor/classes/notification_task.php
admin/tool/monitor/tests/eventobservers_test.php

index 150129c..6e36841 100644 (file)
@@ -82,15 +82,16 @@ class notification_task extends \core\task\adhoc_task {
 
         $template = $subscription->template;
         $template = $this->replace_placeholders($template, $subscription, $eventobj, $context);
 
         $template = $subscription->template;
         $template = $this->replace_placeholders($template, $subscription, $eventobj, $context);
+        $htmlmessage = format_text($template, $subscription->templateformat, array('context' => $context));
         $msgdata = new \stdClass();
         $msgdata->component         = 'tool_monitor'; // Your component name.
         $msgdata->name              = 'notification'; // This is the message name from messages.php.
         $msgdata->userfrom          = \core_user::get_noreply_user();
         $msgdata->userto            = $user;
         $msgdata->subject           = $subscription->get_name($context);
         $msgdata = new \stdClass();
         $msgdata->component         = 'tool_monitor'; // Your component name.
         $msgdata->name              = 'notification'; // This is the message name from messages.php.
         $msgdata->userfrom          = \core_user::get_noreply_user();
         $msgdata->userto            = $user;
         $msgdata->subject           = $subscription->get_name($context);
-        $msgdata->fullmessage       = format_text($template, $subscription->templateformat, array('context' => $context));
-        $msgdata->fullmessageformat = $subscription->templateformat;
-        $msgdata->fullmessagehtml   = format_text($template, $subscription->templateformat, array('context' => $context));
+        $msgdata->fullmessage       = html_to_text($htmlmessage);
+        $msgdata->fullmessageformat = FORMAT_PLAIN;
+        $msgdata->fullmessagehtml   = $htmlmessage;
         $msgdata->smallmessage      = '';
         $msgdata->notification      = 1; // This is only set to 0 for personal messages between users.
 
         $msgdata->smallmessage      = '';
         $msgdata->notification      = 1; // This is only set to 0 for personal messages between users.
 
index 79356c6..9239e16 100644 (file)
@@ -467,7 +467,13 @@ class tool_monitor_eventobservers_testcase extends advanced_testcase {
         $rulerecord->eventname = '\mod_book\event\course_module_viewed';
         $rulerecord->cmid = $book->cmid;
         $rulerecord->frequency = 1;
         $rulerecord->eventname = '\mod_book\event\course_module_viewed';
         $rulerecord->cmid = $book->cmid;
         $rulerecord->frequency = 1;
-        $rulerecord->template = '{link} {modulelink} {rulename} {description} {eventname}';
+        $rulerecord->template = '## {link} ##
+
+* {modulelink}
+* __{rulename}__
+* {description}
+* {eventname}';
+        $rulerecord->templateformat = FORMAT_MARKDOWN;
 
         $rule = $toolgenerator->create_rule($rulerecord);
 
 
         $rule = $toolgenerator->create_rule($rulerecord);
 
@@ -491,13 +497,21 @@ class tool_monitor_eventobservers_testcase extends advanced_testcase {
         $msg = array_pop($msgs);
 
         $modurl = new moodle_url('/mod/book/view.php', array('id' => $book->cmid));
         $msg = array_pop($msgs);
 
         $modurl = new moodle_url('/mod/book/view.php', array('id' => $book->cmid));
-        $expectedmsg = $event->get_url()->out() . ' ' .
-                        $modurl->out()  . ' ' .
-                        $rule->get_name($context) . ' ' .
-                        $rule->get_description($context) . ' ' .
-                        $rule->get_event_name();
 
 
-        $this->assertEquals($expectedmsg, $msg->fullmessage);
+        $this->assertContains('<h2>'.$event->get_url()->out().'</h2>', $msg->fullmessagehtml);
+        $this->assertContains('<li>'.$modurl->out().'</li>', $msg->fullmessagehtml);
+        $this->assertContains('<li><strong>'.$rule->get_name($context).'</strong></li>', $msg->fullmessagehtml);
+        $this->assertContains('<li>'.$rule->get_description($context).'</li>', $msg->fullmessagehtml);
+        $this->assertContains('<li>'.$rule->get_event_name().'</li>', $msg->fullmessagehtml);
+
+        $this->assertEquals(FORMAT_PLAIN, $msg->fullmessageformat);
+        $this->assertNotContains('<h2>', $msg->fullmessage);
+        $this->assertNotContains('##', $msg->fullmessage);
+        $this->assertContains(strtoupper($event->get_url()->out()), $msg->fullmessage);
+        $this->assertContains('* '.$modurl->out(), $msg->fullmessage);
+        $this->assertContains('* '.strtoupper($rule->get_name($context)), $msg->fullmessage);
+        $this->assertContains('* '.$rule->get_description($context), $msg->fullmessage);
+        $this->assertContains('* '.$rule->get_event_name(), $msg->fullmessage);
     }
 
     /**
     }
 
     /**