first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
@@ -0,0 +1,188 @@
@core @core_courseformat @core_course @format_weeks @show_editor @javascript
Feature: Weeks format bulk activity actions.
In order to edit the course weeks
As a teacher
I need to be able to edit weeks in bulk.
Background:
Given the following "course" exists:
| fullname | Course 1 |
| shortname | C1 |
| category | 0 |
| numsections | 4 |
| format | weeks |
| startdate | 957139200 |
And the following "activities" exist:
| activity | name | intro | course | idnumber | section |
| assign | Activity sample 1 | Test assignment description | C1 | sample1 | 1 |
| assign | Activity sample 2 | Test assignment description | C1 | sample2 | 1 |
| assign | Activity sample 3 | Test assignment description | C1 | sample3 | 2 |
| assign | Activity sample 4 | Test assignment description | C1 | sample4 | 2 |
And the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@example.com |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
And I am on the "C1" "Course" page logged in as "teacher1"
And I turn editing mode on
And I click on "Bulk actions" "button"
And I should see "0 selected" in the "sticky-footer" "region"
Scenario: Bulk hide weeks
Given I should not see "Hidden from students" in the "Activity sample 1" "activity"
And I should not see "Hidden from students" in the "Activity sample 2" "activity"
And I should not see "Hidden from students" in the "Activity sample 3" "activity"
And I should not see "Hidden from students" in the "Activity sample 4" "activity"
And I should not see "Hidden from students" in the "1 May - 7 May" "section"
And I should not see "Hidden from students" in the "8 May - 14 May" "section"
And I should not see "Hidden from students" in the "15 May - 21 May" "section"
And I should not see "Hidden from students" in the "22 May - 28 May" "section"
When I click on "Select section 1 May - 7 May" "checkbox"
And I click on "Select section 8 May - 14 May" "checkbox"
And I should see "2 selected" in the "sticky-footer" "region"
And I click on "Sections availability" "button" in the "sticky-footer" "region"
And I click on "Hide on course page" "radio" in the "Sections availability" "dialogue"
And I click on "Apply" "button" in the "Sections availability" "dialogue"
Then I should see "Hidden from students" in the "Activity sample 1" "activity"
And I should see "Hidden from students" in the "Activity sample 2" "activity"
And I should see "Hidden from students" in the "Activity sample 3" "activity"
And I should see "Hidden from students" in the "Activity sample 4" "activity"
And I should see "Hidden from students" in the "1 May - 7 May" "section"
And I should see "Hidden from students" in the "8 May - 14 May" "section"
And I should not see "Hidden from students" in the "15 May - 21 May" "section"
And I should not see "Hidden from students" in the "22 May - 28 May" "section"
And I should see "0 selected" in the "sticky-footer" "region"
Scenario: Bulk show weeks
Given I click on "Select section 1 May - 7 May" "checkbox"
And I click on "Select section 15 May - 21 May" "checkbox"
And I click on "Sections availability" "button" in the "sticky-footer" "region"
And I click on "Hide on course page" "radio" in the "Sections availability" "dialogue"
And I click on "Apply" "button" in the "Sections availability" "dialogue"
And I should see "Hidden from students" in the "Activity sample 1" "activity"
And I should see "Hidden from students" in the "Activity sample 2" "activity"
And I should not see "Hidden from students" in the "Activity sample 3" "activity"
And I should not see "Hidden from students" in the "Activity sample 4" "activity"
And I should see "Hidden from students" in the "1 May - 7 May" "section"
And I should not see "Hidden from students" in the "8 May - 14 May" "section"
And I should see "Hidden from students" in the "15 May - 21 May" "section"
And I should not see "Hidden from students" in the "22 May - 28 May" "section"
When I click on "Select section 1 May - 7 May" "checkbox"
And I click on "Select section 8 May - 14 May" "checkbox"
And I should see "2 selected" in the "sticky-footer" "region"
And I click on "Sections availability" "button" in the "sticky-footer" "region"
And I click on "Show on course page" "radio" in the "Sections availability" "dialogue"
And I click on "Apply" "button" in the "Sections availability" "dialogue"
Then I should not see "Hidden from students" in the "Activity sample 1" "activity"
And I should not see "Hidden from students" in the "Activity sample 2" "activity"
And I should not see "Hidden from students" in the "Activity sample 3" "activity"
And I should not see "Hidden from students" in the "Activity sample 4" "activity"
And I should not see "Hidden from students" in the "1 May - 7 May" "section"
And I should not see "Hidden from students" in the "8 May - 14 May" "section"
And I should see "Hidden from students" in the "15 May - 21 May" "section"
And I should not see "Hidden from students" in the "22 May - 28 May" "section"
Scenario: Delete a single week using bulk action
Given I should see "1 May - 7 May" in the "region-main" "region"
And I should see "8 May - 14 May" in the "region-main" "region"
And I should see "15 May - 21 May" in the "region-main" "region"
And I should see "22 May - 28 May" in the "region-main" "region"
And I should see "Activity sample 1" in the "1 May - 7 May" "section"
And I should see "Activity sample 2" in the "1 May - 7 May" "section"
And I should see "Activity sample 3" in the "8 May - 14 May" "section"
And I should see "Activity sample 4" in the "8 May - 14 May" "section"
And I click on "Select section 1 May - 7 May" "checkbox"
And I click on "Select section 8 May - 14 May" "checkbox"
And I should see "2 selected" in the "sticky-footer" "region"
When I click on "Delete sections" "button" in the "sticky-footer" "region"
And I click on "Delete" "button" in the "Delete selected sections?" "dialogue"
Then I should see "1 May - 7 May" in the "region-main" "region"
And I should see "8 May - 14 May" in the "region-main" "region"
And I should not see "15 May - 21 May" in the "region-main" "region"
And I should not see "22 May - 28 May" in the "region-main" "region"
And I should not see "Activity sample 1" in the "1 May - 7 May" "section"
And I should not see "Activity sample 2" in the "1 May - 7 May" "section"
And I should not see "Activity sample 3" in the "8 May - 14 May" "section"
And I should not see "Activity sample 4" in the "8 May - 14 May" "section"
And I should see "0 selected" in the "sticky-footer" "region"
Scenario: Delete several weeks in bulk
Given I should see "1 May - 7 May" in the "region-main" "region"
And I should see "8 May - 14 May" in the "region-main" "region"
And I should see "15 May - 21 May" in the "region-main" "region"
And I should see "22 May - 28 May" in the "region-main" "region"
And I should see "Activity sample 1" in the "1 May - 7 May" "section"
And I should see "Activity sample 2" in the "1 May - 7 May" "section"
And I should see "Activity sample 3" in the "8 May - 14 May" "section"
And I should see "Activity sample 4" in the "8 May - 14 May" "section"
And I click on "Select section 8 May - 14 May" "checkbox"
And I click on "Select section 15 May - 21 May" "checkbox"
And I should see "2 selected" in the "sticky-footer" "region"
When I click on "Delete sections" "button" in the "sticky-footer" "region"
And I click on "Delete" "button" in the "Delete selected sections?" "dialogue"
Then I should see "1 May - 7 May" in the "region-main" "region"
And I should see "8 May - 14 May" in the "region-main" "region"
And I should not see "15 May - 21 May" in the "region-main" "region"
And I should not see "22 May - 28 May" in the "region-main" "region"
And I should see "Activity sample 1" in the "1 May - 7 May" "section"
And I should see "Activity sample 1" in the "1 May - 7 May" "section"
And I should see "Activity sample 2" in the "1 May - 7 May" "section"
And I should not see "Activity sample 3" in the "8 May - 14 May" "section"
And I should not see "Activity sample 4" in the "8 May - 14 May" "section"
And I should see "0 selected" in the "sticky-footer" "region"
Scenario: Bulk move sections after general section
Given I set the field "Edit section name" in the "8 May - 14 May" "section" to "Move one"
And I set the field "Edit section name" in the "15 May - 21 May" "section" to "Move two"
And I click on "Select section Move one" "checkbox"
And I click on "Select section Move two" "checkbox"
And I should see "2 selected" in the "sticky-footer" "region"
When I click on "Move sections" "button" in the "sticky-footer" "region"
And I click on "General" "link" in the "Move selected sections" "dialogue"
# Check activities are moved with the weeks.
Then I should see "Activity sample 1" in the "15 May - 21 May" "section"
And I should see "Activity sample 2" in the "15 May - 21 May" "section"
And I should see "Activity sample 3" in the "Move one" "section"
And I should see "Activity sample 4" in the "Move one" "section"
# Check new section order.
And "Move one" "section" should appear after "General" "section"
And "Move two" "section" should appear after "Move one" "section"
And "15 May - 21 May" "section" should appear after "Move two" "section"
And "22 May - 28 May" "section" should appear after "15 May - 21 May" "section"
Scenario: Bulk move sections at the end of the course
Given I set the field "Edit section name" in the "15 May - 21 May" "section" to "Move me"
And I click on "Select section 8 May - 14 May" "checkbox"
And I click on "Select section Move me" "checkbox"
And I should see "2 selected" in the "sticky-footer" "region"
When I click on "Move sections" "button" in the "sticky-footer" "region"
And I click on "22 May - 28 May" "link" in the "Move selected sections" "dialogue"
# Check activities are moved with the weeks.
Then I should see "Activity sample 1" in the "1 May - 7 May" "section"
And I should see "Activity sample 2" in the "1 May - 7 May" "section"
And I should see "Activity sample 3" in the "15 May - 21 May" "section"
And I should see "Activity sample 4" in the "15 May - 21 May" "section"
# Check new section order.
And "1 May - 7 May" "section" should appear after "General" "section"
And "8 May - 14 May" "section" should appear after "1 May - 7 May" "section"
And "15 May - 21 May" "section" should appear after "8 May - 14 May" "section"
And "Move me" "section" should appear after "15 May - 21 May" "section"
Scenario: Bulk move sections in the middle of the course
Given I set the field "Edit section name" in the "22 May - 28 May" "section" to "Move me"
And I click on "Select section 1 May - 7 May" "checkbox"
And I click on "Select section Move me" "checkbox"
And I should see "2 selected" in the "sticky-footer" "region"
When I click on "Move sections" "button" in the "sticky-footer" "region"
And I click on "8 May - 14 May" "link" in the "Move selected sections" "dialogue"
# Check activities are moved with the weeks.
Then I should see "Activity sample 1" in the "8 May - 14 May" "section"
And I should see "Activity sample 2" in the "8 May - 14 May" "section"
And I should see "Activity sample 3" in the "1 May - 7 May" "section"
And I should see "Activity sample 4" in the "1 May - 7 May" "section"
# Check new section order.
And "1 May - 7 May" "section" should appear after "General" "section"
And "8 May - 14 May" "section" should appear after "1 May - 7 May" "section"
And "Move me" "section" should appear after "8 May - 14 May" "section"
And "22 May - 28 May" "section" should appear after "Move me" "section"
@@ -0,0 +1,19 @@
@format @format_weeks
Feature: The current week should be highlighted in the course.
In order to know which is the current week
As a user
I need to see the current week highlighted
Scenario: Highlight first week
Given the following "courses" exist:
| fullname | shortname | format | coursedisplay | numsections | startdate |
| Course 1 | C1 | weeks | 0 | 5 | ##yesterday## |
When I am on the "C1" "Course" page logged in as "admin"
Then I should see "Current week" in the "#section-1" "css_element"
Scenario: Highlight next week
Given the following "courses" exist:
| fullname | shortname | format | coursedisplay | numsections | startdate |
| Course 1 | C1 | weeks | 0 | 5 | ##monday last week## |
When I am on the "C1" "Course" page logged in as "admin"
Then I should see "Current week" in the "#section-2" "css_element"
@@ -0,0 +1,83 @@
@format @format_weeks
Feature: Sections can be edited and deleted in weekly sections format
In order to rearrange my course contents
As a teacher
I need to edit and delete sections
Background:
Given the following "users" exist:
| username | firstname | lastname | email |
| teacher1 | Teacher | 1 | teacher1@example.com |
And the following "courses" exist:
| fullname | shortname | format | coursedisplay | numsections | startdate |
| Course 1 | C1 | weeks | 0 | 5 | 957139200 |
And the following "activities" exist:
| activity | name | intro | course | idnumber | section |
| assign | Test assignment name | Test assignment description | C1 | assign1 | 0 |
| book | Test book name | | C1 | book1 | 1 |
| lesson | Test lesson name | Test lesson description | C1 | lesson1 | 4 |
| choice | Test choice name | Test choice description | C1 | choice1 | 5 |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
And I log in as "teacher1"
And I am on "Course 1" course homepage with editing mode on
Scenario: View the default name of the general section in weeks format
When I edit the section "0"
Then the field "Section name" matches value ""
And I should see "General"
Scenario: Edit the default name of the general section in weeks format
When I edit the section "0" and I fill the form with:
| Section name | This is the general section |
Then I should see "This is the general section" in the "page" "region"
Scenario: View the default name of the second section in weeks format
When I edit the section "2"
Then the field "Section name" matches value ""
And I should see "8 May - 14 May"
Scenario: Edit section summary in weeks format
When I edit the section "2" and I fill the form with:
| Description | Welcome to section 2 |
Then I should see "Welcome to section 2" in the "page" "region"
Scenario: Edit section default name in weeks format
Given I should see "8 May - 14 May" in the "8 May - 14 May" "section"
When I edit the section "2" and I fill the form with:
| Section name | This is the second week |
Then I should see "This is the second week" in the "page" "region"
And I should not see "8 May - 14 May"
@javascript
Scenario: Inline edit section name in weeks format
When I set the field "Edit section name" in the "1 May - 7 May" "section" to "Midterm evaluation"
Then I should not see "1 May - 7 May" in the "region-main" "region"
And "New name for week" "field" should not exist
And I should see "Midterm evaluation" in the "Midterm evaluation" "section"
And I am on "Course 1" course homepage
And I should not see "1 May - 7 May" in the "region-main" "region"
And I should see "Midterm evaluation" in the "Midterm evaluation" "section"
Scenario: Deleting the last section in weeks format
Given I should see "29 May - 4 June" in the "29 May - 4 June" "section"
When I delete section "5"
Then I should see "Are you absolutely sure you want to completely delete \"29 May - 4 June\" and all the activities it contains?"
And I press "Delete"
And I should not see "29 May - 4 June"
And I should see "22 May - 28 May"
Scenario: Deleting the middle section in weeks format
Given I should see "29 May - 4 June" in the "29 May - 4 June" "section"
When I delete section "4"
And I press "Delete"
Then I should not see "29 May - 4 June"
And I should not see "Test lesson name"
And I should see "Test choice name" in the "22 May - 28 May" "section"
And I should see "22 May - 28 May"
@javascript
Scenario: Adding sections in weeks format
When I follow "Add week"
Then I should see "5 June - 11 June" in the "5 June - 11 June" "section"
@@ -0,0 +1,281 @@
<?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;
use core_external\external_api;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/course/lib.php');
/**
* format_weeks related unit tests
*
* @package format_weeks
* @copyright 2015 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \format_weeks
*/
class format_weeks_test extends \advanced_testcase {
/**
* Tests for format_weeks::get_section_name method with default section names.
*/
public function test_get_section_name(): void {
global $DB;
$this->resetAfterTest(true);
// Generate a course with 5 sections.
$generator = $this->getDataGenerator();
$numsections = 5;
$course = $generator->create_course(array('numsections' => $numsections, 'format' => 'weeks'),
array('createsections' => true));
// Get section names for course.
$coursesections = $DB->get_records('course_sections', array('course' => $course->id));
// Test get_section_name with default section names.
$courseformat = course_get_format($course);
foreach ($coursesections as $section) {
// Assert that with unmodified section names, get_section_name returns the same result as get_default_section_name.
$this->assertEquals($courseformat->get_default_section_name($section), $courseformat->get_section_name($section));
}
}
/**
* Tests for format_weeks::get_section_name method with modified section names.
*/
public function test_get_section_name_customised(): void {
global $DB;
$this->resetAfterTest(true);
// Generate a course with 5 sections.
$generator = $this->getDataGenerator();
$numsections = 5;
$course = $generator->create_course(array('numsections' => $numsections, 'format' => 'weeks'),
array('createsections' => true));
// Get section names for course.
$coursesections = $DB->get_records('course_sections', array('course' => $course->id));
// Modify section names.
$customname = "Custom Section";
foreach ($coursesections as $section) {
$section->name = "$customname $section->section";
$DB->update_record('course_sections', $section);
}
// Requery updated section names then test get_section_name.
$coursesections = $DB->get_records('course_sections', array('course' => $course->id));
$courseformat = course_get_format($course);
foreach ($coursesections as $section) {
// Assert that with modified section names, get_section_name returns the modified section name.
$this->assertEquals($section->name, $courseformat->get_section_name($section));
}
}
/**
* Tests for format_weeks::get_default_section_name.
*/
public function test_get_default_section_name(): void {
global $DB;
$this->resetAfterTest(true);
// Generate a course with 5 sections.
$generator = $this->getDataGenerator();
$numsections = 5;
$course = $generator->create_course(array('numsections' => $numsections, 'format' => 'weeks'),
array('createsections' => true));
// Get section names for course.
$coursesections = $DB->get_records('course_sections', array('course' => $course->id));
// Test get_default_section_name with default section names.
$courseformat = course_get_format($course);
foreach ($coursesections as $section) {
if ($section->section == 0) {
$sectionname = get_string('section0name', 'format_weeks');
$this->assertEquals($sectionname, $courseformat->get_default_section_name($section));
} else {
$dates = $courseformat->get_section_dates($section);
$dates->end = ($dates->end - 86400);
$dateformat = get_string('strftimedateshort');
$weekday = userdate($dates->start, $dateformat);
$endweekday = userdate($dates->end, $dateformat);
$sectionname = $weekday.' - '.$endweekday;
$this->assertEquals($sectionname, $courseformat->get_default_section_name($section));
}
}
}
/**
* Test web service updating section name
*/
public function test_update_inplace_editable(): void {
global $CFG, $DB, $PAGE;
require_once($CFG->dirroot . '/lib/external/externallib.php');
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
$course = $this->getDataGenerator()->create_course(array('numsections' => 5, 'format' => 'weeks'),
array('createsections' => true));
$section = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 2));
// Call webservice without necessary permissions.
try {
\core_external::update_inplace_editable('format_weeks', 'sectionname', $section->id, 'New section name');
$this->fail('Exception expected');
} catch (\moodle_exception $e) {
$this->assertEquals('Course or activity not accessible. (Not enrolled)',
$e->getMessage());
}
// Change to teacher and make sure that section name can be updated using web service update_inplace_editable().
$teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
$this->getDataGenerator()->enrol_user($user->id, $course->id, $teacherrole->id);
$res = \core_external::update_inplace_editable('format_weeks', 'sectionname', $section->id, 'New section name');
$res = external_api::clean_returnvalue(\core_external::update_inplace_editable_returns(), $res);
$this->assertEquals('New section name', $res['value']);
$this->assertEquals('New section name', $DB->get_field('course_sections', 'name', array('id' => $section->id)));
}
/**
* Test callback updating section name
*/
public function test_inplace_editable(): void {
global $CFG, $DB, $PAGE;
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course(array('numsections' => 5, 'format' => 'weeks'),
array('createsections' => true));
$teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
$this->getDataGenerator()->enrol_user($user->id, $course->id, $teacherrole->id);
$this->setUser($user);
$section = $DB->get_record('course_sections', array('course' => $course->id, 'section' => 2));
// Call callback format_weeks_inplace_editable() directly.
$tmpl = component_callback('format_weeks', 'inplace_editable', array('sectionname', $section->id, 'Rename me again'));
$this->assertInstanceOf('core\output\inplace_editable', $tmpl);
$res = $tmpl->export_for_template($PAGE->get_renderer('core'));
$this->assertEquals('Rename me again', $res['value']);
$this->assertEquals('Rename me again', $DB->get_field('course_sections', 'name', array('id' => $section->id)));
// Try updating using callback from mismatching course format.
try {
$tmpl = component_callback('format_topics', 'inplace_editable', array('sectionname', $section->id, 'New name'));
$this->fail('Exception expected');
} catch (\moodle_exception $e) {
$this->assertEquals(1, preg_match('/^Can\'t find data record in database/', $e->getMessage()));
}
}
/**
* Test get_default_course_enddate.
*
* @return void
*/
public function test_default_course_enddate(): void {
global $CFG, $DB, $PAGE;
$this->resetAfterTest(true);
require_once($CFG->dirroot . '/course/tests/fixtures/testable_course_edit_form.php');
$this->setTimezone('UTC');
$params = array('format' => 'weeks', 'numsections' => 5, 'startdate' => 1445644800);
$course = $this->getDataGenerator()->create_course($params);
$category = $DB->get_record('course_categories', array('id' => $course->category));
$args = [
'course' => $course,
'category' => $category,
'editoroptions' => [
'context' => \context_course::instance($course->id),
'subdirs' => 0
],
'returnto' => new \moodle_url('/'),
'returnurl' => new \moodle_url('/'),
];
$PAGE->set_course($course);
$courseform = new \testable_course_edit_form(null, $args);
$courseform->definition_after_data();
// Calculate the expected end date.
$enddate = $params['startdate'] + (WEEKSECS * $params['numsections']);
$weeksformat = course_get_format($course->id);
$this->assertEquals($enddate, $weeksformat->get_default_course_enddate($courseform->get_quick_form()));
}
/**
* Test for get_view_url().
*
* @covers ::get_view_url
*/
public function test_get_view_url(): void {
global $CFG;
$this->resetAfterTest();
// Generate a course with two sections (0 and 1) and two modules.
$generator = $this->getDataGenerator();
$course1 = $generator->create_course(array('format' => 'weeks'));
course_create_sections_if_missing($course1, array(0, 1));
$data = (object)['id' => $course1->id];
$format = course_get_format($course1);
$format->update_course_format_options($data);
// In page.
$this->assertNotEmpty($format->get_view_url(null));
$this->assertNotEmpty($format->get_view_url(0));
$this->assertNotEmpty($format->get_view_url(1));
// Navigation.
$this->assertStringContainsString('course/view.php', $format->get_view_url(0));
$this->assertStringContainsString('course/view.php', $format->get_view_url(1));
$this->assertStringContainsString('course/section.php', $format->get_view_url(0, ['navigation' => 1]));
$this->assertStringContainsString('course/section.php', $format->get_view_url(1, ['navigation' => 1]));
// When sr parameter is defined, the section.php page should be returned.
$this->assertStringContainsString('course/section.php', $format->get_view_url(0, ['sr' => 1]));
$this->assertStringContainsString('course/section.php', $format->get_view_url(1, ['sr' => 1]));
$this->assertStringContainsString('course/section.php', $format->get_view_url(0, ['sr' => 0]));
$this->assertStringContainsString('course/section.php', $format->get_view_url(1, ['sr' => 0]));
}
/**
* Test get_required_jsfiles().
*
* @covers ::get_required_jsfiles
*/
public function test_get_required_jsfiles(): void {
$this->resetAfterTest();
$generator = $this->getDataGenerator();
$course = $generator->create_course(['format' => 'weeks']);
$format = course_get_format($course);
$this->assertEmpty($format->get_required_jsfiles());
}
}
+201
View File
@@ -0,0 +1,201 @@
<?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(): void {
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(): void {
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(): void {
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(): void {
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(): void {
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(): void {
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);
}
}