Merge branch 'MDL-42592_master' of https://github.com/markn86/moodle
[moodle.git] / lib / tests / coursecatlib_test.php
blob61b3b65520669a19b3060fcee6419885200e9f13
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 * Tests for class coursecat from lib/coursecatlib.php
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
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 require_once($CFG->libdir . '/coursecatlib.php');
31 /**
32 * Functional test for coursecatlib.php
34 class core_coursecatlib_testcase extends advanced_testcase {
36 protected $roles;
38 protected function setUp() {
39 parent::setUp();
40 $this->resetAfterTest();
41 $user = $this->getDataGenerator()->create_user();
42 $this->setUser($user);
45 protected function get_roleid($context = null) {
46 global $USER;
47 if ($context === null) {
48 $context = context_system::instance();
50 if (is_object($context)) {
51 $context = $context->id;
53 if (empty($this->roles)) {
54 $this->roles = array();
56 if (empty($this->roles[$USER->id])) {
57 $this->roles[$USER->id] = array();
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);
63 return $this->roles[$USER->id][$context];
66 protected function assign_capability($capability, $permission = CAP_ALLOW, $contextid = null) {
67 if ($contextid === null) {
68 $contextid = context_system::instance();
70 if (is_object($contextid)) {
71 $contextid = $contextid->id;
73 assign_capability($capability, $permission, $this->get_roleid($contextid), $contextid, true);
74 accesslib_clear_all_caches_for_unit_testing();
77 public function test_create_coursecat() {
78 // Create the category.
79 $data = new stdClass();
80 $data->name = 'aaa';
81 $data->description = 'aaa';
82 $data->idnumber = '';
84 $category1 = coursecat::create($data);
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);
91 $this->assertGreaterThanOrEqual(1, $category1->sortorder);
93 // Create two more categories and test the sortorder worked correctly.
94 $data->name = 'ccc';
95 $category2 = coursecat::create($data);
97 $data->name = 'bbb';
98 $category3 = coursecat::create($data);
100 $this->assertGreaterThan($category1->sortorder, $category2->sortorder);
101 $this->assertGreaterThan($category2->sortorder, $category3->sortorder);
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) {
109 $this->assertInstanceOf('moodle_exception', $e);
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) {
116 $this->assertInstanceOf('moodle_exception', $e);
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) {
122 $this->assertInstanceOf('moodle_exception', $e);
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) {
129 $this->assertInstanceOf('moodle_exception', $e);
133 public function test_visibility() {
134 $this->assign_capability('moodle/category:viewhiddencategories');
135 $this->assign_capability('moodle/category:manage');
137 // Create category 1 initially hidden.
138 $category1 = coursecat::create(array('name' => 'Cat1', 'visible' => 0));
139 $this->assertEquals(0, $category1->visible);
140 $this->assertEquals(0, $category1->visibleold);
142 // Create category 2 initially hidden as a child of hidden category 1.
143 $category2 = coursecat::create(array('name' => 'Cat2', 'visible' => 0, 'parent' => $category1->id));
144 $this->assertEquals(0, $category2->visible);
145 $this->assertEquals(0, $category2->visibleold);
147 // Create category 3 initially visible as a child of hidden category 1.
148 $category3 = coursecat::create(array('name' => 'Cat3', 'visible' => 1, 'parent' => $category1->id));
149 $this->assertEquals(0, $category3->visible);
150 $this->assertEquals(1, $category3->visibleold);
152 // Show category 1 and make sure that category 2 is hidden and category 3 is visible.
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);
158 // Create visible category 4.
159 $category4 = coursecat::create(array('name' => 'Cat4'));
160 $this->assertEquals(1, $category4->visible);
161 $this->assertEquals(1, $category4->visibleold);
163 // Create visible category 5 as a child of visible category 4.
164 $category5 = coursecat::create(array('name' => 'Cat5', 'parent' => $category4->id));
165 $this->assertEquals(1, $category5->visible);
166 $this->assertEquals(1, $category5->visibleold);
168 // Hide category 4 and make sure category 5 is hidden too.
169 $category4->hide();
170 $this->assertEquals(0, $category4->visible);
171 $this->assertEquals(0, $category4->visibleold);
172 $category5 = coursecat::get($category5->id); // We have to re-read from DB.
173 $this->assertEquals(0, $category5->visible);
174 $this->assertEquals(1, $category5->visibleold);
176 // Show category 4 and make sure category 5 is visible too.
177 $category4->show();
178 $this->assertEquals(1, $category4->visible);
179 $this->assertEquals(1, $category4->visibleold);
180 $category5 = coursecat::get($category5->id); // We have to re-read from DB.
181 $this->assertEquals(1, $category5->visible);
182 $this->assertEquals(1, $category5->visibleold);
184 // Move category 5 under hidden category 2 and make sure it became hidden.
185 $category5->change_parent($category2->id);
186 $this->assertEquals(0, $category5->visible);
187 $this->assertEquals(1, $category5->visibleold);
189 // Re-read object for category 5 from DB and check again.
190 $category5 = coursecat::get($category5->id);
191 $this->assertEquals(0, $category5->visible);
192 $this->assertEquals(1, $category5->visibleold);
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.
196 $category5->change_parent(0);
197 $this->assertEquals(0, $category5->visible);
198 $this->assertEquals(1, $category5->visibleold);
201 public function test_hierarchy() {
202 $this->assign_capability('moodle/category:viewhiddencategories');
203 $this->assign_capability('moodle/category:manage');
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));
210 // Check function get_children().
211 $this->assertEquals(array($category2->id, $category3->id), array_keys($category1->get_children()));
212 // Check function get_parents().
213 $this->assertEquals(array($category1->id, $category2->id), $category4->get_parents());
215 // Can not move category to itself or to it's children.
216 $this->assertFalse($category1->can_change_parent($category2->id));
217 $this->assertFalse($category2->can_change_parent($category2->id));
218 // Can move category to grandparent.
219 $this->assertTrue($category4->can_change_parent($category1->id));
221 try {
222 $category2->change_parent($category4->id);
223 $this->fail('Exception expected - can not move category');
224 } catch (moodle_exception $e) {
225 $this->assertInstanceOf('moodle_exception', $e);
228 $category4->change_parent(0);
229 $this->assertEquals(array(), $category4->get_parents());
230 $this->assertEquals(array($category2->id, $category3->id), array_keys($category1->get_children()));
231 $this->assertEquals(array(), array_keys($category2->get_children()));
234 public function test_update() {
235 $category1 = coursecat::create(array('name' => 'Cat1'));
236 $timecreated = $category1->timemodified;
237 $this->assertSame('Cat1', $category1->name);
238 $this->assertTrue(empty($category1->description));
239 sleep(2);
240 $testdescription = 'This is cat 1 а также русский текст';
241 $category1->update(array('description' => $testdescription));
242 $this->assertSame($testdescription, $category1->description);
243 $category1 = coursecat::get($category1->id);
244 $this->assertSame($testdescription, $category1->description);
245 cache_helper::purge_by_event('changesincoursecat');
246 $category1 = coursecat::get($category1->id);
247 $this->assertSame($testdescription, $category1->description);
249 $this->assertGreaterThan($timecreated, $category1->timemodified);
252 public function test_delete() {
253 global $DB;
255 $this->assign_capability('moodle/category:manage');
256 $this->assign_capability('moodle/course:create');
258 $initialcatid = $DB->get_field_sql('SELECT max(id) from {course_categories}');
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));
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));
270 // Now we have
271 // $category1
272 // $category2
273 // $category4
274 // $course2
275 // $course3
276 // $course1
277 // $course4
278 // $category3
279 // structure.
281 // Login as another user to test course:delete capability (user who created course can delete it within 24h even without cap).
282 $this->setUser($this->getDataGenerator()->create_user());
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.
287 $this->assign_capability('moodle/course:create', CAP_ALLOW, context_coursecat::instance($category3->id));
288 $this->assign_capability('moodle/category:manage');
289 $this->assertTrue($category2->can_move_content_to($category3->id)); // Hurray!
290 $category2->delete_move($category3->id);
292 // Make sure we have:
293 // $category1
294 // $course4
295 // $category3
296 // $category4
297 // $course2
298 // $course3
299 // $course1
300 // structure.
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)));
309 // Delete category 3 completely.
310 $this->assertFalse($category3->can_delete_full()); // No luck!
311 // Add necessary capabilities.
312 $this->assign_capability('moodle/course:delete', CAP_ALLOW, context_coursecat::instance($category3->id));
313 $this->assertTrue($category3->can_delete_full()); // Hurray!
314 $category3->delete_full();
316 // Make sure we have:
317 // $category1
318 // $course4
319 // structure.
321 // Note that we also have default 'Miscellaneous' category and default 'site' course.
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)));
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));
338 $children = $category1->get_children();
339 // User does not have the capability to view hidden categories, so the list should be
340 // 2, 4, 6, 7.
341 $this->assertEquals(array($category2->id, $category4->id, $category6->id, $category7->id), array_keys($children));
342 $this->assertEquals(4, $category1->get_children_count());
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());
348 $children = $category1->get_children(array('limit' => 2));
349 $this->assertEquals(array($category2->id, $category4->id), array_keys($children));
351 $children = $category1->get_children(array('offset' => 1, 'limit' => 2));
352 $this->assertEquals(array($category4->id, $category6->id), array_keys($children));
354 $children = $category1->get_children(array('sort' => array('name' => 1)));
355 // Must be 7, 2, 4, 6.
356 $this->assertEquals(array($category7->id, $category2->id, $category4->id, $category6->id), array_keys($children));
358 $children = $category1->get_children(array('sort' => array('idnumber' => 1, 'name' => -1)));
359 // Must be 2, 7, 6, 4.
360 $this->assertEquals(array($category2->id, $category7->id, $category6->id, $category4->id), array_keys($children));
362 // Check that everything is all right after purging the caches.
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());
370 * Test the countall function
372 public function test_count_all() {
373 // There should be just the default category.
374 $this->assertEquals(1, coursecat::count_all());
375 $category1 = coursecat::create(array('name' => 'Cat1'));
376 $category2 = coursecat::create(array('name' => 'Cat2', 'parent' => $category1->id));
377 $category3 = coursecat::create(array('name' => 'Cat3', 'parent' => $category2->id, 'visible' => 0));
378 // Now we've got four.
379 $this->assertEquals(4, coursecat::count_all());
380 cache_helper::purge_by_event('changesincoursecat');
381 // We should still have 4.
382 $this->assertEquals(4, coursecat::count_all());
386 * Test a categories ability to resort courses.
388 public function test_resort_courses() {
389 $this->resetAfterTest(true);
390 $generator = $this->getDataGenerator();
391 $category = $generator->create_category();
392 $course1 = $generator->create_course(array(
393 'category' => $category->id,
394 'idnumber' => '006-01',
395 'shortname' => 'Biome Study',
396 'fullname' => '<span lang="ar" class="multilang">'.'دراسة منطقة إحيائية'.'</span><span lang="en" class="multilang">Biome Study</span>'
398 $course2 = $generator->create_course(array(
399 'category' => $category->id,
400 'idnumber' => '007-02',
401 'shortname' => 'Chemistry Revision',
402 'fullname' => 'Chemistry Revision'
404 $course3 = $generator->create_course(array(
405 'category' => $category->id,
406 'idnumber' => '007-03',
407 'shortname' => 'Swiss Rolls and Sunflowers',
408 'fullname' => 'Aarkvarks guide to Swiss Rolls and Sunflowers'
410 $course4 = $generator->create_course(array(
411 'category' => $category->id,
412 'idnumber' => '006-04',
413 'shortname' => 'Scratch',
414 'fullname' => '<a href="test.php">Basic Scratch</a>'
416 $c1 = (int)$course1->id;
417 $c2 = (int)$course2->id;
418 $c3 = (int)$course3->id;
419 $c4 = (int)$course4->id;
421 $coursecat = coursecat::get($category->id);
422 $this->assertTrue($coursecat->resort_courses('idnumber'));
423 $this->assertSame(array($c1, $c4, $c2, $c3), array_keys($coursecat->get_courses()));
425 $this->assertTrue($coursecat->resort_courses('shortname'));
426 $this->assertSame(array($c1, $c2, $c4, $c3), array_keys($coursecat->get_courses()));
429 try {
430 // Enable the multilang filter and set it to apply to headings and content.
431 filter_set_global_state('multilang', TEXTFILTER_ON);
432 filter_set_applies_to_strings('multilang', true);
433 $expected = array($c3, $c4, $c1, $c2);
434 } catch (coding_exception $ex) {
435 $expected = array($c3, $c4, $c2, $c1);
437 $this->assertTrue($coursecat->resort_courses('fullname'));
438 $this->assertSame($expected, array_keys($coursecat->get_courses()));
441 public function test_get_search_courses() {
442 $cat1 = coursecat::create(array('name' => 'Cat1'));
443 $cat2 = coursecat::create(array('name' => 'Cat2', 'parent' => $cat1->id));
444 $c1 = $this->getDataGenerator()->create_course(array('category' => $cat1->id, 'fullname' => 'Test 3', 'summary' => ' ', 'idnumber' => 'ID3'));
445 $c2 = $this->getDataGenerator()->create_course(array('category' => $cat1->id, 'fullname' => 'Test 1', 'summary' => ' ', 'visible' => 0));
446 $c3 = $this->getDataGenerator()->create_course(array('category' => $cat1->id, 'fullname' => 'Математика', 'summary' => ' Test '));
447 $c4 = $this->getDataGenerator()->create_course(array('category' => $cat1->id, 'fullname' => 'Test 4', 'summary' => ' ', 'idnumber' => 'ID4'));
449 $c5 = $this->getDataGenerator()->create_course(array('category' => $cat2->id, 'fullname' => 'Test 5', 'summary' => ' '));
450 $c6 = $this->getDataGenerator()->create_course(array('category' => $cat2->id, 'fullname' => 'Дискретная Математика', 'summary' => ' '));
451 $c7 = $this->getDataGenerator()->create_course(array('category' => $cat2->id, 'fullname' => 'Test 7', 'summary' => ' ', 'visible' => 0));
452 $c8 = $this->getDataGenerator()->create_course(array('category' => $cat2->id, 'fullname' => 'Test 8', 'summary' => ' '));
454 // Get courses in category 1 (returned visible only because user is not enrolled).
455 $res = $cat1->get_courses(array('sortorder' => 1));
456 $this->assertEquals(array($c4->id, $c3->id, $c1->id), array_keys($res)); // Courses are added in reverse order.
457 $this->assertEquals(3, $cat1->get_courses_count());
459 // Get courses in category 1 recursively (returned visible only because user is not enrolled).
460 $res = $cat1->get_courses(array('recursive' => 1));
461 $this->assertEquals(array($c4->id, $c3->id, $c1->id, $c8->id, $c6->id, $c5->id), array_keys($res));
462 $this->assertEquals(6, $cat1->get_courses_count(array('recursive' => 1)));
464 // Get courses sorted by fullname.
465 $res = $cat1->get_courses(array('sort' => array('fullname' => 1)));
466 $this->assertEquals(array($c1->id, $c4->id, $c3->id), array_keys($res));
467 $this->assertEquals(3, $cat1->get_courses_count(array('sort' => array('fullname' => 1))));
469 // Get courses sorted by fullname recursively.
470 $res = $cat1->get_courses(array('recursive' => 1, 'sort' => array('fullname' => 1)));
471 $this->assertEquals(array($c1->id, $c4->id, $c5->id, $c8->id, $c6->id, $c3->id), array_keys($res));
472 $this->assertEquals(6, $cat1->get_courses_count(array('recursive' => 1, 'sort' => array('fullname' => 1))));
474 // Get courses sorted by fullname recursively, use offset and limit.
475 $res = $cat1->get_courses(array('recursive' => 1, 'offset' => 1, 'limit' => 2, 'sort' => array('fullname' => -1)));
476 $this->assertEquals(array($c6->id, $c8->id), array_keys($res));
477 // Offset and limit do not affect get_courses_count().
478 $this->assertEquals(6, $cat1->get_courses_count(array('recursive' => 1, 'offset' => 1, 'limit' => 2, 'sort' => array('fullname' => 1))));
480 // Calling get_courses_count without prior call to get_courses().
481 $this->assertEquals(3, $cat2->get_courses_count(array('recursive' => 1, 'sort' => array('idnumber' => 1))));
483 // Search courses.
485 // Search by text.
486 $res = coursecat::search_courses(array('search' => 'Test'));
487 $this->assertEquals(array($c4->id, $c3->id, $c1->id, $c8->id, $c5->id), array_keys($res));
488 $this->assertEquals(5, coursecat::search_courses_count(array('search' => 'Test')));
490 // Search by text with specified offset and limit.
491 $options = array('sort' => array('fullname' => 1), 'offset' => 1, 'limit' => 2);
492 $res = coursecat::search_courses(array('search' => 'Test'), $options);
493 $this->assertEquals(array($c4->id, $c5->id), array_keys($res));
494 $this->assertEquals(5, coursecat::search_courses_count(array('search' => 'Test'), $options));
496 // IMPORTANT: the tests below may fail on some databases
497 // case-insensitive search.
498 $res = coursecat::search_courses(array('search' => 'test'));
499 $this->assertEquals(array($c4->id, $c3->id, $c1->id, $c8->id, $c5->id), array_keys($res));
500 $this->assertEquals(5, coursecat::search_courses_count(array('search' => 'test')));
502 // Non-latin language search.
503 $res = coursecat::search_courses(array('search' => 'Математика'));
504 $this->assertEquals(array($c3->id, $c6->id), array_keys($res));
505 $this->assertEquals(2, coursecat::search_courses_count(array('search' => 'Математика'), array()));
508 public function test_course_contacts() {
509 global $DB, $CFG;
510 $teacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'));
511 $managerrole = $DB->get_record('role', array('shortname'=>'manager'));
512 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
513 $oldcoursecontact = $CFG->coursecontact;
515 $CFG->coursecontact = $managerrole->id. ','. $teacherrole->id;
518 * User is listed in course contacts for the course if he has one of the
519 * "course contact" roles ($CFG->coursecontact) AND is enrolled in the course.
520 * If the user has several roles only the highest is displayed.
523 // Test case:
525 // == Cat1 (user2 has teacher role)
526 // == Cat2
527 // -- course21 (user2 is enrolled as manager) | [Expected] Manager: F2 L2
528 // -- course22 (user2 is enrolled as student) | [Expected] Teacher: F2 L2
529 // == Cat4 (user2 has manager role)
530 // -- course41 (user4 is enrolled as teacher, user5 is enrolled as manager) | [Expected] Manager: F5 L5, Teacher: F4 L4
531 // -- course42 (user2 is enrolled as teacher) | [Expected] Manager: F2 L2
532 // == Cat3 (user3 has manager role)
533 // -- course31 (user3 is enrolled as student) | [Expected] Manager: F3 L3
534 // -- course32 | [Expected]
535 // -- course11 (user1 is enrolled as teacher) | [Expected] Teacher: F1 L1
536 // -- course12 (user1 has teacher role) | [Expected]
537 // also user4 is enrolled as teacher but enrolment is not active
538 $category = $course = $enrol = $user = array();
539 $category[1] = coursecat::create(array('name' => 'Cat1'))->id;
540 $category[2] = coursecat::create(array('name' => 'Cat2', 'parent' => $category[1]))->id;
541 $category[3] = coursecat::create(array('name' => 'Cat3', 'parent' => $category[1]))->id;
542 $category[4] = coursecat::create(array('name' => 'Cat4', 'parent' => $category[2]))->id;
543 foreach (array(1, 2, 3, 4) as $catid) {
544 foreach (array(1, 2) as $courseid) {
545 $course[$catid][$courseid] = $this->getDataGenerator()->create_course(array('idnumber' => 'id'.$catid.$courseid,
546 'category' => $category[$catid]))->id;
547 $enrol[$catid][$courseid] = $DB->get_record('enrol', array('courseid'=>$course[$catid][$courseid], 'enrol'=>'manual'), '*', MUST_EXIST);
550 foreach (array(1, 2, 3, 4, 5) as $userid) {
551 $user[$userid] = $this->getDataGenerator()->create_user(array('firstname' => 'F'.$userid, 'lastname' => 'L'.$userid))->id;
554 $manual = enrol_get_plugin('manual');
556 // Cat1 (user2 has teacher role)
557 role_assign($teacherrole->id, $user[2], context_coursecat::instance($category[1]));
558 // course21 (user2 is enrolled as manager)
559 $manual->enrol_user($enrol[2][1], $user[2], $managerrole->id);
560 // course22 (user2 is enrolled as student)
561 $manual->enrol_user($enrol[2][2], $user[2], $studentrole->id);
562 // Cat4 (user2 has manager role)
563 role_assign($managerrole->id, $user[2], context_coursecat::instance($category[4]));
564 // course41 (user4 is enrolled as teacher, user5 is enrolled as manager)
565 $manual->enrol_user($enrol[4][1], $user[4], $teacherrole->id);
566 $manual->enrol_user($enrol[4][1], $user[5], $managerrole->id);
567 // course42 (user2 is enrolled as teacher)
568 $manual->enrol_user($enrol[4][2], $user[2], $teacherrole->id);
569 // Cat3 (user3 has manager role)
570 role_assign($managerrole->id, $user[3], context_coursecat::instance($category[3]));
571 // course31 (user3 is enrolled as student)
572 $manual->enrol_user($enrol[3][1], $user[3], $studentrole->id);
573 // course11 (user1 is enrolled as teacher)
574 $manual->enrol_user($enrol[1][1], $user[1], $teacherrole->id);
575 // -- course12 (user1 has teacher role)
576 // also user4 is enrolled as teacher but enrolment is not active
577 role_assign($teacherrole->id, $user[1], context_course::instance($course[1][2]));
578 $manual->enrol_user($enrol[1][2], $user[4], $teacherrole->id, 0, 0, ENROL_USER_SUSPENDED);
580 $allcourses = coursecat::get(0)->get_courses(array('recursive' => true, 'coursecontacts' => true, 'sort' => array('idnumber' => 1)));
581 // Simplify the list of contacts for each course (similar as renderer would do).
582 $contacts = array();
583 foreach (array(1, 2, 3, 4) as $catid) {
584 foreach (array(1, 2) as $courseid) {
585 $tmp = array();
586 foreach ($allcourses[$course[$catid][$courseid]]->get_course_contacts() as $contact) {
587 $tmp[] = $contact['rolename']. ': '. $contact['username'];
589 $contacts[$catid][$courseid] = join(', ', $tmp);
593 // Assert:
594 // -- course21 (user2 is enrolled as manager) | Manager: F2 L2
595 $this->assertSame('Manager: F2 L2', $contacts[2][1]);
596 // -- course22 (user2 is enrolled as student) | Teacher: F2 L2
597 $this->assertSame('Teacher: F2 L2', $contacts[2][2]);
598 // -- course41 (user4 is enrolled as teacher, user5 is enrolled as manager) | Manager: F5 L5, Teacher: F4 L4
599 $this->assertSame('Manager: F5 L5, Teacher: F4 L4', $contacts[4][1]);
600 // -- course42 (user2 is enrolled as teacher) | [Expected] Manager: F2 L2
601 $this->assertSame('Manager: F2 L2', $contacts[4][2]);
602 // -- course31 (user3 is enrolled as student) | Manager: F3 L3
603 $this->assertSame('Manager: F3 L3', $contacts[3][1]);
604 // -- course32 |
605 $this->assertSame('', $contacts[3][2]);
606 // -- course11 (user1 is enrolled as teacher) | Teacher: F1 L1
607 $this->assertSame('Teacher: F1 L1', $contacts[1][1]);
608 // -- course12 (user1 has teacher role) |
609 $this->assertSame('', $contacts[1][2]);
611 $CFG->coursecontact = $oldcoursecontact;
614 public function test_overview_files() {
615 global $CFG;
616 $this->setAdminUser();
617 $cat1 = coursecat::create(array('name' => 'Cat1'));
619 // Create course c1 with one image file.
620 $dratid1 = $this->fill_draft_area(array('filename.jpg' => 'Test file contents1'));
621 $c1 = $this->getDataGenerator()->create_course(array('category' => $cat1->id,
622 'fullname' => 'Test 1', 'overviewfiles_filemanager' => $dratid1));
623 // Create course c2 with two image files (only one file will be added because of settings).
624 $dratid2 = $this->fill_draft_area(array('filename21.jpg' => 'Test file contents21', 'filename22.jpg' => 'Test file contents22'));
625 $c2 = $this->getDataGenerator()->create_course(array('category' => $cat1->id,
626 'fullname' => 'Test 2', 'overviewfiles_filemanager' => $dratid2));
627 // Create course c3 without files.
628 $c3 = $this->getDataGenerator()->create_course(array('category' => $cat1->id, 'fullname' => 'Test 3'));
630 // Change the settings to allow multiple files of any types.
631 $CFG->courseoverviewfileslimit = 3;
632 $CFG->courseoverviewfilesext = '*';
633 // Create course c5 with two image files.
634 $dratid4 = $this->fill_draft_area(array('filename41.jpg' => 'Test file contents41', 'filename42.jpg' => 'Test file contents42'));
635 $c4 = $this->getDataGenerator()->create_course(array('category' => $cat1->id,
636 'fullname' => 'Test 4', 'overviewfiles_filemanager' => $dratid4));
637 // Create course c6 with non-image file.
638 $dratid5 = $this->fill_draft_area(array('filename51.zip' => 'Test file contents51'));
639 $c5 = $this->getDataGenerator()->create_course(array('category' => $cat1->id,
640 'fullname' => 'Test 5', 'overviewfiles_filemanager' => $dratid5));
642 // Reset default settings.
643 $CFG->courseoverviewfileslimit = 1;
644 $CFG->courseoverviewfilesext = '.jpg,.gif,.png';
646 $courses = $cat1->get_courses();
647 $this->assertTrue($courses[$c1->id]->has_course_overviewfiles());
648 $this->assertTrue($courses[$c2->id]->has_course_overviewfiles());
649 $this->assertFalse($courses[$c3->id]->has_course_overviewfiles());
650 $this->assertTrue($courses[$c4->id]->has_course_overviewfiles());
651 $this->assertTrue($courses[$c5->id]->has_course_overviewfiles()); // Does not validate the filetypes.
653 $this->assertEquals(1, count($courses[$c1->id]->get_course_overviewfiles()));
654 $this->assertEquals(1, count($courses[$c2->id]->get_course_overviewfiles()));
655 $this->assertEquals(0, count($courses[$c3->id]->get_course_overviewfiles()));
656 $this->assertEquals(1, count($courses[$c4->id]->get_course_overviewfiles()));
657 $this->assertEquals(0, count($courses[$c5->id]->get_course_overviewfiles())); // Validate the filetypes.
659 // Overview files are not allowed, all functions return empty values.
660 $CFG->courseoverviewfileslimit = 0;
662 $this->assertFalse($courses[$c1->id]->has_course_overviewfiles());
663 $this->assertFalse($courses[$c2->id]->has_course_overviewfiles());
664 $this->assertFalse($courses[$c3->id]->has_course_overviewfiles());
665 $this->assertFalse($courses[$c4->id]->has_course_overviewfiles());
666 $this->assertFalse($courses[$c5->id]->has_course_overviewfiles());
668 $this->assertEquals(0, count($courses[$c1->id]->get_course_overviewfiles()));
669 $this->assertEquals(0, count($courses[$c2->id]->get_course_overviewfiles()));
670 $this->assertEquals(0, count($courses[$c3->id]->get_course_overviewfiles()));
671 $this->assertEquals(0, count($courses[$c4->id]->get_course_overviewfiles()));
672 $this->assertEquals(0, count($courses[$c5->id]->get_course_overviewfiles()));
674 // Multiple overview files are allowed but still limited to images.
675 $CFG->courseoverviewfileslimit = 3;
677 $this->assertTrue($courses[$c1->id]->has_course_overviewfiles());
678 $this->assertTrue($courses[$c2->id]->has_course_overviewfiles());
679 $this->assertFalse($courses[$c3->id]->has_course_overviewfiles());
680 $this->assertTrue($courses[$c4->id]->has_course_overviewfiles());
681 $this->assertTrue($courses[$c5->id]->has_course_overviewfiles()); // Still does not validate the filetypes.
683 $this->assertEquals(1, count($courses[$c1->id]->get_course_overviewfiles()));
684 $this->assertEquals(1, count($courses[$c2->id]->get_course_overviewfiles())); // Only 1 file was actually added.
685 $this->assertEquals(0, count($courses[$c3->id]->get_course_overviewfiles()));
686 $this->assertEquals(2, count($courses[$c4->id]->get_course_overviewfiles()));
687 $this->assertEquals(0, count($courses[$c5->id]->get_course_overviewfiles()));
689 // Multiple overview files of any type are allowed.
690 $CFG->courseoverviewfilesext = '*';
692 $this->assertTrue($courses[$c1->id]->has_course_overviewfiles());
693 $this->assertTrue($courses[$c2->id]->has_course_overviewfiles());
694 $this->assertFalse($courses[$c3->id]->has_course_overviewfiles());
695 $this->assertTrue($courses[$c4->id]->has_course_overviewfiles());
696 $this->assertTrue($courses[$c5->id]->has_course_overviewfiles());
698 $this->assertEquals(1, count($courses[$c1->id]->get_course_overviewfiles()));
699 $this->assertEquals(1, count($courses[$c2->id]->get_course_overviewfiles()));
700 $this->assertEquals(0, count($courses[$c3->id]->get_course_overviewfiles()));
701 $this->assertEquals(2, count($courses[$c4->id]->get_course_overviewfiles()));
702 $this->assertEquals(1, count($courses[$c5->id]->get_course_overviewfiles()));
706 * Creates a draft area for current user and fills it with fake files
708 * @param array $files array of files that need to be added to filearea, filename => filecontents
709 * @return int draftid for the filearea
711 protected function fill_draft_area(array $files) {
712 global $USER;
713 $usercontext = context_user::instance($USER->id);
714 $draftid = file_get_unused_draft_itemid();
715 foreach ($files as $filename => $filecontents) {
716 // Add actual file there.
717 $filerecord = array('component' => 'user', 'filearea' => 'draft',
718 'contextid' => $usercontext->id, 'itemid' => $draftid,
719 'filename' => $filename, 'filepath' => '/');
720 $fs = get_file_storage();
721 $fs->create_file_from_string($filerecord, $filecontents);
723 return $draftid;