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] [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  namespace core_completion;
  18  
  19  /**
  20   * Test completion API.
  21   *
  22   * @package core_completion
  23   * @category test
  24   * @copyright 2017 Mark Nelson <markn@moodle.com>
  25   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  class api_test extends \advanced_testcase {
  28  
  29      /**
  30       * Test setup.
  31       */
  32      public function setUp(): void {
  33          $this->resetAfterTest();
  34      }
  35  
  36      public function test_update_completion_date_event() {
  37          global $CFG, $DB;
  38  
  39          $this->setAdminUser();
  40  
  41          // Create a course.
  42          $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
  43  
  44          // Create an assign activity.
  45          $time = time();
  46          $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
  47  
  48          // Create the completion event.
  49          $CFG->enablecompletion = true;
  50          \core_completion\api::update_completion_date_event($assign->cmid, 'assign', $assign, $time);
  51  
  52          // Check that there is now an event in the database.
  53          $events = $DB->get_records('event');
  54          $this->assertCount(1, $events);
  55  
  56          // Get the event.
  57          $event = reset($events);
  58  
  59          // Confirm the event is correct.
  60          $this->assertEquals('assign', $event->modulename);
  61          $this->assertEquals($assign->id, $event->instance);
  62          $this->assertEquals(CALENDAR_EVENT_TYPE_ACTION, $event->type);
  63          $this->assertEquals(\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED, $event->eventtype);
  64          $this->assertEquals($time, $event->timestart);
  65          $this->assertEquals($time, $event->timesort);
  66  
  67          require_once($CFG->dirroot . '/course/lib.php');
  68          // Delete the module.
  69          course_delete_module($assign->cmid);
  70  
  71          // Check we don't get a failure when called on a deleted module.
  72          \core_completion\api::update_completion_date_event($assign->cmid, 'assign', null, $time);
  73      }
  74  
  75      public function test_update_completion_date_event_update() {
  76          global $CFG, $DB;
  77  
  78          $this->setAdminUser();
  79  
  80          // Create a course.
  81          $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
  82  
  83          // Create an assign activity.
  84          $time = time();
  85          $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
  86  
  87          // Create the event.
  88          $CFG->enablecompletion = true;
  89          \core_completion\api::update_completion_date_event($assign->cmid, 'assign', $assign, $time);
  90  
  91          // Call it again, but this time with a different time.
  92          \core_completion\api::update_completion_date_event($assign->cmid, 'assign', $assign, $time + DAYSECS);
  93  
  94          // Check that there is still only one event in the database.
  95          $events = $DB->get_records('event');
  96          $this->assertCount(1, $events);
  97  
  98          // Get the event.
  99          $event = reset($events);
 100  
 101          // Confirm that the event has been updated.
 102          $this->assertEquals('assign', $event->modulename);
 103          $this->assertEquals($assign->id, $event->instance);
 104          $this->assertEquals(CALENDAR_EVENT_TYPE_ACTION, $event->type);
 105          $this->assertEquals(\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED, $event->eventtype);
 106          $this->assertEquals($time + DAYSECS, $event->timestart);
 107          $this->assertEquals($time + DAYSECS, $event->timesort);
 108      }
 109  
 110      public function test_update_completion_date_event_delete() {
 111          global $CFG, $DB;
 112  
 113          $this->setAdminUser();
 114  
 115          // Create a course.
 116          $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
 117  
 118          // Create an assign activity.
 119          $time = time();
 120          $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
 121  
 122          // Create the event.
 123          $CFG->enablecompletion = true;
 124          \core_completion\api::update_completion_date_event($assign->cmid, 'assign', $assign, $time);
 125  
 126          // Call it again, but the time specified as null.
 127          \core_completion\api::update_completion_date_event($assign->cmid, 'assign', $assign, null);
 128  
 129          // Check that there is no event in the database.
 130          $this->assertEquals(0, $DB->count_records('event'));
 131      }
 132  
 133      public function test_update_completion_date_event_completion_disabled() {
 134          global $CFG, $DB;
 135  
 136          $this->setAdminUser();
 137  
 138          // Create a course.
 139          $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
 140  
 141          // Create an assign activity.
 142          $time = time();
 143          $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
 144  
 145          // Try and create the completion event with completion disabled.
 146          $CFG->enablecompletion = false;
 147          \core_completion\api::update_completion_date_event($assign->cmid, 'assign', $assign, $time);
 148  
 149          // Check that there is no event in the database.
 150          $this->assertEquals(0, $DB->count_records('event'));
 151      }
 152  
 153      public function test_update_completion_date_event_update_completion_disabled() {
 154          global $CFG, $DB;
 155  
 156          $this->setAdminUser();
 157  
 158          // Create a course.
 159          $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
 160  
 161          // Create an assign activity.
 162          $time = time();
 163          $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
 164  
 165          // Create the completion event.
 166          $CFG->enablecompletion = true;
 167          \core_completion\api::update_completion_date_event($assign->cmid, 'assign', $assign, $time);
 168  
 169          // Disable completion.
 170          $CFG->enablecompletion = false;
 171  
 172          // Try and update the completion date.
 173          \core_completion\api::update_completion_date_event($assign->cmid, 'assign', $assign, $time + DAYSECS);
 174  
 175          // Check that there is an event in the database.
 176          $events = $DB->get_records('event');
 177          $this->assertCount(1, $events);
 178  
 179          // Get the event.
 180          $event = reset($events);
 181  
 182          // Confirm the event has not changed.
 183          $this->assertEquals('assign', $event->modulename);
 184          $this->assertEquals($assign->id, $event->instance);
 185          $this->assertEquals(CALENDAR_EVENT_TYPE_ACTION, $event->type);
 186          $this->assertEquals(\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED, $event->eventtype);
 187          $this->assertEquals($time, $event->timestart);
 188          $this->assertEquals($time, $event->timesort);
 189      }
 190  
 191      public function test_update_completion_date_event_delete_completion_disabled() {
 192          global $CFG, $DB;
 193  
 194          $this->setAdminUser();
 195  
 196          // Create a course.
 197          $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
 198  
 199          // Create an assign activity.
 200          $time = time();
 201          $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id));
 202  
 203          // Create the completion event.
 204          $CFG->enablecompletion = true;
 205          \core_completion\api::update_completion_date_event($assign->cmid, 'assign', $assign, $time);
 206  
 207          // Disable completion.
 208          $CFG->enablecompletion = false;
 209  
 210          // Should still be able to delete completion events even when completion is disabled.
 211          \core_completion\api::update_completion_date_event($assign->cmid, 'assign', $assign, null);
 212  
 213          // Check that there is now no event in the database.
 214          $this->assertEquals(0, $DB->count_records('event'));
 215      }
 216  }