From ea106d2a1dbc633a22f12a119763b2af5f33542b Mon Sep 17 00:00:00 2001 From: Jake Dallimore Date: Wed, 25 May 2016 14:50:29 +0800 Subject: [PATCH] MDL-53431 tool_monitor: Access control for tool monitor subscriptions --- admin/tool/monitor/classes/eventobservers.php | 4 + admin/tool/monitor/classes/subscription.php | 13 +- .../monitor/classes/subscription_manager.php | 78 ++++ .../classes/task/check_subscriptions.php | 274 +++++++++++++ admin/tool/monitor/db/install.xml | 1 + admin/tool/monitor/db/tasks.php | 9 + admin/tool/monitor/db/upgrade.php | 15 + admin/tool/monitor/lang/en/tool_monitor.php | 1 + .../tool/monitor/tests/subscription_test.php | 65 ++++ .../tests/task_check_subscriptions_test.php | 364 ++++++++++++++++++ admin/tool/monitor/version.php | 2 +- 11 files changed, 823 insertions(+), 3 deletions(-) create mode 100644 admin/tool/monitor/classes/task/check_subscriptions.php create mode 100644 admin/tool/monitor/tests/subscription_test.php create mode 100644 admin/tool/monitor/tests/task_check_subscriptions_test.php diff --git a/admin/tool/monitor/classes/eventobservers.php b/admin/tool/monitor/classes/eventobservers.php index c11665e57fa..793b11f315b 100644 --- a/admin/tool/monitor/classes/eventobservers.php +++ b/admin/tool/monitor/classes/eventobservers.php @@ -140,6 +140,10 @@ class eventobservers { $subscriptions = subscription_manager::get_subscriptions_by_event($eventobj); $idstosend = array(); foreach ($subscriptions as $subscription) { + // Only proceed to fire events and notifications if the subscription is active. + if (!subscription_manager::subscription_is_active($subscription)) { + continue; + } $starttime = $now - $subscription->timewindow; $starttime = ($starttime > $subscription->lastnotificationsent) ? $starttime : $subscription->lastnotificationsent; if ($subscription->courseid == 0) { diff --git a/admin/tool/monitor/classes/subscription.php b/admin/tool/monitor/classes/subscription.php index 6e25f6ed084..98518e9a4df 100644 --- a/admin/tool/monitor/classes/subscription.php +++ b/admin/tool/monitor/classes/subscription.php @@ -55,17 +55,26 @@ class subscription { * Magic get method. * * @param string $prop property to get. - * * @return mixed * @throws \coding_exception */ public function __get($prop) { - if (property_exists($this->subscription, $prop)) { + if (isset($this->subscription->$prop)) { return $this->subscription->$prop; } throw new \coding_exception('Property "' . $prop . '" doesn\'t exist'); } + /** + * Magic isset method. + * + * @param string $prop the property to get. + * @return bool true if the property is set, false otherwise. + */ + public function __isset($prop) { + return property_exists($this->subscription, $prop); + } + /** * Get a human readable name for instances associated with this subscription. * diff --git a/admin/tool/monitor/classes/subscription_manager.php b/admin/tool/monitor/classes/subscription_manager.php index 421984caf6a..c4382e79a7f 100644 --- a/admin/tool/monitor/classes/subscription_manager.php +++ b/admin/tool/monitor/classes/subscription_manager.php @@ -35,6 +35,10 @@ defined('MOODLE_INTERNAL') || die(); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class subscription_manager { + + /** @const Period of time, in days, after which an inactive subscription will be removed completely.*/ + const INACTIVE_SUBSCRIPTION_LIFESPAN_IN_DAYS = 30; + /** * Subscribe a user to a given rule. * @@ -456,4 +460,78 @@ class subscription_manager { return false; } + + /** + * Activates a group of subscriptions based on an input array of ids. + * + * @since 3.2.0 + * @param array $ids of subscription ids. + * @return bool true if the operation was successful, false otherwise. + */ + public static function activate_subscriptions(array $ids) { + global $DB; + if (!empty($ids)) { + list($sql, $params) = $DB->get_in_or_equal($ids); + $success = $DB->set_field_select('tool_monitor_subscriptions', 'inactivedate', '0', 'id ' . $sql, $params); + return $success; + } + return false; + } + + /** + * Deactivates a group of subscriptions based on an input array of ids. + * + * @since 3.2.0 + * @param array $ids of subscription ids. + * @return bool true if the operation was successful, false otherwise. + */ + public static function deactivate_subscriptions(array $ids) { + global $DB; + if (!empty($ids)) { + $inactivedate = time(); + list($sql, $params) = $DB->get_in_or_equal($ids); + $success = $DB->set_field_select('tool_monitor_subscriptions', 'inactivedate', $inactivedate, 'id ' . $sql, + $params); + return $success; + } + return false; + } + + /** + * Deletes subscriptions which have been inactive for a period of time. + * + * @since 3.2.0 + * @param int $userid if provided, only this user's stale subscriptions will be deleted. + * @return bool true if the operation was successful, false otherwise. + */ + public static function delete_stale_subscriptions($userid = 0) { + global $DB; + // Get the expiry duration, in days. + $cutofftime = strtotime("-" . self::INACTIVE_SUBSCRIPTION_LIFESPAN_IN_DAYS . " days", time()); + + if (!empty($userid)) { + // Remove any stale subscriptions for the desired user only. + $success = $DB->delete_records_select('tool_monitor_subscriptions', + 'userid = ? AND inactivedate < ? AND inactivedate <> 0', + array($userid, $cutofftime)); + + } else { + // Remove all stale subscriptions. + $success = $DB->delete_records_select('tool_monitor_subscriptions', + 'inactivedate < ? AND inactivedate <> 0', + array($cutofftime)); + } + return $success; + } + + /** + * Check whether a subscription is active. + * + * @since 3.2.0 + * @param \tool_monitor\subscription $subscription instance. + * @return bool true if the subscription is active, false otherwise. + */ + public static function subscription_is_active(subscription $subscription) { + return empty($subscription->inactivedate); + } } diff --git a/admin/tool/monitor/classes/task/check_subscriptions.php b/admin/tool/monitor/classes/task/check_subscriptions.php new file mode 100644 index 00000000000..8262f102f53 --- /dev/null +++ b/admin/tool/monitor/classes/task/check_subscriptions.php @@ -0,0 +1,274 @@ +. +namespace tool_monitor\task; +use tool_monitor\subscription; +use tool_monitor\subscription_manager; + +/** + * Simple task class responsible for activating, deactivating and removing subscriptions. + * + * Activation/deactivation is managed by looking at the same access rules used to determine whether a user can + * subscribe to the rule in the first place. + * + * Removal occurs when a subscription has been inactive for a period of time exceeding the lifespan, as set by + * subscription_manager::get_inactive_subscription_lifespan(). + * + * I.e. + * - Activation: If a user can subscribe currently, then an existing subscription should be made active. + * - Deactivation: If a user cannot subscribe currently, then an existing subscription should be made inactive. + * - Removal: If a user has a subscription that has been inactive for longer than the prescribed period, then + * delete the subscription entirely. + * + * @since 3.2.0 + * @package tool_monitor + * @copyright 2016 Jake Dallimore + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class check_subscriptions extends \core\task\scheduled_task { + + /** @var array 1d static cache, indexed by userid, storing whether or not the user has been fully set up.*/ + protected $userssetupcache = array(); + + /** @var array 2d static cache, indexed by courseid and userid, storing whether a user can access the course with + * the 'tool/monitor:subscribe' capability. + */ + protected $courseaccesscache = array(); + + /** + * Get a descriptive name for this task. + * + * @since 3.2.0 + * @return string name of the task. + */ + public function get_name() { + return get_string('taskchecksubscriptions', 'tool_monitor'); + } + + /** + * Checks all course-level rule subscriptions and activates/deactivates based on current course access. + * + * The ordering of checks within the task is important for optimisation purposes. The aim is to be able to make a decision + * about whether to activate/deactivate each subscription without making unnecessary checks. The ordering roughly follows the + * context model, starting with system and user checks and moving down to course and course-module only when necessary. + * + * For example, if the user is suspended, then any active subscription is made inactive right away. I.e. there is no need to + * check site-level, course-level or course-module-level permissions. Likewise, if a subscriptions is site-level, there is no + * need to check course-level and course-module-level permissions. + * + * The task performs the following checks, in this order: + * 1. Check for a suspended user, breaking if suspended. + * 2. Check for an incomplete (not set up) user, breaking if not fully set up. + * 3. Check for the required capability in the relevant context, breaking if the capability is not found. + * 4. Check whether the subscription is site-context, breaking if true. + * 5. Check whether the user has course access, breaking only if the subscription is not also course-module-level. + * 6. Check whether the user has course-module access. + * + * @since 3.2.0 + */ + public function execute() { + global $DB; + + if (!get_config('tool_monitor', 'enablemonitor')) { + return; // The tool is disabled. Nothing to do. + } + + $toactivate = array(); // Store the ids of subscriptions to be activated upon completion. + $todeactivate = array(); // Store the ids of subscriptions to be deactivated upon completion. + + // Resultset rows are ordered by userid and courseid to work nicely with get_fast_modinfo() caching. + $sql = "SELECT u.id AS userid, u.firstname AS userfirstname, u.lastname AS userlastname, u.suspended AS usersuspended, + u.email AS useremail, c.visible as coursevisible, c.cacherev as coursecacherev, s.courseid AS subcourseid, + s.userid AS subuserid, s.cmid AS subcmid, s.inactivedate AS subinactivedate, s.id AS subid + FROM {user} u + JOIN {tool_monitor_subscriptions} s ON (s.userid = u.id) + LEFT JOIN {course} c ON (c.id = s.courseid) + WHERE u.id = s.userid + ORDER BY s.userid, s.courseid"; + $rs = $DB->get_recordset_sql($sql); + + foreach ($rs as $row) { + // Create skeleton records from the result. This should be enough to use in subsequent access calls and avoids DB hits. + $sub = $this->get_subscription_from_rowdata($row); + $sub = new subscription($sub); + if (!isset($user) || $user->id != $sub->userid) { + $user= $this->get_user_from_rowdata($row); + } + if ((!isset($course) || $course->id != $sub->courseid) && !empty($sub->courseid)) { + $course = $this->get_course_from_rowdata($row); + } + + // The user is suspended at site level, so deactivate any active subscriptions. + if ($user->suspended) { + if (subscription_manager::subscription_is_active($sub)) { + $todeactivate[] = $sub->id; + } + continue; + } + + // Is the user fully set up? As per require_login on the subscriptions page. + if (!$this->is_user_setup($user)) { + if (subscription_manager::subscription_is_active($sub)) { + $todeactivate[] = $sub->id; + } + continue; + } + + // Determine the context, based on the subscription course id. + $sitelevelsubscription = false; + if (empty($sub->courseid)) { + $context = \context_system::instance(); + $sitelevelsubscription = true; + } else { + $context = \context_course::instance($sub->courseid); + } + + // Check capability in the context. + if (!has_capability('tool/monitor:subscribe', $context, $user)) { + if (subscription_manager::subscription_is_active($sub)) { + $todeactivate[] = $sub->id; + } + continue; + } + + // If the subscription is site-level, then we've run all the checks required to make an access decision. + if ($sitelevelsubscription) { + if (!subscription_manager::subscription_is_active($sub)) { + $toactivate[] = $sub->id; + } + continue; + } + + // Check course access. + if (!$this->user_can_access_course($user, $course, 'tool/monitor:subscribe')) { + if (subscription_manager::subscription_is_active($sub)) { + $todeactivate[] = $sub->id; + } + continue; + } + + // If the subscription has no course module relationship. + if (empty($sub->cmid)) { + if (!subscription_manager::subscription_is_active($sub)) { + $toactivate[] = $sub->id; + } + continue; + } + + // Otherwise, check the course module info. We use the same checks as on the subscription page. + $modinfo = get_fast_modinfo($course, $sub->userid); + $cm = $modinfo->get_cm($sub->cmid); + if (!$cm || !$cm->uservisible || !$cm->available) { + if (subscription_manager::subscription_is_active($sub)) { + $todeactivate[] = $sub->id; + } + continue; + } + + // The course module is available and visible, so make a decision. + if (!subscription_manager::subscription_is_active($sub)) { + $toactivate[] = $sub->id; + } + } + $rs->close(); + + // Activate/deactivate/delete relevant subscriptions. + subscription_manager::activate_subscriptions($toactivate); + subscription_manager::deactivate_subscriptions($todeactivate); + subscription_manager::delete_stale_subscriptions(); + } + + /** + * Determines whether a user is fully set up, using cached results where possible. + * + * @since 3.2.0 + * @param \stdClass $user the user record. + * @return bool true if the user is fully set up, false otherwise. + */ + protected function is_user_setup($user) { + if (!isset($this->userssetupcache[$user->id])) { + $this->userssetupcache[$user->id] = !user_not_fully_set_up($user); + } + return $this->userssetupcache[$user->id]; + } + + /** + * Determines a user's access to a course with a given capability, using cached results where possible. + * + * @since 3.2.0 + * @param \stdClass $user the user record. + * @param \stdClass $course the course record. + * @param string $capability the capability to check. + * @return bool true if the user can access the course with the specified capability, false otherwise. + */ + protected function user_can_access_course($user, $course, $capability) { + if (!isset($this->courseaccesscache[$course->id][$user->id][$capability])) { + $this->courseaccesscache[$course->id][$user->id][$capability] = can_access_course($course, $user, $capability, true); + } + return $this->courseaccesscache[$course->id][$user->id][$capability]; + } + + /** + * Returns a partial subscription record, created from properties of the supplied recordset row object. + * Intended to return a minimal record for specific use within this class and in subsequent access control calls only. + * + * @since 3.2.0 + * @param \stdClass $rowdata the row object. + * @return \stdClass a partial subscription record. + */ + protected function get_subscription_from_rowdata($rowdata) { + $sub = new \stdClass(); + $sub->id = $rowdata->subid; + $sub->userid = $rowdata->subuserid; + $sub->courseid = $rowdata->subcourseid; + $sub->cmid = $rowdata->subcmid; + $sub->inactivedate = $rowdata->subinactivedate; + return $sub; + } + + /** + * Returns a partial course record, created from properties of the supplied recordset row object. + * Intended to return a minimal record for specific use within this class and in subsequent access control calls only. + * + * @since 3.2.0 + * @param \stdClass $rowdata the row object. + * @return \stdClass a partial course record. + */ + protected function get_course_from_rowdata($rowdata) { + $course = new \stdClass(); + $course->id = $rowdata->subcourseid; + $course->visible = $rowdata->coursevisible; + $course->cacherev = $rowdata->coursecacherev; + return $course; + } + + /** + * Returns a partial user record, created from properties of the supplied recordset row object. + * Intended to return a minimal record for specific use within this class and in subsequent access control calls only. + * + * @since 3.2.0 + * @param \stdClass $rowdata the row object. + * @return \stdClass a partial user record. + */ + protected function get_user_from_rowdata($rowdata) { + $user = new \stdClass(); + $user->id = $rowdata->userid; + $user->firstname = $rowdata->userfirstname; + $user->lastname = $rowdata->userlastname; + $user->email = $rowdata->useremail; + $user->suspended = $rowdata->usersuspended; + return $user; + } +} diff --git a/admin/tool/monitor/db/install.xml b/admin/tool/monitor/db/install.xml index 7729199f1b8..9a3f1a16f32 100644 --- a/admin/tool/monitor/db/install.xml +++ b/admin/tool/monitor/db/install.xml @@ -38,6 +38,7 @@ + diff --git a/admin/tool/monitor/db/tasks.php b/admin/tool/monitor/db/tasks.php index e70c3448110..20f324d88c1 100644 --- a/admin/tool/monitor/db/tasks.php +++ b/admin/tool/monitor/db/tasks.php @@ -32,5 +32,14 @@ $tasks = array( 'day' => '*', 'dayofweek' => '*', 'month' => '*' + ), + array( + 'classname' => 'tool_monitor\task\check_subscriptions', + 'blocking' => 0, + 'minute' => 'R', + 'hour' => 'R', + 'day' => '*', + 'dayofweek' => '*', + 'month' => '*' ) ); diff --git a/admin/tool/monitor/db/upgrade.php b/admin/tool/monitor/db/upgrade.php index 9f8e53bcf8b..aaea29a1503 100644 --- a/admin/tool/monitor/db/upgrade.php +++ b/admin/tool/monitor/db/upgrade.php @@ -62,5 +62,20 @@ function xmldb_tool_monitor_upgrade($oldversion) { // Moodle v3.1.0 release upgrade line. // Put any upgrade step following this. + if ($oldversion < 2016052305) { + + // Define field inactivedate to be added to tool_monitor_subscriptions. + $table = new xmldb_table('tool_monitor_subscriptions'); + $field = new xmldb_field('inactivedate', XMLDB_TYPE_INTEGER, '10', null, true, null, 0, 'lastnotificationsent'); + + // Conditionally launch add field inactivedate. + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + // Monitor savepoint reached. + upgrade_plugin_savepoint(true, 2016052305, 'tool', 'monitor'); + } + return true; } diff --git a/admin/tool/monitor/lang/en/tool_monitor.php b/admin/tool/monitor/lang/en/tool_monitor.php index c0fbf7fc4be..fb477172103 100644 --- a/admin/tool/monitor/lang/en/tool_monitor.php +++ b/admin/tool/monitor/lang/en/tool_monitor.php @@ -99,4 +99,5 @@ $string['subhelp'] = 'Subscription details'; $string['subhelp_help'] = 'This subscription listens for when the event \'{$a->eventname}\' has been triggered in \'{$a->moduleinstance}\' {$a->frequency} time(s) in {$a->minutes} minute(s).'; $string['subscribeto'] = 'Subscribe to rule "{$a}"'; $string['taskcleanevents'] = 'Removes any unnecessary event monitor events'; +$string['taskchecksubscriptions'] = 'Activate/deactivate invalid rule subscriptions'; $string['unsubscribe'] = 'Unsubscribe'; diff --git a/admin/tool/monitor/tests/subscription_test.php b/admin/tool/monitor/tests/subscription_test.php new file mode 100644 index 00000000000..3548c7f563d --- /dev/null +++ b/admin/tool/monitor/tests/subscription_test.php @@ -0,0 +1,65 @@ +. +defined('MOODLE_INTERNAL') || exit(); + +/** + * Unit tests for the subscription class. + * @since 3.2.0 + * + * @package tool_monitor + * @category test + * @copyright 2016 Jake Dallimore + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class tool_monitor_subscription_testcase extends advanced_testcase { + + /** + * @var \tool_monitor\subscription $subscription object. + */ + private $subscription; + + /** + * Test set up. + */ + public function setUp() { + $this->resetAfterTest(true); + + // Create the mock subscription. + $sub = new stdClass(); + $sub->id = 100; + $sub->name = 'My test rule'; + $sub->courseid = 20; + $this->subscription = $this->getMock('\tool_monitor\subscription',null, array($sub)); + } + + /** + * Test for the magic __isset method. + */ + public function test_magic_isset() { + $this->assertEquals(true, isset($this->subscription->name)); + $this->assertEquals(true, isset($this->subscription->courseid)); + $this->assertEquals(false, isset($this->subscription->ruleid)); + } + + /** + * Test for the magic __get method. + */ + public function test_magic_get() { + $this->assertEquals(20, $this->subscription->courseid); + $this->setExpectedException('coding_exception'); + $this->subscription->ruleid; + } +} diff --git a/admin/tool/monitor/tests/task_check_subscriptions_test.php b/admin/tool/monitor/tests/task_check_subscriptions_test.php new file mode 100644 index 00000000000..b52678436e6 --- /dev/null +++ b/admin/tool/monitor/tests/task_check_subscriptions_test.php @@ -0,0 +1,364 @@ +. +defined('MOODLE_INTERNAL') || exit(); + +/** + * Unit tests for the tool_monitor clean events task. + * @since 3.2.0 + * + * @package tool_monitor + * @category test + * @copyright 2016 Jake Dallimore + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class tool_monitor_task_check_subscriptions_testcase extends advanced_testcase { + + private $course; + private $user; + private $rule; + private $subscription; + private $teacherrole; + private $studentrole; + + /** + * Test set up. + */ + public function setUp() { + global $DB; + set_config('enablemonitor', 1, 'tool_monitor'); + $this->resetAfterTest(true); + + // All tests defined herein need a user, course, rule and subscription, so set these up. + $this->user = $this->getDataGenerator()->create_user(); + $this->course = $this->getDataGenerator()->create_course(); + + $rule = new stdClass(); + $rule->userid = 2; // Rule created by admin. + $rule->courseid = $this->course->id; + $rule->plugin = 'mod_book'; + $rule->eventname = '\mod_book\event\course_module_viewed'; + $rule->timewindow = 500; + $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor'); + $this->rule = $monitorgenerator->create_rule($rule); + + $sub = new stdClass(); + $sub->courseid = $this->course->id; + $sub->userid = $this->user->id; + $sub->ruleid = $this->rule->id; + $this->subscription = $monitorgenerator->create_subscription($sub); + + // Also set up a student and a teacher role for use in some tests. + $this->teacherrole = $DB->get_record('role', array('shortname' => 'teacher')); + $this->studentrole = $DB->get_record('role', array('shortname' => 'student')); + } + + /** + * Reloads the subscription object from the DB. + * + * @return void. + */ + private function reload_subscription() { + global $DB; + $sub = $DB->get_record('tool_monitor_subscriptions', array('id' => $this->subscription->id)); + $this->subscription = new \tool_monitor\subscription($sub); + } + + /** + * Test to confirm the task is named correctly. + */ + public function test_task_name() { + $task = new \tool_monitor\task\check_subscriptions(); + $this->assertEquals(get_string('taskchecksubscriptions', 'tool_monitor'), $task->get_name()); + } + + /** + * Test to confirm that site level subscriptions are activated and deactivated according to system capabilities. + */ + public function test_site_level_subscription() { + // Create a site level subscription. + $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor'); + $sub = new stdClass(); + $sub->userid = $this->user->id; + $sub->ruleid = $this->rule->id; + $this->subscription = $monitorgenerator->create_subscription($sub); + + // Run the task. + $task = new \tool_monitor\task\check_subscriptions(); + $task->execute(); + + // The subscription should be inactive as the user doesn't have the capability. Pass in the id only to refetch the data. + $this->reload_subscription(); + $this->assertEquals(false, \tool_monitor\subscription_manager::subscription_is_active($this->subscription)); + + // Now, assign the user as a teacher role at system context. + $this->getDataGenerator()->role_assign($this->teacherrole->id, $this->user->id, context_system::instance()); + + // Run the task. + $task = new \tool_monitor\task\check_subscriptions(); + $task->execute(); + + // The subscription should be active now. Pass in the id only to refetch the data. + $this->reload_subscription(); + $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription)); + } + + /** + * Test to confirm that if the module is disabled, no changes are made to active subscriptions. + */ + public function test_module_disabled() { + set_config('enablemonitor', 0, 'tool_monitor'); + + // Subscription should be active to start with. + $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription)); + + // Run the task. Note, we never enrolled the user. + $task = new \tool_monitor\task\check_subscriptions(); + $task->execute(); + + // The subscription should still be active. Pass in the id only to refetch the data. + $this->reload_subscription(); + $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription)); + } + + /** + * Test to confirm an active, valid subscription stays active once the scheduled task is run. + */ + public function test_active_unaffected() { + // Enrol the user as a teacher. This role should have the required capability. + $this->getDataGenerator()->enrol_user($this->user->id, $this->course->id, $this->teacherrole->id); + + // Subscription should be active to start with. + $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription)); + + // Run the task. + $task = new \tool_monitor\task\check_subscriptions(); + $task->execute(); + + // The subscription should still be active. Pass in the id only to refetch the data. + $this->reload_subscription(); + $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription)); + } + + /** + * Test to confirm that a subscription for a user without an enrolment to the course is made inactive. + */ + public function test_course_enrolment() { + // Subscription should be active until deactivated by the scheduled task. Remember, by default the test setup + // doesn't enrol the user, so the first run of the task should deactivate it. + $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription)); + + // Run the task. + $task = new \tool_monitor\task\check_subscriptions(); + $task->execute(); + + // The subscription should NOT be active. Pass in the id only to refetch the data. + $this->reload_subscription(); + $this->assertEquals(false, \tool_monitor\subscription_manager::subscription_is_active($this->subscription)); + + // Enrol the user. + $this->getDataGenerator()->enrol_user($this->user->id, $this->course->id, $this->teacherrole->id); + + // Run the task. + $task = new \tool_monitor\task\check_subscriptions(); + $task->execute(); + + // Subscription should now be active again. + $this->reload_subscription(); + $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription)); + } + + /** + * Test to confirm that subscriptions for enrolled users without the required capability are made inactive. + */ + public function test_enrolled_user_with_no_capability() { + // Enrol the user. By default, students won't have the required capability. + $this->getDataGenerator()->enrol_user($this->user->id, $this->course->id, $this->studentrole->id); + + // The subscription should be active to start with. Pass in the id only to refetch the data. + $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription)); + + // Run the task. + $task = new \tool_monitor\task\check_subscriptions(); + $task->execute(); + + // The subscription should NOT be active. Pass in the id only to refetch the data. + $this->reload_subscription(); + $this->assertEquals(false, \tool_monitor\subscription_manager::subscription_is_active($this->subscription)); + } + + /** + * Test to confirm that subscriptions for users who fail can_access_course(), are deactivated. + */ + public function test_can_access_course() { + // Enrol the user as a teacher. This role should have the required capability. + $this->getDataGenerator()->enrol_user($this->user->id, $this->course->id, $this->teacherrole->id); + + // Strip the ability to see hidden courses, so we'll fail the check_subscriptions->user_can_access_course call. + $context = \context_course::instance($this->course->id); + assign_capability('moodle/course:viewhiddencourses', CAP_PROHIBIT, $this->teacherrole->id, $context); + + // Subscription should be active to start with. + $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription)); + + // Hide the course. + course_change_visibility($this->course->id, false); + + // Run the task. + $task = new \tool_monitor\task\check_subscriptions(); + $task->execute(); + + // The subscription should be inactive. Pass in the id only to refetch the data. + $this->reload_subscription(); + $this->assertEquals(false, \tool_monitor\subscription_manager::subscription_is_active($this->subscription)); + } + + /** + * Test to confirm that subscriptions for enrolled users who don't have CM access, are deactivated. + */ + public function test_cm_access() { + // Enrol the user as a student but grant to ability to subscribe. Students cannot view hidden activities. + $context = \context_course::instance($this->course->id); + assign_capability('tool/monitor:subscribe', CAP_ALLOW, $this->studentrole->id, $context); + $this->getDataGenerator()->enrol_user($this->user->id, $this->course->id, $this->studentrole->id); + + // Generate a course module. + $book = $this->getDataGenerator()->create_module('book', array('course' => $this->course->id)); + + // And add a subscription to it. + $sub = new stdClass(); + $sub->courseid = $this->course->id; + $sub->userid = $this->user->id; + $sub->ruleid = $this->rule->id; + $sub->cmid = $book->cmid; + $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor'); + $this->subscription = $monitorgenerator->create_subscription($sub); + + // The subscription should be active to start with. Pass in the id only to refetch the data. + $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription)); + + // Run the task. + $task = new \tool_monitor\task\check_subscriptions(); + $task->execute(); + + // The subscription should still be active. Pass in the id only to refetch the data. + $this->reload_subscription(); + $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription)); + + // Make the course module invisible, which should in turn make the subscription inactive. + set_coursemodule_visible($book->cmid, false); + + // Run the task. + $task = new \tool_monitor\task\check_subscriptions(); + $task->execute(); + + // The subscription should NOT be active. Pass in the id only to refetch the data. + $this->reload_subscription(); + $this->assertEquals(false, \tool_monitor\subscription_manager::subscription_is_active($this->subscription)); + + // Make the course module visible again. + set_coursemodule_visible($book->cmid, true); + + // Run the task. + $task = new \tool_monitor\task\check_subscriptions(); + $task->execute(); + + // The subscription should be active. Pass in the id only to refetch the data. + $this->reload_subscription(); + $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription)); + } + + /** + * Test to confirm that long term inactive subscriptions are removed entirely. + */ + public function test_stale_subscription_removal() { + global $DB; + // Manually set the inactivedate to 1 day older than the limit allowed. + $daysold = 1 + \tool_monitor\subscription_manager::INACTIVE_SUBSCRIPTION_LIFESPAN_IN_DAYS; + + $inactivedate = strtotime("-$daysold days", time()); + $DB->set_field('tool_monitor_subscriptions', 'inactivedate', $inactivedate, array('id' => $this->subscription->id)); + + // Subscription should be inactive to start with. + $this->reload_subscription(); + $this->assertEquals(false, \tool_monitor\subscription_manager::subscription_is_active($this->subscription)); + + // Run the task. + $task = new \tool_monitor\task\check_subscriptions(); + $task->execute(); + + // Subscription should now not exist at all. + $this->assertEquals(false, $DB->record_exists('tool_monitor_subscriptions', array('id' => $this->subscription->id))); + } + + /** + * Test to confirm that subscriptions for a partially set up user are deactivated. + */ + public function test_user_not_fully_set_up() { + global $DB; + + // Enrol the user as a teacher. + $this->getDataGenerator()->enrol_user($this->user->id, $this->course->id, $this->teacherrole->id); + + // The subscription should be active to start. + $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription)); + + // Unset the user's email address, so we fail the check_subscriptions->is_user_setup() call. + $DB->set_field('user', 'email', '', array('id' => $this->user->id)); + + // Run the task. + $task = new \tool_monitor\task\check_subscriptions(); + $task->execute(); + + // The subscription should now be inactive. + $this->reload_subscription(); + $this->assertEquals(false, \tool_monitor\subscription_manager::subscription_is_active($this->subscription)); + } + + /** + * Test to confirm that a suspended user's subscriptions are deactivated properly. + */ + public function test_suspended_user() { + global $DB; + + // Enrol the user as a teacher. This role should have the required capability. + $this->getDataGenerator()->enrol_user($this->user->id, $this->course->id, $this->teacherrole->id); + + // Subscription should be active to start with. + $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription)); + + // Suspend the user. + $DB->set_field('user', 'suspended', '1', array('id' => $this->user->id)); + + // Run the task. + $task = new \tool_monitor\task\check_subscriptions(); + $task->execute(); + + // The subscription should now be inactive. + $this->reload_subscription(); + $this->assertEquals(false, \tool_monitor\subscription_manager::subscription_is_active($this->subscription)); + + // Unsuspend the user. + $DB->set_field('user', 'suspended', '0', array('id' => $this->user->id)); + + // Run the task. + $task = new \tool_monitor\task\check_subscriptions(); + $task->execute(); + + // The subscription should now be active again. + $this->reload_subscription(); + $this->assertEquals(true, \tool_monitor\subscription_manager::subscription_is_active($this->subscription)); + } +} diff --git a/admin/tool/monitor/version.php b/admin/tool/monitor/version.php index c078165c1e6..9e5a5a394ba 100644 --- a/admin/tool/monitor/version.php +++ b/admin/tool/monitor/version.php @@ -26,6 +26,6 @@ defined('MOODLE_INTERNAL') || die; -$plugin->version = 2016052300; // The current plugin version (Date: YYYYMMDDXX). +$plugin->version = 2016052305; // The current plugin version (Date: YYYYMMDDXX). $plugin->requires = 2016051900; // Requires this Moodle version. $plugin->component = 'tool_monitor'; // Full name of the plugin (used for diagnostics).