mirror of
https://github.com/moodle/moodle.git
synced 2025-04-19 15:32:32 +02:00
MDL-65809 upgrade: remove format_xxx_upgrade_remove_numsections
These functions were used only by deleted upgrade steps so it's safe to proceed with straight deletion, considering them internal. Deletion has been documented in corresponding upgrade.txt files: - format_topics_upgrade_remove_numsections() - format_topics_upgrade_hide_extra_sections() - format_topics_upgrade_add_empty_sections() - format_weeks_upgrade_remove_numsections() - format_weeks_upgrade_hide_extra_sections() - format_weeks_upgrade_add_empty_sections()
This commit is contained in:
parent
089e918033
commit
3f224a1a41
@ -33,16 +33,6 @@ defined('MOODLE_INTERNAL') || die();
|
||||
function xmldb_format_topics_upgrade($oldversion) {
|
||||
global $CFG, $DB;
|
||||
|
||||
require_once($CFG->dirroot . '/course/format/topics/db/upgradelib.php');
|
||||
|
||||
if ($oldversion < 2017020200) {
|
||||
|
||||
// Remove 'numsections' option and hide or delete orphaned sections.
|
||||
format_topics_upgrade_remove_numsections();
|
||||
|
||||
upgrade_plugin_savepoint(true, 2017020200, 'format', 'topics');
|
||||
}
|
||||
|
||||
// Automatically generated Moodle v3.3.0 release upgrade line.
|
||||
// Put any upgrade step following this.
|
||||
|
||||
|
@ -1,144 +0,0 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Upgrade scripts for course format "Topics"
|
||||
*
|
||||
* @package format_topics
|
||||
* @copyright 2017 Marina Glancy
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* This method finds all courses in 'topics' format that have actual number of sections
|
||||
* different than their 'numsections' course format option.
|
||||
*
|
||||
* For courses where there are more sections than numsections, we call
|
||||
* {@link format_topics_upgrade_hide_extra_sections()} and
|
||||
* either delete or hide "orphaned" sections. For courses where there are fewer sections
|
||||
* than numsections, we call {@link format_topics_upgrade_add_empty_sections()} to add
|
||||
* these sections.
|
||||
*/
|
||||
function format_topics_upgrade_remove_numsections() {
|
||||
global $DB;
|
||||
|
||||
$sql1 = "SELECT c.id, max(cs.section) AS sectionsactual
|
||||
FROM {course} c
|
||||
JOIN {course_sections} cs ON cs.course = c.id
|
||||
WHERE c.format = :format1
|
||||
GROUP BY c.id";
|
||||
|
||||
$sql2 = "SELECT c.id, n.value AS numsections
|
||||
FROM {course} c
|
||||
JOIN {course_format_options} n ON n.courseid = c.id AND n.format = :format1 AND n.name = :numsections AND n.sectionid = 0
|
||||
WHERE c.format = :format2";
|
||||
|
||||
$params = ['format1' => 'topics', 'format2' => 'topics', 'numsections' => 'numsections'];
|
||||
|
||||
$actual = $DB->get_records_sql_menu($sql1, $params);
|
||||
$numsections = $DB->get_records_sql_menu($sql2, $params);
|
||||
$needfixing = [];
|
||||
$needsections = [];
|
||||
|
||||
$defaultnumsections = get_config('moodlecourse', 'numsections');
|
||||
|
||||
foreach ($actual as $courseid => $sectionsactual) {
|
||||
if (array_key_exists($courseid, $numsections)) {
|
||||
$n = (int)$numsections[$courseid];
|
||||
} else {
|
||||
$n = $defaultnumsections;
|
||||
}
|
||||
if ($sectionsactual > $n) {
|
||||
$needfixing[$courseid] = $n;
|
||||
} else if ($sectionsactual < $n) {
|
||||
$needsections[$courseid] = $n;
|
||||
}
|
||||
}
|
||||
unset($actual);
|
||||
unset($numsections);
|
||||
|
||||
foreach ($needfixing as $courseid => $numsections) {
|
||||
format_topics_upgrade_hide_extra_sections($courseid, $numsections);
|
||||
}
|
||||
|
||||
foreach ($needsections as $courseid => $numsections) {
|
||||
format_topics_upgrade_add_empty_sections($courseid, $numsections);
|
||||
}
|
||||
|
||||
$DB->delete_records('course_format_options', ['format' => 'topics', 'sectionid' => 0, 'name' => 'numsections']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find all sections in the course with sectionnum bigger than numsections.
|
||||
* Either delete these sections or hide them
|
||||
*
|
||||
* We will only delete a section if it is completely empty and all sections below
|
||||
* it are also empty
|
||||
*
|
||||
* @param int $courseid
|
||||
* @param int $numsections
|
||||
*/
|
||||
function format_topics_upgrade_hide_extra_sections($courseid, $numsections) {
|
||||
global $DB;
|
||||
$sections = $DB->get_records_sql('SELECT id, name, summary, sequence, visible
|
||||
FROM {course_sections}
|
||||
WHERE course = ? AND section > ?
|
||||
ORDER BY section DESC', [$courseid, $numsections]);
|
||||
$candelete = true;
|
||||
$tohide = [];
|
||||
$todelete = [];
|
||||
foreach ($sections as $section) {
|
||||
if ($candelete && (!empty($section->summary) || !empty($section->sequence) || !empty($section->name))) {
|
||||
$candelete = false;
|
||||
}
|
||||
if ($candelete) {
|
||||
$todelete[] = $section->id;
|
||||
} else if ($section->visible) {
|
||||
$tohide[] = $section->id;
|
||||
}
|
||||
}
|
||||
if ($todelete) {
|
||||
// Delete empty sections in the end.
|
||||
// This is an upgrade script - no events or cache resets are needed.
|
||||
// We also know that these sections do not have any modules so it is safe to just delete records in the table.
|
||||
$DB->delete_records_list('course_sections', 'id', $todelete);
|
||||
}
|
||||
if ($tohide) {
|
||||
// Hide other orphaned sections.
|
||||
// This is different from what set_section_visible() does but we want to preserve actual
|
||||
// module visibility in this case.
|
||||
list($sql, $params) = $DB->get_in_or_equal($tohide);
|
||||
$DB->execute("UPDATE {course_sections} SET visible = 0 WHERE id " . $sql, $params);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method adds empty sections to courses which have fewer sections than their
|
||||
* 'numsections' course format option and adds these empty sections.
|
||||
*
|
||||
* @param int $courseid
|
||||
* @param int $numsections
|
||||
*/
|
||||
function format_topics_upgrade_add_empty_sections($courseid, $numsections) {
|
||||
global $DB;
|
||||
$existingsections = $DB->get_fieldset_sql('SELECT section from {course_sections} WHERE course = ?', [$courseid]);
|
||||
$newsections = array_diff(range(0, $numsections), $existingsections);
|
||||
foreach ($newsections as $sectionnum) {
|
||||
course_create_section($courseid, $sectionnum, true);
|
||||
}
|
||||
}
|
@ -1,153 +0,0 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* format_topics unit tests for upgradelib
|
||||
*
|
||||
* @package format_topics
|
||||
* @copyright 2015 Marina Glancy
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
global $CFG;
|
||||
require_once($CFG->dirroot . '/course/lib.php');
|
||||
require_once($CFG->dirroot . '/course/format/topics/db/upgradelib.php');
|
||||
|
||||
/**
|
||||
* format_topics unit tests for upgradelib
|
||||
*
|
||||
* @package format_topics
|
||||
* @copyright 2017 Marina Glancy
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class format_topics_upgrade_testcase extends advanced_testcase {
|
||||
|
||||
/**
|
||||
* Test upgrade step to remove orphaned sections.
|
||||
*/
|
||||
public function test_numsections_no_actions() {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
$params = array('format' => 'topics', 'numsections' => 5, 'startdate' => 1445644800);
|
||||
$course = $this->getDataGenerator()->create_course($params);
|
||||
// This test is executed after 'numsections' option was already removed, add it manually.
|
||||
$DB->insert_record('course_format_options', ['courseid' => $course->id, 'format' => 'topics',
|
||||
'sectionid' => 0, 'name' => 'numsections', 'value' => '5']);
|
||||
|
||||
// There are 6 sections in the course (0-section and sections 1, ... 5).
|
||||
$this->assertEquals(6, $DB->count_records('course_sections', ['course' => $course->id]));
|
||||
|
||||
format_topics_upgrade_remove_numsections();
|
||||
|
||||
// There are still 6 sections in the course.
|
||||
$this->assertEquals(6, $DB->count_records('course_sections', ['course' => $course->id]));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test upgrade step to remove orphaned sections.
|
||||
*/
|
||||
public function test_numsections_delete_empty() {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
// Set default number of sections to 10.
|
||||
set_config('numsections', 10, 'moodlecourse');
|
||||
|
||||
$params1 = array('format' => 'topics', 'numsections' => 5, 'startdate' => 1445644800);
|
||||
$course1 = $this->getDataGenerator()->create_course($params1);
|
||||
$params2 = array('format' => 'topics', 'numsections' => 20, 'startdate' => 1445644800);
|
||||
$course2 = $this->getDataGenerator()->create_course($params2);
|
||||
// This test is executed after 'numsections' option was already removed, add it manually and
|
||||
// set it to be 2 less than actual number of sections.
|
||||
$DB->insert_record('course_format_options', ['courseid' => $course1->id, 'format' => 'topics',
|
||||
'sectionid' => 0, 'name' => 'numsections', 'value' => '3']);
|
||||
|
||||
// There are 6 sections in the first course (0-section and sections 1, ... 5).
|
||||
$this->assertEquals(6, $DB->count_records('course_sections', ['course' => $course1->id]));
|
||||
// There are 21 sections in the second course.
|
||||
$this->assertEquals(21, $DB->count_records('course_sections', ['course' => $course2->id]));
|
||||
|
||||
format_topics_upgrade_remove_numsections();
|
||||
|
||||
// Two sections were deleted in the first course.
|
||||
$this->assertEquals(4, $DB->count_records('course_sections', ['course' => $course1->id]));
|
||||
// The second course was reset to 11 sections (default plus 0-section).
|
||||
$this->assertEquals(11, $DB->count_records('course_sections', ['course' => $course2->id]));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test upgrade step to remove orphaned sections.
|
||||
*/
|
||||
public function test_numsections_hide_non_empty() {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
$params = array('format' => 'topics', 'numsections' => 5, 'startdate' => 1445644800);
|
||||
$course = $this->getDataGenerator()->create_course($params);
|
||||
|
||||
// Add a module to the second last section.
|
||||
$cm = $this->getDataGenerator()->create_module('forum', ['course' => $course->id, 'section' => 4]);
|
||||
|
||||
// This test is executed after 'numsections' option was already removed, add it manually and
|
||||
// set it to be 2 less than actual number of sections.
|
||||
$DB->insert_record('course_format_options', ['courseid' => $course->id, 'format' => 'topics',
|
||||
'sectionid' => 0, 'name' => 'numsections', 'value' => '3']);
|
||||
|
||||
// There are 6 sections.
|
||||
$this->assertEquals(6, $DB->count_records('course_sections', ['course' => $course->id]));
|
||||
|
||||
format_topics_upgrade_remove_numsections();
|
||||
|
||||
// One section was deleted and one hidden.
|
||||
$this->assertEquals(5, $DB->count_records('course_sections', ['course' => $course->id]));
|
||||
$this->assertEquals(0, $DB->get_field('course_sections', 'visible', ['course' => $course->id, 'section' => 4]));
|
||||
// The module is still visible.
|
||||
$this->assertEquals(1, $DB->get_field('course_modules', 'visible', ['id' => $cm->cmid]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test upgrade step to add empty sections.
|
||||
*/
|
||||
public function test_numsections_add_empty_sections() {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
$params = array('format' => 'topics', 'numsections' => 16, 'startdate' => 1445644800);
|
||||
$course = $this->getDataGenerator()->create_course($params);
|
||||
|
||||
// This test is executed after 'numsections' option was already removed.
|
||||
// Set the 'numsections' course format value to 18, simulating the scenario in which there are fewer real sections.
|
||||
$DB->insert_record('course_format_options', ['courseid' => $course->id, 'format' => 'topics',
|
||||
'sectionid' => 0, 'name' => 'numsections', 'value' => '18']);
|
||||
|
||||
// There are 16 sections.
|
||||
$this->assertEquals(17, $DB->count_records('course_sections', ['course' => $course->id]));
|
||||
|
||||
format_topics_upgrade_remove_numsections();
|
||||
|
||||
// Confirm that the upgrade method added the missing empty sections.
|
||||
$this->assertEquals(19, $DB->count_records('course_sections', ['course' => $course->id]));
|
||||
}
|
||||
}
|
@ -2,6 +2,16 @@ This files describes API changes for course formats
|
||||
|
||||
Overview of this plugin type at http://docs.moodle.org/dev/Course_formats
|
||||
|
||||
=== 3.9 ===
|
||||
|
||||
* The following functions, previously used (exclusively) by upgrade steps are not available anymore because of the upgrade cleanup performed for this version. See MDL-65809 for more info:
|
||||
- format_topics_upgrade_remove_numsections()
|
||||
- format_topics_upgrade_hide_extra_sections()
|
||||
- format_topics_upgrade_add_empty_sections()
|
||||
- format_weeks_upgrade_remove_numsections()
|
||||
- format_weeks_upgrade_hide_extra_sections()
|
||||
- format_weeks_upgrade_add_empty_sections()
|
||||
|
||||
=== 3.8 ===
|
||||
|
||||
* The following functions have been finally deprecated and can not be used anymore:
|
||||
|
@ -34,15 +34,6 @@ function xmldb_format_weeks_upgrade($oldversion) {
|
||||
global $CFG, $DB;
|
||||
|
||||
require_once($CFG->dirroot . '/course/format/weeks/lib.php');
|
||||
require_once($CFG->dirroot . '/course/format/weeks/db/upgradelib.php');
|
||||
|
||||
if ($oldversion < 2017020200) {
|
||||
|
||||
// Remove 'numsections' option and hide or delete orphaned sections.
|
||||
format_weeks_upgrade_remove_numsections();
|
||||
|
||||
upgrade_plugin_savepoint(true, 2017020200, 'format', 'weeks');
|
||||
}
|
||||
|
||||
if ($oldversion < 2017050300) {
|
||||
// Go through the existing courses using the weeks format with no value set for the 'automaticenddate'.
|
||||
|
@ -1,144 +0,0 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Upgrade scripts for course format "Weeks"
|
||||
*
|
||||
* @package format_weeks
|
||||
* @copyright 2017 Marina Glancy
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* This method finds all courses in 'topics' format that have actual number of sections
|
||||
* different than their 'numsections' course format option.
|
||||
*
|
||||
* For courses where there are more sections than numsections, we call
|
||||
* {@link format_weeks_upgrade_hide_extra_sections()} and
|
||||
* either delete or hide "orphaned" sections. For courses where there are fewer sections
|
||||
* than numsections, we call {@link format_weeks_upgrade_add_empty_sections()} to add
|
||||
* these sections.
|
||||
*/
|
||||
function format_weeks_upgrade_remove_numsections() {
|
||||
global $DB;
|
||||
|
||||
$sql1 = "SELECT c.id, max(cs.section) AS sectionsactual
|
||||
FROM {course} c
|
||||
JOIN {course_sections} cs ON cs.course = c.id
|
||||
WHERE c.format = :format1
|
||||
GROUP BY c.id";
|
||||
|
||||
$sql2 = "SELECT c.id, n.value AS numsections
|
||||
FROM {course} c
|
||||
JOIN {course_format_options} n ON n.courseid = c.id AND n.format = :format1 AND n.name = :numsections AND n.sectionid = 0
|
||||
WHERE c.format = :format2";
|
||||
|
||||
$params = ['format1' => 'weeks', 'format2' => 'weeks', 'numsections' => 'numsections'];
|
||||
|
||||
$actual = $DB->get_records_sql_menu($sql1, $params);
|
||||
$numsections = $DB->get_records_sql_menu($sql2, $params);
|
||||
$needfixing = [];
|
||||
$needsections = [];
|
||||
|
||||
$defaultnumsections = get_config('moodlecourse', 'numsections');
|
||||
|
||||
foreach ($actual as $courseid => $sectionsactual) {
|
||||
if (array_key_exists($courseid, $numsections)) {
|
||||
$n = (int)$numsections[$courseid];
|
||||
} else {
|
||||
$n = $defaultnumsections;
|
||||
}
|
||||
if ($sectionsactual > $n) {
|
||||
$needfixing[$courseid] = $n;
|
||||
} else if ($sectionsactual < $n) {
|
||||
$needsections[$courseid] = $n;
|
||||
}
|
||||
}
|
||||
unset($actual);
|
||||
unset($numsections);
|
||||
|
||||
foreach ($needfixing as $courseid => $numsections) {
|
||||
format_weeks_upgrade_hide_extra_sections($courseid, $numsections);
|
||||
}
|
||||
|
||||
foreach ($needsections as $courseid => $numsections) {
|
||||
format_weeks_upgrade_add_empty_sections($courseid, $numsections);
|
||||
}
|
||||
|
||||
$DB->delete_records('course_format_options', ['format' => 'weeks', 'sectionid' => 0, 'name' => 'numsections']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find all sections in the course with sectionnum bigger than numsections.
|
||||
* Either delete these sections or hide them
|
||||
*
|
||||
* We will only delete a section if it is completely empty and all sections below
|
||||
* it are also empty
|
||||
*
|
||||
* @param int $courseid
|
||||
* @param int $numsections
|
||||
*/
|
||||
function format_weeks_upgrade_hide_extra_sections($courseid, $numsections) {
|
||||
global $DB;
|
||||
$sections = $DB->get_records_sql('SELECT id, name, summary, sequence, visible
|
||||
FROM {course_sections}
|
||||
WHERE course = ? AND section > ?
|
||||
ORDER BY section DESC', [$courseid, $numsections]);
|
||||
$candelete = true;
|
||||
$tohide = [];
|
||||
$todelete = [];
|
||||
foreach ($sections as $section) {
|
||||
if ($candelete && (!empty($section->summary) || !empty($section->sequence) || !empty($section->name))) {
|
||||
$candelete = false;
|
||||
}
|
||||
if ($candelete) {
|
||||
$todelete[] = $section->id;
|
||||
} else if ($section->visible) {
|
||||
$tohide[] = $section->id;
|
||||
}
|
||||
}
|
||||
if ($todelete) {
|
||||
// Delete empty sections in the end.
|
||||
// This is an upgrade script - no events or cache resets are needed.
|
||||
// We also know that these sections do not have any modules so it is safe to just delete records in the table.
|
||||
$DB->delete_records_list('course_sections', 'id', $todelete);
|
||||
}
|
||||
if ($tohide) {
|
||||
// Hide other orphaned sections.
|
||||
// This is different from what set_section_visible() does but we want to preserve actual
|
||||
// module visibility in this case.
|
||||
list($sql, $params) = $DB->get_in_or_equal($tohide);
|
||||
$DB->execute("UPDATE {course_sections} SET visible = 0 WHERE id " . $sql, $params);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method adds empty sections to courses which have fewer sections than their
|
||||
* 'numsections' course format option and adds these empty sections.
|
||||
*
|
||||
* @param int $courseid
|
||||
* @param int $numsections
|
||||
*/
|
||||
function format_weeks_upgrade_add_empty_sections($courseid, $numsections) {
|
||||
global $DB;
|
||||
$existingsections = $DB->get_fieldset_sql('SELECT section from {course_sections} WHERE course = ?', [$courseid]);
|
||||
$newsections = array_diff(range(0, $numsections), $existingsections);
|
||||
foreach ($newsections as $sectionnum) {
|
||||
course_create_section($courseid, $sectionnum, true);
|
||||
}
|
||||
}
|
@ -1,153 +0,0 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* format_weeks unit tests for upgradelib
|
||||
*
|
||||
* @package format_weeks
|
||||
* @copyright 2015 Marina Glancy
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
global $CFG;
|
||||
require_once($CFG->dirroot . '/course/lib.php');
|
||||
require_once($CFG->dirroot . '/course/format/weeks/db/upgradelib.php');
|
||||
|
||||
/**
|
||||
* format_weeks unit tests for upgradelib
|
||||
*
|
||||
* @package format_weeks
|
||||
* @copyright 2017 Marina Glancy
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class format_weeks_upgrade_testcase extends advanced_testcase {
|
||||
|
||||
/**
|
||||
* Test upgrade step to remove orphaned sections.
|
||||
*/
|
||||
public function test_numsections_no_actions() {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
$params = array('format' => 'weeks', 'numsections' => 5, 'startdate' => 1445644800);
|
||||
$course = $this->getDataGenerator()->create_course($params);
|
||||
// This test is executed after 'numsections' option was already removed, add it manually.
|
||||
$DB->insert_record('course_format_options', ['courseid' => $course->id, 'format' => 'weeks',
|
||||
'sectionid' => 0, 'name' => 'numsections', 'value' => '5']);
|
||||
|
||||
// There are 6 sections in the course (0-section and sections 1, ... 5).
|
||||
$this->assertEquals(6, $DB->count_records('course_sections', ['course' => $course->id]));
|
||||
|
||||
format_weeks_upgrade_remove_numsections();
|
||||
|
||||
// There are still 6 sections in the course.
|
||||
$this->assertEquals(6, $DB->count_records('course_sections', ['course' => $course->id]));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test upgrade step to remove orphaned sections.
|
||||
*/
|
||||
public function test_numsections_delete_empty() {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
// Set default number of sections to 10.
|
||||
set_config('numsections', 10, 'moodlecourse');
|
||||
|
||||
$params1 = array('format' => 'weeks', 'numsections' => 5, 'startdate' => 1445644800);
|
||||
$course1 = $this->getDataGenerator()->create_course($params1);
|
||||
$params2 = array('format' => 'weeks', 'numsections' => 20, 'startdate' => 1445644800);
|
||||
$course2 = $this->getDataGenerator()->create_course($params2);
|
||||
// This test is executed after 'numsections' option was already removed, add it manually and
|
||||
// set it to be 2 less than actual number of sections.
|
||||
$DB->insert_record('course_format_options', ['courseid' => $course1->id, 'format' => 'weeks',
|
||||
'sectionid' => 0, 'name' => 'numsections', 'value' => '3']);
|
||||
|
||||
// There are 6 sections in the first course (0-section and sections 1, ... 5).
|
||||
$this->assertEquals(6, $DB->count_records('course_sections', ['course' => $course1->id]));
|
||||
// There are 21 sections in the second course.
|
||||
$this->assertEquals(21, $DB->count_records('course_sections', ['course' => $course2->id]));
|
||||
|
||||
format_weeks_upgrade_remove_numsections();
|
||||
|
||||
// Two sections were deleted in the first course.
|
||||
$this->assertEquals(4, $DB->count_records('course_sections', ['course' => $course1->id]));
|
||||
// The second course was reset to 11 sections (default plus 0-section).
|
||||
$this->assertEquals(11, $DB->count_records('course_sections', ['course' => $course2->id]));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test upgrade step to remove orphaned sections.
|
||||
*/
|
||||
public function test_numsections_hide_non_empty() {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
$params = array('format' => 'weeks', 'numsections' => 5, 'startdate' => 1445644800);
|
||||
$course = $this->getDataGenerator()->create_course($params);
|
||||
|
||||
// Add a module to the second last section.
|
||||
$cm = $this->getDataGenerator()->create_module('forum', ['course' => $course->id, 'section' => 4]);
|
||||
|
||||
// This test is executed after 'numsections' option was already removed, add it manually and
|
||||
// set it to be 2 less than actual number of sections.
|
||||
$DB->insert_record('course_format_options', ['courseid' => $course->id, 'format' => 'weeks',
|
||||
'sectionid' => 0, 'name' => 'numsections', 'value' => '3']);
|
||||
|
||||
// There are 6 sections.
|
||||
$this->assertEquals(6, $DB->count_records('course_sections', ['course' => $course->id]));
|
||||
|
||||
format_weeks_upgrade_remove_numsections();
|
||||
|
||||
// One section was deleted and one hidden.
|
||||
$this->assertEquals(5, $DB->count_records('course_sections', ['course' => $course->id]));
|
||||
$this->assertEquals(0, $DB->get_field('course_sections', 'visible', ['course' => $course->id, 'section' => 4]));
|
||||
// The module is still visible.
|
||||
$this->assertEquals(1, $DB->get_field('course_modules', 'visible', ['id' => $cm->cmid]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test upgrade step to add empty sections.
|
||||
*/
|
||||
public function test_numsections_add_empty_sections() {
|
||||
global $DB;
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
$params = array('format' => 'weeks', 'numsections' => 16, 'startdate' => 1445644800);
|
||||
$course = $this->getDataGenerator()->create_course($params);
|
||||
|
||||
// This test is executed after 'numsections' option was already removed.
|
||||
// Set the 'numsections' course format value to 18, simulating the scenario in which there are fewer real sections.
|
||||
$DB->insert_record('course_format_options', ['courseid' => $course->id, 'format' => 'weeks',
|
||||
'sectionid' => 0, 'name' => 'numsections', 'value' => '18']);
|
||||
|
||||
// There are 16 sections.
|
||||
$this->assertEquals(17, $DB->count_records('course_sections', ['course' => $course->id]));
|
||||
|
||||
format_weeks_upgrade_remove_numsections();
|
||||
|
||||
// Confirm that the upgrade method added the missing empty sections.
|
||||
$this->assertEquals(19, $DB->count_records('course_sections', ['course' => $course->id]));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user