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 core_course\task;
  18  
  19  use context_user;
  20  
  21  /**
  22   * Contains tests for course related notifications.
  23   *
  24   * @package    core
  25   * @subpackage course
  26   * @covers     \core_course\task\content_notification_task
  27   * @copyright  2021 Juan Leyva <juan@moodle.com>
  28   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29   */
  30  class content_notification_task_test extends \advanced_testcase {
  31  
  32      /**
  33       * Test execution of task
  34       */
  35      public function test_execute(): void {
  36          global $DB, $CFG, $USER;
  37  
  38          $this->resetAfterTest();
  39          $this->setAdminUser();
  40  
  41          // Create course, with a course image.
  42          $draft = get_file_storage()->create_file_from_pathname([
  43              'component' => 'user',
  44              'filearea' => 'draft',
  45              'contextid' => context_user::instance($USER->id)->id,
  46              'itemid' => file_get_unused_draft_itemid(),
  47              'filename' => 'gd-logo.png',
  48              'filepath' => '/',
  49          ], "{$CFG->libdir}/tests/fixtures/gd-logo.png");
  50  
  51          $course = $this->getDataGenerator()->create_course(['overviewfiles_filemanager' => $draft->get_itemid()]);
  52  
  53          // Enrol couple of students to receive a notification and one unactive enrolment.
  54          $user1 = self::getDataGenerator()->create_user();
  55          $user2 = self::getDataGenerator()->create_user();
  56          $user3 = self::getDataGenerator()->create_user();
  57          self::getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
  58          self::getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
  59          self::getDataGenerator()->enrol_user($user3->id, $course->id, 'student', 'manual', time() - YEARSECS, time() - WEEKSECS);
  60  
  61          $url = self::getDataGenerator()->create_module('url', ['course' => $course]);
  62  
  63          // Test update.
  64          $moduleinfo = $DB->get_record('course_modules', array('id' => $url->cmid));
  65          $moduleinfo->modulename = 'url';
  66          $moduleinfo->coursemodule = $url->cmid;
  67          $moduleinfo->display = 1;
  68          $moduleinfo->externalurl = '';
  69          $moduleinfo->update = 1;
  70          $draftid = 0;
  71          file_prepare_draft_area($draftid, \context_module::instance($url->cmid)->id, 'mod_url', 'intro', 0);
  72          $moduleinfo->introeditor = [
  73              'itemid' => $draftid,
  74              'text' => '<p>Yo</p>',
  75              'format' => FORMAT_HTML
  76          ];
  77          $modurl = (new \moodle_url('/mod/url/view.php', ['id' => $url->cmid]))->out(false);
  78  
  79          // Check course content changed notifications.
  80          $moduleinfo->coursecontentnotification = 1;
  81  
  82          // Create the module.
  83          update_module(clone $moduleinfo);   // We use clone to keep the original object untouch for later use.
  84  
  85          // Redirect messages to sink and stop buffer output from CLI task.
  86          $sink = $this->redirectMessages();
  87          ob_start();
  88          $this->runAdhocTasks('\core_course\task\content_notification_task');
  89          $output = ob_get_contents();
  90          ob_end_clean();
  91          $messages = $sink->get_messages();
  92          $sink->close();
  93  
  94          // We have 3 students, one with a non-active enrolment that should not receive a notification.
  95          $this->assertCount(2, $messages);
  96          foreach ($messages as $message) {
  97              $this->assertEquals('coursecontentupdated', $message->eventtype);
  98              $this->assertEquals($modurl, $message->contexturl);
  99  
 100              $messagecustomdata = json_decode($message->customdata);
 101              $this->assertEquals($course->id, $messagecustomdata->courseid);
 102              $this->assertObjectHasAttribute('notificationiconurl', $messagecustomdata);
 103              $this->assertObjectHasAttribute('notificationpictureurl', $messagecustomdata);
 104          }
 105  
 106          // Now, set the course to not visible.
 107          $DB->set_field('course', 'visible', 0, ['id' => $course->id]);
 108          $this->setAdminUser();
 109          update_module(clone $moduleinfo);
 110  
 111          // Redirect messages to sink and stop buffer output from CLI task.
 112          $sink = $this->redirectMessages();
 113          ob_start();
 114          $this->runAdhocTasks('\core_course\task\content_notification_task');
 115          $output = ob_get_contents();
 116          ob_end_clean();
 117          $messages = $sink->get_messages();
 118          $sink->close();
 119          // No messages, course not visible.
 120          $this->assertCount(0, $messages);
 121  
 122          // Now, set the module to not visible.
 123          $DB->set_field('course', 'visible', 1, ['id' => $course->id]);
 124          $this->setAdminUser();
 125          $moduleinfo->visible = 0;
 126          update_module(clone $moduleinfo);
 127  
 128          // Redirect messages to sink and stop buffer output from CLI task.
 129          $sink = $this->redirectMessages();
 130          ob_start();
 131          $this->runAdhocTasks('\core_course\task\content_notification_task');
 132          $output = ob_get_contents();
 133          ob_end_clean();
 134          $messages = $sink->get_messages();
 135          $sink->close();
 136          // No messages, module not visible.
 137          $this->assertCount(0, $messages);
 138      }
 139  }