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] [Versions 39 and 310]

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