MDL-41578 Fixed bug in coursecat::has_course_overviewfiles
[moodle.git] / lib / tests / coursecatlib_test.php
CommitLineData
b28bb7e8
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 * Tests for class coursecat from lib/coursecatlib.php
19 *
20 * @package core
21 * @category phpunit
22 * @copyright 2013 Marina Glancy
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29require_once($CFG->libdir . '/coursecatlib.php');
30
31/**
7db309f3 32 * Functional test for coursecatlib.php
b28bb7e8 33 */
7db309f3 34class core_coursecatlib_testcase extends advanced_testcase {
b28bb7e8 35
7db309f3 36 protected $roles;
b28bb7e8 37
7db309f3 38 protected function setUp() {
b28bb7e8 39 parent::setUp();
7db309f3 40 $this->resetAfterTest();
b28bb7e8
MG
41 $user = $this->getDataGenerator()->create_user();
42 $this->setUser($user);
43 }
44
45 protected function get_roleid($context = null) {
46 global $USER;
47 if ($context === null) {
48 $context = context_system::instance();
49 }
50 if (is_object($context)) {
51 $context = $context->id;
52 }
53 if (empty($this->roles)) {
54 $this->roles = array();
55 }
56 if (empty($this->roles[$USER->id])) {
57 $this->roles[$USER->id] = array();
58 }
59 if (empty($this->roles[$USER->id][$context])) {
60 $this->roles[$USER->id][$context] = create_role('Role for '.$USER->id.' in '.$context, 'role'.$USER->id.'-'.$context, '-');
61 role_assign($this->roles[$USER->id][$context], $USER->id, $context);
62 }
63 return $this->roles[$USER->id][$context];
64 }
65
66 protected function assign_capability($capability, $permission = CAP_ALLOW, $contextid = null) {
67 if ($contextid === null) {
68 $contextid = context_system::instance();
69 }
70 if (is_object($contextid)) {
71 $contextid = $contextid->id;
72 }
73 assign_capability($capability, $permission, $this->get_roleid($contextid), $contextid, true);
74 accesslib_clear_all_caches_for_unit_testing();
75 }
76
77 public function test_create_coursecat() {
7db309f3 78 // Create the category.
b28bb7e8
MG
79 $data = new stdClass();
80 $data->name = 'aaa';
81 $data->description = 'aaa';
82 $data->idnumber = '';
83
84 $category1 = coursecat::create($data);
85
7db309f3
PS
86 // Initially confirm that base data was inserted correctly.
87 $this->assertSame($data->name, $category1->name);
88 $this->assertSame($data->description, $category1->description);
89 $this->assertSame($data->idnumber, $category1->idnumber);
b28bb7e8
MG
90
91 $this->assertGreaterThanOrEqual(1, $category1->sortorder);
92
7db309f3 93 // Create two more categories and test the sortorder worked correctly.
b28bb7e8
MG
94 $data->name = 'ccc';
95 $category2 = coursecat::create($data);
96
97 $data->name = 'bbb';
98 $category3 = coursecat::create($data);
99
100 $this->assertGreaterThan($category1->sortorder, $category2->sortorder);
101 $this->assertGreaterThan($category2->sortorder, $category3->sortorder);
102 }
103
104 public function test_name_idnumber_exceptions() {
105 try {
106 coursecat::create(array('name' => ''));
107 $this->fail('Missing category name exception expected in coursecat::create');
108 } catch (moodle_exception $e) {
7db309f3 109 $this->assertInstanceOf('moodle_exception', $e);
b28bb7e8
MG
110 }
111 $cat1 = coursecat::create(array('name' => 'Cat1', 'idnumber' => '1'));
112 try {
113 $cat1->update(array('name' => ''));
114 $this->fail('Missing category name exception expected in coursecat::update');
115 } catch (moodle_exception $e) {
7db309f3 116 $this->assertInstanceOf('moodle_exception', $e);
b28bb7e8
MG
117 }
118 try {
119 coursecat::create(array('name' => 'Cat2', 'idnumber' => '1'));
120 $this->fail('Duplicate idnumber exception expected in coursecat::create');
121 } catch (moodle_exception $e) {
7db309f3 122 $this->assertInstanceOf('moodle_exception', $e);
b28bb7e8
MG
123 }
124 $cat2 = coursecat::create(array('name' => 'Cat2', 'idnumber' => '2'));
125 try {
126 $cat2->update(array('idnumber' => '1'));
127 $this->fail('Duplicate idnumber exception expected in coursecat::update');
128 } catch (moodle_exception $e) {
7db309f3 129 $this->assertInstanceOf('moodle_exception', $e);
b28bb7e8
MG
130 }
131 }
132
133 public function test_visibility() {
134 $this->assign_capability('moodle/category:viewhiddencategories');
135 $this->assign_capability('moodle/category:manage');
136
7db309f3 137 // Create category 1 initially hidden.
b28bb7e8
MG
138 $category1 = coursecat::create(array('name' => 'Cat1', 'visible' => 0));
139 $this->assertEquals(0, $category1->visible);
140 $this->assertEquals(0, $category1->visibleold);
141
7db309f3 142 // Create category 2 initially hidden as a child of hidden category 1.
b28bb7e8
MG
143 $category2 = coursecat::create(array('name' => 'Cat2', 'visible' => 0, 'parent' => $category1->id));
144 $this->assertEquals(0, $category2->visible);
145 $this->assertEquals(0, $category2->visibleold);
146
7db309f3 147 // Create category 3 initially visible as a child of hidden category 1.
b28bb7e8
MG
148 $category3 = coursecat::create(array('name' => 'Cat3', 'visible' => 1, 'parent' => $category1->id));
149 $this->assertEquals(0, $category3->visible);
150 $this->assertEquals(1, $category3->visibleold);
151
7db309f3 152 // Show category 1 and make sure that category 2 is hidden and category 3 is visible.
b28bb7e8
MG
153 $category1->show();
154 $this->assertEquals(1, coursecat::get($category1->id)->visible);
155 $this->assertEquals(0, coursecat::get($category2->id)->visible);
156 $this->assertEquals(1, coursecat::get($category3->id)->visible);
157
7db309f3 158 // Create visible category 4.
b28bb7e8
MG
159 $category4 = coursecat::create(array('name' => 'Cat4'));
160 $this->assertEquals(1, $category4->visible);
161 $this->assertEquals(1, $category4->visibleold);
162
7db309f3 163 // Create visible category 5 as a child of visible category 4.
b28bb7e8
MG
164 $category5 = coursecat::create(array('name' => 'Cat5', 'parent' => $category4->id));
165 $this->assertEquals(1, $category5->visible);
166 $this->assertEquals(1, $category5->visibleold);
167
7db309f3 168 // Hide category 4 and make sure category 5 is hidden too.
b28bb7e8
MG
169 $category4->hide();
170 $this->assertEquals(0, $category4->visible);
171 $this->assertEquals(0, $category4->visibleold);
7db309f3 172 $category5 = coursecat::get($category5->id); // We have to re-read from DB.
b28bb7e8
MG
173 $this->assertEquals(0, $category5->visible);
174 $this->assertEquals(1, $category5->visibleold);
175
7db309f3 176 // Show category 4 and make sure category 5 is visible too.
b28bb7e8
MG
177 $category4->show();
178 $this->assertEquals(1, $category4->visible);
179 $this->assertEquals(1, $category4->visibleold);
7db309f3 180 $category5 = coursecat::get($category5->id); // We have to re-read from DB.
b28bb7e8
MG
181 $this->assertEquals(1, $category5->visible);
182 $this->assertEquals(1, $category5->visibleold);
183
7db309f3 184 // Move category 5 under hidden category 2 and make sure it became hidden.
b28bb7e8
MG
185 $category5->change_parent($category2->id);
186 $this->assertEquals(0, $category5->visible);
187 $this->assertEquals(1, $category5->visibleold);
188
7db309f3 189 // Re-read object for category 5 from DB and check again.
b28bb7e8
MG
190 $category5 = coursecat::get($category5->id);
191 $this->assertEquals(0, $category5->visible);
192 $this->assertEquals(1, $category5->visibleold);
193
7db309f3
PS
194 // Rricky one! Move hidden category 5 under visible category ("Top") and make sure it is still hidden-
195 // WHY? Well, different people may expect different behaviour here. So better keep it hidden.
b28bb7e8
MG
196 $category5->change_parent(0);
197 $this->assertEquals(0, $category5->visible);
198 $this->assertEquals(1, $category5->visibleold);
199 }
200
201 public function test_hierarchy() {
202 $this->assign_capability('moodle/category:viewhiddencategories');
203 $this->assign_capability('moodle/category:manage');
204
205 $category1 = coursecat::create(array('name' => 'Cat1'));
206 $category2 = coursecat::create(array('name' => 'Cat2', 'parent' => $category1->id));
207 $category3 = coursecat::create(array('name' => 'Cat3', 'parent' => $category1->id));
208 $category4 = coursecat::create(array('name' => 'Cat4', 'parent' => $category2->id));
209
7db309f3 210 // Check function get_children().
b28bb7e8 211 $this->assertEquals(array($category2->id, $category3->id), array_keys($category1->get_children()));
7db309f3 212 // Check function get_parents().
15d50fff 213 $this->assertEquals(array($category1->id, $category2->id), $category4->get_parents());
b28bb7e8 214
7db309f3 215 // Can not move category to itself or to it's children.
b28bb7e8
MG
216 $this->assertFalse($category1->can_change_parent($category2->id));
217 $this->assertFalse($category2->can_change_parent($category2->id));
7db309f3 218 // Can move category to grandparent.
b28bb7e8
MG
219 $this->assertTrue($category4->can_change_parent($category1->id));
220
221 try {
222 $category2->change_parent($category4->id);
223 $this->fail('Exception expected - can not move category');
224 } catch (moodle_exception $e) {
7db309f3 225 $this->assertInstanceOf('moodle_exception', $e);
b28bb7e8
MG
226 }
227
228 $category4->change_parent(0);
15d50fff 229 $this->assertEquals(array(), $category4->get_parents());
b28bb7e8
MG
230 $this->assertEquals(array($category2->id, $category3->id), array_keys($category1->get_children()));
231 $this->assertEquals(array(), array_keys($category2->get_children()));
232 }
233
234 public function test_update() {
235 $category1 = coursecat::create(array('name' => 'Cat1'));
236 $timecreated = $category1->timemodified;
7db309f3 237 $this->assertSame('Cat1', $category1->name);
b28bb7e8
MG
238 $this->assertTrue(empty($category1->description));
239 sleep(2);
240 $testdescription = 'This is cat 1 а также русский текст';
241 $category1->update(array('description' => $testdescription));
7db309f3 242 $this->assertSame($testdescription, $category1->description);
b28bb7e8 243 $category1 = coursecat::get($category1->id);
7db309f3 244 $this->assertSame($testdescription, $category1->description);
eabbfa82 245 cache_helper::purge_by_event('changesincoursecat');
b28bb7e8 246 $category1 = coursecat::get($category1->id);
7db309f3 247 $this->assertSame($testdescription, $category1->description);
b28bb7e8
MG
248
249 $this->assertGreaterThan($timecreated, $category1->timemodified);
250 }
251
252 public function test_delete() {
253 global $DB;
254
255 $this->assign_capability('moodle/category:manage');
256 $this->assign_capability('moodle/course:create');
257
258 $initialcatid = $DB->get_field_sql('SELECT max(id) from {course_categories}');
259
260 $category1 = coursecat::create(array('name' => 'Cat1'));
261 $category2 = coursecat::create(array('name' => 'Cat2', 'parent' => $category1->id));
262 $category3 = coursecat::create(array('name' => 'Cat3'));
263 $category4 = coursecat::create(array('name' => 'Cat4', 'parent' => $category2->id));
264
265 $course1 = $this->getDataGenerator()->create_course(array('category' => $category2->id));
266 $course2 = $this->getDataGenerator()->create_course(array('category' => $category4->id));
267 $course3 = $this->getDataGenerator()->create_course(array('category' => $category4->id));
268 $course4 = $this->getDataGenerator()->create_course(array('category' => $category1->id));
269
270 // Now we have
271 // $category1
272 // $category2
273 // $category4
274 // $course2
275 // $course3
276 // $course1
277 // $course4
278 // $category3
7db309f3 279 // structure.
b28bb7e8 280
7db309f3 281 // Login as another user to test course:delete capability (user who created course can delete it within 24h even without cap).
b28bb7e8
MG
282 $this->setUser($this->getDataGenerator()->create_user());
283
7db309f3
PS
284 // Delete category 2 and move content to category 3.
285 $this->assertFalse($category2->can_move_content_to($category3->id)); // No luck!
286 // Add necessary capabilities.
b28bb7e8
MG
287 $this->assign_capability('moodle/course:create', CAP_ALLOW, context_coursecat::instance($category3->id));
288 $this->assign_capability('moodle/category:manage');
7db309f3 289 $this->assertTrue($category2->can_move_content_to($category3->id)); // Hurray!
b28bb7e8
MG
290 $category2->delete_move($category3->id);
291
292 // Make sure we have:
293 // $category1
294 // $course4
295 // $category3
296 // $category4
297 // $course2
298 // $course3
299 // $course1
7db309f3 300 // structure.
b28bb7e8
MG
301
302 $this->assertNull(coursecat::get($category2->id, IGNORE_MISSING, true));
303 $this->assertEquals(array(), $category1->get_children());
304 $this->assertEquals(array($category4->id), array_keys($category3->get_children()));
305 $this->assertEquals($category4->id, $DB->get_field('course', 'category', array('id' => $course2->id)));
306 $this->assertEquals($category4->id, $DB->get_field('course', 'category', array('id' => $course3->id)));
307 $this->assertEquals($category3->id, $DB->get_field('course', 'category', array('id' => $course1->id)));
308
7db309f3
PS
309 // Delete category 3 completely.
310 $this->assertFalse($category3->can_delete_full()); // No luck!
311 // Add necessary capabilities.
b28bb7e8 312 $this->assign_capability('moodle/course:delete', CAP_ALLOW, context_coursecat::instance($category3->id));
7db309f3 313 $this->assertTrue($category3->can_delete_full()); // Hurray!
b28bb7e8
MG
314 $category3->delete_full();
315
316 // Make sure we have:
317 // $category1
318 // $course4
7db309f3 319 // structure.
b28bb7e8 320
7db309f3 321 // Note that we also have default 'Miscellaneous' category and default 'site' course.
b28bb7e8
MG
322 $this->assertEquals(1, $DB->get_field_sql('SELECT count(*) FROM {course_categories} WHERE id > ?', array($initialcatid)));
323 $this->assertEquals($category1->id, $DB->get_field_sql('SELECT max(id) FROM {course_categories}'));
324 $this->assertEquals(1, $DB->get_field_sql('SELECT count(*) FROM {course} WHERE id <> ?', array(SITEID)));
325 $this->assertEquals(array('id' => $course4->id, 'category' => $category1->id),
326 (array)$DB->get_record_sql('SELECT id, category from {course} where id <> ?', array(SITEID)));
327 }
eabbfa82
MG
328
329 public function test_get_children() {
330 $category1 = coursecat::create(array('name' => 'Cat1'));
331 $category2 = coursecat::create(array('name' => 'Cat2', 'parent' => $category1->id));
332 $category3 = coursecat::create(array('name' => 'Cat3', 'parent' => $category1->id, 'visible' => 0));
333 $category4 = coursecat::create(array('name' => 'Cat4', 'idnumber' => '12', 'parent' => $category1->id));
334 $category5 = coursecat::create(array('name' => 'Cat5', 'idnumber' => '11', 'parent' => $category1->id, 'visible' => 0));
335 $category6 = coursecat::create(array('name' => 'Cat6', 'idnumber' => '10', 'parent' => $category1->id));
336 $category7 = coursecat::create(array('name' => 'Cat0', 'parent' => $category1->id));
337
338 $children = $category1->get_children();
7db309f3
PS
339 // User does not have the capability to view hidden categories, so the list should be
340 // 2, 4, 6, 7.
eabbfa82
MG
341 $this->assertEquals(array($category2->id, $category4->id, $category6->id, $category7->id), array_keys($children));
342 $this->assertEquals(4, $category1->get_children_count());
343
344 $children = $category1->get_children(array('offset' => 2));
345 $this->assertEquals(array($category6->id, $category7->id), array_keys($children));
346 $this->assertEquals(4, $category1->get_children_count());
347
348 $children = $category1->get_children(array('limit' => 2));
349 $this->assertEquals(array($category2->id, $category4->id), array_keys($children));
350
351 $children = $category1->get_children(array('offset' => 1, 'limit' => 2));
352 $this->assertEquals(array($category4->id, $category6->id), array_keys($children));
353
354 $children = $category1->get_children(array('sort' => array('name' => 1)));
7db309f3 355 // Must be 7, 2, 4, 6.
eabbfa82
MG
356 $this->assertEquals(array($category7->id, $category2->id, $category4->id, $category6->id), array_keys($children));
357
358 $children = $category1->get_children(array('sort' => array('idnumber' => 1, 'name' => -1)));
7db309f3 359 // Must be 2, 7, 6, 4.
eabbfa82
MG
360 $this->assertEquals(array($category2->id, $category7->id, $category6->id, $category4->id), array_keys($children));
361
7db309f3 362 // Check that everything is all right after purging the caches.
eabbfa82
MG
363 cache_helper::purge_by_event('changesincoursecat');
364 $children = $category1->get_children();
365 $this->assertEquals(array($category2->id, $category4->id, $category6->id, $category7->id), array_keys($children));
366 $this->assertEquals(4, $category1->get_children_count());
367 }
93c544bd
MG
368
369 public function test_get_search_courses() {
370 $cat1 = coursecat::create(array('name' => 'Cat1'));
371 $cat2 = coursecat::create(array('name' => 'Cat2', 'parent' => $cat1->id));
372 $c1 = $this->getDataGenerator()->create_course(array('category' => $cat1->id, 'fullname' => 'Test 3', 'summary' => ' ', 'idnumber' => 'ID3'));
373 $c2 = $this->getDataGenerator()->create_course(array('category' => $cat1->id, 'fullname' => 'Test 1', 'summary' => ' ', 'visible' => 0));
374 $c3 = $this->getDataGenerator()->create_course(array('category' => $cat1->id, 'fullname' => 'Математика', 'summary' => ' Test '));
375 $c4 = $this->getDataGenerator()->create_course(array('category' => $cat1->id, 'fullname' => 'Test 4', 'summary' => ' ', 'idnumber' => 'ID4'));
376
377 $c5 = $this->getDataGenerator()->create_course(array('category' => $cat2->id, 'fullname' => 'Test 5', 'summary' => ' '));
378 $c6 = $this->getDataGenerator()->create_course(array('category' => $cat2->id, 'fullname' => 'Дискретная Математика', 'summary' => ' '));
379 $c7 = $this->getDataGenerator()->create_course(array('category' => $cat2->id, 'fullname' => 'Test 7', 'summary' => ' ', 'visible' => 0));
380 $c8 = $this->getDataGenerator()->create_course(array('category' => $cat2->id, 'fullname' => 'Test 8', 'summary' => ' '));
381
7db309f3 382 // Get courses in category 1 (returned visible only because user is not enrolled).
93c544bd 383 $res = $cat1->get_courses(array('sortorder' => 1));
7db309f3 384 $this->assertEquals(array($c4->id, $c3->id, $c1->id), array_keys($res)); // Courses are added in reverse order.
93c544bd
MG
385 $this->assertEquals(3, $cat1->get_courses_count());
386
7db309f3 387 // Get courses in category 1 recursively (returned visible only because user is not enrolled).
93c544bd
MG
388 $res = $cat1->get_courses(array('recursive' => 1));
389 $this->assertEquals(array($c4->id, $c3->id, $c1->id, $c8->id, $c6->id, $c5->id), array_keys($res));
390 $this->assertEquals(6, $cat1->get_courses_count(array('recursive' => 1)));
391
7db309f3 392 // Get courses sorted by fullname.
93c544bd
MG
393 $res = $cat1->get_courses(array('sort' => array('fullname' => 1)));
394 $this->assertEquals(array($c1->id, $c4->id, $c3->id), array_keys($res));
395 $this->assertEquals(3, $cat1->get_courses_count(array('sort' => array('fullname' => 1))));
396
7db309f3 397 // Get courses sorted by fullname recursively.
93c544bd
MG
398 $res = $cat1->get_courses(array('recursive' => 1, 'sort' => array('fullname' => 1)));
399 $this->assertEquals(array($c1->id, $c4->id, $c5->id, $c8->id, $c6->id, $c3->id), array_keys($res));
400 $this->assertEquals(6, $cat1->get_courses_count(array('recursive' => 1, 'sort' => array('fullname' => 1))));
401
7db309f3 402 // Get courses sorted by fullname recursively, use offset and limit.
93c544bd
MG
403 $res = $cat1->get_courses(array('recursive' => 1, 'offset' => 1, 'limit' => 2, 'sort' => array('fullname' => -1)));
404 $this->assertEquals(array($c6->id, $c8->id), array_keys($res));
7db309f3 405 // Offset and limit do not affect get_courses_count().
93c544bd
MG
406 $this->assertEquals(6, $cat1->get_courses_count(array('recursive' => 1, 'offset' => 1, 'limit' => 2, 'sort' => array('fullname' => 1))));
407
7db309f3 408 // Calling get_courses_count without prior call to get_courses().
93c544bd
MG
409 $this->assertEquals(3, $cat2->get_courses_count(array('recursive' => 1, 'sort' => array('idnumber' => 1))));
410
7db309f3 411 // Search courses.
93c544bd 412
7db309f3 413 // Search by text.
5e71c37e
MG
414 $res = coursecat::search_courses(array('search' => 'Test'));
415 $this->assertEquals(array($c4->id, $c3->id, $c1->id, $c8->id, $c5->id), array_keys($res));
416 $this->assertEquals(5, coursecat::search_courses_count(array('search' => 'Test')));
417
7db309f3 418 // Search by text with specified offset and limit.
5e71c37e
MG
419 $options = array('sort' => array('fullname' => 1), 'offset' => 1, 'limit' => 2);
420 $res = coursecat::search_courses(array('search' => 'Test'), $options);
421 $this->assertEquals(array($c4->id, $c5->id), array_keys($res));
422 $this->assertEquals(5, coursecat::search_courses_count(array('search' => 'Test'), $options));
423
424 // IMPORTANT: the tests below may fail on some databases
7db309f3 425 // case-insensitive search.
93c544bd
MG
426 $res = coursecat::search_courses(array('search' => 'test'));
427 $this->assertEquals(array($c4->id, $c3->id, $c1->id, $c8->id, $c5->id), array_keys($res));
428 $this->assertEquals(5, coursecat::search_courses_count(array('search' => 'test')));
429
7db309f3 430 // Non-latin language search.
93c544bd
MG
431 $res = coursecat::search_courses(array('search' => 'Математика'));
432 $this->assertEquals(array($c3->id, $c6->id), array_keys($res));
433 $this->assertEquals(2, coursecat::search_courses_count(array('search' => 'Математика'), array()));
93c544bd 434 }
31b0530a
MG
435
436 public function test_course_contacts() {
437 global $DB, $CFG;
438 $teacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'));
439 $managerrole = $DB->get_record('role', array('shortname'=>'manager'));
440 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
441 $oldcoursecontact = $CFG->coursecontact;
442
443 $CFG->coursecontact = $managerrole->id. ','. $teacherrole->id;
444
7db309f3 445 /*
31b0530a
MG
446 * User is listed in course contacts for the course if he has one of the
447 * "course contact" roles ($CFG->coursecontact) AND is enrolled in the course.
448 * If the user has several roles only the highest is displayed.
449 */
450
451 // Test case:
452 //
453 // == Cat1 (user2 has teacher role)
454 // == Cat2
455 // -- course21 (user2 is enrolled as manager) | [Expected] Manager: F2 L2
456 // -- course22 (user2 is enrolled as student) | [Expected] Teacher: F2 L2
457 // == Cat4 (user2 has manager role)
458 // -- course41 (user4 is enrolled as teacher, user5 is enrolled as manager) | [Expected] Manager: F5 L5, Teacher: F4 L4
459 // -- course42 (user2 is enrolled as teacher) | [Expected] Manager: F2 L2
460 // == Cat3 (user3 has manager role)
461 // -- course31 (user3 is enrolled as student) | [Expected] Manager: F3 L3
462 // -- course32 | [Expected]
463 // -- course11 (user1 is enrolled as teacher) | [Expected] Teacher: F1 L1
464 // -- course12 (user1 has teacher role) | [Expected]
465 // also user4 is enrolled as teacher but enrolment is not active
466 $category = $course = $enrol = $user = array();
467 $category[1] = coursecat::create(array('name' => 'Cat1'))->id;
468 $category[2] = coursecat::create(array('name' => 'Cat2', 'parent' => $category[1]))->id;
469 $category[3] = coursecat::create(array('name' => 'Cat3', 'parent' => $category[1]))->id;
470 $category[4] = coursecat::create(array('name' => 'Cat4', 'parent' => $category[2]))->id;
7db309f3
PS
471 foreach (array(1, 2, 3, 4) as $catid) {
472 foreach (array(1, 2) as $courseid) {
31b0530a
MG
473 $course[$catid][$courseid] = $this->getDataGenerator()->create_course(array('idnumber' => 'id'.$catid.$courseid,
474 'category' => $category[$catid]))->id;
475 $enrol[$catid][$courseid] = $DB->get_record('enrol', array('courseid'=>$course[$catid][$courseid], 'enrol'=>'manual'), '*', MUST_EXIST);
476 }
477 }
7db309f3 478 foreach (array(1, 2, 3, 4, 5) as $userid) {
31b0530a
MG
479 $user[$userid] = $this->getDataGenerator()->create_user(array('firstname' => 'F'.$userid, 'lastname' => 'L'.$userid))->id;
480 }
481
482 $manual = enrol_get_plugin('manual');
483
484 // Cat1 (user2 has teacher role)
485 role_assign($teacherrole->id, $user[2], context_coursecat::instance($category[1]));
486 // course21 (user2 is enrolled as manager)
487 $manual->enrol_user($enrol[2][1], $user[2], $managerrole->id);
488 // course22 (user2 is enrolled as student)
489 $manual->enrol_user($enrol[2][2], $user[2], $studentrole->id);
490 // Cat4 (user2 has manager role)
491 role_assign($managerrole->id, $user[2], context_coursecat::instance($category[4]));
492 // course41 (user4 is enrolled as teacher, user5 is enrolled as manager)
493 $manual->enrol_user($enrol[4][1], $user[4], $teacherrole->id);
494 $manual->enrol_user($enrol[4][1], $user[5], $managerrole->id);
495 // course42 (user2 is enrolled as teacher)
496 $manual->enrol_user($enrol[4][2], $user[2], $teacherrole->id);
497 // Cat3 (user3 has manager role)
498 role_assign($managerrole->id, $user[3], context_coursecat::instance($category[3]));
499 // course31 (user3 is enrolled as student)
500 $manual->enrol_user($enrol[3][1], $user[3], $studentrole->id);
501 // course11 (user1 is enrolled as teacher)
502 $manual->enrol_user($enrol[1][1], $user[1], $teacherrole->id);
503 // -- course12 (user1 has teacher role)
504 // also user4 is enrolled as teacher but enrolment is not active
505 role_assign($teacherrole->id, $user[1], context_course::instance($course[1][2]));
506 $manual->enrol_user($enrol[1][2], $user[4], $teacherrole->id, 0, 0, ENROL_USER_SUSPENDED);
507
508 $allcourses = coursecat::get(0)->get_courses(array('recursive' => true, 'coursecontacts' => true, 'sort' => array('idnumber' => 1)));
7db309f3 509 // Simplify the list of contacts for each course (similar as renderer would do).
31b0530a 510 $contacts = array();
7db309f3
PS
511 foreach (array(1, 2, 3, 4) as $catid) {
512 foreach (array(1, 2) as $courseid) {
31b0530a
MG
513 $tmp = array();
514 foreach ($allcourses[$course[$catid][$courseid]]->get_course_contacts() as $contact) {
515 $tmp[] = $contact['rolename']. ': '. $contact['username'];
516 }
517 $contacts[$catid][$courseid] = join(', ', $tmp);
518 }
519 }
520
521 // Assert:
522 // -- course21 (user2 is enrolled as manager) | Manager: F2 L2
7db309f3 523 $this->assertSame('Manager: F2 L2', $contacts[2][1]);
31b0530a 524 // -- course22 (user2 is enrolled as student) | Teacher: F2 L2
7db309f3 525 $this->assertSame('Teacher: F2 L2', $contacts[2][2]);
31b0530a 526 // -- course41 (user4 is enrolled as teacher, user5 is enrolled as manager) | Manager: F5 L5, Teacher: F4 L4
7db309f3 527 $this->assertSame('Manager: F5 L5, Teacher: F4 L4', $contacts[4][1]);
31b0530a 528 // -- course42 (user2 is enrolled as teacher) | [Expected] Manager: F2 L2
7db309f3 529 $this->assertSame('Manager: F2 L2', $contacts[4][2]);
31b0530a 530 // -- course31 (user3 is enrolled as student) | Manager: F3 L3
7db309f3 531 $this->assertSame('Manager: F3 L3', $contacts[3][1]);
31b0530a 532 // -- course32 |
7db309f3 533 $this->assertSame('', $contacts[3][2]);
31b0530a 534 // -- course11 (user1 is enrolled as teacher) | Teacher: F1 L1
7db309f3 535 $this->assertSame('Teacher: F1 L1', $contacts[1][1]);
31b0530a 536 // -- course12 (user1 has teacher role) |
7db309f3 537 $this->assertSame('', $contacts[1][2]);
31b0530a
MG
538
539 $CFG->coursecontact = $oldcoursecontact;
540 }
961403c4
MG
541
542 public function test_overview_files() {
543 global $CFG;
544 $this->setAdminUser();
545 $cat1 = coursecat::create(array('name' => 'Cat1'));
546
547 // Create course c1 with one image file.
548 $dratid1 = $this->fill_draft_area(array('filename.jpg' => 'Test file contents1'));
549 $c1 = $this->getDataGenerator()->create_course(array('category' => $cat1->id,
550 'fullname' => 'Test 1', 'overviewfiles_filemanager' => $dratid1));
551 // Create course c2 with two image files (only one file will be added because of settings).
552 $dratid2 = $this->fill_draft_area(array('filename21.jpg' => 'Test file contents21', 'filename22.jpg' => 'Test file contents22'));
553 $c2 = $this->getDataGenerator()->create_course(array('category' => $cat1->id,
554 'fullname' => 'Test 2', 'overviewfiles_filemanager' => $dratid2));
555 // Create course c3 without files.
556 $c3 = $this->getDataGenerator()->create_course(array('category' => $cat1->id, 'fullname' => 'Test 3'));
557
558 // Change the settings to allow multiple files of any types.
559 $CFG->courseoverviewfileslimit = 3;
560 $CFG->courseoverviewfilesext = '*';
561 // Create course c5 with two image files.
562 $dratid4 = $this->fill_draft_area(array('filename41.jpg' => 'Test file contents41', 'filename42.jpg' => 'Test file contents42'));
563 $c4 = $this->getDataGenerator()->create_course(array('category' => $cat1->id,
564 'fullname' => 'Test 4', 'overviewfiles_filemanager' => $dratid4));
565 // Create course c6 with non-image file.
566 $dratid5 = $this->fill_draft_area(array('filename51.zip' => 'Test file contents51'));
567 $c5 = $this->getDataGenerator()->create_course(array('category' => $cat1->id,
568 'fullname' => 'Test 5', 'overviewfiles_filemanager' => $dratid5));
569
570 // Reset default settings.
571 $CFG->courseoverviewfileslimit = 1;
572 $CFG->courseoverviewfilesext = '.jpg,.gif,.png';
573
574 $courses = $cat1->get_courses();
575 $this->assertTrue($courses[$c1->id]->has_course_overviewfiles());
576 $this->assertTrue($courses[$c2->id]->has_course_overviewfiles());
577 $this->assertFalse($courses[$c3->id]->has_course_overviewfiles());
578 $this->assertTrue($courses[$c4->id]->has_course_overviewfiles());
579 $this->assertTrue($courses[$c5->id]->has_course_overviewfiles()); // Does not validate the filetypes.
580
581 $this->assertEquals(1, count($courses[$c1->id]->get_course_overviewfiles()));
582 $this->assertEquals(1, count($courses[$c2->id]->get_course_overviewfiles()));
583 $this->assertEquals(0, count($courses[$c3->id]->get_course_overviewfiles()));
584 $this->assertEquals(1, count($courses[$c4->id]->get_course_overviewfiles()));
585 $this->assertEquals(0, count($courses[$c5->id]->get_course_overviewfiles())); // Validate the filetypes.
586
587 // Overview files are not allowed, all functions return empty values.
588 $CFG->courseoverviewfileslimit = 0;
589
590 $this->assertFalse($courses[$c1->id]->has_course_overviewfiles());
591 $this->assertFalse($courses[$c2->id]->has_course_overviewfiles());
592 $this->assertFalse($courses[$c3->id]->has_course_overviewfiles());
593 $this->assertFalse($courses[$c4->id]->has_course_overviewfiles());
594 $this->assertFalse($courses[$c5->id]->has_course_overviewfiles());
595
596 $this->assertEquals(0, count($courses[$c1->id]->get_course_overviewfiles()));
597 $this->assertEquals(0, count($courses[$c2->id]->get_course_overviewfiles()));
598 $this->assertEquals(0, count($courses[$c3->id]->get_course_overviewfiles()));
599 $this->assertEquals(0, count($courses[$c4->id]->get_course_overviewfiles()));
600 $this->assertEquals(0, count($courses[$c5->id]->get_course_overviewfiles()));
601
602 // Multiple overview files are allowed but still limited to images.
603 $CFG->courseoverviewfileslimit = 3;
604
605 $this->assertTrue($courses[$c1->id]->has_course_overviewfiles());
606 $this->assertTrue($courses[$c2->id]->has_course_overviewfiles());
607 $this->assertFalse($courses[$c3->id]->has_course_overviewfiles());
608 $this->assertTrue($courses[$c4->id]->has_course_overviewfiles());
609 $this->assertTrue($courses[$c5->id]->has_course_overviewfiles()); // Still does not validate the filetypes.
610
611 $this->assertEquals(1, count($courses[$c1->id]->get_course_overviewfiles()));
612 $this->assertEquals(1, count($courses[$c2->id]->get_course_overviewfiles())); // Only 1 file was actually added.
613 $this->assertEquals(0, count($courses[$c3->id]->get_course_overviewfiles()));
614 $this->assertEquals(2, count($courses[$c4->id]->get_course_overviewfiles()));
615 $this->assertEquals(0, count($courses[$c5->id]->get_course_overviewfiles()));
616
617 // Multiple overview files of any type are allowed.
618 $CFG->courseoverviewfilesext = '*';
619
620 $this->assertTrue($courses[$c1->id]->has_course_overviewfiles());
621 $this->assertTrue($courses[$c2->id]->has_course_overviewfiles());
622 $this->assertFalse($courses[$c3->id]->has_course_overviewfiles());
623 $this->assertTrue($courses[$c4->id]->has_course_overviewfiles());
624 $this->assertTrue($courses[$c5->id]->has_course_overviewfiles());
625
626 $this->assertEquals(1, count($courses[$c1->id]->get_course_overviewfiles()));
627 $this->assertEquals(1, count($courses[$c2->id]->get_course_overviewfiles()));
628 $this->assertEquals(0, count($courses[$c3->id]->get_course_overviewfiles()));
629 $this->assertEquals(2, count($courses[$c4->id]->get_course_overviewfiles()));
630 $this->assertEquals(1, count($courses[$c5->id]->get_course_overviewfiles()));
631 }
632
633 /**
634 * Creates a draft area for current user and fills it with fake files
635 *
636 * @param array $files array of files that need to be added to filearea, filename => filecontents
637 * @return int draftid for the filearea
638 */
639 protected function fill_draft_area(array $files) {
640 global $USER;
641 $usercontext = context_user::instance($USER->id);
642 $draftid = file_get_unused_draft_itemid();
643 foreach ($files as $filename => $filecontents) {
644 // Add actual file there.
645 $filerecord = array('component' => 'user', 'filearea' => 'draft',
646 'contextid' => $usercontext->id, 'itemid' => $draftid,
647 'filename' => $filename, 'filepath' => '/');
648 $fs = get_file_storage();
649 $fs->create_file_from_string($filerecord, $filecontents);
650 }
651 return $draftid;
652 }
7db309f3 653}