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