Merge branch 'MDL-48947_v3' of https://github.com/Syxton/moodle
[moodle.git] / course / format / weeks / tests / format_weeks_test.php
CommitLineData
bb018838
MG
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 * format_weeks related unit tests
19 *
20 * @package format_weeks
21 * @copyright 2015 Marina Glancy
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27global $CFG;
28require_once($CFG->dirroot . '/course/lib.php');
29
30/**
31 * format_weeks related unit tests
32 *
33 * @package format_weeks
34 * @copyright 2015 Marina Glancy
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37class format_weeks_testcase extends advanced_testcase {
38
39 public function test_update_course_numsections() {
40 global $DB;
41 $this->resetAfterTest(true);
42
43 $generator = $this->getDataGenerator();
44
45 $course = $generator->create_course(array('numsections' => 10, 'format' => 'weeks'),
46 array('createsections' => true));
47 $generator->create_module('assign', array('course' => $course, 'section' => 7));
48
49 $this->setAdminUser();
50
51 $this->assertEquals(11, $DB->count_records('course_sections', array('course' => $course->id)));
52
53 // Change the numsections to 8, last two sections did not have any activities, they should be deleted.
54 update_course((object)array('id' => $course->id, 'numsections' => 8));
55 $this->assertEquals(9, $DB->count_records('course_sections', array('course' => $course->id)));
56 $this->assertEquals(9, count(get_fast_modinfo($course)->get_section_info_all()));
57
58 // Change the numsections to 5, section 8 should be deleted but section 7 should remain as it has activities.
59 update_course((object)array('id' => $course->id, 'numsections' => 6));
60 $this->assertEquals(8, $DB->count_records('course_sections', array('course' => $course->id)));
61 $this->assertEquals(8, count(get_fast_modinfo($course)->get_section_info_all()));
62 $this->assertEquals(6, course_get_format($course)->get_course()->numsections);
63 }
64}