Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 39 and 402]

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