Files
moodle/course/format/weeks/tests/observer_test.php
T
Eloy Lafuente (stronk7) 4003e31983 MDL-73785 phpunit: Move more tests to use correct names and namespaces
Applied the following changes to various testcase classes:

- Namespaced with component[\level2-API]
- Moved to level2-API subdirectory when required.
- Fixed incorrect use statements with leading backslash.
- Remove file phpdoc block
- Remove MOODLE_INTERNAL if not needed.
- Changed code to point to global scope when needed.
- Fix some relative paths and comments here and there.
- All them passing individually.
- Complete runs passing too.

Special mention to:

- When belonging to other components and being valid api:
  - analytics related tests have been moved to tests/analytics subdir.
  - backup & restore related tests have been moved to tests/backup subdir.
  - events related tests have been moved to tests/event subdir.
  - privacy related tests have been moved to tests/privacy.
  - task related tests have been moved to tests/task subdir.
- Some simple renames, not including the component part anymore (not
  needed now that they are namespaced):
  - some xxxlib_test.php have been renamed lib_test.php
    (when they where testing the corresponding lib.php).
  - cache stores tests have been all renamed store_test, originally
    each one had its own name (file_test, apcu_test, redis_test...)
  - assign feedback tests have been all renamed feedback_test, originally
    each one had its own name (file_test, editpdf_test...)
2022-03-03 12:19:30 +01:00

202 lines
7.1 KiB
PHP

<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace format_weeks;
/**
* Unit tests for the event observers used by the weeks course format.
*
* @package format_weeks
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class observer_test extends \advanced_testcase {
/**
* Test setup.
*/
public function setUp(): void {
$this->resetAfterTest();
}
/**
* Tests when we update a course with automatic end date set.
*/
public function test_course_updated_with_automatic_end_date() {
global $DB;
// Generate a course with some sections.
$numsections = 6;
$startdate = time();
$course = $this->getDataGenerator()->create_course(array(
'numsections' => $numsections,
'format' => 'weeks',
'startdate' => $startdate,
'automaticenddate' => 1));
// Ok, let's update the course start date.
$newstartdate = $startdate + WEEKSECS;
update_course((object)['id' => $course->id, 'startdate' => $newstartdate]);
// Get the updated course end date.
$enddate = $DB->get_field('course', 'enddate', array('id' => $course->id));
$format = course_get_format($course->id);
$this->assertEquals($numsections, $format->get_last_section_number());
$this->assertEquals($newstartdate, $format->get_course()->startdate);
$dates = $format->get_section_dates($numsections);
$this->assertEquals($dates->end, $enddate);
}
/**
* Tests when we update a course with automatic end date set but no actual change is made.
*/
public function test_course_updated_with_automatic_end_date_no_change() {
global $DB;
// Generate a course with some sections.
$course = $this->getDataGenerator()->create_course(array(
'numsections' => 6,
'format' => 'weeks',
'startdate' => time(),
'automaticenddate' => 1));
// Get the end date from the DB as the results will have changed from $course above after observer processing.
$createenddate = $DB->get_field('course', 'enddate', array('id' => $course->id));
// Ok, let's update the course - but actually not change anything.
update_course((object)['id' => $course->id]);
// Get the updated course end date.
$updateenddate = $DB->get_field('course', 'enddate', array('id' => $course->id));
// Confirm nothing changed.
$this->assertEquals($createenddate, $updateenddate);
}
/**
* Tests when we update a course without automatic end date set.
*/
public function test_course_updated_without_automatic_end_date() {
global $DB;
// Generate a course with some sections.
$startdate = time();
$enddate = $startdate + WEEKSECS;
$course = $this->getDataGenerator()->create_course(array(
'numsections' => 6,
'format' => 'weeks',
'startdate' => $startdate,
'enddate' => $enddate,
'automaticenddate' => 0));
// Ok, let's update the course start date.
$newstartdate = $startdate + WEEKSECS;
update_course((object)['id' => $course->id, 'startdate' => $newstartdate]);
// Get the updated course end date.
$updateenddate = $DB->get_field('course', 'enddate', array('id' => $course->id));
// Confirm nothing changed.
$this->assertEquals($enddate, $updateenddate);
}
/**
* Tests when we adding a course section with automatic end date set.
*/
public function test_course_section_created_with_automatic_end_date() {
global $DB;
$numsections = 6;
$course = $this->getDataGenerator()->create_course(array(
'numsections' => $numsections,
'format' => 'weeks',
'startdate' => time(),
'automaticenddate' => 1));
// Add a section to the course.
course_create_section($course->id);
// Get the updated course end date.
$enddate = $DB->get_field('course', 'enddate', array('id' => $course->id));
$format = course_get_format($course->id);
$dates = $format->get_section_dates($numsections + 1);
// Confirm end date was updated.
$this->assertEquals($enddate, $dates->end);
}
/**
* Tests when we update a course without automatic end date set.
*/
public function test_create_section_without_automatic_end_date() {
global $DB;
// Generate a course with some sections.
$startdate = time();
$enddate = $startdate + WEEKSECS;
$course = $this->getDataGenerator()->create_course(array(
'numsections' => 6,
'format' => 'weeks',
'startdate' => $startdate,
'enddate' => $enddate,
'automaticenddate' => 0));
// Delete automatic end date from the database.
$DB->delete_records('course_format_options', ['courseid' => $course->id, 'name' => 'automaticenddate']);
// Create a new section.
course_create_section($course->id, 0);
// Get the updated course end date.
$updateenddate = $DB->get_field('course', 'enddate', array('id' => $course->id));
// Confirm enddate is automatic now - since automatic end date is not set it is assumed default (which is '1').
$format = course_get_format($course->id);
$this->assertEquals(7, $format->get_last_section_number());
$dates = $format->get_section_dates(7);
$this->assertEquals($dates->end, $updateenddate);
}
/**
* Tests when we deleting a course section with automatic end date set.
*/
public function test_course_section_deleted_with_automatic_end_date() {
global $DB;
// Generate a course with some sections.
$numsections = 6;
$course = $this->getDataGenerator()->create_course(array(
'numsections' => $numsections,
'format' => 'weeks',
'startdate' => time(),
'automaticenddate' => 1));
// Add a section to the course.
course_delete_section($course, $numsections);
// Get the updated course end date.
$enddate = $DB->get_field('course', 'enddate', array('id' => $course->id));
$format = course_get_format($course->id);
$dates = $format->get_section_dates($numsections - 1);
// Confirm end date was updated.
$this->assertEquals($enddate, $dates->end);
}
}