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]

   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_completion;
  18  
  19  /**
  20   * Test completion criteria.
  21   *
  22   * @package   core_completion
  23   * @category  test
  24   * @copyright 2021 Mikhail Golenkov <mikhailgolenkov@catalyst-au.net>
  25   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  class core_completion_criteria_testcase extends \advanced_testcase {
  28  
  29      /**
  30       * Test setup.
  31       */
  32      public function setUp(): void {
  33          global $CFG;
  34          require_once($CFG->dirroot.'/completion/criteria/completion_criteria_course.php');
  35          require_once($CFG->dirroot.'/completion/criteria/completion_criteria_activity.php');
  36          require_once($CFG->dirroot.'/completion/criteria/completion_criteria_duration.php');
  37          require_once($CFG->dirroot.'/completion/criteria/completion_criteria_grade.php');
  38          require_once($CFG->dirroot.'/completion/criteria/completion_criteria_date.php');
  39  
  40          $this->setAdminUser();
  41          $this->resetAfterTest();
  42      }
  43  
  44      /**
  45       * Test that activity completion dates are used when activity criteria is marked as completed.
  46       */
  47      public function test_completion_criteria_activity(): void {
  48          global $DB;
  49          $timestarted = time();
  50          $timecompleted = 1620000000;
  51  
  52          // Create a course, an activity and enrol a couple of users.
  53          $course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);
  54          $assign = $this->getDataGenerator()->create_module('assign', ['course' => $course->id], ['completion' => 1]);
  55          $user1 = $this->getDataGenerator()->create_user();
  56          $user2 = $this->getDataGenerator()->create_user();
  57          $studentrole = $DB->get_record('role', ['shortname' => 'student']);
  58          $this->getDataGenerator()->enrol_user($user1->id, $course->id, $studentrole->id);
  59          $this->getDataGenerator()->enrol_user($user2->id, $course->id, $studentrole->id);
  60  
  61          // Set completion criteria and mark both users to complete the criteria.
  62          $criteriadata = (object) [
  63              'id' => $course->id,
  64              'criteria_activity' => [$assign->cmid => 1],
  65          ];
  66          $criterion = new \completion_criteria_activity();
  67          $criterion->update_config($criteriadata);
  68          $cmassign = get_coursemodule_from_id('assign', $assign->cmid);
  69          $completion = new \completion_info($course);
  70          $completion->update_state($cmassign, COMPLETION_COMPLETE, $user1->id);
  71          $completion->update_state($cmassign, COMPLETION_COMPLETE, $user2->id);
  72  
  73          // Update User 1 to complete the activity at $timecompleted.
  74          $params = ['coursemoduleid' => $assign->cmid, 'userid' => $user1->id];
  75          $DB->set_field('course_modules_completion', 'timemodified', $timecompleted, $params);
  76  
  77          // Run completion scheduled task.
  78          $task = new \core\task\completion_regular_task();
  79          $this->expectOutputRegex("/Marking complete/");
  80          $task->execute();
  81          // Hopefully, some day MDL-33320 will be fixed and all these sleeps
  82          // and double cron calls in behat and unit tests will be removed.
  83          sleep(1);
  84          $task->execute();
  85  
  86          // Completion criteria for User 1 is supposed to be marked as completed at $timecompleted.
  87          $result = \core_completion_external::get_activities_completion_status($course->id, $user1->id);
  88          $actual = reset($result['statuses']);
  89          $this->assertEquals(1, $actual['state']);
  90          $this->assertEquals($timecompleted, $actual['timecompleted']);
  91  
  92          // And the whole course is marked as completed at $timecompleted for User 1 because
  93          // it's the latest criteria completion date.
  94          $ccompletion = new \completion_completion(['userid' => $user1->id, 'course' => $course->id]);
  95          $this->assertEquals($timecompleted, $ccompletion->timecompleted);
  96          $this->assertTrue($ccompletion->is_complete());
  97  
  98          // Completion criteria for User 2 is supposed to be marked as completed at now().
  99          $result = \core_completion_external::get_activities_completion_status($course->id, $user2->id);
 100          $actual = reset($result['statuses']);
 101          $this->assertEquals(1, $actual['state']);
 102          $this->assertGreaterThanOrEqual($timestarted, $actual['timecompleted']);
 103  
 104          // And the whole course is marked as completed at now() for User 2.
 105          $ccompletion = new \completion_completion(['userid' => $user2->id, 'course' => $course->id]);
 106          $this->assertGreaterThanOrEqual($timestarted, $ccompletion->timecompleted);
 107          $this->assertTrue($ccompletion->is_complete());
 108      }
 109  
 110      /**
 111       * Test that enrolment timestart/timecreated are used when duration criteria is marked as completed.
 112       */
 113      public function test_completion_criteria_duration(): void {
 114          global $DB;
 115          $timestarted = 1610000000;
 116          $timecreated = 1620000000;
 117          $durationperiod = DAYSECS;
 118  
 119          // Create a course and users.
 120          $course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);
 121          $user1 = $this->getDataGenerator()->create_user();
 122          $user2 = $this->getDataGenerator()->create_user();
 123          $studentrole = $DB->get_record('role', ['shortname' => 'student']);
 124  
 125          // Enrol User 1 with time start = $timestarted.
 126          $this->getDataGenerator()->enrol_user($user1->id, $course->id, $studentrole->id, 'manual', $timestarted);
 127  
 128          // Enrol User 2 with an empty time start, but update the record like it was created at $timecreated.
 129          $this->getDataGenerator()->enrol_user($user2->id, $course->id, $studentrole->id);
 130          $DB->set_field('user_enrolments', 'timecreated', $timecreated, ['userid' => $user2->id]);
 131  
 132          // Set completion criteria.
 133          $criteriadata = (object) [
 134              'id' => $course->id,
 135              'criteria_duration' => 1,
 136              'criteria_duration_days' => $durationperiod,
 137          ];
 138          $criterion = new \completion_criteria_duration();
 139          $criterion->update_config($criteriadata);
 140  
 141          // Run completion scheduled task.
 142          $task = new \core\task\completion_regular_task();
 143          $this->expectOutputRegex("/Marking complete/");
 144          $task->execute();
 145          // Hopefully, some day MDL-33320 will be fixed and all these sleeps
 146          // and double cron calls in behat and unit tests will be removed.
 147          sleep(1);
 148          $task->execute();
 149  
 150          // The course for User 1 is supposed to be marked as completed at $timestarted + $durationperiod.
 151          $ccompletion = new \completion_completion(['userid' => $user1->id, 'course' => $course->id]);
 152          $this->assertEquals($timestarted + $durationperiod, $ccompletion->timecompleted);
 153          $this->assertTrue($ccompletion->is_complete());
 154  
 155          // The course for User 2 is supposed to be marked as completed at $timecreated + $durationperiod.
 156          $ccompletion = new \completion_completion(['userid' => $user2->id, 'course' => $course->id]);
 157          $this->assertEquals($timecreated + $durationperiod, $ccompletion->timecompleted);
 158          $this->assertTrue($ccompletion->is_complete());
 159      }
 160  
 161      /**
 162       * Test that criteria date is used as a course completion date.
 163       */
 164      public function test_completion_criteria_date(): void {
 165          global $DB;
 166          $timeend = 1610000000;
 167  
 168          // Create a course and enrol a user.
 169          $course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);
 170          $user = $this->getDataGenerator()->create_user();
 171          $studentrole = $DB->get_record('role', ['shortname' => 'student']);
 172          $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
 173  
 174          // Set completion criteria.
 175          $criteriadata = (object) [
 176              'id' => $course->id,
 177              'criteria_date' => 1,
 178              'criteria_date_value' => $timeend,
 179          ];
 180          $criterion = new \completion_criteria_date();
 181          $criterion->update_config($criteriadata);
 182  
 183          // Run completion scheduled task.
 184          $task = new \core\task\completion_regular_task();
 185          $this->expectOutputRegex("/Marking complete/");
 186          $task->execute();
 187          // Hopefully, some day MDL-33320 will be fixed and all these sleeps
 188          // and double cron calls in behat and unit tests will be removed.
 189          sleep(1);
 190          $task->execute();
 191  
 192          // The course is supposed to be marked as completed at $timeend.
 193          $ccompletion = new \completion_completion(['userid' => $user->id, 'course' => $course->id]);
 194          $this->assertEquals($timeend, $ccompletion->timecompleted);
 195          $this->assertTrue($ccompletion->is_complete());
 196      }
 197  
 198      /**
 199       * Test that grade timemodified is used when grade criteria is marked as completed.
 200       */
 201      public function test_completion_criteria_grade(): void {
 202          global $DB;
 203          $timegraded = 1610000000;
 204  
 205          // Create a course and enrol a couple of users.
 206          $course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);
 207          $user1 = $this->getDataGenerator()->create_user();
 208          $user2 = $this->getDataGenerator()->create_user();
 209          $studentrole = $DB->get_record('role', ['shortname' => 'student']);
 210          $this->getDataGenerator()->enrol_user($user1->id, $course->id, $studentrole->id);
 211          $this->getDataGenerator()->enrol_user($user2->id, $course->id, $studentrole->id);
 212  
 213          // Set completion criteria.
 214          $criteriadata = (object) [
 215              'id' => $course->id,
 216              'criteria_grade' => 1,
 217              'criteria_grade_value' => 66,
 218          ];
 219          $criterion = new \completion_criteria_grade();
 220          $criterion->update_config($criteriadata);
 221  
 222          $coursegradeitem = \grade_item::fetch_course_item($course->id);
 223  
 224          // Grade User 1 with a passing grade.
 225          $grade1 = new \grade_grade();
 226          $grade1->itemid = $coursegradeitem->id;
 227          $grade1->timemodified = $timegraded;
 228          $grade1->userid = $user1->id;
 229          $grade1->finalgrade = 80;
 230          $grade1->insert();
 231  
 232          // Grade User 2 with a non-passing grade.
 233          $grade2 = new \grade_grade();
 234          $grade2->itemid = $coursegradeitem->id;
 235          $grade2->timemodified = $timegraded;
 236          $grade2->userid = $user2->id;
 237          $grade2->finalgrade = 40;
 238          $grade2->insert();
 239  
 240          // Run completion scheduled task.
 241          $task = new \core\task\completion_regular_task();
 242          $this->expectOutputRegex("/Marking complete/");
 243          $task->execute();
 244          // Hopefully, some day MDL-33320 will be fixed and all these sleeps
 245          // and double cron calls in behat and unit tests will be removed.
 246          sleep(1);
 247          $task->execute();
 248  
 249          // The course for User 1 is supposed to be marked as completed when the user was graded.
 250          $ccompletion = new \completion_completion(['userid' => $user1->id, 'course' => $course->id]);
 251          $this->assertEquals($timegraded, $ccompletion->timecompleted);
 252          $this->assertTrue($ccompletion->is_complete());
 253  
 254          // The course for User 2 is supposed to be marked as not completed.
 255          $ccompletion = new \completion_completion(['userid' => $user2->id, 'course' => $course->id]);
 256          $this->assertFalse($ccompletion->is_complete());
 257      }
 258  }