Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  namespace core;
  18  
  19  /**
  20   * Test script for message class.
  21   *
  22   * Test classes for \core\message\message.
  23   *
  24   * @package core
  25   * @category test
  26   * @copyright 2015 onwards Ankit Agarwal
  27   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  28   */
  29  class message_test extends \advanced_testcase {
  30  
  31      /**
  32       * Test the method get_eventobject_for_processor().
  33       */
  34      public function test_get_eventobject_for_processor() {
  35          global $USER;
  36          $this->resetAfterTest();
  37          $this->setAdminUser();
  38  
  39          $user = $this->getDataGenerator()->create_user();
  40  
  41          $message = new \core\message\message();
  42          $message->courseid = SITEID;
  43          $message->component = 'moodle';
  44          $message->name = 'instantmessage';
  45          $message->userfrom = $USER;
  46          $message->userto = $user;
  47          $message->subject = 'message subject 1';
  48          $message->fullmessage = 'message body';
  49          $message->fullmessageformat = FORMAT_MARKDOWN;
  50          $message->fullmessagehtml = '<p>message body</p>';
  51          $message->smallmessage = 'small message';
  52          $message->notification = '0';
  53          $message->contexturl = 'http://GalaxyFarFarAway.com';
  54          $message->contexturlname = 'Context name';
  55          $message->replyto = "random@example.com";
  56          $message->replytoname = fullname($USER);
  57          $message->attachname = 'attachment';
  58          $content = array('*' => array('header' => ' test ', 'footer' => ' test ')); // Extra content for all types of messages.
  59          $message->set_additional_content('test', $content);
  60  
  61          // Create a file instance.
  62          $usercontext = \context_user::instance($user->id);
  63          $file = new \stdClass;
  64          $file->contextid = $usercontext->id;
  65          $file->component = 'user';
  66          $file->filearea  = 'private';
  67          $file->itemid    = 0;
  68          $file->filepath  = '/';
  69          $file->filename  = '1.txt';
  70          $file->source    = 'test';
  71  
  72          $fs = get_file_storage();
  73          $file = $fs->create_file_from_string($file, 'file1 content');
  74          $message->attachment = $file;
  75  
  76          $stdClass = $message->get_eventobject_for_processor('test');
  77  
  78          $this->assertSame($message->courseid, $stdClass->courseid);
  79          $this->assertSame($message->component, $stdClass->component);
  80          $this->assertSame($message->name, $stdClass->name);
  81          $this->assertSame($message->userfrom, $stdClass->userfrom);
  82          $this->assertSame($message->userto, $stdClass->userto);
  83          $this->assertSame($message->subject, $stdClass->subject);
  84          $this->assertSame(' test ' . $message->fullmessage . ' test ', $stdClass->fullmessage);
  85          $this->assertSame(' test ' . $message->fullmessagehtml . ' test ', $stdClass->fullmessagehtml);
  86          $this->assertSame(' test ' . $message->smallmessage . ' test ', $stdClass->smallmessage);
  87          $this->assertSame($message->notification, $stdClass->notification);
  88          $this->assertSame($message->contexturl, $stdClass->contexturl);
  89          $this->assertSame($message->contexturlname, $stdClass->contexturlname);
  90          $this->assertSame($message->replyto, $stdClass->replyto);
  91          $this->assertSame($message->replytoname, $stdClass->replytoname);
  92          $this->assertSame($message->attachname, $stdClass->attachname);
  93  
  94          // Extra content for fullmessage only.
  95          $content = array('fullmessage' => array('header' => ' test ', 'footer' => ' test '));
  96          $message->set_additional_content('test', $content);
  97          $stdClass = $message->get_eventobject_for_processor('test');
  98          $this->assertSame(' test ' . $message->fullmessage . ' test ', $stdClass->fullmessage);
  99          $this->assertSame($message->fullmessagehtml, $stdClass->fullmessagehtml);
 100          $this->assertSame($message->smallmessage, $stdClass->smallmessage);
 101  
 102          // Extra content for fullmessagehtml and smallmessage only.
 103          $content = array('fullmessagehtml' => array('header' => ' test ', 'footer' => ' test '),
 104                           'smallmessage' => array('header' => ' testsmall ', 'footer' => ' testsmall '));
 105          $message->set_additional_content('test', $content);
 106          $stdClass = $message->get_eventobject_for_processor('test');
 107          $this->assertSame($message->fullmessage, $stdClass->fullmessage);
 108          $this->assertSame(' test ' . $message->fullmessagehtml . ' test ', $stdClass->fullmessagehtml);
 109          $this->assertSame(' testsmall ' . $message->smallmessage . ' testsmall ', $stdClass->smallmessage);
 110  
 111          // Extra content for * and smallmessage.
 112          $content = array('*' => array('header' => ' test ', 'footer' => ' test '),
 113                           'smallmessage' => array('header' => ' testsmall ', 'footer' => ' testsmall '));
 114          $message->set_additional_content('test', $content);
 115          $stdClass = $message->get_eventobject_for_processor('test');
 116          $this->assertSame(' test ' . $message->fullmessage . ' test ', $stdClass->fullmessage);
 117          $this->assertSame(' test ' . $message->fullmessagehtml . ' test ', $stdClass->fullmessagehtml);
 118          $this->assertSame(' testsmall ' . ' test ' .  $message->smallmessage . ' test ' . ' testsmall ', $stdClass->smallmessage);
 119      }
 120  
 121      /**
 122       * Test sending messages as email works with the new class.
 123       */
 124      public function test_send_message() {
 125          global $DB, $CFG;
 126          $this->preventResetByRollback();
 127          $this->resetAfterTest();
 128  
 129          $user1 = $this->getDataGenerator()->create_user(array('maildisplay' => 1));
 130          $user2 = $this->getDataGenerator()->create_user();
 131          set_config('allowedemaildomains', 'example.com');
 132  
 133          // Test basic email processor.
 134          $this->assertFileExists("$CFG->dirroot/message/output/email/version.php");
 135          $this->assertFileExists("$CFG->dirroot/message/output/popup/version.php");
 136  
 137          $DB->set_field_select('message_processors', 'enabled', 0, "name <> 'email'");
 138          set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'email', $user2);
 139  
 140          // Extra content for all types of messages.
 141          $message = new \core\message\message();
 142          $message->courseid          = 1;
 143          $message->component         = 'moodle';
 144          $message->name              = 'instantmessage';
 145          $message->userfrom          = $user1;
 146          $message->userto            = $user2;
 147          $message->subject           = 'message subject 1';
 148          $message->fullmessage       = 'message body';
 149          $message->fullmessageformat = FORMAT_MARKDOWN;
 150          $message->fullmessagehtml   = '<p>message body</p>';
 151          $message->smallmessage      = 'small message';
 152          $message->notification      = '0';
 153          $content = array('*' => array('header' => ' test ', 'footer' => ' test '));
 154          $message->set_additional_content('email', $content);
 155  
 156          $sink = $this->redirectEmails();
 157          $messageid = message_send($message);
 158          $emails = $sink->get_messages();
 159          $this->assertCount(1, $emails);
 160          $email = reset($emails);
 161          $recordexists = $DB->record_exists('messages', array('id' => $messageid));
 162          $this->assertSame(true, $recordexists);
 163          $this->assertSame($user1->email, $email->from);
 164          $this->assertSame($user2->email, $email->to);
 165          $this->assertSame(get_string('unreadnewmessage', 'message', fullname($user1)), $email->subject);
 166          $this->assertNotEmpty($email->header);
 167          $this->assertNotEmpty($email->body);
 168          $this->assertMatchesRegularExpression('/test.*message body.*test/s', $email->body);
 169          $sink->clear();
 170  
 171          // Test that event fired includes the courseid.
 172          $eventsink = $this->redirectEvents();
 173          $messageid = message_send($message);
 174          $events = $eventsink->get_events();
 175          $event = reset($events);
 176          $this->assertEquals($message->courseid, $event->other['courseid']);
 177          $eventsink->clear();
 178          $sink->clear();
 179  
 180          // Extra content for small message only. Shouldn't show up in emails as we sent fullmessage and fullmessagehtml only in
 181          // the emails.
 182          $message = new \core\message\message();
 183          $message->courseid          = 1;
 184          $message->component         = 'moodle';
 185          $message->name              = 'instantmessage';
 186          $message->userfrom          = $user1;
 187          $message->userto            = $user2;
 188          $message->subject           = 'message subject 1';
 189          $message->fullmessage       = 'message body';
 190          $message->fullmessageformat = FORMAT_MARKDOWN;
 191          $message->fullmessagehtml   = '<p>message body</p>';
 192          $message->smallmessage      = 'small message';
 193          $message->notification      = '0';
 194          $content = array('smallmessage' => array('header' => ' test ', 'footer' => ' test '));
 195          $message->set_additional_content('email', $content);
 196  
 197          $messageid = message_send($message);
 198          $emails = $sink->get_messages();
 199          $this->assertCount(1, $emails);
 200          $email = reset($emails);
 201          $recordexists = $DB->record_exists('messages', array('id' => $messageid));
 202          $this->assertSame(true, $recordexists);
 203          $this->assertSame($user1->email, $email->from);
 204          $this->assertSame($user2->email, $email->to);
 205          $this->assertSame(get_string('unreadnewmessage', 'message', fullname($user1)), $email->subject);
 206          $this->assertNotEmpty($email->header);
 207          $this->assertNotEmpty($email->body);
 208          $this->assertDoesNotMatchRegularExpression('/test.*message body test/', $email->body);
 209  
 210          // Test that event fired includes the courseid.
 211          $eventsink = $this->redirectEvents();
 212          $messageid = message_send($message);
 213          $events = $eventsink->get_events();
 214          $event = reset($events);
 215          $this->assertEquals($message->courseid, $event->other['courseid']);
 216          $eventsink->close();
 217          $sink->close();
 218      }
 219  
 220      public function test_send_message_with_prefix() {
 221          global $DB, $CFG;
 222          $this->preventResetByRollback();
 223          $this->resetAfterTest();
 224  
 225          $user1 = $this->getDataGenerator()->create_user(array('maildisplay' => 1));
 226          $user2 = $this->getDataGenerator()->create_user();
 227          set_config('allowedemaildomains', 'example.com');
 228          set_config('emailsubjectprefix', '[Prefix Text]');
 229  
 230          // Test basic email processor.
 231          $this->assertFileExists("$CFG->dirroot/message/output/email/version.php");
 232          $this->assertFileExists("$CFG->dirroot/message/output/popup/version.php");
 233  
 234          $DB->set_field_select('message_processors', 'enabled', 0, "name <> 'email'");
 235          set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'email', $user2);
 236  
 237          // Check that prefix is ammended to the subject of the email.
 238          $message = new \core\message\message();
 239          $message->courseid = 1;
 240          $message->component = 'moodle';
 241          $message->name = 'instantmessage';
 242          $message->userfrom = $user1;
 243          $message->userto = $user2;
 244          $message->subject = get_string('unreadnewmessage', 'message', fullname($user1));
 245          $message->fullmessage = 'message body';
 246          $message->fullmessageformat = FORMAT_MARKDOWN;
 247          $message->fullmessagehtml = '<p>message body</p>';
 248          $message->smallmessage = 'small message';
 249          $message->notification = '0';
 250          $content = array('*' => array('header' => ' test ', 'footer' => ' test '));
 251          $message->set_additional_content('email', $content);
 252          $sink = $this->redirectEmails();
 253          $messageid = message_send($message);
 254          $emails = $sink->get_messages();
 255          $this->assertCount(1, $emails);
 256          $email = reset($emails);
 257          $this->assertSame('[Prefix Text] '. get_string('unreadnewmessage', 'message', fullname($user1)), $email->subject);
 258          $sink->clear();
 259      }
 260  }