MDL-57268 auth_db: Unit tests for deletion from a large user set
[moodle.git] / course / tests / courseformat_test.php
blob47d7dabdef888c0940ba2ad05b5b05aa8b3db522
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.
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/>.
17 /**
18 * Course related unit tests
20 * @package core_course
21 * @copyright 2014 Marina Glancy
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 global $CFG;
28 require_once($CFG->dirroot . '/course/lib.php');
29 require_once($CFG->dirroot . '/course/tests/fixtures/format_theunittest.php');
31 class core_course_courseformat_testcase extends advanced_testcase {
32 public function test_available_hook() {
33 global $DB;
34 $this->resetAfterTest();
36 // Generate a course with two sections (0 and 1) and two modules. Course format is set to 'theunittest'.
37 $generator = $this->getDataGenerator();
38 $course1 = $generator->create_course(array('format' => 'theunittest'));
39 $this->assertEquals('theunittest', $course1->format);
40 course_create_sections_if_missing($course1, array(0, 1));
41 $assign0 = $generator->create_module('assign', array('course' => $course1, 'section' => 0));
42 $assign1 = $generator->create_module('assign', array('course' => $course1, 'section' => 1));
44 // Enrol student and teacher.
45 $roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
46 $student = $generator->create_user();
47 $generator->enrol_user($student->id, $course1->id, $roleids['student']);
48 $teacher = $generator->create_user();
49 $generator->enrol_user($teacher->id, $course1->id, $roleids['editingteacher']);
51 // Make sure that initially both sections and both modules are available and visible for a student.
52 $modinfostudent = get_fast_modinfo($course1, $student->id);
53 $this->assertTrue($modinfostudent->get_section_info(1)->available);
54 $this->assertTrue($modinfostudent->get_cm($assign0->cmid)->available);
55 $this->assertTrue($modinfostudent->get_cm($assign0->cmid)->uservisible);
56 $this->assertTrue($modinfostudent->get_cm($assign1->cmid)->available);
57 $this->assertTrue($modinfostudent->get_cm($assign1->cmid)->uservisible);
59 // Set 'hideoddsections' for the course to 1.
60 // Section1 and assign1 will be unavailable, uservisible will be false for student and true for teacher.
61 $data = (object)array('id' => $course1->id, 'hideoddsections' => 1);
62 course_get_format($course1)->update_course_format_options($data);
63 $modinfostudent = get_fast_modinfo($course1, $student->id);
64 $this->assertFalse($modinfostudent->get_section_info(1)->available);
65 $this->assertEmpty($modinfostudent->get_section_info(1)->availableinfo);
66 $this->assertFalse($modinfostudent->get_section_info(1)->uservisible);
67 $this->assertTrue($modinfostudent->get_cm($assign0->cmid)->available);
68 $this->assertTrue($modinfostudent->get_cm($assign0->cmid)->uservisible);
69 $this->assertFalse($modinfostudent->get_cm($assign1->cmid)->available);
70 $this->assertFalse($modinfostudent->get_cm($assign1->cmid)->uservisible);
72 $modinfoteacher = get_fast_modinfo($course1, $teacher->id);
73 $this->assertFalse($modinfoteacher->get_section_info(1)->available);
74 $this->assertEmpty($modinfoteacher->get_section_info(1)->availableinfo);
75 $this->assertTrue($modinfoteacher->get_section_info(1)->uservisible);
76 $this->assertTrue($modinfoteacher->get_cm($assign0->cmid)->available);
77 $this->assertTrue($modinfoteacher->get_cm($assign0->cmid)->uservisible);
78 $this->assertFalse($modinfoteacher->get_cm($assign1->cmid)->available);
79 $this->assertTrue($modinfoteacher->get_cm($assign1->cmid)->uservisible);
81 // Set 'hideoddsections' for the course to 2.
82 // Section1 and assign1 will be unavailable, uservisible will be false for student and true for teacher.
83 // Property availableinfo will be not empty.
84 $data = (object)array('id' => $course1->id, 'hideoddsections' => 2);
85 course_get_format($course1)->update_course_format_options($data);
86 $modinfostudent = get_fast_modinfo($course1, $student->id);
87 $this->assertFalse($modinfostudent->get_section_info(1)->available);
88 $this->assertNotEmpty($modinfostudent->get_section_info(1)->availableinfo);
89 $this->assertFalse($modinfostudent->get_section_info(1)->uservisible);
90 $this->assertTrue($modinfostudent->get_cm($assign0->cmid)->available);
91 $this->assertTrue($modinfostudent->get_cm($assign0->cmid)->uservisible);
92 $this->assertFalse($modinfostudent->get_cm($assign1->cmid)->available);
93 $this->assertFalse($modinfostudent->get_cm($assign1->cmid)->uservisible);
95 $modinfoteacher = get_fast_modinfo($course1, $teacher->id);
96 $this->assertFalse($modinfoteacher->get_section_info(1)->available);
97 $this->assertNotEmpty($modinfoteacher->get_section_info(1)->availableinfo);
98 $this->assertTrue($modinfoteacher->get_section_info(1)->uservisible);
99 $this->assertTrue($modinfoteacher->get_cm($assign0->cmid)->available);
100 $this->assertTrue($modinfoteacher->get_cm($assign0->cmid)->uservisible);
101 $this->assertFalse($modinfoteacher->get_cm($assign1->cmid)->available);
102 $this->assertTrue($modinfoteacher->get_cm($assign1->cmid)->uservisible);
106 * Test for supports_news() with a course format plugin that doesn't define 'news_items' in default blocks.
108 public function test_supports_news() {
109 $this->resetAfterTest();
110 $format = course_get_format((object)['format' => 'testformat']);
111 $this->assertFalse($format->supports_news());
115 * Test for supports_news() for old course format plugins that defines 'news_items' in default blocks.
117 public function test_supports_news_legacy() {
118 $this->resetAfterTest();
119 $format = course_get_format((object)['format' => 'testlegacy']);
120 $this->assertTrue($format->supports_news());
125 * Class format_testformat.
127 * A test class that simulates a course format that doesn't define 'news_items' in default blocks.
129 * @copyright 2016 Jun Pataleta <jun@moodle.com>
130 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
132 class format_testformat extends format_base {
134 * Returns the list of blocks to be automatically added for the newly created course.
136 * @return array
138 public function get_default_blocks() {
139 return [
140 BLOCK_POS_RIGHT => [],
141 BLOCK_POS_LEFT => []
147 * Class format_testlegacy.
149 * A test class that simulates old course formats that define 'news_items' in default blocks.
151 * @copyright 2016 Jun Pataleta <jun@moodle.com>
152 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
154 class format_testlegacy extends format_base {
156 * Returns the list of blocks to be automatically added for the newly created course.
158 * @return array
160 public function get_default_blocks() {
161 return [
162 BLOCK_POS_RIGHT => ['news_items'],
163 BLOCK_POS_LEFT => []