1 |
efrain |
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 |
namespace format_topics;
|
|
|
18 |
|
|
|
19 |
use core_external\external_api;
|
|
|
20 |
|
|
|
21 |
defined('MOODLE_INTERNAL') || die();
|
|
|
22 |
|
|
|
23 |
global $CFG;
|
|
|
24 |
require_once($CFG->dirroot . '/course/lib.php');
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
* Topics course format related unit tests.
|
|
|
28 |
*
|
|
|
29 |
* @package format_topics
|
|
|
30 |
* @copyright 2015 Marina Glancy
|
|
|
31 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
32 |
* @covers \format_topics
|
|
|
33 |
*/
|
|
|
34 |
class format_topics_test extends \advanced_testcase {
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* Tests for format_topics::get_section_name method with default section names.
|
|
|
38 |
*
|
|
|
39 |
* @return void
|
|
|
40 |
*/
|
11 |
efrain |
41 |
public function test_get_section_name(): void {
|
1 |
efrain |
42 |
global $DB;
|
|
|
43 |
$this->resetAfterTest(true);
|
|
|
44 |
|
|
|
45 |
// Generate a course with 5 sections.
|
|
|
46 |
$generator = $this->getDataGenerator();
|
|
|
47 |
$numsections = 5;
|
|
|
48 |
$course = $generator->create_course(['numsections' => $numsections, 'format' => 'topics'],
|
|
|
49 |
['createsections' => true]);
|
|
|
50 |
|
|
|
51 |
// Get section names for course.
|
|
|
52 |
$coursesections = $DB->get_records('course_sections', ['course' => $course->id]);
|
|
|
53 |
|
|
|
54 |
// Test get_section_name with default section names.
|
|
|
55 |
$courseformat = course_get_format($course);
|
|
|
56 |
foreach ($coursesections as $section) {
|
|
|
57 |
// Assert that with unmodified section names, get_section_name returns the same result as get_default_section_name.
|
|
|
58 |
$this->assertEquals($courseformat->get_default_section_name($section), $courseformat->get_section_name($section));
|
|
|
59 |
}
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
* Tests for format_topics::get_section_name method with modified section names.
|
|
|
64 |
*
|
|
|
65 |
* @return void
|
|
|
66 |
*/
|
11 |
efrain |
67 |
public function test_get_section_name_customised(): void {
|
1 |
efrain |
68 |
global $DB;
|
|
|
69 |
$this->resetAfterTest(true);
|
|
|
70 |
|
|
|
71 |
// Generate a course with 5 sections.
|
|
|
72 |
$generator = $this->getDataGenerator();
|
|
|
73 |
$numsections = 5;
|
|
|
74 |
$course = $generator->create_course(['numsections' => $numsections, 'format' => 'topics'],
|
|
|
75 |
['createsections' => true]);
|
|
|
76 |
|
|
|
77 |
// Get section names for course.
|
|
|
78 |
$coursesections = $DB->get_records('course_sections', ['course' => $course->id]);
|
|
|
79 |
|
|
|
80 |
// Modify section names.
|
|
|
81 |
$customname = "Custom Section";
|
|
|
82 |
foreach ($coursesections as $section) {
|
|
|
83 |
$section->name = "$customname $section->section";
|
|
|
84 |
$DB->update_record('course_sections', $section);
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
// Requery updated section names then test get_section_name.
|
|
|
88 |
$coursesections = $DB->get_records('course_sections', ['course' => $course->id]);
|
|
|
89 |
$courseformat = course_get_format($course);
|
|
|
90 |
foreach ($coursesections as $section) {
|
|
|
91 |
// Assert that with modified section names, get_section_name returns the modified section name.
|
|
|
92 |
$this->assertEquals($section->name, $courseformat->get_section_name($section));
|
|
|
93 |
}
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
/**
|
|
|
97 |
* Tests for format_topics::get_default_section_name.
|
|
|
98 |
*
|
|
|
99 |
* @return void
|
|
|
100 |
*/
|
11 |
efrain |
101 |
public function test_get_default_section_name(): void {
|
1 |
efrain |
102 |
global $DB;
|
|
|
103 |
$this->resetAfterTest(true);
|
|
|
104 |
|
|
|
105 |
// Generate a course with 5 sections.
|
|
|
106 |
$generator = $this->getDataGenerator();
|
|
|
107 |
$numsections = 5;
|
|
|
108 |
$course = $generator->create_course(['numsections' => $numsections, 'format' => 'topics'],
|
|
|
109 |
['createsections' => true]);
|
|
|
110 |
|
|
|
111 |
// Get section names for course.
|
|
|
112 |
$coursesections = $DB->get_records('course_sections', ['course' => $course->id]);
|
|
|
113 |
|
|
|
114 |
// Test get_default_section_name with default section names.
|
|
|
115 |
$courseformat = course_get_format($course);
|
|
|
116 |
foreach ($coursesections as $section) {
|
|
|
117 |
if ($section->section == 0) {
|
|
|
118 |
$sectionname = get_string('section0name', 'format_topics');
|
|
|
119 |
$this->assertEquals($sectionname, $courseformat->get_default_section_name($section));
|
|
|
120 |
} else {
|
|
|
121 |
$sectionname = get_string('newsection', 'format_topics');
|
|
|
122 |
$this->assertEquals($sectionname, $courseformat->get_default_section_name($section));
|
|
|
123 |
}
|
|
|
124 |
}
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
/**
|
|
|
128 |
* Test web service updating section name.
|
|
|
129 |
*
|
|
|
130 |
* @return void
|
|
|
131 |
*/
|
11 |
efrain |
132 |
public function test_update_inplace_editable(): void {
|
1 |
efrain |
133 |
global $CFG, $DB, $PAGE;
|
|
|
134 |
require_once($CFG->dirroot . '/lib/external/externallib.php');
|
|
|
135 |
|
|
|
136 |
$this->resetAfterTest();
|
|
|
137 |
$user = $this->getDataGenerator()->create_user();
|
|
|
138 |
$this->setUser($user);
|
|
|
139 |
$course = $this->getDataGenerator()->create_course(['numsections' => 5, 'format' => 'topics'],
|
|
|
140 |
['createsections' => true]);
|
|
|
141 |
$section = $DB->get_record('course_sections', ['course' => $course->id, 'section' => 2]);
|
|
|
142 |
|
|
|
143 |
// Call webservice without necessary permissions.
|
|
|
144 |
try {
|
|
|
145 |
\core_external::update_inplace_editable('format_topics', 'sectionname', $section->id, 'New section name');
|
|
|
146 |
$this->fail('Exception expected');
|
|
|
147 |
} catch (\moodle_exception $e) {
|
|
|
148 |
$this->assertEquals('Course or activity not accessible. (Not enrolled)',
|
|
|
149 |
$e->getMessage());
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
// Change to teacher and make sure that section name can be updated using web service update_inplace_editable().
|
|
|
153 |
$teacherrole = $DB->get_record('role', ['shortname' => 'editingteacher']);
|
|
|
154 |
$this->getDataGenerator()->enrol_user($user->id, $course->id, $teacherrole->id);
|
|
|
155 |
|
|
|
156 |
$res = \core_external::update_inplace_editable('format_topics', 'sectionname', $section->id, 'New section name');
|
|
|
157 |
$res = external_api::clean_returnvalue(\core_external::update_inplace_editable_returns(), $res);
|
|
|
158 |
$this->assertEquals('New section name', $res['value']);
|
|
|
159 |
$this->assertEquals('New section name', $DB->get_field('course_sections', 'name', ['id' => $section->id]));
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
/**
|
|
|
163 |
* Test callback updating section name.
|
|
|
164 |
*
|
|
|
165 |
* @return void
|
|
|
166 |
*/
|
11 |
efrain |
167 |
public function test_inplace_editable(): void {
|
1 |
efrain |
168 |
global $DB, $PAGE;
|
|
|
169 |
|
|
|
170 |
$this->resetAfterTest();
|
|
|
171 |
$user = $this->getDataGenerator()->create_user();
|
|
|
172 |
$course = $this->getDataGenerator()->create_course(['numsections' => 5, 'format' => 'topics'],
|
|
|
173 |
['createsections' => true]);
|
|
|
174 |
$teacherrole = $DB->get_record('role', ['shortname' => 'editingteacher']);
|
|
|
175 |
$this->getDataGenerator()->enrol_user($user->id, $course->id, $teacherrole->id);
|
|
|
176 |
$this->setUser($user);
|
|
|
177 |
|
|
|
178 |
$section = $DB->get_record('course_sections', ['course' => $course->id, 'section' => 2]);
|
|
|
179 |
|
|
|
180 |
// Call callback format_topics_inplace_editable() directly.
|
|
|
181 |
$tmpl = component_callback('format_topics', 'inplace_editable', ['sectionname', $section->id, 'Rename me again']);
|
|
|
182 |
$this->assertInstanceOf('core\output\inplace_editable', $tmpl);
|
|
|
183 |
$res = $tmpl->export_for_template($PAGE->get_renderer('core'));
|
|
|
184 |
$this->assertEquals('Rename me again', $res['value']);
|
|
|
185 |
$this->assertEquals('Rename me again', $DB->get_field('course_sections', 'name', ['id' => $section->id]));
|
|
|
186 |
|
|
|
187 |
// Try updating using callback from mismatching course format.
|
|
|
188 |
try {
|
|
|
189 |
component_callback('format_weeks', 'inplace_editable', ['sectionname', $section->id, 'New name']);
|
|
|
190 |
$this->fail('Exception expected');
|
|
|
191 |
} catch (\moodle_exception $e) {
|
|
|
192 |
$this->assertEquals(1, preg_match('/^Can\'t find data record in database/', $e->getMessage()));
|
|
|
193 |
}
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
/**
|
|
|
197 |
* Test get_default_course_enddate.
|
|
|
198 |
*
|
|
|
199 |
* @return void
|
|
|
200 |
*/
|
11 |
efrain |
201 |
public function test_default_course_enddate(): void {
|
1 |
efrain |
202 |
global $CFG, $DB;
|
|
|
203 |
|
|
|
204 |
$this->resetAfterTest(true);
|
|
|
205 |
|
|
|
206 |
require_once($CFG->dirroot . '/course/tests/fixtures/testable_course_edit_form.php');
|
|
|
207 |
|
|
|
208 |
$this->setTimezone('UTC');
|
|
|
209 |
|
|
|
210 |
$params = ['format' => 'topics', 'numsections' => 5, 'startdate' => 1445644800];
|
|
|
211 |
$course = $this->getDataGenerator()->create_course($params);
|
|
|
212 |
$category = $DB->get_record('course_categories', ['id' => $course->category]);
|
|
|
213 |
|
|
|
214 |
$args = [
|
|
|
215 |
'course' => $course,
|
|
|
216 |
'category' => $category,
|
|
|
217 |
'editoroptions' => [
|
|
|
218 |
'context' => \context_course::instance($course->id),
|
|
|
219 |
'subdirs' => 0
|
|
|
220 |
],
|
|
|
221 |
'returnto' => new \moodle_url('/'),
|
|
|
222 |
'returnurl' => new \moodle_url('/'),
|
|
|
223 |
];
|
|
|
224 |
|
|
|
225 |
$courseform = new \testable_course_edit_form(null, $args);
|
|
|
226 |
$courseform->definition_after_data();
|
|
|
227 |
|
|
|
228 |
$enddate = $params['startdate'] + get_config('moodlecourse', 'courseduration');
|
|
|
229 |
|
|
|
230 |
$weeksformat = course_get_format($course->id);
|
|
|
231 |
$this->assertEquals($enddate, $weeksformat->get_default_course_enddate($courseform->get_quick_form()));
|
|
|
232 |
|
|
|
233 |
}
|
|
|
234 |
|
|
|
235 |
/**
|
|
|
236 |
* Test for get_view_url().
|
|
|
237 |
*
|
|
|
238 |
* @covers ::get_view_url
|
|
|
239 |
*/
|
|
|
240 |
public function test_get_view_url(): void {
|
|
|
241 |
global $CFG;
|
|
|
242 |
$this->resetAfterTest();
|
|
|
243 |
|
|
|
244 |
// Generate a course with two sections (0 and 1) and two modules.
|
|
|
245 |
$generator = $this->getDataGenerator();
|
|
|
246 |
$course1 = $generator->create_course(['format' => 'topics']);
|
|
|
247 |
course_create_sections_if_missing($course1, [0, 1]);
|
|
|
248 |
|
|
|
249 |
$data = (object)['id' => $course1->id];
|
|
|
250 |
$format = course_get_format($course1);
|
|
|
251 |
$format->update_course_format_options($data);
|
|
|
252 |
|
|
|
253 |
// In page.
|
|
|
254 |
$this->assertNotEmpty($format->get_view_url(null));
|
|
|
255 |
$this->assertNotEmpty($format->get_view_url(0));
|
|
|
256 |
$this->assertNotEmpty($format->get_view_url(1));
|
|
|
257 |
|
|
|
258 |
// Navigation.
|
|
|
259 |
$this->assertStringContainsString('course/view.php', $format->get_view_url(0));
|
|
|
260 |
$this->assertStringContainsString('course/view.php', $format->get_view_url(1));
|
|
|
261 |
$this->assertStringContainsString('course/section.php', $format->get_view_url(0, ['navigation' => 1]));
|
|
|
262 |
$this->assertStringContainsString('course/section.php', $format->get_view_url(1, ['navigation' => 1]));
|
|
|
263 |
// When sr parameter is defined, the section.php page should be returned.
|
|
|
264 |
$this->assertStringContainsString('course/section.php', $format->get_view_url(0, ['sr' => 1]));
|
|
|
265 |
$this->assertStringContainsString('course/section.php', $format->get_view_url(1, ['sr' => 1]));
|
|
|
266 |
$this->assertStringContainsString('course/section.php', $format->get_view_url(0, ['sr' => 0]));
|
|
|
267 |
$this->assertStringContainsString('course/section.php', $format->get_view_url(1, ['sr' => 0]));
|
|
|
268 |
}
|
|
|
269 |
|
|
|
270 |
/**
|
|
|
271 |
* Test get_required_jsfiles().
|
|
|
272 |
*
|
|
|
273 |
* @covers ::get_required_jsfiles
|
|
|
274 |
*/
|
|
|
275 |
public function test_get_required_jsfiles(): void {
|
|
|
276 |
$this->resetAfterTest();
|
|
|
277 |
|
|
|
278 |
$generator = $this->getDataGenerator();
|
|
|
279 |
|
|
|
280 |
$course = $generator->create_course(['format' => 'topics']);
|
|
|
281 |
$format = course_get_format($course);
|
|
|
282 |
$this->assertEmpty($format->get_required_jsfiles());
|
|
|
283 |
}
|
|
|
284 |
}
|