forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
management_helper_test.php
1298 lines (1142 loc) · 64.5 KB
/
management_helper_test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
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
803
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
848
849
850
851
852
853
854
855
856
857
858
859
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
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
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
997
998
999
1000
<?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/>.
/**
* Course and category management helper class tests.
*
* @package core_course
* @copyright 2013 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot.'/course/lib.php');
require_once($CFG->dirroot.'/course/tests/fixtures/course_capability_assignment.php');
/**
* Class core_course_management_helper_test
*
* This class tests \core_course\management\helper
*/
class core_course_management_helper_test extends advanced_testcase {
/** Category management capability: moodle/category:manage */
const CATEGORY_MANAGE = 'moodle/category:manage';
/** View hidden category capability: moodle/category:viewhiddencategories */
const CATEGORY_VIEWHIDDEN = 'moodle/category:viewhiddencategories';
/** View course capability: moodle/course:visibility */
const COURSE_VIEW = 'moodle/course:visibility';
/** View hidden course capability: moodle/course:viewhiddencourses */
const COURSE_VIEWHIDDEN = 'moodle/course:viewhiddencourses';
/**
* Returns a user object and its assigned new role.
*
* @param testing_data_generator $generator
* @param $contextid
* @return array The user object and the role ID
*/
protected function get_user_objects(testing_data_generator $generator, $contextid) {
global $USER;
if (empty($USER->id)) {
$user = $generator->create_user();
$this->setUser($user);
}
$roleid = create_role('Test role', 'testrole', 'Test role description');
if (!is_array($contextid)) {
$contextid = array($contextid);
}
foreach ($contextid as $cid) {
$assignid = role_assign($roleid, $user->id, $cid);
}
return array($user, $roleid);
}
/**
* Tests:
* - action_category_hide
* - action_category_show
*
* In order to show/hide the user must have moodle/category:manage on the parent context.
* In order to view hidden categories the user must have moodle/category:viewhiddencategories
*/
public function test_action_category_hide_and_show() {
global $DB;
$this->resetAfterTest(true);
$generator = $this->getDataGenerator();
$category = $generator->create_category();
$subcategory = $generator->create_category(array('parent' => $category->id));
$course = $generator->create_course(array('category' => $subcategory->id));
$context = $category->get_context();
$subcontext = $subcategory->get_context();
$parentcontext = $context->get_parent_context();
list($user, $roleid) = $this->get_user_objects($generator, $parentcontext->id);
$this->assertEquals(1, $category->visible);
$parentassignment = course_capability_assignment::allow(self::CATEGORY_MANAGE, $roleid, $parentcontext->id);
course_capability_assignment::allow(self::CATEGORY_VIEWHIDDEN, $roleid, $parentcontext->id);
course_capability_assignment::allow(self::CATEGORY_MANAGE, $roleid, $context->id);
course_capability_assignment::allow(array(self::COURSE_VIEW, self::COURSE_VIEWHIDDEN), $roleid, $subcontext->id);
$this->assertTrue(\core_course\management\helper::action_category_hide($category));
$cat = coursecat::get($category->id);
$subcat = coursecat::get($subcategory->id);
$course = $DB->get_record('course', array('id' => $course->id), 'id, visible, visibleold', MUST_EXIST);
$this->assertEquals(0, $cat->visible);
$this->assertEquals(0, $cat->visibleold);
$this->assertEquals(0, $subcat->visible);
$this->assertEquals(1, $subcat->visibleold);
$this->assertEquals(0, $course->visible);
$this->assertEquals(1, $course->visibleold);
// This doesn't change anything but should succeed still.
$this->assertTrue(\core_course\management\helper::action_category_hide($category));
$cat = coursecat::get($category->id);
$subcat = coursecat::get($subcategory->id);
$course = $DB->get_record('course', array('id' => $course->id), 'id, visible, visibleold', MUST_EXIST);
$this->assertEquals(0, $cat->visible);
$this->assertEquals(0, $cat->visibleold);
$this->assertEquals(0, $subcat->visible);
$this->assertEquals(1, $subcat->visibleold);
$this->assertEquals(0, $course->visible);
$this->assertEquals(1, $course->visibleold);
$this->assertTrue(\core_course\management\helper::action_category_show($category));
$cat = coursecat::get($category->id);
$subcat = coursecat::get($subcategory->id);
$course = $DB->get_record('course', array('id' => $course->id), 'id, visible, visibleold', MUST_EXIST);
$this->assertEquals(1, $cat->visible);
$this->assertEquals(1, $cat->visibleold);
$this->assertEquals(1, $subcat->visible);
$this->assertEquals(1, $subcat->visibleold);
$this->assertEquals(1, $course->visible);
$this->assertEquals(1, $course->visibleold);
// This doesn't change anything but should succeed still.
$this->assertTrue(\core_course\management\helper::action_category_show($category));
$cat = coursecat::get($category->id);
$subcat = coursecat::get($subcategory->id);
$course = $DB->get_record('course', array('id' => $course->id), 'id, visible, visibleold', MUST_EXIST);
$this->assertEquals(1, $cat->visible);
$this->assertEquals(1, $cat->visibleold);
$this->assertEquals(1, $subcat->visible);
$this->assertEquals(1, $subcat->visibleold);
$this->assertEquals(1, $course->visible);
$this->assertEquals(1, $course->visibleold);
$parentassignment->assign(CAP_PROHIBIT);
try {
\core_course\management\helper::action_category_hide($category);
$this->fail('Expected exception did not occur when trying to hide a category without permission.');
} catch (moodle_exception $ex) {
// The category must still be visible.
$cat = coursecat::get($category->id);
$subcat = coursecat::get($subcategory->id);
$course = $DB->get_record('course', array('id' => $course->id), 'id, visible, visibleold', MUST_EXIST);
$this->assertEquals(1, $cat->visible);
$this->assertEquals(1, $cat->visibleold);
$this->assertEquals(1, $subcat->visible);
$this->assertEquals(1, $subcat->visibleold);
$this->assertEquals(1, $course->visible);
$this->assertEquals(1, $course->visibleold);
}
// Hide the category so that we can test helper::show.
$parentassignment->assign(CAP_ALLOW);
\core_course\management\helper::action_category_hide($category);
$cat = coursecat::get($category->id);
$subcat = coursecat::get($subcategory->id);
$course = $DB->get_record('course', array('id' => $course->id), 'id, visible, visibleold', MUST_EXIST);
$this->assertEquals(0, $cat->visible);
$this->assertEquals(0, $cat->visibleold);
$this->assertEquals(0, $subcat->visible);
$this->assertEquals(1, $subcat->visibleold);
$this->assertEquals(0, $course->visible);
$this->assertEquals(1, $course->visibleold);
$parentassignment->assign(CAP_PROHIBIT);
try {
\core_course\management\helper::action_category_show($category);
$this->fail('Expected exception did not occur when trying to show a category without permission.');
} catch (moodle_exception $ex) {
// The category must still be hidden.
$cat = coursecat::get($category->id);
$subcat = coursecat::get($subcategory->id);
$course = $DB->get_record('course', array('id' => $course->id), 'id, visible, visibleold', MUST_EXIST);
$this->assertEquals(0, $cat->visible);
$this->assertEquals(0, $cat->visibleold);
$this->assertEquals(0, $subcat->visible);
$this->assertEquals(1, $subcat->visibleold);
$this->assertEquals(0, $course->visible);
$this->assertEquals(1, $course->visibleold);
}
$parentassignment->assign(CAP_PREVENT);
// Now we have capability on the category and subcategory but not the parent.
// Try to mark the subcategory as visible. This should be possible although its parent is set to hidden.
$this->assertTrue(\core_course\management\helper::action_category_show($subcategory));
$cat = coursecat::get($category->id);
$subcat = coursecat::get($subcategory->id);
$course = $DB->get_record('course', array('id' => $course->id), 'id, visible, visibleold', MUST_EXIST);
$this->assertEquals(0, $cat->visible);
$this->assertEquals(0, $cat->visibleold);
$this->assertEquals(0, $subcat->visible);
$this->assertEquals(1, $subcat->visibleold);
$this->assertEquals(0, $course->visible);
$this->assertEquals(1, $course->visibleold);
// Now make the parent visible for the next test.
$parentassignment->assign(CAP_ALLOW);
$this->assertTrue(\core_course\management\helper::action_category_show($category));
$cat = coursecat::get($category->id);
$subcat = coursecat::get($subcategory->id);
$course = $DB->get_record('course', array('id' => $course->id), 'id, visible, visibleold', MUST_EXIST);
$this->assertEquals(1, $cat->visible);
$this->assertEquals(1, $cat->visibleold);
$this->assertEquals(1, $subcat->visible);
$this->assertEquals(1, $subcat->visibleold);
$this->assertEquals(1, $course->visible);
$this->assertEquals(1, $course->visibleold);
$parentassignment->assign(CAP_PREVENT);
// Make sure we can change the subcategory visibility.
$this->assertTrue(\core_course\management\helper::action_category_hide($subcategory));
// But not the category visibility.
try {
\core_course\management\helper::action_category_hide($category);
$this->fail('Expected exception did not occur when trying to hide a category without permission.');
} catch (moodle_exception $ex) {
// The category must still be visible.
$this->assertEquals(1, coursecat::get($category->id)->visible);
}
}
/**
* Tests hiding and showing of a category by its ID.
*
* This mimics the logic of {@link test_action_category_hide_and_show()}
*/
public function test_action_category_hide_and_show_by_id() {
global $DB;
$this->resetAfterTest(true);
$generator = $this->getDataGenerator();
$category = $generator->create_category();
$subcategory = $generator->create_category(array('parent' => $category->id));
$course = $generator->create_course(array('category' => $subcategory->id));
$context = $category->get_context();
$parentcontext = $context->get_parent_context();
$subcontext = $subcategory->get_context();
list($user, $roleid) = $this->get_user_objects($generator, $parentcontext->id);
$this->assertEquals(1, $category->visible);
$parentassignment = course_capability_assignment::allow(self::CATEGORY_MANAGE, $roleid, $parentcontext->id);
course_capability_assignment::allow(self::CATEGORY_VIEWHIDDEN, $roleid, $parentcontext->id);
course_capability_assignment::allow(self::CATEGORY_MANAGE, $roleid, $context->id);
course_capability_assignment::allow(array(self::COURSE_VIEW, self::COURSE_VIEWHIDDEN), $roleid, $subcontext->id);
$this->assertTrue(\core_course\management\helper::action_category_hide_by_id($category->id));
$cat = coursecat::get($category->id);
$subcat = coursecat::get($subcategory->id);
$course = $DB->get_record('course', array('id' => $course->id), 'id, visible, visibleold', MUST_EXIST);
$this->assertEquals(0, $cat->visible);
$this->assertEquals(0, $cat->visibleold);
$this->assertEquals(0, $subcat->visible);
$this->assertEquals(1, $subcat->visibleold);
$this->assertEquals(0, $course->visible);
$this->assertEquals(1, $course->visibleold);
// This doesn't change anything but should succeed still.
$this->assertTrue(\core_course\management\helper::action_category_hide_by_id($category->id));
$cat = coursecat::get($category->id);
$subcat = coursecat::get($subcategory->id);
$course = $DB->get_record('course', array('id' => $course->id), 'id, visible, visibleold', MUST_EXIST);
$this->assertEquals(0, $cat->visible);
$this->assertEquals(0, $cat->visibleold);
$this->assertEquals(0, $subcat->visible);
$this->assertEquals(1, $subcat->visibleold);
$this->assertEquals(0, $course->visible);
$this->assertEquals(1, $course->visibleold);
$this->assertTrue(\core_course\management\helper::action_category_show_by_id($category->id));
$cat = coursecat::get($category->id);
$subcat = coursecat::get($subcategory->id);
$course = $DB->get_record('course', array('id' => $course->id), 'id, visible, visibleold', MUST_EXIST);
$this->assertEquals(1, $cat->visible);
$this->assertEquals(1, $cat->visibleold);
$this->assertEquals(1, $subcat->visible);
$this->assertEquals(1, $subcat->visibleold);
$this->assertEquals(1, $course->visible);
$this->assertEquals(1, $course->visibleold);
// This doesn't change anything but should succeed still.
$this->assertTrue(\core_course\management\helper::action_category_show_by_id($category->id));
$cat = coursecat::get($category->id);
$subcat = coursecat::get($subcategory->id);
$course = $DB->get_record('course', array('id' => $course->id), 'id, visible, visibleold', MUST_EXIST);
$this->assertEquals(1, $cat->visible);
$this->assertEquals(1, $cat->visibleold);
$this->assertEquals(1, $subcat->visible);
$this->assertEquals(1, $subcat->visibleold);
$this->assertEquals(1, $course->visible);
$this->assertEquals(1, $course->visibleold);
$parentassignment->assign(CAP_PROHIBIT);
try {
\core_course\management\helper::action_category_hide_by_id($category->id);
$this->fail('Expected exception did not occur when trying to hide a category without permission.');
} catch (moodle_exception $ex) {
// The category must still be visible.
$cat = coursecat::get($category->id);
$subcat = coursecat::get($subcategory->id);
$this->assertEquals(1, $cat->visible);
$this->assertEquals(1, $cat->visibleold);
$this->assertEquals(1, $subcat->visible);
$this->assertEquals(1, $subcat->visibleold);
$this->assertEquals(1, $course->visible);
$this->assertEquals(1, $course->visibleold);
}
// Hide the category so that we can test helper::show.
$parentassignment->assign(CAP_ALLOW);
\core_course\management\helper::action_category_hide_by_id($category->id);
$cat = coursecat::get($category->id);
$subcat = coursecat::get($subcategory->id);
$course = $DB->get_record('course', array('id' => $course->id), 'id, visible, visibleold', MUST_EXIST);
$this->assertEquals(0, $cat->visible);
$this->assertEquals(0, $cat->visibleold);
$this->assertEquals(0, $subcat->visible);
$this->assertEquals(1, $subcat->visibleold);
$this->assertEquals(0, $course->visible);
$this->assertEquals(1, $course->visibleold);
$parentassignment->assign(CAP_PROHIBIT);
try {
\core_course\management\helper::action_category_show_by_id($category->id);
$this->fail('Expected exception did not occur when trying to show a category without permission.');
} catch (moodle_exception $ex) {
// The category must still be hidden.
$cat = coursecat::get($category->id);
$subcat = coursecat::get($subcategory->id);
$course = $DB->get_record('course', array('id' => $course->id), 'id, visible, visibleold', MUST_EXIST);
$this->assertEquals(0, $cat->visible);
$this->assertEquals(0, $cat->visibleold);
$this->assertEquals(0, $subcat->visible);
$this->assertEquals(1, $subcat->visibleold);
$this->assertEquals(0, $course->visible);
$this->assertEquals(1, $course->visibleold);
}
$parentassignment->assign(CAP_PREVENT);
// Now we have capability on the category and subcategory but not the parent.
// Try to mark the subcategory as visible. This should be possible although its parent is set to hidden.
$this->assertTrue(\core_course\management\helper::action_category_show($subcategory));
$cat = coursecat::get($category->id);
$subcat = coursecat::get($subcategory->id);
$course = $DB->get_record('course', array('id' => $course->id), 'id, visible, visibleold', MUST_EXIST);
$this->assertEquals(0, $cat->visible);
$this->assertEquals(0, $cat->visibleold);
$this->assertEquals(0, $subcat->visible);
$this->assertEquals(1, $subcat->visibleold);
$this->assertEquals(0, $course->visible);
$this->assertEquals(1, $course->visibleold);
// Now make the parent visible for the next test.
$parentassignment->assign(CAP_ALLOW);
$this->assertTrue(\core_course\management\helper::action_category_show_by_id($category->id));
$cat = coursecat::get($category->id);
$subcat = coursecat::get($subcategory->id);
$course = $DB->get_record('course', array('id' => $course->id), 'id, visible, visibleold', MUST_EXIST);
$this->assertEquals(1, $cat->visible);
$this->assertEquals(1, $cat->visibleold);
$this->assertEquals(1, $subcat->visible);
$this->assertEquals(1, $subcat->visibleold);
$this->assertEquals(1, $course->visible);
$this->assertEquals(1, $course->visibleold);
$parentassignment->assign(CAP_PREVENT);
// Make sure we can change the subcategory visibility.
$this->assertTrue(\core_course\management\helper::action_category_hide($subcategory));
// But not the category visibility.
try {
\core_course\management\helper::action_category_hide_by_id($category->id);
$this->fail('Expected exception did not occur when trying to hide a category without permission.');
} catch (moodle_exception $ex) {
// The category must still be visible.
$this->assertEquals(1, coursecat::get($category->id)->visible);
}
}
/**
* Test moving courses between categories.
*/
public function test_action_category_move_courses_into() {
global $DB, $CFG;
$this->resetAfterTest(true);
$generator = $this->getDataGenerator();
$cat1 = $generator->create_category();
$cat2 = $generator->create_category();
$sub1 = $generator->create_category(array('parent' => $cat1->id));
$sub2 = $generator->create_category(array('parent' => $cat1->id));
$course1 = $generator->create_course(array('category' => $cat1->id));
$course2 = $generator->create_course(array('category' => $sub1->id));
$course3 = $generator->create_course(array('category' => $sub1->id));
$course4 = $generator->create_course(array('category' => $cat2->id));
$syscontext = context_system::instance();
list($user, $roleid) = $this->get_user_objects($generator, $syscontext->id);
course_capability_assignment::allow(array(self::CATEGORY_MANAGE, self::CATEGORY_VIEWHIDDEN), $roleid, $syscontext->id);
// Check they are where we think they are.
$this->assertEquals(1, $cat1->get_courses_count());
$this->assertEquals(1, $cat2->get_courses_count());
$this->assertEquals(2, $sub1->get_courses_count());
$this->assertEquals(0, $sub2->get_courses_count());
// Move the courses in sub category 1 to sub category 2.
$this->assertTrue(
\core_course\management\helper::action_category_move_courses_into($sub1, $sub2, array($course2->id, $course3->id))
);
$this->assertEquals(1, $cat1->get_courses_count());
$this->assertEquals(1, $cat2->get_courses_count());
$this->assertEquals(0, $sub1->get_courses_count());
$this->assertEquals(2, $sub2->get_courses_count());
$courses = $DB->get_records('course', array('category' => $sub2->id), 'id');
$this->assertEquals(array((int)$course2->id, (int)$course3->id), array_keys($courses));
// Move the courses in sub category 2 back into to sub category 1.
$this->assertTrue(
\core_course\management\helper::action_category_move_courses_into($sub2, $sub1, array($course2->id, $course3->id))
);
$this->assertEquals(1, $cat1->get_courses_count());
$this->assertEquals(1, $cat2->get_courses_count());
$this->assertEquals(2, $sub1->get_courses_count());
$this->assertEquals(0, $sub2->get_courses_count());
$courses = $DB->get_records('course', array('category' => $sub1->id), 'id');
$this->assertEquals(array((int)$course2->id, (int)$course3->id), array_keys($courses));
// Try moving just one course.
$this->assertTrue(
\core_course\management\helper::action_category_move_courses_into($cat2, $sub2, array($course4->id))
);
$this->assertEquals(1, $cat1->get_courses_count());
$this->assertEquals(0, $cat2->get_courses_count());
$this->assertEquals(2, $sub1->get_courses_count());
$this->assertEquals(1, $sub2->get_courses_count());
$courses = $DB->get_records('course', array('category' => $sub2->id), 'id');
$this->assertEquals(array((int)$course4->id), array_keys($courses));
// Try moving a course from a category its not part of.
try {
\core_course\management\helper::action_category_move_courses_into($cat2, $sub2, array($course4->id));
$this->fail('Moved a course from a category it wasn\'t within');
} catch (moodle_exception $exception) {
// Check that everything is as it was.
$this->assertEquals(1, $cat1->get_courses_count());
$this->assertEquals(0, $cat2->get_courses_count());
$this->assertEquals(2, $sub1->get_courses_count());
$this->assertEquals(1, $sub2->get_courses_count());
}
// Now try that again with two courses, one of which is in the right place.
try {
\core_course\management\helper::action_category_move_courses_into($cat2, $sub2, array($course4->id, $course1->id));
$this->fail('Moved a course from a category it wasn\'t within');
} catch (moodle_exception $exception) {
// Check that everything is as it was. Nothing should have been moved.
$this->assertEquals(1, $cat1->get_courses_count());
$this->assertEquals(0, $cat2->get_courses_count());
$this->assertEquals(2, $sub1->get_courses_count());
$this->assertEquals(1, $sub2->get_courses_count());
}
// Current state:
// * $cat1 => $course1
// * $sub1 => $course2, $course3
// * $sub2 => $course4
// * $cat2 =>.
// Prevent the user from being able to move into $sub2.
$sub2cap = course_capability_assignment::prohibit(self::CATEGORY_MANAGE, $roleid, $sub2->get_context()->id);
$sub2 = coursecat::get($sub2->id);
// Suppress debugging messages for a moment.
$olddebug = $CFG->debug;
$CFG->debug = 0;
// Try to move a course into sub2. This shouldn't be possible because you should always be able to undo what you've done.
// Try moving just one course.
try {
\core_course\management\helper::action_category_move_courses_into($sub1, $sub2, array($course2->id));
$this->fail('Invalid move of course between categories, action can\'t be undone.');
} catch (moodle_exception $ex) {
$this->assertEquals(get_string('cannotmovecourses', 'error'), $ex->getMessage());
}
// Nothing should have changed.
$this->assertEquals(1, $cat1->get_courses_count());
$this->assertEquals(0, $cat2->get_courses_count());
$this->assertEquals(2, $sub1->get_courses_count());
$this->assertEquals(1, $sub2->get_courses_count());
// Now try moving a course out of sub2. Again should not be possible.
// Try to move a course into sub2. This shouldn't be possible because you should always be able to undo what you've done.
// Try moving just one course.
try {
\core_course\management\helper::action_category_move_courses_into($sub2, $cat2, array($course4->id));
$this->fail('Invalid move of course between categories, action can\'t be undone.');
} catch (moodle_exception $ex) {
$this->assertEquals(get_string('cannotmovecourses', 'error'), $ex->getMessage());
}
// Nothing should have changed.
$this->assertEquals(1, $cat1->get_courses_count());
$this->assertEquals(0, $cat2->get_courses_count());
$this->assertEquals(2, $sub1->get_courses_count());
$this->assertEquals(1, $sub2->get_courses_count());
$CFG->debug = $olddebug;
}
/**
* Test moving a categories up and down.
*/
public function test_action_category_movedown_and_moveup() {
global $DB;
$this->resetAfterTest(true);
$generator = $this->getDataGenerator();
$parent = $generator->create_category();
$cat1 = $generator->create_category(array('parent' => $parent->id, 'name' => 'One'));
$cat2 = $generator->create_category(array('parent' => $parent->id, 'name' => 'Two'));
$cat3 = $generator->create_category(array('parent' => $parent->id, 'name' => 'Three'));
$syscontext = context_system::instance();
list($user, $roleid) = $this->get_user_objects($generator, $syscontext->id);
course_capability_assignment::allow(self::CATEGORY_MANAGE, $roleid, $syscontext->id);
// Check everything is where we expect it to be.
$this->assertEquals(
array('One', 'Two', 'Three'),
array_keys($DB->get_records('course_categories', array('parent' => $parent->id), 'sortorder', 'name'))
);
// Move the top category down one.
$this->assertTrue(\core_course\management\helper::action_category_change_sortorder_down_one($cat1));
// Reload out objects.
$cat1 = coursecat::get($cat1->id);
$cat2 = coursecat::get($cat2->id);
$cat3 = coursecat::get($cat3->id);
// Verify that caches were cleared.
$this->assertEquals($DB->get_field('course_categories', 'sortorder', array('id' => $cat1->id)), $cat1->sortorder);
$this->assertEquals($DB->get_field('course_categories', 'sortorder', array('id' => $cat2->id)), $cat2->sortorder);
$this->assertEquals($DB->get_field('course_categories', 'sortorder', array('id' => $cat3->id)), $cat3->sortorder);
// Verify sorting.
$this->assertEquals(
array('Two', 'One', 'Three'),
array_keys($DB->get_records('course_categories', array('parent' => $parent->id), 'sortorder', 'name'))
);
// Move the bottom category up one.
$this->assertTrue(\core_course\management\helper::action_category_change_sortorder_up_one($cat3));
// Reload out objects.
$cat1 = coursecat::get($cat1->id);
$cat2 = coursecat::get($cat2->id);
$cat3 = coursecat::get($cat3->id);
// Verify that caches were cleared.
$this->assertEquals($DB->get_field('course_categories', 'sortorder', array('id' => $cat1->id)), $cat1->sortorder);
$this->assertEquals($DB->get_field('course_categories', 'sortorder', array('id' => $cat2->id)), $cat2->sortorder);
$this->assertEquals($DB->get_field('course_categories', 'sortorder', array('id' => $cat3->id)), $cat3->sortorder);
// Verify sorting.
$this->assertEquals(
array('Two', 'Three', 'One'),
array_keys($DB->get_records('course_categories', array('parent' => $parent->id), 'sortorder', 'name'))
);
// Move the top category down one.
$this->assertTrue(\core_course\management\helper::action_category_change_sortorder_down_one_by_id($cat2->id));
$this->assertEquals(
array('Three', 'Two', 'One'),
array_keys($DB->get_records('course_categories', array('parent' => $parent->id), 'sortorder', 'name'))
);
// Move the top category down one.
$this->assertTrue(\core_course\management\helper::action_category_change_sortorder_up_one_by_id($cat1->id));
$this->assertEquals(
array('Three', 'One', 'Two'),
array_keys($DB->get_records('course_categories', array('parent' => $parent->id), 'sortorder', 'name'))
);
// Reload out objects the above actions will have caused the objects to become stale.
$cat1 = coursecat::get($cat1->id);
$cat2 = coursecat::get($cat2->id);
$cat3 = coursecat::get($cat3->id);
// Verify that caches were cleared.
$this->assertEquals($DB->get_field('course_categories', 'sortorder', array('id' => $cat1->id)), $cat1->sortorder);
$this->assertEquals($DB->get_field('course_categories', 'sortorder', array('id' => $cat2->id)), $cat2->sortorder);
$this->assertEquals($DB->get_field('course_categories', 'sortorder', array('id' => $cat3->id)), $cat3->sortorder);
// Verify sorting.
// Test moving the top category up one. Nothing should change but it should return false.
$this->assertFalse(\core_course\management\helper::action_category_change_sortorder_up_one($cat3));
// Reload out objects.
$cat1 = coursecat::get($cat1->id);
$cat2 = coursecat::get($cat2->id);
$cat3 = coursecat::get($cat3->id);
// Verify that caches were cleared.
$this->assertEquals($DB->get_field('course_categories', 'sortorder', array('id' => $cat1->id)), $cat1->sortorder);
$this->assertEquals($DB->get_field('course_categories', 'sortorder', array('id' => $cat2->id)), $cat2->sortorder);
$this->assertEquals($DB->get_field('course_categories', 'sortorder', array('id' => $cat3->id)), $cat3->sortorder);
// Verify sorting.
$this->assertEquals(
array('Three', 'One', 'Two'),
array_keys($DB->get_records('course_categories', array('parent' => $parent->id), 'sortorder', 'name'))
);
// Test moving the bottom category down one. Nothing should change but it should return false.
$this->assertFalse(\core_course\management\helper::action_category_change_sortorder_down_one($cat2));
// Reload out objects.
$cat1 = coursecat::get($cat1->id);
$cat2 = coursecat::get($cat2->id);
$cat3 = coursecat::get($cat3->id);
// Verify that caches were cleared.
$this->assertEquals($DB->get_field('course_categories', 'sortorder', array('id' => $cat1->id)), $cat1->sortorder);
$this->assertEquals($DB->get_field('course_categories', 'sortorder', array('id' => $cat2->id)), $cat2->sortorder);
$this->assertEquals($DB->get_field('course_categories', 'sortorder', array('id' => $cat3->id)), $cat3->sortorder);
// Verify sorting.
$this->assertEquals(
array('Three', 'One', 'Two'),
array_keys($DB->get_records('course_categories', array('parent' => $parent->id), 'sortorder', 'name'))
);
// Prevent moving on the parent.
course_capability_assignment::prevent(self::CATEGORY_MANAGE, $roleid, $parent->get_context()->id);
try {
\core_course\management\helper::action_category_change_sortorder_up_one($cat1);
} catch (moodle_exception $exception) {
// Check everything is still where it should be.
$this->assertEquals(
array('Three', 'One', 'Two'),
array_keys($DB->get_records('course_categories', array('parent' => $parent->id), 'sortorder', 'name'))
);
}
try {
\core_course\management\helper::action_category_change_sortorder_down_one($cat3);
} catch (moodle_exception $exception) {
// Check everything is still where it should be.
$this->assertEquals(
array('Three', 'One', 'Two'),
array_keys($DB->get_records('course_categories', array('parent' => $parent->id), 'sortorder', 'name'))
);
}
}
/**
* Test resorting of courses within a category.
*
* \core_course\management\helper::action_category_resort_courses
*/
public function test_action_category_resort_courses() {
global $DB;
$this->resetAfterTest(true);
$generator = $this->getDataGenerator();
$category = $generator->create_category();
$course1 = $generator->create_course(array('category' => $category->id, 'fullname' => 'Experimental Chemistry',
'shortname' => 'Course A', 'idnumber' => '10001'));
$course2 = $generator->create_course(array('category' => $category->id, 'fullname' => 'Learn to program: Jade',
'shortname' => 'Beginning Jade', 'idnumber' => '10003'));
$course3 = $generator->create_course(array('category' => $category->id, 'fullname' => 'Advanced algebra',
'shortname' => 'Advanced algebra', 'idnumber' => '10002'));
$syscontext = context_system::instance();
// Update category object from DB so the course count is correct.
$category = coursecat::get($category->id);
list($user, $roleid) = $this->get_user_objects($generator, $syscontext->id);
$caps = course_capability_assignment::allow(self::CATEGORY_MANAGE, $roleid, $syscontext->id);
// Check that sort order in the DB matches what we've got in the cache.
$courses = $category->get_courses();
$this->assertInternalType('array', $courses);
$dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder');
$this->assertEquals(array_keys($dbcourses), array_keys($courses));
// Resort by fullname.
\core_course\management\helper::action_category_resort_courses($category, 'fullname');
$courses = $category->get_courses();
$this->assertInternalType('array', $courses);
$this->assertEquals(array($course3->id, $course1->id, $course2->id), array_keys($courses));
$dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder');
$this->assertEquals(array_keys($dbcourses), array_keys($courses));
// Resort by shortname.
\core_course\management\helper::action_category_resort_courses($category, 'shortname');
$courses = $category->get_courses();
$this->assertInternalType('array', $courses);
$this->assertEquals(array($course3->id, $course2->id, $course1->id), array_keys($courses));
$dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder');
$this->assertEquals(array_keys($dbcourses), array_keys($courses));
// Resort by idnumber.
\core_course\management\helper::action_category_resort_courses($category, 'idnumber');
$courses = $category->get_courses();
$this->assertInternalType('array', $courses);
$this->assertEquals(array($course1->id, $course3->id, $course2->id), array_keys($courses));
$dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder');
$this->assertEquals(array_keys($dbcourses), array_keys($courses));
// Try with a field that cannot be sorted on.
try {
\core_course\management\helper::action_category_resort_courses($category, 'category');
$this->fail('Category courses resorted by invalid sort field.');
} catch (coding_exception $exception) {
// Test things are as they were before.
$courses = $category->get_courses();
$this->assertInternalType('array', $courses);
$this->assertEquals(array($course1->id, $course3->id, $course2->id), array_keys($courses));
$dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder');
$this->assertEquals(array_keys($dbcourses), array_keys($courses));
}
// Try with a completely bogus field.
try {
\core_course\management\helper::action_category_resort_courses($category, 'monkeys');
$this->fail('Category courses resorted by completely ridiculous field.');
} catch (coding_exception $exception) {
// Test things are as they were before.
$courses = $category->get_courses();
$this->assertInternalType('array', $courses);
$this->assertEquals(array($course1->id, $course3->id, $course2->id), array_keys($courses));
$dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder');
$this->assertEquals(array_keys($dbcourses), array_keys($courses));
}
// Prohibit resorting.
$caps->assign(CAP_PROHIBIT);
// Refresh our coursecat object.
$category = coursecat::get($category->id);
// We should no longer have permission to do this. Test it out!
try {
\core_course\management\helper::action_category_resort_courses($category, 'shortname');
$this->fail('Courses sorted without having the required permission.');
} catch (moodle_exception $exception) {
// Check its the right exception.
$this->assertEquals('coursecat::can_resort', $exception->debuginfo);
// Test things are as they were before.
$courses = $category->get_courses();
$this->assertInternalType('array', $courses);
$this->assertEquals(array($course1->id, $course3->id, $course2->id), array_keys($courses));
$dbcourses = $DB->get_records('course', array('category' => $category->id), 'sortorder');
$this->assertEquals(array_keys($dbcourses), array_keys($courses));
}
}
/**
* Tests resorting sub categories of a course.
*
* \core_course\management\helper::action_category_resort_courses
*/
public function test_action_category_resort_subcategories() {
global $DB;
$this->resetAfterTest(true);
$generator = $this->getDataGenerator();
$parent = $generator->create_category();
$cat1 = $generator->create_category(array('parent' => $parent->id, 'name' => 'School of Science', 'idnumber' => '10001'));
$cat2 = $generator->create_category(array('parent' => $parent->id, 'name' => 'School of Commerce', 'idnumber' => '10003'));
$cat3 = $generator->create_category(array('parent' => $parent->id, 'name' => 'School of Arts', 'idnumber' => '10002'));
$syscontext = context_system::instance();
list($user, $roleid) = $this->get_user_objects($generator, $syscontext->id);
$caps = course_capability_assignment::allow(self::CATEGORY_MANAGE, $roleid, $syscontext->id);
$categories = $parent->get_children();
$this->assertInternalType('array', $categories);
$dbcategories = $DB->get_records('course_categories', array('parent' => $parent->id), 'sortorder');
$this->assertEquals(array_keys($dbcategories), array_keys($categories));
// Test sorting by name.
\core_course\management\helper::action_category_resort_subcategories($parent, 'name');
$categories = $parent->get_children();
$this->assertInternalType('array', $categories);
$this->assertEquals(array($cat3->id, $cat2->id, $cat1->id), array_keys($categories));
$dbcategories = $DB->get_records('course_categories', array('parent' => $parent->id), 'sortorder');
$this->assertEquals(array_keys($dbcategories), array_keys($categories));
// Test sorting by idnumber.
\core_course\management\helper::action_category_resort_subcategories($parent, 'idnumber');
$categories = $parent->get_children();
$this->assertInternalType('array', $categories);
$this->assertEquals(array($cat1->id, $cat3->id, $cat2->id), array_keys($categories));
$dbcategories = $DB->get_records('course_categories', array('parent' => $parent->id), 'sortorder');
$this->assertEquals(array_keys($dbcategories), array_keys($categories));
// Try with an invalid field.
try {
\core_course\management\helper::action_category_resort_subcategories($parent, 'summary');
$this->fail('Categories resorted by invalid field.');
} catch (coding_exception $exception) {
// Check that nothing was changed.
$categories = $parent->get_children();
$this->assertInternalType('array', $categories);
$this->assertEquals(array($cat1->id, $cat3->id, $cat2->id), array_keys($categories));
$dbcategories = $DB->get_records('course_categories', array('parent' => $parent->id), 'sortorder');
$this->assertEquals(array_keys($dbcategories), array_keys($categories));
}
// Try with a completely bogus field.
try {
\core_course\management\helper::action_category_resort_subcategories($parent, 'monkeys');
$this->fail('Categories resorted by completely bogus field.');
} catch (coding_exception $exception) {
// Check that nothing was changed.
$categories = $parent->get_children();
$this->assertInternalType('array', $categories);
$this->assertEquals(array($cat1->id, $cat3->id, $cat2->id), array_keys($categories));
$dbcategories = $DB->get_records('course_categories', array('parent' => $parent->id), 'sortorder');
$this->assertEquals(array_keys($dbcategories), array_keys($categories));
}
// Test resorting the top level category (puke).
$topcat = coursecat::get(0);
\core_course\management\helper::action_category_resort_subcategories($topcat, 'name');
$categories = $topcat->get_children();
$this->assertInternalType('array', $categories);
$dbcategories = $DB->get_records('course_categories', array('parent' => '0'), 'sortorder');
$this->assertEquals(array_keys($dbcategories), array_keys($categories));
// Prohibit resorting.
$caps->assign(CAP_PROHIBIT);
// Refresh our coursecat object.
$parent = coursecat::get($parent->id);
// We should no longer have permission to do this. Test it out!
try {
\core_course\management\helper::action_category_resort_subcategories($parent, 'idnumber');
$this->fail('Categories sorted without having the required permission.');
} catch (moodle_exception $exception) {
// Check its the right exception.
$this->assertEquals('coursecat::can_resort', $exception->debuginfo);
// Test things are as they were before.
$categories = $parent->get_children();
$this->assertInternalType('array', $categories);
$this->assertEquals(array($cat1->id, $cat3->id, $cat2->id), array_keys($categories));
$dbcategories = $DB->get_records('course_categories', array('parent' => $parent->id), 'sortorder');
$this->assertEquals(array_keys($dbcategories), array_keys($categories));
}
}
/**
* Test hiding and showing of a course.
*
* @see \core_course\management\helper::action_course_hide
* @see \core_course\management\helper::action_course_show
*/
public function test_action_course_hide_show() {
$this->resetAfterTest(true);
$generator = $this->getDataGenerator();
$category = $generator->create_category();
$course = $generator->create_course();
$coursecontext = context_course::instance($course->id);
list($user, $roleid) = $this->get_user_objects($generator, $coursecontext->id);
$caps = array(self::COURSE_VIEW, self::COURSE_VIEWHIDDEN);
$assignment = course_capability_assignment::allow($caps, $roleid, $coursecontext->id);
$course = new course_in_list(get_course($course->id));
// Check it is set to what we think it is.
$this->assertEquals('1', $course->visible);
$this->assertEquals('1', $course->visibleold);
// Test hiding the course.
$this->assertTrue(\core_course\management\helper::action_course_hide($course));
// Refresh the course.
$course = new course_in_list(get_course($course->id));
$this->assertEquals('0', $course->visible);
$this->assertEquals('0', $course->visibleold);
// Test hiding the course again.
$this->assertTrue(\core_course\management\helper::action_course_hide($course));
// Refresh the course.
$course = new course_in_list(get_course($course->id));
$this->assertEquals('0', $course->visible);
$this->assertEquals('0', $course->visibleold);
// Test showing the course.
$this->assertTrue(\core_course\management\helper::action_course_show($course));
// Refresh the course.
$course = new course_in_list(get_course($course->id));
$this->assertEquals('1', $course->visible);
$this->assertEquals('1', $course->visibleold);
// Test showing the course again. Shouldn't change anything.
$this->assertTrue(\core_course\management\helper::action_course_show($course));
// Refresh the course.
$course = new course_in_list(get_course($course->id));
$this->assertEquals('1', $course->visible);
$this->assertEquals('1', $course->visibleold);
// Revoke the permissions.
$assignment->revoke();
$course = new course_in_list(get_course($course->id));
try {
\core_course\management\helper::action_course_show($course);
} catch (moodle_exception $exception) {
$this->assertEquals('course_in_list::can_change_visbility', $exception->debuginfo);
}
}
/**
* Test hiding and showing of a course.
*
* @see \core_course\management\helper::action_course_hide_by_record
* @see \core_course\management\helper::action_course_show_by_record
*/
public function test_action_course_hide_show_by_record() {
$this->resetAfterTest(true);
$generator = $this->getDataGenerator();
$category = $generator->create_category();
$course = $generator->create_course();
$coursecontext = context_course::instance($course->id);
list($user, $roleid) = $this->get_user_objects($generator, $coursecontext->id);
$caps = array(self::COURSE_VIEW, self::COURSE_VIEWHIDDEN);
$assignment = course_capability_assignment::allow($caps, $roleid, $coursecontext->id);
$course = get_course($course->id);
// Check it is set to what we think it is.
$this->assertEquals('1', $course->visible);
$this->assertEquals('1', $course->visibleold);
// Test hiding the course.
$this->assertTrue(\core_course\management\helper::action_course_hide_by_record($course));
// Refresh the course.
$course = get_course($course->id);
$this->assertEquals('0', $course->visible);
$this->assertEquals('0', $course->visibleold);
// Test hiding the course again. Shouldn't change anything.
$this->assertTrue(\core_course\management\helper::action_course_hide_by_record($course));
// Refresh the course.
$course = get_course($course->id);
$this->assertEquals('0', $course->visible);
$this->assertEquals('0', $course->visibleold);
// Test showing the course.
$this->assertTrue(\core_course\management\helper::action_course_show_by_record($course));
// Refresh the course.
$course = get_course($course->id);
$this->assertEquals('1', $course->visible);
$this->assertEquals('1', $course->visibleold);
// Test showing the course again. Shouldn't change anything.
$this->assertTrue(\core_course\management\helper::action_course_show_by_record($course));
// Refresh the course.
$course = get_course($course->id);
$this->assertEquals('1', $course->visible);
$this->assertEquals('1', $course->visibleold);
// Revoke the permissions.
$assignment->revoke();
$course = get_course($course->id);
try {
\core_course\management\helper::action_course_show_by_record($course);
} catch (moodle_exception $exception) {
$this->assertEquals('course_in_list::can_change_visbility', $exception->debuginfo);
}
}
/**
* Tests moving a course up and down by one.
*/
public function test_action_course_movedown_and_moveup() {
global $DB;
$this->resetAfterTest(true);
$generator = $this->getDataGenerator();
$category = $generator->create_category();
$course3 = $generator->create_course(array('category' => $category->id));
$course2 = $generator->create_course(array('category' => $category->id));
$course1 = $generator->create_course(array('category' => $category->id));
$context = $category->get_context();
// Update category object from DB so the course count is correct.
$category = coursecat::get($category->id);
list($user, $roleid) = $this->get_user_objects($generator, $context->id);