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.
   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\task;
  18  
  19  use advanced_testcase;
  20  
  21  /**
  22   * Class containing unit tests for the daily completion cron task.
  23   *
  24   * @package core
  25   * @copyright 2020 Jun Pataleta
  26   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  27   */
  28  class completion_daily_task_test extends advanced_testcase {
  29  
  30      /**
  31       * Test calendar cron task with a broken subscription URL.
  32       */
  33      public function test_completion_daily_cron() {
  34          global $DB;
  35  
  36          $this->resetAfterTest();
  37  
  38          set_config('enablecompletion', 1);
  39          set_config('enrol_plugins_enabled', 'self,manual');
  40  
  41          $generator = $this->getDataGenerator();
  42  
  43          $now = time();
  44          $lastweek = $now - WEEKSECS;
  45          $yesterday = $now - DAYSECS;
  46          $tomorrow = $now + DAYSECS;
  47  
  48          // Course with completion enabled and has already started.
  49          $c1 = $generator->create_course(['enablecompletion' => 1, 'startdate' => $lastweek]);
  50          // Course with completion enabled but hasn't started yet.
  51          $c2 = $generator->create_course(['enablecompletion' => 1, 'startdate' => $tomorrow]);
  52          // Completion not enabled.
  53          $c3 = $generator->create_course();
  54  
  55          // Create users.
  56          $t1 = $generator->create_user(['username' => 't1']);
  57          $t2 = $generator->create_user(['username' => 't2']);
  58          $s1 = $generator->create_user(['username' => 's1']);
  59          $s2 = $generator->create_user(['username' => 's2']);
  60  
  61          // Enrol s1 by self and manual methods to c1.
  62          $generator->enrol_user($s1->id, $c1->id, 'student', 'self', $lastweek);
  63          $generator->enrol_user($s1->id, $c1->id, 'student', 'manual', $yesterday);
  64  
  65          // Enrol s1 by self and manual methods to c2.
  66          $generator->enrol_user($s1->id, $c2->id, 'student', 'self');
  67          $generator->enrol_user($s1->id, $c2->id, 'student', 'manual', $tomorrow);
  68  
  69          // Enrol s1 by self and manual methods to c3.
  70          $generator->enrol_user($s1->id, $c3->id, 'student', 'self', $lastweek);
  71          $generator->enrol_user($s1->id, $c3->id, 'student');
  72  
  73          // Enrol the rest.
  74          foreach ([$c1, $c2, $c3] as $course) {
  75              // Enrol s2 by manual and self enrol methods.
  76              $generator->enrol_user($s2->id, $course->id, 'student', 'self');
  77              $generator->enrol_user($s2->id, $course->id, 'student', 'manual', $course->startdate);
  78  
  79              // Enrol t1 as teacher to these courses.
  80              $generator->enrol_user($t1->id, $course->id, 'editingteacher', 'manual', $course->startdate);
  81              $generator->enrol_user($t1->id, $course->id, 'editingteacher', 'manual', $course->startdate);
  82  
  83              // Enrol t2 as a non-editing teacher to these courses.
  84              $generator->enrol_user($t1->id, $course->id, 'teacher', 'manual', $course->startdate);
  85              $generator->enrol_user($t1->id, $course->id, 'teacher', 'manual', $course->startdate);
  86          }
  87  
  88          // The course completion table should be empty prior to running the task.
  89          $this->assertEquals(0, $DB->count_records('course_completions'));
  90  
  91          // Run the daily completion task.
  92          ob_start();
  93          $task = new completion_daily_task();
  94          $task->execute();
  95          ob_end_clean();
  96  
  97          // Confirm there are no completion records for teachers nor for courses that haven't started yet or without completion.
  98          list($tsql, $tparams) = $DB->get_in_or_equal([$t1->id, $t2->id], SQL_PARAMS_NAMED);
  99          list($csql, $cparams) = $DB->get_in_or_equal([$c2->id, $c3->id], SQL_PARAMS_NAMED);
 100          $select = "userid $tsql OR course $csql";
 101          $params = array_merge($tparams, $cparams);
 102          $this->assertEmpty($DB->get_records_select('course_completions', $select, $params));
 103  
 104          // We should have 2 completion records for both s1 and s2 in course c1.
 105          $this->assertCount(2, $DB->get_records('course_completions'));
 106  
 107          // Get s1's completion record from c1.
 108          $s1c1 = $DB->get_record('course_completions', ['userid' => $s1->id, 'course' => $c1->id]);
 109          $this->assertGreaterThanOrEqual($now, $s1c1->timeenrolled);
 110          $this->assertLessThanOrEqual(time(), $s1c1->timeenrolled);
 111  
 112          // Get s2's completion record from c1.
 113          $s2c1 = $DB->get_record('course_completions', ['userid' => $s2->id, 'course' => $c1->id]);
 114          $this->assertGreaterThanOrEqual($now, $s2c1->timeenrolled);
 115          $this->assertLessThanOrEqual(time(), $s2c1->timeenrolled);
 116      }
 117  }