Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 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 completion_criteria_test 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 are used when duration criteria is marked as completed.
 112       */
 113      public function test_completion_criteria_duration_timestart(): void {
 114          global $DB;
 115          $timestarted = 1610000000;
 116          $durationperiod = DAYSECS;
 117  
 118          // Create a course and users.
 119          $course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);
 120          $user = $this->getDataGenerator()->create_and_enrol($course, 'student', null, 'manual', $timestarted);
 121  
 122          // Set completion criteria.
 123          $criteriadata = (object) [
 124              'id' => $course->id,
 125              'criteria_duration' => 1,
 126              'criteria_duration_days' => $durationperiod,
 127          ];
 128          $criterion = new \completion_criteria_duration();
 129          $criterion->update_config($criteriadata);
 130  
 131          // Run completion scheduled task.
 132          $task = new \core\task\completion_regular_task();
 133          $this->expectOutputRegex("/Marking complete/");
 134          $task->execute();
 135          // Hopefully, some day MDL-33320 will be fixed and all these sleeps
 136          // and double cron calls in behat and unit tests will be removed.
 137          sleep(1);
 138          $task->execute();
 139  
 140          // The course for User is supposed to be marked as completed at $timestarted + $durationperiod.
 141          $ccompletion = new \completion_completion(['userid' => $user->id, 'course' => $course->id]);
 142          $this->assertEquals($timestarted + $durationperiod, $ccompletion->timecompleted);
 143          $this->assertTrue($ccompletion->is_complete());
 144      }
 145  
 146      /**
 147       * Test that enrolment timecreated are used when duration criteria is marked as completed.
 148       */
 149      public function test_completion_criteria_duration_timecreated(): void {
 150          global $DB;
 151  
 152          $timecreated = 1620000000;
 153          $durationperiod = DAYSECS;
 154  
 155          // Create a course and users.
 156          $course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);
 157  
 158          // Create and enrol user with an empty time start, but update the record like it was created at $timecreated.
 159          $user = $this->getDataGenerator()->create_and_enrol($course);
 160          $DB->set_field('user_enrolments', 'timecreated', $timecreated, ['userid' => $user->id]);
 161  
 162          // Set completion criteria.
 163          $criteriadata = (object) [
 164              'id' => $course->id,
 165              'criteria_duration' => 1,
 166              'criteria_duration_days' => $durationperiod,
 167          ];
 168          $criterion = new \completion_criteria_duration();
 169          $criterion->update_config($criteriadata);
 170  
 171          // Run completion scheduled task.
 172          $task = new \core\task\completion_regular_task();
 173          $this->expectOutputRegex("/Marking complete/");
 174          $task->execute();
 175  
 176          // Hopefully, some day MDL-33320 will be fixed and all these sleeps
 177          // and double cron calls in behat and unit tests will be removed.
 178          sleep(1);
 179          $task->execute();
 180  
 181          // The course for user is supposed to be marked as completed at $timecreated + $durationperiod.
 182          $ccompletion = new \completion_completion(['userid' => $user->id, 'course' => $course->id]);
 183          $this->assertEquals($timecreated + $durationperiod, $ccompletion->timecompleted);
 184          $this->assertTrue($ccompletion->is_complete());
 185      }
 186  
 187      /**
 188       * Test that criteria date is used as a course completion date.
 189       */
 190      public function test_completion_criteria_date(): void {
 191          global $DB;
 192          $timeend = 1610000000;
 193  
 194          // Create a course and enrol a user.
 195          $course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);
 196          $user = $this->getDataGenerator()->create_user();
 197          $studentrole = $DB->get_record('role', ['shortname' => 'student']);
 198          $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
 199  
 200          // Set completion criteria.
 201          $criteriadata = (object) [
 202              'id' => $course->id,
 203              'criteria_date' => 1,
 204              'criteria_date_value' => $timeend,
 205          ];
 206          $criterion = new \completion_criteria_date();
 207          $criterion->update_config($criteriadata);
 208  
 209          // Run completion scheduled task.
 210          $task = new \core\task\completion_regular_task();
 211          $this->expectOutputRegex("/Marking complete/");
 212          $task->execute();
 213          // Hopefully, some day MDL-33320 will be fixed and all these sleeps
 214          // and double cron calls in behat and unit tests will be removed.
 215          sleep(1);
 216          $task->execute();
 217  
 218          // The course is supposed to be marked as completed at $timeend.
 219          $ccompletion = new \completion_completion(['userid' => $user->id, 'course' => $course->id]);
 220          $this->assertEquals($timeend, $ccompletion->timecompleted);
 221          $this->assertTrue($ccompletion->is_complete());
 222      }
 223  
 224      /**
 225       * Test that grade timemodified is used when grade criteria is marked as completed.
 226       */
 227      public function test_completion_criteria_grade(): void {
 228          global $DB;
 229          $timegraded = 1610000000;
 230  
 231          // Create a course and enrol a couple of users.
 232          $course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]);
 233          $user1 = $this->getDataGenerator()->create_user();
 234          $user2 = $this->getDataGenerator()->create_user();
 235          $studentrole = $DB->get_record('role', ['shortname' => 'student']);
 236          $this->getDataGenerator()->enrol_user($user1->id, $course->id, $studentrole->id);
 237          $this->getDataGenerator()->enrol_user($user2->id, $course->id, $studentrole->id);
 238  
 239          // Set completion criteria.
 240          $criteriadata = (object) [
 241              'id' => $course->id,
 242              'criteria_grade' => 1,
 243              'criteria_grade_value' => 66,
 244          ];
 245          $criterion = new \completion_criteria_grade();
 246          $criterion->update_config($criteriadata);
 247  
 248          $coursegradeitem = \grade_item::fetch_course_item($course->id);
 249  
 250          // Grade User 1 with a passing grade.
 251          $grade1 = new \grade_grade();
 252          $grade1->itemid = $coursegradeitem->id;
 253          $grade1->timemodified = $timegraded;
 254          $grade1->userid = $user1->id;
 255          $grade1->finalgrade = 80;
 256          $grade1->insert();
 257  
 258          // Grade User 2 with a non-passing grade.
 259          $grade2 = new \grade_grade();
 260          $grade2->itemid = $coursegradeitem->id;
 261          $grade2->timemodified = $timegraded;
 262          $grade2->userid = $user2->id;
 263          $grade2->finalgrade = 40;
 264          $grade2->insert();
 265  
 266          // Run completion scheduled task.
 267          $task = new \core\task\completion_regular_task();
 268          $this->expectOutputRegex("/Marking complete/");
 269          $task->execute();
 270          // Hopefully, some day MDL-33320 will be fixed and all these sleeps
 271          // and double cron calls in behat and unit tests will be removed.
 272          sleep(1);
 273          $task->execute();
 274  
 275          // The course for User 1 is supposed to be marked as completed when the user was graded.
 276          $ccompletion = new \completion_completion(['userid' => $user1->id, 'course' => $course->id]);
 277          $this->assertEquals($timegraded, $ccompletion->timecompleted);
 278          $this->assertTrue($ccompletion->is_complete());
 279  
 280          // The course for User 2 is supposed to be marked as not completed.
 281          $ccompletion = new \completion_completion(['userid' => $user2->id, 'course' => $course->id]);
 282          $this->assertFalse($ccompletion->is_complete());
 283      }
 284  }