Search moodle.org's
Developer Documentation

  • 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 37 and 311] [Versions 38 and 311] [Versions 39 and 311]

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