category_test.php 55.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
18
 * Tests for class core_course_category
19
 *
20
 * @package    core_course
21
22
23
24
25
26
27
28
 * @category   phpunit
 * @copyright  2013 Marina Glancy
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

defined('MOODLE_INTERNAL') || die();

/**
29
 * Functional test for class core_course_category
30
 */
31
class core_course_category_testcase extends advanced_testcase {
32

33
    protected $roles;
34

35
    protected function setUp() {
36
        parent::setUp();
37
        $this->resetAfterTest();
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
        $user = $this->getDataGenerator()->create_user();
        $this->setUser($user);
    }

    protected function get_roleid($context = null) {
        global $USER;
        if ($context === null) {
            $context = context_system::instance();
        }
        if (is_object($context)) {
            $context = $context->id;
        }
        if (empty($this->roles)) {
            $this->roles = array();
        }
        if (empty($this->roles[$USER->id])) {
            $this->roles[$USER->id] = array();
        }
        if (empty($this->roles[$USER->id][$context])) {
            $this->roles[$USER->id][$context] = create_role('Role for '.$USER->id.' in '.$context, 'role'.$USER->id.'-'.$context, '-');
            role_assign($this->roles[$USER->id][$context], $USER->id, $context);
        }
        return $this->roles[$USER->id][$context];
    }

    protected function assign_capability($capability, $permission = CAP_ALLOW, $contextid = null) {
        if ($contextid === null) {
            $contextid = context_system::instance();
        }
        if (is_object($contextid)) {
            $contextid = $contextid->id;
        }
        assign_capability($capability, $permission, $this->get_roleid($contextid), $contextid, true);
        accesslib_clear_all_caches_for_unit_testing();
    }

    public function test_create_coursecat() {
75
        // Create the category.
76
77
78
79
80
        $data = new stdClass();
        $data->name = 'aaa';
        $data->description = 'aaa';
        $data->idnumber = '';

81
        $category1 = core_course_category::create($data);
82

83
84
85
86
        // Initially confirm that base data was inserted correctly.
        $this->assertSame($data->name, $category1->name);
        $this->assertSame($data->description, $category1->description);
        $this->assertSame($data->idnumber, $category1->idnumber);
87
88
89

        $this->assertGreaterThanOrEqual(1, $category1->sortorder);

90
        // Create two more categories and test the sortorder worked correctly.
91
        $data->name = 'ccc';
92
        $category2 = core_course_category::create($data);
93
94

        $data->name = 'bbb';
95
        $category3 = core_course_category::create($data);
96
97
98
99
100
101
102

        $this->assertGreaterThan($category1->sortorder, $category2->sortorder);
        $this->assertGreaterThan($category2->sortorder, $category3->sortorder);
    }

    public function test_name_idnumber_exceptions() {
        try {
103
104
            core_course_category::create(array('name' => ''));
            $this->fail('Missing category name exception expected in core_course_category::create');
105
        } catch (moodle_exception $e) {
106
            $this->assertInstanceOf('moodle_exception', $e);
107
        }
108
        $cat1 = core_course_category::create(array('name' => 'Cat1', 'idnumber' => '1'));
109
110
        try {
            $cat1->update(array('name' => ''));
111
            $this->fail('Missing category name exception expected in core_course_category::update');
112
        } catch (moodle_exception $e) {
113
            $this->assertInstanceOf('moodle_exception', $e);
114
115
        }
        try {
116
117
            core_course_category::create(array('name' => 'Cat2', 'idnumber' => '1'));
            $this->fail('Duplicate idnumber exception expected in core_course_category::create');
118
        } catch (moodle_exception $e) {
119
            $this->assertInstanceOf('moodle_exception', $e);
120
        }
121
        $cat2 = core_course_category::create(array('name' => 'Cat2', 'idnumber' => '2'));
122
123
        try {
            $cat2->update(array('idnumber' => '1'));
124
            $this->fail('Duplicate idnumber exception expected in core_course_category::update');
125
        } catch (moodle_exception $e) {
126
            $this->assertInstanceOf('moodle_exception', $e);
127
        }
128
        // Test that duplicates with an idnumber of 0 cannot be created.
129
        core_course_category::create(array('name' => 'Cat3', 'idnumber' => '0'));
130
        try {
131
132
            core_course_category::create(array('name' => 'Cat4', 'idnumber' => '0'));
            $this->fail('Duplicate idnumber "0" exception expected in core_course_category::create');
133
134
135
136
137
138
        } catch (moodle_exception $e) {
            $this->assertInstanceOf('moodle_exception', $e);
        }
        // Test an update cannot make a duplicate idnumber of 0.
        try {
            $cat2->update(array('idnumber' => '0'));
139
            $this->fail('Duplicate idnumber "0" exception expected in core_course_category::update');
140
141
142
        } catch (Exception $e) {
            $this->assertInstanceOf('moodle_exception', $e);
        }
143
144
145
146
147
148
    }

    public function test_visibility() {
        $this->assign_capability('moodle/category:viewhiddencategories');
        $this->assign_capability('moodle/category:manage');

149
        // Create category 1 initially hidden.
150
        $category1 = core_course_category::create(array('name' => 'Cat1', 'visible' => 0));
151
152
153
        $this->assertEquals(0, $category1->visible);
        $this->assertEquals(0, $category1->visibleold);

154
        // Create category 2 initially hidden as a child of hidden category 1.
155
        $category2 = core_course_category::create(array('name' => 'Cat2', 'visible' => 0, 'parent' => $category1->id));
156
157
158
        $this->assertEquals(0, $category2->visible);
        $this->assertEquals(0, $category2->visibleold);

159
        // Create category 3 initially visible as a child of hidden category 1.
160
        $category3 = core_course_category::create(array('name' => 'Cat3', 'visible' => 1, 'parent' => $category1->id));
161
162
163
        $this->assertEquals(0, $category3->visible);
        $this->assertEquals(1, $category3->visibleold);

164
        // Show category 1 and make sure that category 2 is hidden and category 3 is visible.
165
        $category1->show();
166
167
168
        $this->assertEquals(1, core_course_category::get($category1->id)->visible);
        $this->assertEquals(0, core_course_category::get($category2->id)->visible);
        $this->assertEquals(1, core_course_category::get($category3->id)->visible);
169

170
        // Create visible category 4.
171
        $category4 = core_course_category::create(array('name' => 'Cat4'));
172
173
174
        $this->assertEquals(1, $category4->visible);
        $this->assertEquals(1, $category4->visibleold);

175
        // Create visible category 5 as a child of visible category 4.
176
        $category5 = core_course_category::create(array('name' => 'Cat5', 'parent' => $category4->id));
177
178
179
        $this->assertEquals(1, $category5->visible);
        $this->assertEquals(1, $category5->visibleold);

180
        // Hide category 4 and make sure category 5 is hidden too.
181
182
183
        $category4->hide();
        $this->assertEquals(0, $category4->visible);
        $this->assertEquals(0, $category4->visibleold);
184
        $category5 = core_course_category::get($category5->id); // We have to re-read from DB.
185
186
187
        $this->assertEquals(0, $category5->visible);
        $this->assertEquals(1, $category5->visibleold);

188
        // Show category 4 and make sure category 5 is visible too.
189
190
191
        $category4->show();
        $this->assertEquals(1, $category4->visible);
        $this->assertEquals(1, $category4->visibleold);
192
        $category5 = core_course_category::get($category5->id); // We have to re-read from DB.
193
194
195
        $this->assertEquals(1, $category5->visible);
        $this->assertEquals(1, $category5->visibleold);

196
        // Move category 5 under hidden category 2 and make sure it became hidden.
197
198
199
200
        $category5->change_parent($category2->id);
        $this->assertEquals(0, $category5->visible);
        $this->assertEquals(1, $category5->visibleold);

201
        // Re-read object for category 5 from DB and check again.
202
        $category5 = core_course_category::get($category5->id);
203
204
205
        $this->assertEquals(0, $category5->visible);
        $this->assertEquals(1, $category5->visibleold);

206
207
        // Rricky one! Move hidden category 5 under visible category ("Top") and make sure it is still hidden-
        // WHY? Well, different people may expect different behaviour here. So better keep it hidden.
208
209
210
211
212
213
214
215
216
        $category5->change_parent(0);
        $this->assertEquals(0, $category5->visible);
        $this->assertEquals(1, $category5->visibleold);
    }

    public function test_hierarchy() {
        $this->assign_capability('moodle/category:viewhiddencategories');
        $this->assign_capability('moodle/category:manage');

217
218
219
220
        $category1 = core_course_category::create(array('name' => 'Cat1'));
        $category2 = core_course_category::create(array('name' => 'Cat2', 'parent' => $category1->id));
        $category3 = core_course_category::create(array('name' => 'Cat3', 'parent' => $category1->id));
        $category4 = core_course_category::create(array('name' => 'Cat4', 'parent' => $category2->id));
221

222
        // Check function get_children().
223
        $this->assertEquals(array($category2->id, $category3->id), array_keys($category1->get_children()));
224
        // Check function get_parents().
225
        $this->assertEquals(array($category1->id, $category2->id), $category4->get_parents());
226

227
        // Can not move category to itself or to it's children.
228
229
        $this->assertFalse($category1->can_change_parent($category2->id));
        $this->assertFalse($category2->can_change_parent($category2->id));
230
        // Can move category to grandparent.
231
232
233
234
235
236
        $this->assertTrue($category4->can_change_parent($category1->id));

        try {
            $category2->change_parent($category4->id);
            $this->fail('Exception expected - can not move category');
        } catch (moodle_exception $e) {
237
            $this->assertInstanceOf('moodle_exception', $e);
238
239
240
        }

        $category4->change_parent(0);
241
        $this->assertEquals(array(), $category4->get_parents());
242
243
244
245
246
        $this->assertEquals(array($category2->id, $category3->id), array_keys($category1->get_children()));
        $this->assertEquals(array(), array_keys($category2->get_children()));
    }

    public function test_update() {
247
        $category1 = core_course_category::create(array('name' => 'Cat1'));
248
        $timecreated = $category1->timemodified;
249
        $this->assertSame('Cat1', $category1->name);
250
        $this->assertTrue(empty($category1->description));
251
        $this->waitForSecond();
252
253
        $testdescription = 'This is cat 1 а также русский текст';
        $category1->update(array('description' => $testdescription));
254
        $this->assertSame($testdescription, $category1->description);
255
        $category1 = core_course_category::get($category1->id);
256
        $this->assertSame($testdescription, $category1->description);
257
        cache_helper::purge_by_event('changesincoursecat');
258
        $category1 = core_course_category::get($category1->id);
259
        $this->assertSame($testdescription, $category1->description);
260
261
262
263
264
265
266
267
268
269
270
271

        $this->assertGreaterThan($timecreated, $category1->timemodified);
    }

    public function test_delete() {
        global $DB;

        $this->assign_capability('moodle/category:manage');
        $this->assign_capability('moodle/course:create');

        $initialcatid = $DB->get_field_sql('SELECT max(id) from {course_categories}');

272
273
274
275
        $category1 = core_course_category::create(array('name' => 'Cat1'));
        $category2 = core_course_category::create(array('name' => 'Cat2', 'parent' => $category1->id));
        $category3 = core_course_category::create(array('name' => 'Cat3'));
        $category4 = core_course_category::create(array('name' => 'Cat4', 'parent' => $category2->id));
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290

        $course1 = $this->getDataGenerator()->create_course(array('category' => $category2->id));
        $course2 = $this->getDataGenerator()->create_course(array('category' => $category4->id));
        $course3 = $this->getDataGenerator()->create_course(array('category' => $category4->id));
        $course4 = $this->getDataGenerator()->create_course(array('category' => $category1->id));

        // Now we have
        // $category1
        //   $category2
        //      $category4
        //        $course2
        //        $course3
        //      $course1
        //   $course4
        // $category3
291
        // structure.
292

293
        // Login as another user to test course:delete capability (user who created course can delete it within 24h even without cap).
294
295
        $this->setUser($this->getDataGenerator()->create_user());

296
297
298
        // Delete category 2 and move content to category 3.
        $this->assertFalse($category2->can_move_content_to($category3->id)); // No luck!
        // Add necessary capabilities.
299
300
        $this->assign_capability('moodle/course:create', CAP_ALLOW, context_coursecat::instance($category3->id));
        $this->assign_capability('moodle/category:manage');
301
        $this->assertTrue($category2->can_move_content_to($category3->id)); // Hurray!
302
303
304
305
306
307
308
309
310
311
        $category2->delete_move($category3->id);

        // Make sure we have:
        // $category1
        //   $course4
        // $category3
        //    $category4
        //      $course2
        //      $course3
        //    $course1
312
        // structure.
313

314
        $this->assertNull(core_course_category::get($category2->id, IGNORE_MISSING, true));
315
316
317
318
319
320
        $this->assertEquals(array(), $category1->get_children());
        $this->assertEquals(array($category4->id), array_keys($category3->get_children()));
        $this->assertEquals($category4->id, $DB->get_field('course', 'category', array('id' => $course2->id)));
        $this->assertEquals($category4->id, $DB->get_field('course', 'category', array('id' => $course3->id)));
        $this->assertEquals($category3->id, $DB->get_field('course', 'category', array('id' => $course1->id)));

321
322
323
        // Delete category 3 completely.
        $this->assertFalse($category3->can_delete_full()); // No luck!
        // Add necessary capabilities.
324
        $this->assign_capability('moodle/course:delete', CAP_ALLOW, context_coursecat::instance($category3->id));
325
        $this->assertTrue($category3->can_delete_full()); // Hurray!
326
327
328
329
330
        $category3->delete_full();

        // Make sure we have:
        // $category1
        //   $course4
331
        // structure.
332

333
        // Note that we also have default 'Miscellaneous' category and default 'site' course.
334
335
336
337
338
339
        $this->assertEquals(1, $DB->get_field_sql('SELECT count(*) FROM {course_categories} WHERE id > ?', array($initialcatid)));
        $this->assertEquals($category1->id, $DB->get_field_sql('SELECT max(id) FROM {course_categories}'));
        $this->assertEquals(1, $DB->get_field_sql('SELECT count(*) FROM {course} WHERE id <> ?', array(SITEID)));
        $this->assertEquals(array('id' => $course4->id, 'category' => $category1->id),
                (array)$DB->get_record_sql('SELECT id, category from {course} where id <> ?', array(SITEID)));
    }
340
341

    public function test_get_children() {
342
343
344
345
346
347
348
349
        $category1 = core_course_category::create(array('name' => 'Cat1'));
        $category2 = core_course_category::create(array('name' => 'Cat2', 'parent' => $category1->id));
        $category3 = core_course_category::create(array('name' => 'Cat3', 'parent' => $category1->id, 'visible' => 0));
        $category4 = core_course_category::create(array('name' => 'Cat4', 'idnumber' => '12', 'parent' => $category1->id));
        $category5 = core_course_category::create(array('name' => 'Cat5', 'idnumber' => '11',
            'parent' => $category1->id, 'visible' => 0));
        $category6 = core_course_category::create(array('name' => 'Cat6', 'idnumber' => '10', 'parent' => $category1->id));
        $category7 = core_course_category::create(array('name' => 'Cat0', 'parent' => $category1->id));
350
351

        $children = $category1->get_children();
352
353
        // User does not have the capability to view hidden categories, so the list should be
        // 2, 4, 6, 7.
354
355
356
357
358
359
360
361
362
363
364
365
366
367
        $this->assertEquals(array($category2->id, $category4->id, $category6->id, $category7->id), array_keys($children));
        $this->assertEquals(4, $category1->get_children_count());

        $children = $category1->get_children(array('offset' => 2));
        $this->assertEquals(array($category6->id, $category7->id), array_keys($children));
        $this->assertEquals(4, $category1->get_children_count());

        $children = $category1->get_children(array('limit' => 2));
        $this->assertEquals(array($category2->id, $category4->id), array_keys($children));

        $children = $category1->get_children(array('offset' => 1, 'limit' => 2));
        $this->assertEquals(array($category4->id, $category6->id), array_keys($children));

        $children = $category1->get_children(array('sort' => array('name' => 1)));
368
        // Must be 7, 2, 4, 6.
369
370
371
        $this->assertEquals(array($category7->id, $category2->id, $category4->id, $category6->id), array_keys($children));

        $children = $category1->get_children(array('sort' => array('idnumber' => 1, 'name' => -1)));
372
        // Must be 2, 7, 6, 4.
373
374
        $this->assertEquals(array($category2->id, $category7->id, $category6->id, $category4->id), array_keys($children));

375
        // Check that everything is all right after purging the caches.
376
377
378
379
380
        cache_helper::purge_by_event('changesincoursecat');
        $children = $category1->get_children();
        $this->assertEquals(array($category2->id, $category4->id, $category6->id, $category7->id), array_keys($children));
        $this->assertEquals(4, $category1->get_children_count());
    }
381

382
383
384
385
    /**
     * Test the get_all_children_ids function.
     */
    public function test_get_all_children_ids() {
386
387
388
389
390
391
392
393
        $category1 = core_course_category::create(array('name' => 'Cat1'));
        $category2 = core_course_category::create(array('name' => 'Cat2'));
        $category11 = core_course_category::create(array('name' => 'Cat11', 'parent' => $category1->id));
        $category12 = core_course_category::create(array('name' => 'Cat12', 'parent' => $category1->id));
        $category13 = core_course_category::create(array('name' => 'Cat13', 'parent' => $category1->id));
        $category111 = core_course_category::create(array('name' => 'Cat111', 'parent' => $category11->id));
        $category112 = core_course_category::create(array('name' => 'Cat112', 'parent' => $category11->id));
        $category1121 = core_course_category::create(array('name' => 'Cat1121', 'parent' => $category112->id));
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412

        $this->assertCount(0, $category2->get_all_children_ids());
        $this->assertCount(6, $category1->get_all_children_ids());

        $cmpchildrencat1 = array($category11->id, $category12->id, $category13->id, $category111->id, $category112->id,
                $category1121->id);
        $childrencat1 = $category1->get_all_children_ids();
        // Order of values does not matter. Compare sorted arrays.
        sort($cmpchildrencat1);
        sort($childrencat1);
        $this->assertEquals($cmpchildrencat1, $childrencat1);

        $this->assertCount(3, $category11->get_all_children_ids());
        $this->assertCount(0, $category111->get_all_children_ids());
        $this->assertCount(1, $category112->get_all_children_ids());

        $this->assertEquals(array($category1121->id), $category112->get_all_children_ids());
    }

413
414
415
416
    /**
     * Test the countall function
     */
    public function test_count_all() {
417
418
419
        global $DB;
        // Dont assume there is just one. An add-on might create a category as part of the install.
        $numcategories = $DB->count_records('course_categories');
420
421
422
423
        $this->assertEquals($numcategories, core_course_category::count_all());
        $category1 = core_course_category::create(array('name' => 'Cat1'));
        $category2 = core_course_category::create(array('name' => 'Cat2', 'parent' => $category1->id));
        $category3 = core_course_category::create(array('name' => 'Cat3', 'parent' => $category2->id, 'visible' => 0));
424
        // Now we've got three more.
425
        $this->assertEquals($numcategories + 3, core_course_category::count_all());
426
427
        cache_helper::purge_by_event('changesincoursecat');
        // We should still have 4.
428
        $this->assertEquals($numcategories + 3, core_course_category::count_all());
429
430
431
432
433
434
435
436
437
438
439
440
441
    }

    /**
     * Test a categories ability to resort courses.
     */
    public function test_resort_courses() {
        $this->resetAfterTest(true);
        $generator = $this->getDataGenerator();
        $category = $generator->create_category();
        $course1 = $generator->create_course(array(
            'category' => $category->id,
            'idnumber' => '006-01',
            'shortname' => 'Biome Study',
442
            'fullname' => '<span lang="ar" class="multilang">'.'دراسة منطقة إحيائية'.'</span><span lang="en" class="multilang">Biome Study</span>',
443
            'timecreated' => '1000000001'
444
445
446
447
448
        ));
        $course2 = $generator->create_course(array(
            'category' => $category->id,
            'idnumber' => '007-02',
            'shortname' => 'Chemistry Revision',
449
            'fullname' => 'Chemistry Revision',
450
            'timecreated' => '1000000002'
451
452
453
454
455
        ));
        $course3 = $generator->create_course(array(
            'category' => $category->id,
            'idnumber' => '007-03',
            'shortname' => 'Swiss Rolls and Sunflowers',
456
            'fullname' => 'Aarkvarks guide to Swiss Rolls and Sunflowers',
457
            'timecreated' => '1000000003'
458
459
460
461
462
        ));
        $course4 = $generator->create_course(array(
            'category' => $category->id,
            'idnumber' => '006-04',
            'shortname' => 'Scratch',
463
            'fullname' => '<a href="test.php">Basic Scratch</a>',
464
            'timecreated' => '1000000004'
465
466
467
468
469
470
        ));
        $c1 = (int)$course1->id;
        $c2 = (int)$course2->id;
        $c3 = (int)$course3->id;
        $c4 = (int)$course4->id;

471
        $coursecat = core_course_category::get($category->id);
472
473
474
475
476
477
        $this->assertTrue($coursecat->resort_courses('idnumber'));
        $this->assertSame(array($c1, $c4, $c2, $c3), array_keys($coursecat->get_courses()));

        $this->assertTrue($coursecat->resort_courses('shortname'));
        $this->assertSame(array($c1, $c2, $c4, $c3), array_keys($coursecat->get_courses()));

478
479
        $this->assertTrue($coursecat->resort_courses('timecreated'));
        $this->assertSame(array($c1, $c2, $c3, $c4), array_keys($coursecat->get_courses()));
480
481
482

        try {
            // Enable the multilang filter and set it to apply to headings and content.
483
            filter_manager::reset_caches();
484
485
486
487
488
489
490
491
492
493
            filter_set_global_state('multilang', TEXTFILTER_ON);
            filter_set_applies_to_strings('multilang', true);
            $expected = array($c3, $c4, $c1, $c2);
        } catch (coding_exception $ex) {
            $expected = array($c3, $c4, $c2, $c1);
        }
        $this->assertTrue($coursecat->resort_courses('fullname'));
        $this->assertSame($expected, array_keys($coursecat->get_courses()));
    }

494
    public function test_get_search_courses() {
495
496
        $cat1 = core_course_category::create(array('name' => 'Cat1'));
        $cat2 = core_course_category::create(array('name' => 'Cat2', 'parent' => $cat1->id));
497
498
499
500
501
502
503
504
505
506
        $c1 = $this->getDataGenerator()->create_course(array('category' => $cat1->id, 'fullname' => 'Test 3', 'summary' => ' ', 'idnumber' => 'ID3'));
        $c2 = $this->getDataGenerator()->create_course(array('category' => $cat1->id, 'fullname' => 'Test 1', 'summary' => ' ', 'visible' => 0));
        $c3 = $this->getDataGenerator()->create_course(array('category' => $cat1->id, 'fullname' => 'Математика', 'summary' => ' Test '));
        $c4 = $this->getDataGenerator()->create_course(array('category' => $cat1->id, 'fullname' => 'Test 4', 'summary' => ' ', 'idnumber' => 'ID4'));

        $c5 = $this->getDataGenerator()->create_course(array('category' => $cat2->id, 'fullname' => 'Test 5', 'summary' => ' '));
        $c6 = $this->getDataGenerator()->create_course(array('category' => $cat2->id, 'fullname' => 'Дискретная Математика', 'summary' => ' '));
        $c7 = $this->getDataGenerator()->create_course(array('category' => $cat2->id, 'fullname' => 'Test 7', 'summary' => ' ', 'visible' => 0));
        $c8 = $this->getDataGenerator()->create_course(array('category' => $cat2->id, 'fullname' => 'Test 8', 'summary' => ' '));

507
        // Get courses in category 1 (returned visible only because user is not enrolled).
508
        $res = $cat1->get_courses(array('sortorder' => 1));
509
        $this->assertEquals(array($c4->id, $c3->id, $c1->id), array_keys($res)); // Courses are added in reverse order.
510
511
        $this->assertEquals(3, $cat1->get_courses_count());

512
        // Get courses in category 1 recursively (returned visible only because user is not enrolled).
513
514
515
516
        $res = $cat1->get_courses(array('recursive' => 1));
        $this->assertEquals(array($c4->id, $c3->id, $c1->id, $c8->id, $c6->id, $c5->id), array_keys($res));
        $this->assertEquals(6, $cat1->get_courses_count(array('recursive' => 1)));

517
        // Get courses sorted by fullname.
518
519
520
521
        $res = $cat1->get_courses(array('sort' => array('fullname' => 1)));
        $this->assertEquals(array($c1->id, $c4->id, $c3->id), array_keys($res));
        $this->assertEquals(3, $cat1->get_courses_count(array('sort' => array('fullname' => 1))));

522
        // Get courses sorted by fullname recursively.
523
524
525
526
        $res = $cat1->get_courses(array('recursive' => 1, 'sort' => array('fullname' => 1)));
        $this->assertEquals(array($c1->id, $c4->id, $c5->id, $c8->id, $c6->id, $c3->id), array_keys($res));
        $this->assertEquals(6, $cat1->get_courses_count(array('recursive' => 1, 'sort' => array('fullname' => 1))));

527
        // Get courses sorted by fullname recursively, use offset and limit.
528
529
        $res = $cat1->get_courses(array('recursive' => 1, 'offset' => 1, 'limit' => 2, 'sort' => array('fullname' => -1)));
        $this->assertEquals(array($c6->id, $c8->id), array_keys($res));
530
        // Offset and limit do not affect get_courses_count().
531
532
        $this->assertEquals(6, $cat1->get_courses_count(array('recursive' => 1, 'offset' => 1, 'limit' => 2, 'sort' => array('fullname' => 1))));

533
        // Calling get_courses_count without prior call to get_courses().
534
535
        $this->assertEquals(3, $cat2->get_courses_count(array('recursive' => 1, 'sort' => array('idnumber' => 1))));

536
        // Search courses.
537

538
        // Search by text.
539
        $res = core_course_category::search_courses(array('search' => 'Test'));
540
        $this->assertEquals(array($c4->id, $c3->id, $c1->id, $c8->id, $c5->id), array_keys($res));
541
        $this->assertEquals(5, core_course_category::search_courses_count(array('search' => 'Test')));
542

543
        // Search by text with specified offset and limit.
544
        $options = array('sort' => array('fullname' => 1), 'offset' => 1, 'limit' => 2);
545
        $res = core_course_category::search_courses(array('search' => 'Test'), $options);
546
        $this->assertEquals(array($c4->id, $c5->id), array_keys($res));
547
        $this->assertEquals(5, core_course_category::search_courses_count(array('search' => 'Test'), $options));
548
549

        // IMPORTANT: the tests below may fail on some databases
550
        // case-insensitive search.
551
        $res = core_course_category::search_courses(array('search' => 'test'));
552
        $this->assertEquals(array($c4->id, $c3->id, $c1->id, $c8->id, $c5->id), array_keys($res));
553
        $this->assertEquals(5, core_course_category::search_courses_count(array('search' => 'test')));
554

555
        // Non-latin language search.
556
        $res = core_course_category::search_courses(array('search' => 'Математика'));
557
        $this->assertEquals(array($c3->id, $c6->id), array_keys($res));
558
        $this->assertEquals(2, core_course_category::search_courses_count(array('search' => 'Математика'), array()));
559
560
561
562
563
564
565

        $this->setUser($this->getDataGenerator()->create_user());

        // Add necessary capabilities.
        $this->assign_capability('moodle/course:create', CAP_ALLOW, context_coursecat::instance($cat2->id));
        // Do another search with restricted capabilities.
        $reqcaps = array('moodle/course:create');
566
        $res = core_course_category::search_courses(array('search' => 'test'), array(), $reqcaps);
567
        $this->assertEquals(array($c8->id, $c5->id), array_keys($res));
568
        $this->assertEquals(2, core_course_category::search_courses_count(array('search' => 'test'), array(), $reqcaps));
569
    }
570
571
572

    public function test_course_contacts() {
        global $DB, $CFG;
573
574
575

        set_config('coursecontactduplicates', false);

576
577
578
579
580
581
582
        $teacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'));
        $managerrole = $DB->get_record('role', array('shortname'=>'manager'));
        $studentrole = $DB->get_record('role', array('shortname'=>'student'));
        $oldcoursecontact = $CFG->coursecontact;

        $CFG->coursecontact = $managerrole->id. ','. $teacherrole->id;

583
        /*
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
         * User is listed in course contacts for the course if he has one of the
         * "course contact" roles ($CFG->coursecontact) AND is enrolled in the course.
         * If the user has several roles only the highest is displayed.
         */

        // Test case:
        //
        // == Cat1 (user2 has teacher role)
        //   == Cat2
        //     -- course21 (user2 is enrolled as manager) | [Expected] Manager: F2 L2
        //     -- course22 (user2 is enrolled as student) | [Expected] Teacher: F2 L2
        //     == Cat4 (user2 has manager role)
        //       -- course41 (user4 is enrolled as teacher, user5 is enrolled as manager) | [Expected] Manager: F5 L5, Teacher: F4 L4
        //       -- course42 (user2 is enrolled as teacher) | [Expected] Manager: F2 L2
        //   == Cat3 (user3 has manager role)
        //     -- course31 (user3 is enrolled as student) | [Expected] Manager: F3 L3
        //     -- course32                                | [Expected]
        //   -- course11 (user1 is enrolled as teacher)   | [Expected] Teacher: F1 L1
        //   -- course12 (user1 has teacher role)         | [Expected]
        //                also user4 is enrolled as teacher but enrolment is not active
        $category = $course = $enrol = $user = array();
605
606
607
608
        $category[1] = core_course_category::create(array('name' => 'Cat1'))->id;
        $category[2] = core_course_category::create(array('name' => 'Cat2', 'parent' => $category[1]))->id;
        $category[3] = core_course_category::create(array('name' => 'Cat3', 'parent' => $category[1]))->id;
        $category[4] = core_course_category::create(array('name' => 'Cat4', 'parent' => $category[2]))->id;
609
610
        foreach (array(1, 2, 3, 4) as $catid) {
            foreach (array(1, 2) as $courseid) {
611
612
613
614
615
                $course[$catid][$courseid] = $this->getDataGenerator()->create_course(array('idnumber' => 'id'.$catid.$courseid,
                    'category' => $category[$catid]))->id;
                $enrol[$catid][$courseid] = $DB->get_record('enrol', array('courseid'=>$course[$catid][$courseid], 'enrol'=>'manual'), '*', MUST_EXIST);
            }
        }
616
        foreach (array(1, 2, 3, 4, 5) as $userid) {
617
618
619
620
621
            $user[$userid] = $this->getDataGenerator()->create_user(array('firstname' => 'F'.$userid, 'lastname' => 'L'.$userid))->id;
        }

        $manual = enrol_get_plugin('manual');

622
        // Nobody is enrolled now and course contacts are empty.
623
624
        $allcourses = core_course_category::get(0)->get_courses(
            array('recursive' => true, 'coursecontacts' => true, 'sort' => array('idnumber' => 1)));
625
626
627
628
        foreach ($allcourses as $onecourse) {
            $this->assertEmpty($onecourse->get_course_contacts());
        }

629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
        // Cat1 (user2 has teacher role)
        role_assign($teacherrole->id, $user[2], context_coursecat::instance($category[1]));
        // course21 (user2 is enrolled as manager)
        $manual->enrol_user($enrol[2][1], $user[2], $managerrole->id);
        // course22 (user2 is enrolled as student)
        $manual->enrol_user($enrol[2][2], $user[2], $studentrole->id);
        // Cat4 (user2 has manager role)
        role_assign($managerrole->id, $user[2], context_coursecat::instance($category[4]));
        // course41 (user4 is enrolled as teacher, user5 is enrolled as manager)
        $manual->enrol_user($enrol[4][1], $user[4], $teacherrole->id);
        $manual->enrol_user($enrol[4][1], $user[5], $managerrole->id);
        // course42 (user2 is enrolled as teacher)
        $manual->enrol_user($enrol[4][2], $user[2], $teacherrole->id);
        // Cat3 (user3 has manager role)
        role_assign($managerrole->id, $user[3], context_coursecat::instance($category[3]));
        // course31 (user3 is enrolled as student)
        $manual->enrol_user($enrol[3][1], $user[3], $studentrole->id);
        // course11 (user1 is enrolled as teacher)
        $manual->enrol_user($enrol[1][1], $user[1], $teacherrole->id);
        // -- course12 (user1 has teacher role)
        //                also user4 is enrolled as teacher but enrolment is not active
        role_assign($teacherrole->id, $user[1], context_course::instance($course[1][2]));
        $manual->enrol_user($enrol[1][2], $user[4], $teacherrole->id, 0, 0, ENROL_USER_SUSPENDED);

653
654
        $allcourses = core_course_category::get(0)->get_courses(
            array('recursive' => true, 'coursecontacts' => true, 'sort' => array('idnumber' => 1)));
655
        // Simplify the list of contacts for each course (similar as renderer would do).
656
        $contacts = array();
657
658
        foreach (array(1, 2, 3, 4) as $catid) {
            foreach (array(1, 2) as $courseid) {
659
660
661
662
663
664
665
666
667
668
                $tmp = array();
                foreach ($allcourses[$course[$catid][$courseid]]->get_course_contacts() as $contact) {
                    $tmp[] = $contact['rolename']. ': '. $contact['username'];
                }
                $contacts[$catid][$courseid] = join(', ', $tmp);
            }
        }

        // Assert:
        //     -- course21 (user2 is enrolled as manager) | Manager: F2 L2
669
        $this->assertSame('Manager: F2 L2', $contacts[2][1]);
670
        //     -- course22 (user2 is enrolled as student) | Teacher: F2 L2
671
        $this->assertSame('Teacher: F2 L2', $contacts[2][2]);
672
        //       -- course41 (user4 is enrolled as teacher, user5 is enrolled as manager) | Manager: F5 L5, Teacher: F4 L4
673
        $this->assertSame('Manager: F5 L5, Teacher: F4 L4', $contacts[4][1]);
674
        //       -- course42 (user2 is enrolled as teacher) | [Expected] Manager: F2 L2
675
        $this->assertSame('Manager: F2 L2', $contacts[4][2]);
676
        //     -- course31 (user3 is enrolled as student) | Manager: F3 L3
677
        $this->assertSame('Manager: F3 L3', $contacts[3][1]);
678
        //     -- course32                                |
679
        $this->assertSame('', $contacts[3][2]);
680
        //   -- course11 (user1 is enrolled as teacher)   | Teacher: F1 L1
681
        $this->assertSame('Teacher: F1 L1', $contacts[1][1]);
682
        //   -- course12 (user1 has teacher role)         |
683
        $this->assertSame('', $contacts[1][2]);
684

685
686
        // Suspend user 4 and make sure he is no longer in contacts of course 1 in category 4.
        $manual->enrol_user($enrol[4][1], $user[4], $teacherrole->id, 0, 0, ENROL_USER_SUSPENDED);
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
        $allcourses = core_course_category::get(0)->get_courses(array(
                'recursive' => true,
                'coursecontacts' => true,
                'sort' => array('idnumber' => 1))
        );
        $contacts = $allcourses[$course[4][1]]->get_course_contacts();
        $this->assertCount(1, $contacts);
        $contact = reset($contacts);
        $this->assertEquals('F5 L5', $contact['username']);

        $CFG->coursecontact = $oldcoursecontact;
    }

    public function test_course_contacts_with_duplicates() {
        global $DB, $CFG;

        set_config('coursecontactduplicates', true);

        $displayall = get_config('core', 'coursecontactduplicates');
        $this->assertEquals(true, $displayall);

        $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
        $managerrole = $DB->get_record('role', array('shortname' => 'manager'));
        $studentrole = $DB->get_record('role', array('shortname' => 'student'));
        $oldcoursecontact = $CFG->coursecontact;

        $CFG->coursecontact = $managerrole->id. ','. $teacherrole->id;

        /*
        * User is listed in course contacts for the course if he has one of the
        * "course contact" roles ($CFG->coursecontact) AND is enrolled in the course.
        * If the user has several roles all roles are displayed, but each role only once per user.
        */

        /*
        * Test case:
        *
        * == Cat1 (user2 has teacher role)
        *    == Cat2
        *      -- course21 (user2 is enrolled as manager) | [Expected] Manager: F2 L2
        *      -- course22 (user2 is enrolled as student) | [Expected] Teacher: F2 L2
        *      == Cat4 (user2 has manager role)
        *        -- course41 (user4 is enrolled as teacher, user5 is enrolled as manager)
        *                                                 | [Expected] Manager: F5 L5, Teacher: F4 L4
        *        -- course42 (user2 is enrolled as teacher) | [Expected] Manager: F2 L2
        *    == Cat3 (user3 has manager role)
        *      -- course31 (user3 is enrolled as student) | [Expected] Manager: F3 L3
        *      -- course32                                | [Expected]
        *    -- course11 (user1 is enrolled as teacher)   | [Expected] Teacher: F1 L1
        *    -- course12 (user1 has teacher role)         | [Expected]
        *                 also user4 is enrolled as teacher but enrolment is not active
        */
        $category = $course = $enrol = $user = array();
        $category[1] = core_course_category::create(array('name' => 'Cat1'))->id;
        $category[2] = core_course_category::create(array('name' => 'Cat2', 'parent' => $category[1]))->id;
        $category[3] = core_course_category::create(array('name' => 'Cat3', 'parent' => $category[1]))->id;
        $category[4] = core_course_category::create(array('name' => 'Cat4', 'parent' => $category[2]))->id;
        foreach (array(1, 2, 3, 4) as $catid) {
            foreach (array(1, 2) as $courseid) {
                $course[$catid][$courseid] = $this->getDataGenerator()->create_course(array(
                        'idnumber' => 'id'.$catid.$courseid,
                        'category' => $category[$catid])
                )->id;
                $enrol[$catid][$courseid] = $DB->get_record(
                        'enrol',
                        array('courseid' => $course[$catid][$courseid], 'enrol' => 'manual'),
                        '*',
                        MUST_EXIST
                );
            }
        }
        foreach (array(1, 2, 3, 4, 5) as $userid) {
            $user[$userid] = $this->getDataGenerator()->create_user(array(
                            'firstname' => 'F'.$userid,
                            'lastname' => 'L'.$userid)
            )->id;
        }

        $manual = enrol_get_plugin('manual');

        // Nobody is enrolled now and course contacts are empty.
        $allcourses = core_course_category::get(0)->get_courses(array(
                'recursive' => true,
                'coursecontacts' => true,
                'sort' => array('idnumber' => 1))
        );
        foreach ($allcourses as $onecourse) {
            $this->assertEmpty($onecourse->get_course_contacts());
        }

        // Cat1: user2 has teacher role.
        role_assign($teacherrole->id, $user[2], context_coursecat::instance($category[1]));
        // Course21: user2 is enrolled as manager.
        $manual->enrol_user($enrol[2][1], $user[2], $managerrole->id);
        // Course22: user2 is enrolled as student.
        $manual->enrol_user($enrol[2][2], $user[2], $studentrole->id);
        // Cat4: user2 has manager role.
        role_assign($managerrole->id, $user[2], context_coursecat::instance($category[4]));
        // Course41: user4 is enrolled as teacher, user5 is enrolled as manager.
        $manual->enrol_user($enrol[4][1], $user[4], $teacherrole->id);
        $manual->enrol_user($enrol[4][1], $user[5], $managerrole->id);
        // Course42: user2 is enrolled as teacher.
        $manual->enrol_user($enrol[4][2], $user[2], $teacherrole->id);
        // Cat3: user3 has manager role.
        role_assign($managerrole->id, $user[3], context_coursecat::instance($category[3]));
        // Course31: user3 is enrolled as student.
        $manual->enrol_user($enrol[3][1], $user[3], $studentrole->id);
        // Course11: user1 is enrolled as teacher and user4 is enrolled as teacher and has manager role.
        $manual->enrol_user($enrol[1][1], $user[1], $teacherrole->id);
        $manual->enrol_user($enrol[1][1], $user[4], $teacherrole->id);
        role_assign($managerrole->id, $user[4], context_course::instance($course[1][1]));
        // Course12: user1 has teacher role, but is not enrolled, as well as user4 is enrolled as teacher, but user4's enrolment is
        // not active.
        role_assign($teacherrole->id, $user[1], context_course::instance($course[1][2]));
        $manual->enrol_user($enrol[1][2], $user[4], $teacherrole->id, 0, 0, ENROL_USER_SUSPENDED);

803
        $allcourses = core_course_category::get(0)->get_courses(
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
                array('recursive' => true, 'coursecontacts' => true, 'sort' => array('idnumber' => 1)));
        // Simplify the list of contacts for each course (similar as renderer would do).
        $contacts = array();
        foreach (array(1, 2, 3, 4) as $catid) {
            foreach (array(1, 2) as $courseid) {
                $tmp = array();
                foreach ($allcourses[$course[$catid][$courseid]]->get_course_contacts() as $contact) {
                    $rolenames = array_map(function ($role) {
                        return $role->displayname;
                    }, $contact['roles']);
                    $tmp[] = implode(", ", $rolenames). ': '.
                            $contact['username'];
                }
                $contacts[$catid][$courseid] = join(', ', $tmp);
            }
        }

        // Assert:
        // Course21: user2 is enrolled as manager. [Expected] Manager: F2 L2, Teacher: F2 L2.
        $this->assertSame('Manager, Teacher: F2 L2', $contacts[2][1]);
        // Course22: user2 is enrolled as student. [Expected] Teacher: F2 L2.
        $this->assertSame('Teacher: F2 L2', $contacts[2][2]);
        // Course41: user4 is enrolled as teacher, user5 is enrolled as manager. [Expected] Manager: F5 L5, Teacher: F4 L4.
        $this->assertSame('Manager: F5 L5, Teacher: F4 L4', $contacts[4][1]);
        // Course42: user2 is enrolled as teacher. [Expected] Manager: F2 L2, Teacher: F2 L2.
        $this->assertSame('Manager, Teacher: F2 L2', $contacts[4][2]);
        // Course31: user3 is enrolled as student. [Expected] Manager: F3 L3.
        $this->assertSame('Manager: F3 L3', $contacts[3][1]);
        // Course32: nobody is enrolled. [Expected] (nothing).
        $this->assertSame('', $contacts[3][2]);
        // Course11: user1 is enrolled as teacher and user4 is enrolled as teacher and has manager role. [Expected] Manager: F4 L4,
        // Teacher: F1 L1, Teacher: F4 L4.
        $this->assertSame('Manager, Teacher: F4 L4, Teacher: F1 L1', $contacts[1][1]);
        // Course12: user1 has teacher role, but is not enrolled, as well as user4 is enrolled as teacher, but user4's enrolment is
        // not active. [Expected] (nothing).
        $this->assertSame('', $contacts[1][2]);

        // Suspend user 4 and make sure he is no longer in contacts of course 1 in category 4.
        $manual->enrol_user($enrol[4][1], $user[4], $teacherrole->id, 0, 0, ENROL_USER_SUSPENDED);
        $allcourses = core_course_category::get(0)->get_courses(array(
                'recursive' => true,
                'coursecontacts' => true,
                'sort' => array('idnumber' => 1)
        ));
848
849
850
851
852
        $contacts = $allcourses[$course[4][1]]->get_course_contacts();
        $this->assertCount(1, $contacts);
        $contact = reset($contacts);
        $this->assertEquals('F5 L5', $contact['username']);

853
854
        $CFG->coursecontact = $oldcoursecontact;
    }
855
856
857
858

    public function test_overview_files() {
        global $CFG;
        $this->setAdminUser();
859
        $cat1 = core_course_category::create(array('name' => 'Cat1'));
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946

        // Create course c1 with one image file.
        $dratid1 = $this->fill_draft_area(array('filename.jpg' => 'Test file contents1'));
        $c1 = $this->getDataGenerator()->create_course(array('category' => $cat1->id,
            'fullname' => 'Test 1', 'overviewfiles_filemanager' => $dratid1));
        // Create course c2 with two image files (only one file will be added because of settings).
        $dratid2 = $this->fill_draft_area(array('filename21.jpg' => 'Test file contents21', 'filename22.jpg' => 'Test file contents22'));
        $c2 = $this->getDataGenerator()->create_course(array('category' => $cat1->id,
            'fullname' => 'Test 2', 'overviewfiles_filemanager' => $dratid2));
        // Create course c3 without files.
        $c3 = $this->getDataGenerator()->create_course(array('category' => $cat1->id, 'fullname' => 'Test 3'));

        // Change the settings to allow multiple files of any types.
        $CFG->courseoverviewfileslimit = 3;
        $CFG->courseoverviewfilesext = '*';
        // Create course c5 with two image files.
        $dratid4 = $this->fill_draft_area(array('filename41.jpg' => 'Test file contents41', 'filename42.jpg' => 'Test file contents42'));
        $c4 = $this->getDataGenerator()->create_course(array('category' => $cat1->id,
            'fullname' => 'Test 4', 'overviewfiles_filemanager' => $dratid4));
        // Create course c6 with non-image file.
        $dratid5 = $this->fill_draft_area(array('filename51.zip' => 'Test file contents51'));
        $c5 = $this->getDataGenerator()->create_course(array('category' => $cat1->id,
            'fullname' => 'Test 5', 'overviewfiles_filemanager' => $dratid5));

        // Reset default settings.
        $CFG->courseoverviewfileslimit = 1;
        $CFG->courseoverviewfilesext = '.jpg,.gif,.png';

        $courses = $cat1->get_courses();
        $this->assertTrue($courses[$c1->id]->has_course_overviewfiles());
        $this->assertTrue($courses[$c2->id]->has_course_overviewfiles());
        $this->assertFalse($courses[$c3->id]->has_course_overviewfiles());
        $this->assertTrue($courses[$c4->id]->has_course_overviewfiles());
        $this->assertTrue($courses[$c5->id]->has_course_overviewfiles()); // Does not validate the filetypes.

        $this->assertEquals(1, count($courses[$c1->id]->get_course_overviewfiles()));
        $this->assertEquals(1, count($courses[$c2->id]->get_course_overviewfiles()));
        $this->assertEquals(0, count($courses[$c3->id]->get_course_overviewfiles()));
        $this->assertEquals(1, count($courses[$c4->id]->get_course_overviewfiles()));
        $this->assertEquals(0, count($courses[$c5->id]->get_course_overviewfiles())); // Validate the filetypes.

        // Overview files are not allowed, all functions return empty values.
        $CFG->courseoverviewfileslimit = 0;

        $this->assertFalse($courses[$c1->id]->has_course_overviewfiles());
        $this->assertFalse($courses[$c2->id]->has_course_overviewfiles());
        $this->assertFalse($courses[$c3->id]->has_course_overviewfiles());
        $this->assertFalse($courses[$c4->id]->has_course_overviewfiles());
        $this->assertFalse($courses[$c5->id]->has_course_overviewfiles());

        $this->assertEquals(0, count($courses[$c1->id]->get_course_overviewfiles()));
        $this->assertEquals(0, count($courses[$c2->id]->get_course_overviewfiles()));
        $this->assertEquals(0, count($courses[$c3->id]->get_course_overviewfiles()));
        $this->assertEquals(0, count($courses[$c4->id]->get_course_overviewfiles()));
        $this->assertEquals(0, count($courses[$c5->id]->get_course_overviewfiles()));

        // Multiple overview files are allowed but still limited to images.
        $CFG->courseoverviewfileslimit = 3;

        $this->assertTrue($courses[$c1->id]->has_course_overviewfiles());
        $this->assertTrue($courses[$c2->id]->has_course_overviewfiles());
        $this->assertFalse($courses[$c3->id]->has_course_overviewfiles());
        $this->assertTrue($courses[$c4->id]->has_course_overviewfiles());
        $this->assertTrue($courses[$c5->id]->has_course_overviewfiles()); // Still does not validate the filetypes.

        $this->assertEquals(1, count($courses[$c1->id]->get_course_overviewfiles()));
        $this->assertEquals(1, count($courses[$c2->id]->get_course_overviewfiles())); // Only 1 file was actually added.
        $this->assertEquals(0, count($courses[$c3->id]->get_course_overviewfiles()));
        $this->assertEquals(2, count($courses[$c4->id]->get_course_overviewfiles()));
        $this->assertEquals(0, count($courses[$c5->id]->get_course_overviewfiles()));

        // Multiple overview files of any type are allowed.
        $CFG->courseoverviewfilesext = '*';

        $this->assertTrue($courses[$c1->id]->has_course_overviewfiles());
        $this->assertTrue($courses[$c2->id]->has_course_overviewfiles());
        $this->assertFalse($courses[$c3->id]->has_course_overviewfiles());
        $this->assertTrue($courses[$c4->id]->has_course_overviewfiles());
        $this->assertTrue($courses[$c5->id]->has_course_overviewfiles());

        $this->assertEquals(1, count($courses[$c1->id]->get_course_overviewfiles()));
        $this->assertEquals(1, count($courses[$c2->id]->get_course_overviewfiles()));
        $this->assertEquals(0, count($courses[$c3->id]->get_course_overviewfiles()));
        $this->assertEquals(2, count($courses[$c4->id]->get_course_overviewfiles()));
        $this->assertEquals(1, count($courses[$c5->id]->get_course_overviewfiles()));
    }

947
948
949
950
951
    public function test_get_nested_name() {
        $cat1name = 'Cat1';
        $cat2name = 'Cat2';
        $cat3name = 'Cat3';
        $cat4name = 'Cat4';
952
953
954
955
        $category1 = core_course_category::create(array('name' => $cat1name));
        $category2 = core_course_category::create(array('name' => $cat2name, 'parent' => $category1->id));
        $category3 = core_course_category::create(array('name' => $cat3name, 'parent' => $category2->id));
        $category4 = core_course_category::create(array('name' => $cat4name, 'parent' => $category2->id));
956
957
958
959
960
961
962

        $this->assertEquals($cat1name, $category1->get_nested_name(false));
        $this->assertEquals("{$cat1name} / {$cat2name}", $category2->get_nested_name(false));
        $this->assertEquals("{$cat1name} / {$cat2name} / {$cat3name}", $category3->get_nested_name(false));
        $this->assertEquals("{$cat1name} / {$cat2name} / {$cat4name}", $category4->get_nested_name(false));
    }

963
964
965
966
    public function test_coursecat_is_uservisible() {
        global $USER;

        // Create category 1 as visible.
967
        $category1 = core_course_category::create(array('name' => 'Cat1', 'visible' => 1));
968
        // Create category 2 as hidden.
969
        $category2 = core_course_category::create(array('name' => 'Cat2', 'visible' => 0));
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996

        $this->assertTrue($category1->is_uservisible());
        $this->assertFalse($category2->is_uservisible());

        $this->assign_capability('moodle/category:viewhiddencategories');

        $this->assertTrue($category1->is_uservisible());
        $this->assertTrue($category2->is_uservisible());

        // First, store current user's id, then login as another user.
        $userid = $USER->id;
        $this->setUser($this->getDataGenerator()->create_user());

        // User $user should still have the moodle/category:viewhiddencategories capability.
        $this->assertTrue($category1->is_uservisible($userid));
        $this->assertTrue($category2->is_uservisible($userid));

        $this->assign_capability('moodle/category:viewhiddencategories', CAP_INHERIT);

        $this->assertTrue($category1->is_uservisible());
        $this->assertFalse($category2->is_uservisible());
    }

    public function test_current_user_coursecat_get() {
        $this->assign_capability('moodle/category:viewhiddencategories');

        // Create category 1 as visible.
997
        $category1 = core_course_category::create(array('name' => 'Cat1', 'visible' => 1));
998
        // Create category 2 as hidden.
999
        $category2 = core_course_category::create(array('name' => 'Cat2', 'visible' => 0));
1000