Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   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 message_email\task;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  
  21  global $CFG;
  22  
  23  require_once($CFG->dirroot . '/message/tests/messagelib_test.php');
  24  
  25  /**
  26   * Class for testing the send email task.
  27   *
  28   * @package message_email
  29   * @category test
  30   * @copyright 2019 Mark Nelson <markn@moodle.com>
  31   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class send_email_test extends \advanced_testcase {
  34  
  35      /**
  36       * Test sending email task.
  37       */
  38      public function test_sending_email_task() {
  39          global $DB, $SITE;
  40  
  41          $this->preventResetByRollback(); // Messaging is not compatible with transactions.
  42  
  43          $this->resetAfterTest();
  44  
  45          // Create a course.
  46          $course = $this->getDataGenerator()->create_course();
  47  
  48          $user1 = $this->getDataGenerator()->create_and_enrol($course, 'student');
  49          $user2 = $this->getDataGenerator()->create_and_enrol($course, 'student');
  50  
  51          // Create two groups in the course.
  52          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
  53          $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
  54  
  55          groups_add_member($group1->id, $user1->id);
  56          groups_add_member($group2->id, $user1->id);
  57  
  58          groups_add_member($group1->id, $user2->id);
  59          groups_add_member($group2->id, $user2->id);
  60  
  61          $conversation1 = \core_message\api::create_conversation(
  62              \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
  63              [$user1->id, $user2->id],
  64              'Group 1', \core_message\api::MESSAGE_CONVERSATION_ENABLED,
  65              'core_group',
  66              'groups',
  67              $group1->id,
  68              \context_course::instance($course->id)->id
  69          );
  70  
  71          $conversation2 = \core_message\api::create_conversation(
  72              \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
  73              [$user1->id, $user2->id],
  74              'Group 2',
  75              \core_message\api::MESSAGE_CONVERSATION_ENABLED,
  76              'core_group',
  77              'groups',
  78              $group2->id,
  79              \context_course::instance($course->id)->id
  80          );
  81  
  82          // Go through each conversation.
  83          if ($conversations = $DB->get_records('message_conversations')) {
  84              foreach ($conversations as $conversation) {
  85                  $conversationid = $conversation->id;
  86  
  87                  // Let's send 5 messages.
  88                  for ($i = 1; $i <= 5; $i++) {
  89                      $message = new \core\message\message();
  90                      $message->courseid = 1;
  91                      $message->component = 'moodle';
  92                      $message->name = 'instantmessage';
  93                      $message->userfrom = $user1;
  94                      $message->convid = $conversationid;
  95                      $message->subject = 'message subject';
  96                      $message->fullmessage = 'message body';
  97                      $message->fullmessageformat = FORMAT_MARKDOWN;
  98                      $message->fullmessagehtml = '<p>message body</p>';
  99                      $message->smallmessage = 'small message';
 100                      $message->notification = '0';
 101  
 102                      message_send($message);
 103                  }
 104              }
 105          }
 106  
 107          $this->assertEquals(10, $DB->count_records('message_email_messages'));
 108  
 109          // Only 1 email is sent as the messages are included in it at a digest.
 110          $sink = $this->redirectEmails();
 111          $task = new send_email_task();
 112          $task->execute();
 113          $this->assertEquals(1, $sink->count());
 114  
 115          // Confirm it contains the correct data.
 116          $emails = $sink->get_messages();
 117          $email = reset($emails);
 118          $sitename = format_string($SITE->fullname);
 119          $this->assertSame(get_string('messagedigestemailsubject', 'message_email', $sitename), $email->subject);
 120          $this->assertSame($user2->email, $email->to);
 121          $this->assertNotEmpty($email->header);
 122          $emailbody = quoted_printable_decode($email->body);
 123          $this->assertStringContainsString('Group 1', $emailbody);
 124          $this->assertStringContainsString('Group 2', $emailbody);
 125          // 5 unread messages per conversation, this will be listed twice.
 126          $this->assertMatchesRegularExpression("/<span\b[^>]*>5<\/span> <span\b[^>]*>Unread message\w+/", $emailbody);
 127  
 128          // Confirm table was emptied after task was run.
 129          $this->assertEquals(0, $DB->count_records('message_email_messages'));
 130  
 131          // Confirm running it again does not send another.
 132          $sink = $this->redirectEmails();
 133          $task = new send_email_task();
 134          $task->execute();
 135          $this->assertEquals(0, $sink->count());
 136      }
 137  }