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   * Unit tests for the event observers used by the weeks course format.
  19   *
  20   * @package format_weeks
  21   * @copyright 2017 Mark Nelson <markn@moodle.com>
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * Unit tests for the event observers used by the weeks course format.
  29   *
  30   * @package format_weeks
  31   * @copyright 2017 Mark Nelson <markn@moodle.com>
  32   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class format_weeks_observer_testcase extends advanced_testcase {
  35  
  36      /**
  37       * Test setup.
  38       */
  39      public function setUp(): void {
  40          $this->resetAfterTest();
  41      }
  42  
  43      /**
  44       * Tests when we update a course with automatic end date set.
  45       */
  46      public function test_course_updated_with_automatic_end_date() {
  47          global $DB;
  48  
  49          // Generate a course with some sections.
  50          $numsections = 6;
  51          $startdate = time();
  52          $course = $this->getDataGenerator()->create_course(array(
  53              'numsections' => $numsections,
  54              'format' => 'weeks',
  55              'startdate' => $startdate,
  56              'automaticenddate' => 1));
  57  
  58          // Ok, let's update the course start date.
  59          $newstartdate = $startdate + WEEKSECS;
  60          update_course((object)['id' => $course->id, 'startdate' => $newstartdate]);
  61  
  62          // Get the updated course end date.
  63          $enddate = $DB->get_field('course', 'enddate', array('id' => $course->id));
  64  
  65          $format = course_get_format($course->id);
  66          $this->assertEquals($numsections, $format->get_last_section_number());
  67          $this->assertEquals($newstartdate, $format->get_course()->startdate);
  68          $dates = $format->get_section_dates($numsections);
  69          $this->assertEquals($dates->end, $enddate);
  70      }
  71  
  72      /**
  73       * Tests when we update a course with automatic end date set but no actual change is made.
  74       */
  75      public function test_course_updated_with_automatic_end_date_no_change() {
  76          global $DB;
  77  
  78          // Generate a course with some sections.
  79          $course = $this->getDataGenerator()->create_course(array(
  80              'numsections' => 6,
  81              'format' => 'weeks',
  82              'startdate' => time(),
  83              'automaticenddate' => 1));
  84  
  85          // Get the end date from the DB as the results will have changed from $course above after observer processing.
  86          $createenddate = $DB->get_field('course', 'enddate', array('id' => $course->id));
  87  
  88          // Ok, let's update the course - but actually not change anything.
  89          update_course((object)['id' => $course->id]);
  90  
  91          // Get the updated course end date.
  92          $updateenddate = $DB->get_field('course', 'enddate', array('id' => $course->id));
  93  
  94          // Confirm nothing changed.
  95          $this->assertEquals($createenddate, $updateenddate);
  96      }
  97  
  98      /**
  99       * Tests when we update a course without automatic end date set.
 100       */
 101      public function test_course_updated_without_automatic_end_date() {
 102          global $DB;
 103  
 104          // Generate a course with some sections.
 105          $startdate = time();
 106          $enddate = $startdate + WEEKSECS;
 107          $course = $this->getDataGenerator()->create_course(array(
 108              'numsections' => 6,
 109              'format' => 'weeks',
 110              'startdate' => $startdate,
 111              'enddate' => $enddate,
 112              'automaticenddate' => 0));
 113  
 114          // Ok, let's update the course start date.
 115          $newstartdate = $startdate + WEEKSECS;
 116          update_course((object)['id' => $course->id, 'startdate' => $newstartdate]);
 117  
 118          // Get the updated course end date.
 119          $updateenddate = $DB->get_field('course', 'enddate', array('id' => $course->id));
 120  
 121          // Confirm nothing changed.
 122          $this->assertEquals($enddate, $updateenddate);
 123      }
 124  
 125      /**
 126       * Tests when we adding a course section with automatic end date set.
 127       */
 128      public function test_course_section_created_with_automatic_end_date() {
 129          global $DB;
 130  
 131          $numsections = 6;
 132          $course = $this->getDataGenerator()->create_course(array(
 133              'numsections' => $numsections,
 134              'format' => 'weeks',
 135              'startdate' => time(),
 136              'automaticenddate' => 1));
 137  
 138          // Add a section to the course.
 139          course_create_section($course->id);
 140  
 141          // Get the updated course end date.
 142          $enddate = $DB->get_field('course', 'enddate', array('id' => $course->id));
 143  
 144          $format = course_get_format($course->id);
 145          $dates = $format->get_section_dates($numsections + 1);
 146  
 147          // Confirm end date was updated.
 148          $this->assertEquals($enddate, $dates->end);
 149      }
 150  
 151      /**
 152       * Tests when we update a course without automatic end date set.
 153       */
 154      public function test_create_section_without_automatic_end_date() {
 155          global $DB;
 156  
 157          // Generate a course with some sections.
 158          $startdate = time();
 159          $enddate = $startdate + WEEKSECS;
 160          $course = $this->getDataGenerator()->create_course(array(
 161              'numsections' => 6,
 162              'format' => 'weeks',
 163              'startdate' => $startdate,
 164              'enddate' => $enddate,
 165              'automaticenddate' => 0));
 166  
 167          // Delete automatic end date from the database.
 168          $DB->delete_records('course_format_options', ['courseid' => $course->id, 'name' => 'automaticenddate']);
 169  
 170          // Create a new section.
 171          course_create_section($course->id, 0);
 172  
 173          // Get the updated course end date.
 174          $updateenddate = $DB->get_field('course', 'enddate', array('id' => $course->id));
 175  
 176          // Confirm enddate is automatic now - since automatic end date is not set it is assumed default (which is '1').
 177          $format = course_get_format($course->id);
 178          $this->assertEquals(7, $format->get_last_section_number());
 179          $dates = $format->get_section_dates(7);
 180          $this->assertEquals($dates->end, $updateenddate);
 181      }
 182  
 183      /**
 184       * Tests when we deleting a course section with automatic end date set.
 185       */
 186      public function test_course_section_deleted_with_automatic_end_date() {
 187          global $DB;
 188  
 189          // Generate a course with some sections.
 190          $numsections = 6;
 191          $course = $this->getDataGenerator()->create_course(array(
 192              'numsections' => $numsections,
 193              'format' => 'weeks',
 194              'startdate' => time(),
 195              'automaticenddate' => 1));
 196  
 197          // Add a section to the course.
 198          course_delete_section($course, $numsections);
 199  
 200          // Get the updated course end date.
 201          $enddate = $DB->get_field('course', 'enddate', array('id' => $course->id));
 202  
 203          $format = course_get_format($course->id);
 204          $dates = $format->get_section_dates($numsections - 1);
 205  
 206          // Confirm end date was updated.
 207          $this->assertEquals($enddate, $dates->end);
 208      }
 209  }