Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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

   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  defined('MOODLE_INTERNAL') || exit();
  17  
  18  /**
  19   * Unit tests for the tool_monitor clean events task.
  20   * @since 3.2.0
  21   *
  22   * @package    tool_monitor
  23   * @category   test
  24   * @copyright  2016 Jake Dallimore <jrhdallimore@gmail.com>
  25   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  class tool_monitor_task_check_subscriptions_testcase extends advanced_testcase {
  28  
  29      private $course;
  30      private $user;
  31      private $rule;
  32      private $subscription;
  33      private $teacherrole;
  34      private $studentrole;
  35  
  36      /**
  37       * Test set up.
  38       */
  39      public function setUp(): void {
  40          global $DB;
  41          set_config('enablemonitor', 1, 'tool_monitor');
  42          $this->resetAfterTest(true);
  43  
  44          // All tests defined herein need a user, course, rule and subscription, so set these up.
  45          $this->user = $this->getDataGenerator()->create_user();
  46          $this->course = $this->getDataGenerator()->create_course();
  47  
  48          $rule = new stdClass();
  49          $rule->userid = 2; // Rule created by admin.
  50          $rule->courseid = $this->course->id;
  51          $rule->plugin = 'mod_book';
  52          $rule->eventname = '\mod_book\event\course_module_viewed';
  53          $rule->timewindow = 500;
  54          $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
  55          $this->rule = $monitorgenerator->create_rule($rule);
  56  
  57          $sub = new stdClass();
  58          $sub->courseid = $this->course->id;
  59          $sub->userid = $this->user->id;
  60          $sub->ruleid = $this->rule->id;
  61          $this->subscription = $monitorgenerator->create_subscription($sub);
  62  
  63          // Also set up a student and a teacher role for use in some tests.
  64          $this->teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
  65          $this->studentrole = $DB->get_record('role', array('shortname' => 'student'));
  66      }
  67  
  68      /**
  69       * Reloads the subscription object from the DB.
  70       *
  71       * @return void.
  72       */
  73      private function reload_subscription() {
  74          global $DB;
  75          $sub = $DB->get_record('tool_monitor_subscriptions', array('id' => $this->subscription->id));
  76          $this->subscription = new \tool_monitor\subscription($sub);
  77      }
  78  
  79      /**
  80       * Test to confirm the task is named correctly.
  81       */
  82      public function test_task_name() {
  83          $task = new \tool_monitor\task\check_subscriptions();
  84          $this->assertEquals(get_string('taskchecksubscriptions', 'tool_monitor'), $task->get_name());
  85      }
  86  
  87      /**
  88       * Test to confirm that site level subscriptions are activated and deactivated according to system capabilities.
  89       */
  90      public function test_site_level_subscription() {
  91          // Create a site level subscription.
  92          $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
  93          $sub = new stdClass();
  94          $sub->userid = $this->user->id;
  95          $sub->ruleid = $this->rule->id;
  96          $this->subscription = $monitorgenerator->create_subscription($sub);
  97  
  98          // Run the task.
  99          $task = new \tool_monitor\task\check_subscriptions();
 100          $task->execute();
 101  
 102          // The subscription should be inactive as the user doesn't have the capability. Pass in the id only to refetch the data.
 103          $this->reload_subscription();
 104          $this->assertEquals(false, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
 105  
 106          // Now, assign the user as a teacher role at system context.
 107          $this->getDataGenerator()->role_assign($this->teacherrole->id, $this->user->id, context_system::instance());
 108  
 109          // Run the task.
 110          $task = new \tool_monitor\task\check_subscriptions();
 111          $task->execute();
 112  
 113          // The subscription should be active now. Pass in the id only to refetch the data.
 114          $this->reload_subscription();
 115          $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
 116      }
 117  
 118      /**
 119       * Test to confirm that if the module is disabled, no changes are made to active subscriptions.
 120       */
 121      public function test_module_disabled() {
 122          set_config('enablemonitor', 0, 'tool_monitor');
 123  
 124          // Subscription should be active to start with.
 125          $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
 126  
 127          // Run the task. Note, we never enrolled the user.
 128          $task = new \tool_monitor\task\check_subscriptions();
 129          $task->execute();
 130  
 131          // The subscription should still be active. Pass in the id only to refetch the data.
 132          $this->reload_subscription();
 133          $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
 134      }
 135  
 136      /**
 137       * Test to confirm an active, valid subscription stays active once the scheduled task is run.
 138       */
 139      public function test_active_unaffected() {
 140          // Enrol the user as a teacher. This role should have the required capability.
 141          $this->getDataGenerator()->enrol_user($this->user->id, $this->course->id, $this->teacherrole->id);
 142  
 143          // Subscription should be active to start with.
 144          $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
 145  
 146          // Run the task.
 147          $task = new \tool_monitor\task\check_subscriptions();
 148          $task->execute();
 149  
 150          // The subscription should still be active. Pass in the id only to refetch the data.
 151          $this->reload_subscription();
 152          $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
 153      }
 154  
 155      /**
 156       * Test to confirm that a subscription for a user without an enrolment to the course is made inactive.
 157       */
 158      public function test_course_enrolment() {
 159          // Subscription should be active until deactivated by the scheduled task. Remember, by default the test setup
 160          // doesn't enrol the user, so the first run of the task should deactivate it.
 161          $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
 162  
 163          // Run the task.
 164          $task = new \tool_monitor\task\check_subscriptions();
 165          $task->execute();
 166  
 167          // The subscription should NOT be active. Pass in the id only to refetch the data.
 168          $this->reload_subscription();
 169          $this->assertEquals(false, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
 170  
 171          // Enrol the user.
 172          $this->getDataGenerator()->enrol_user($this->user->id, $this->course->id, $this->teacherrole->id);
 173  
 174          // Run the task.
 175          $task = new \tool_monitor\task\check_subscriptions();
 176          $task->execute();
 177  
 178          // Subscription should now be active again.
 179          $this->reload_subscription();
 180          $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
 181      }
 182  
 183      /**
 184       * Test to confirm that subscriptions for enrolled users without the required capability are made inactive.
 185       */
 186      public function test_enrolled_user_with_no_capability() {
 187          // Enrol the user. By default, students won't have the required capability.
 188          $this->getDataGenerator()->enrol_user($this->user->id, $this->course->id, $this->studentrole->id);
 189  
 190          // The subscription should be active to start with. Pass in the id only to refetch the data.
 191          $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
 192  
 193          // Run the task.
 194          $task = new \tool_monitor\task\check_subscriptions();
 195          $task->execute();
 196  
 197          // The subscription should NOT be active. Pass in the id only to refetch the data.
 198          $this->reload_subscription();
 199          $this->assertEquals(false, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
 200      }
 201  
 202      /**
 203       * Test to confirm that subscriptions for users who fail can_access_course(), are deactivated.
 204       */
 205      public function test_can_access_course() {
 206          // Enrol the user as a teacher. This role should have the required capability.
 207          $this->getDataGenerator()->enrol_user($this->user->id, $this->course->id, $this->teacherrole->id);
 208  
 209          // Strip the ability to see hidden courses, so we'll fail the check_subscriptions->user_can_access_course call.
 210          $context = \context_course::instance($this->course->id);
 211          assign_capability('moodle/course:viewhiddencourses', CAP_PROHIBIT, $this->teacherrole->id, $context);
 212  
 213          // Subscription should be active to start with.
 214          $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
 215  
 216          // Hide the course.
 217          course_change_visibility($this->course->id, false);
 218  
 219          // Run the task.
 220          $task = new \tool_monitor\task\check_subscriptions();
 221          $task->execute();
 222  
 223          // The subscription should be inactive. Pass in the id only to refetch the data.
 224          $this->reload_subscription();
 225          $this->assertEquals(false, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
 226      }
 227  
 228      /**
 229       * Test to confirm that subscriptions for enrolled users who don't have CM access, are deactivated.
 230       */
 231      public function test_cm_access() {
 232          // Enrol the user as a student but grant to ability to subscribe. Students cannot view hidden activities.
 233          $context = \context_course::instance($this->course->id);
 234          assign_capability('tool/monitor:subscribe', CAP_ALLOW, $this->studentrole->id, $context);
 235          $this->getDataGenerator()->enrol_user($this->user->id, $this->course->id, $this->studentrole->id);
 236  
 237          // Generate a course module.
 238          $book = $this->getDataGenerator()->create_module('book', array('course' => $this->course->id));
 239  
 240          // And add a subscription to it.
 241          $sub = new stdClass();
 242          $sub->courseid = $this->course->id;
 243          $sub->userid = $this->user->id;
 244          $sub->ruleid = $this->rule->id;
 245          $sub->cmid = $book->cmid;
 246          $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
 247          $this->subscription = $monitorgenerator->create_subscription($sub);
 248  
 249          // The subscription should be active to start with. Pass in the id only to refetch the data.
 250          $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
 251  
 252          // Run the task.
 253          $task = new \tool_monitor\task\check_subscriptions();
 254          $task->execute();
 255  
 256          // The subscription should still be active. Pass in the id only to refetch the data.
 257          $this->reload_subscription();
 258          $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
 259  
 260          // Make the course module invisible, which should in turn make the subscription inactive.
 261          set_coursemodule_visible($book->cmid, false);
 262  
 263          // Run the task.
 264          $task = new \tool_monitor\task\check_subscriptions();
 265          $task->execute();
 266  
 267          // The subscription should NOT be active. Pass in the id only to refetch the data.
 268          $this->reload_subscription();
 269          $this->assertEquals(false, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
 270  
 271          // Make the course module visible again.
 272          set_coursemodule_visible($book->cmid, true);
 273  
 274          // Run the task.
 275          $task = new \tool_monitor\task\check_subscriptions();
 276          $task->execute();
 277  
 278          // The subscription should be active. Pass in the id only to refetch the data.
 279          $this->reload_subscription();
 280          $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
 281      }
 282  
 283      /**
 284       * Test to confirm that long term inactive subscriptions are removed entirely.
 285       */
 286      public function test_stale_subscription_removal() {
 287          global $DB;
 288          // Manually set the inactivedate to 1 day older than the limit allowed.
 289          $daysold = 1 + \tool_monitor\subscription_manager::INACTIVE_SUBSCRIPTION_LIFESPAN_IN_DAYS;
 290  
 291          $inactivedate = strtotime("-$daysold days", time());
 292          $DB->set_field('tool_monitor_subscriptions', 'inactivedate', $inactivedate, array('id' => $this->subscription->id));
 293  
 294          // Subscription should be inactive to start with.
 295          $this->reload_subscription();
 296          $this->assertEquals(false, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
 297  
 298          // Run the task.
 299          $task = new \tool_monitor\task\check_subscriptions();
 300          $task->execute();
 301  
 302          // Subscription should now not exist at all.
 303          $this->assertEquals(false, $DB->record_exists('tool_monitor_subscriptions', array('id' => $this->subscription->id)));
 304      }
 305  
 306      /**
 307       * Test to confirm that subscriptions for a partially set up user are deactivated.
 308       */
 309      public function test_user_not_fully_set_up() {
 310          global $DB;
 311  
 312          // Enrol the user as a teacher.
 313          $this->getDataGenerator()->enrol_user($this->user->id, $this->course->id, $this->teacherrole->id);
 314  
 315          // The subscription should be active to start.
 316          $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
 317  
 318          // Unset the user's email address, so we fail the check_subscriptions->is_user_setup() call.
 319          $DB->set_field('user', 'email', '', array('id' => $this->user->id));
 320  
 321          // Run the task.
 322          $task = new \tool_monitor\task\check_subscriptions();
 323          $task->execute();
 324  
 325          // The subscription should now be inactive.
 326          $this->reload_subscription();
 327          $this->assertEquals(false, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
 328      }
 329  
 330      /**
 331       * Test to confirm that a suspended user's subscriptions are deactivated properly.
 332       */
 333      public function test_suspended_user() {
 334          global $DB;
 335  
 336          // Enrol the user as a teacher. This role should have the required capability.
 337          $this->getDataGenerator()->enrol_user($this->user->id, $this->course->id, $this->teacherrole->id);
 338  
 339          // Subscription should be active to start with.
 340          $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
 341  
 342          // Suspend the user.
 343          $DB->set_field('user', 'suspended', '1', array('id' => $this->user->id));
 344  
 345          // Run the task.
 346          $task = new \tool_monitor\task\check_subscriptions();
 347          $task->execute();
 348  
 349          // The subscription should now be inactive.
 350          $this->reload_subscription();
 351          $this->assertEquals(false, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
 352  
 353          // Unsuspend the user.
 354          $DB->set_field('user', 'suspended', '0', array('id' => $this->user->id));
 355  
 356          // Run the task.
 357          $task = new \tool_monitor\task\check_subscriptions();
 358          $task->execute();
 359  
 360          // The subscription should now be active again.
 361          $this->reload_subscription();
 362          $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription));
 363      }
 364  }