2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Unit tests for calendar_information.
20 * @package core_calendar
21 * @copyright 2017 Andrew Nicols <andrew@nicols.co.uk>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') ||
die();
27 require_once(__DIR__
. '/helpers.php');
30 * Unit tests for calendar_information.
32 * @package core_calendar
33 * @copyright 2017 Andrew Nicols <andrew@nicols.co.uk>
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class calendar_information_testcase
extends advanced_testcase
{
39 * Helper to mock a course and category structure.
43 protected function mock_structure() {
44 $this->resetAfterTest();
46 $generator = $this->getDataGenerator();
51 $categories['A'] = $generator->create_category(['name' => 'A']);
52 $courses['A.1'] = $generator->create_course(['category' => $categories['A']->id
]);
53 $courses['A.2'] = $generator->create_course(['category' => $categories['A']->id
]);
54 $categories['A1'] = $generator->create_category(['name' => 'A1', 'parent' => $categories['A']->id
]);
55 $courses['A1.1'] = $generator->create_course(['category' => $categories['A1']->id
]);
56 $courses['A1.2'] = $generator->create_course(['category' => $categories['A1']->id
]);
57 $categories['A1i'] = $generator->create_category(['name' => 'A1i', 'parent' => $categories['A1']->id
]);
58 $categories['A1ii'] = $generator->create_category(['name' => 'A1ii', 'parent' => $categories['A1']->id
]);
59 $categories['A2'] = $generator->create_category(['name' => 'A2', 'parent' => $categories['A']->id
]);
60 $courses['A2.1'] = $generator->create_course(['category' => $categories['A2']->id
]);
61 $courses['A2.2'] = $generator->create_course(['category' => $categories['A2']->id
]);
62 $categories['A2i'] = $generator->create_category(['name' => 'A2i', 'parent' => $categories['A2']->id
]);
63 $categories['A2ii'] = $generator->create_category(['name' => 'A2ii', 'parent' => $categories['A2']->id
]);
64 $categories['B'] = $generator->create_category(['name' => 'B']);
65 $courses['B.1'] = $generator->create_course(['category' => $categories['B']->id
]);
66 $courses['B.2'] = $generator->create_course(['category' => $categories['B']->id
]);
67 $categories['B1'] = $generator->create_category(['name' => 'B1', 'parent' => $categories['B']->id
]);
68 $categories['B1i'] = $generator->create_category(['name' => 'B1i', 'parent' => $categories['B1']->id
]);
69 $categories['B1ii'] = $generator->create_category(['name' => 'B1ii', 'parent' => $categories['B1']->id
]);
70 $categories['B2'] = $generator->create_category(['name' => 'B2', 'parent' => $categories['B']->id
]);
71 $categories['B2i'] = $generator->create_category(['name' => 'B2i', 'parent' => $categories['B2']->id
]);
72 $categories['B2ii'] = $generator->create_category(['name' => 'B2ii', 'parent' => $categories['B2']->id
]);
73 $categories['C'] = $generator->create_category(['name' => 'C']);
75 return [$courses, $categories];
79 * Given a user has no enrolments.
80 * And I ask for the site information.
81 * Then I should see the site.
82 * And I should see no other courses.
83 * And I should see no categories.
85 public function test_site_visibility_no_enrolment() {
86 $this->resetAfterTest();
87 list ($courses, $categories) = $this->mock_structure();
89 $generator = $this->getDataGenerator();
90 $user = $generator->create_user();
91 $this->setUser($user);
93 $calendar = \calendar_information
::create(time(), SITEID
, null);
95 $this->assertCount(1, $calendar->courses
);
96 $this->assertCount(0, $calendar->categories
);
97 $this->assertEquals(SITEID
, $calendar->courseid
);
98 $this->assertEquals(SITEID
, reset($calendar->courses
));
99 $this->assertEquals(\context_system
::instance(), $calendar->context
);
103 * Given a user has no enrolments.
104 * And I ask for a category.
105 * Then I should see the category.
106 * And I should see the category parents.
107 * And I should see the category descendants.
108 * And I should see the site course.
109 * And I should see no other courses.
111 public function test_site_visibility_no_enrolment_category() {
112 $this->resetAfterTest();
113 list ($courses, $categories) = $this->mock_structure();
115 $generator = $this->getDataGenerator();
116 $user = $generator->create_user();
117 $this->setUser($user);
119 $category = $categories['A1'];
120 $calendar = \calendar_information
::create(time(), SITEID
, $category->id
);
122 $this->assertCount(1, $calendar->courses
);
123 $this->assertCount(4, $calendar->categories
);
124 $this->assertEquals(SITEID
, $calendar->courseid
);
125 $this->assertEquals(SITEID
, reset($calendar->courses
));
126 $this->assertArrayHasKey($categories['A']->id
, array_flip($calendar->categories
));
127 $this->assertArrayHasKey($categories['A1']->id
, array_flip($calendar->categories
));
128 $this->assertArrayHasKey($categories['A1i']->id
, array_flip($calendar->categories
));
129 $this->assertArrayHasKey($categories['A1ii']->id
, array_flip($calendar->categories
));
130 $this->assertEquals(\context_coursecat
::instance($category->id
), $calendar->context
);
134 * Given a user has a role assignment to manage a category.
135 * And I ask for the site information.
136 * Then I should see that category.
137 * And I should see the category parents.
138 * And I should see the category descendants.
139 * And I should see the site course.
140 * And I should see no other courses.
142 public function test_site_visibility_category_manager_site() {
145 $this->resetAfterTest();
146 list ($courses, $categories) = $this->mock_structure();
148 $generator = $this->getDataGenerator();
149 $user = $generator->create_user();
150 $category = $categories['A1'];
152 $roles = $DB->get_records('role', [], '', 'shortname, id');
153 $generator->role_assign($roles['manager']->id
, $user->id
, \context_coursecat
::instance($category->id
));
155 $this->setUser($user);
157 $calendar = \calendar_information
::create(time(), SITEID
, null);
159 $this->assertCount(1, $calendar->courses
);
160 $this->assertCount(4, $calendar->categories
);
161 $this->assertEquals(SITEID
, $calendar->courseid
);
162 $this->assertEquals(SITEID
, reset($calendar->courses
));
163 $this->assertArrayHasKey($categories['A']->id
, array_flip($calendar->categories
));
164 $this->assertArrayHasKey($categories['A1']->id
, array_flip($calendar->categories
));
165 $this->assertArrayHasKey($categories['A1i']->id
, array_flip($calendar->categories
));
166 $this->assertArrayHasKey($categories['A1ii']->id
, array_flip($calendar->categories
));
167 $this->assertEquals(\context_system
::instance(), $calendar->context
);
171 * Given a user has a role assignment to manage a category.
172 * And I ask for that category.
173 * Then I should see that category.
174 * And I should see the category parents.
175 * And I should see the category descendants.
176 * And I should see the site course.
177 * And I should see no other courses.
179 public function test_site_visibility_category_manager_own_category() {
182 $this->resetAfterTest();
183 list ($courses, $categories) = $this->mock_structure();
185 $generator = $this->getDataGenerator();
186 $user = $generator->create_user();
187 $category = $categories['A1'];
189 $roles = $DB->get_records('role', [], '', 'shortname, id');
190 $generator->role_assign($roles['manager']->id
, $user->id
, \context_coursecat
::instance($category->id
));
192 $this->setUser($user);
194 $calendar = \calendar_information
::create(time(), SITEID
, $category->id
);
196 $this->assertCount(1, $calendar->courses
);
197 $this->assertCount(4, $calendar->categories
);
198 $this->assertEquals(SITEID
, $calendar->courseid
);
199 $this->assertEquals(SITEID
, reset($calendar->courses
));
200 $this->assertArrayHasKey($categories['A']->id
, array_flip($calendar->categories
));
201 $this->assertArrayHasKey($categories['A1']->id
, array_flip($calendar->categories
));
202 $this->assertArrayHasKey($categories['A1i']->id
, array_flip($calendar->categories
));
203 $this->assertArrayHasKey($categories['A1ii']->id
, array_flip($calendar->categories
));
204 $this->assertEquals(\context_coursecat
::instance($category->id
), $calendar->context
);
208 * Given a user has a role assignment to manage a category.
209 * And I ask for the parent of that category.
210 * Then I should see that category.
211 * And I should see the category parents.
212 * And I should see the category descendants.
213 * And I should see the site course.
214 * And I should see no other courses.
216 public function test_site_visibility_category_manager_parent_category() {
219 $this->resetAfterTest();
220 list ($courses, $categories) = $this->mock_structure();
222 $generator = $this->getDataGenerator();
223 $user = $generator->create_user();
224 $category = $categories['A1'];
226 $roles = $DB->get_records('role', [], '', 'shortname, id');
227 $generator->role_assign($roles['manager']->id
, $user->id
, \context_coursecat
::instance($category->id
));
229 $this->setUser($user);
231 $calendar = \calendar_information
::create(time(), SITEID
, $category->parent
);
233 $this->assertCount(1, $calendar->courses
);
234 $this->assertCount(7, $calendar->categories
);
235 $this->assertEquals(SITEID
, $calendar->courseid
);
236 $this->assertEquals(SITEID
, reset($calendar->courses
));
237 $this->assertArrayHasKey($categories['A']->id
, array_flip($calendar->categories
));
238 $this->assertArrayHasKey($categories['A1']->id
, array_flip($calendar->categories
));
239 $this->assertArrayHasKey($categories['A1i']->id
, array_flip($calendar->categories
));
240 $this->assertArrayHasKey($categories['A1ii']->id
, array_flip($calendar->categories
));
241 $this->assertEquals(\context_coursecat
::instance($category->parent
), $calendar->context
);
245 * Given a user has a role assignment to manage a category.
246 * And I ask for a child of that category.
247 * Then I should see that category.
248 * And I should see the category parents.
249 * And I should see the category descendants.
250 * And I should see the site course.
251 * And I should see no other courses.
253 public function test_site_visibility_category_manager_child_category() {
256 $this->resetAfterTest();
257 list ($courses, $categories) = $this->mock_structure();
259 $generator = $this->getDataGenerator();
260 $user = $generator->create_user();
261 $enrolledcategory = $categories['A1'];
262 $category = $categories['A1i'];
264 $roles = $DB->get_records('role', [], '', 'shortname, id');
265 $generator->role_assign($roles['manager']->id
, $user->id
, \context_coursecat
::instance($enrolledcategory->id
));
267 $this->setUser($user);
269 $calendar = \calendar_information
::create(time(), SITEID
, $category->id
);
271 $this->assertCount(1, $calendar->courses
);
272 $this->assertCount(3, $calendar->categories
);
273 $this->assertEquals(SITEID
, $calendar->courseid
);
274 $this->assertEquals(SITEID
, reset($calendar->courses
));
275 $this->assertArrayHasKey($categories['A']->id
, array_flip($calendar->categories
));
276 $this->assertArrayHasKey($categories['A1']->id
, array_flip($calendar->categories
));
277 $this->assertArrayHasKey($categories['A1i']->id
, array_flip($calendar->categories
));
278 $this->assertEquals(\context_coursecat
::instance($category->id
), $calendar->context
);
282 * Given a user has an enrolment in a single course.
283 * And I ask for the site information.
284 * Then I should see the site.
285 * And I should see the course I am enrolled in.
286 * And I should see the category that my enrolled course is in.
287 * And I should see the parents of the category that my enrolled course is in.
289 public function test_site_visibility_single_course_site() {
290 $this->resetAfterTest();
291 list ($courses, $categories) = $this->mock_structure();
293 $generator = $this->getDataGenerator();
294 $user = $generator->create_user();
295 $course = $courses['A1.1'];
296 $category = \coursecat
::get($course->category
);
297 $wrongcategory = $categories['B1'];
298 $generator->enrol_user($user->id
, $course->id
);
300 $this->setUser($user);
302 // Viewing the site as a whole.
303 // Should see all courses that this user is enrolled in, and their
304 // categories, and those categories parents.
305 $calendar = \calendar_information
::create(time(), SITEID
, null);
307 $this->assertCount(2, $calendar->courses
);
308 $this->assertCount(2, $calendar->categories
);
309 $this->assertEquals(SITEID
, $calendar->courseid
);
310 $this->assertArrayHasKey($course->id
, array_flip($calendar->courses
));
311 $this->assertArrayHasKey(SITEID
, array_flip($calendar->courses
));
312 $this->assertArrayHasKey($categories['A']->id
, array_flip($calendar->categories
));
313 $this->assertArrayHasKey($categories['A1']->id
, array_flip($calendar->categories
));
314 $this->assertEquals(\context_system
::instance(), $calendar->context
);
318 * Given a user has an enrolment in a single course.
319 * And I ask for the course information.
320 * Then I should see the site.
321 * And I should see that course.
322 * And I should see the category of that course.
323 * And I should see the parents of that course category.
325 public function test_site_visibility_single_course_course_course() {
326 $this->resetAfterTest();
327 list ($courses, $categories) = $this->mock_structure();
329 $generator = $this->getDataGenerator();
330 $user = $generator->create_user();
331 $course = $courses['A1.1'];
332 $category = \coursecat
::get($course->category
);
333 $wrongcategory = $categories['B1'];
334 $generator->enrol_user($user->id
, $course->id
);
336 $this->setUser($user);
339 // Viewing the course calendar.
340 // Should see just this course, and all parent categories.
341 $calendar = \calendar_information
::create($time, $course->id
, null);
343 $this->assertCount(2, $calendar->courses
);
344 $this->assertCount(2, $calendar->categories
);
345 $this->assertEquals($course->id
, $calendar->courseid
);
346 $this->assertArrayHasKey($course->id
, array_flip($calendar->courses
));
347 $this->assertArrayHasKey(SITEID
, array_flip($calendar->courses
));
348 $this->assertArrayHasKey($categories['A']->id
, array_flip($calendar->categories
));
349 $this->assertArrayHasKey($categories['A1']->id
, array_flip($calendar->categories
));
350 $this->assertEquals(\context_course
::instance($course->id
), $calendar->context
);
352 // Viewing the course calendar while specifying the category too.
353 // The category is essentially ignored. No change expected.
354 $calendarwithcategory = \calendar_information
::create($time, $course->id
, $category->id
);
355 $this->assertEquals($calendar, $calendarwithcategory);
357 // Viewing the course calendar while specifying the wrong category.
358 // The category is essentially ignored. No change expected.
359 $calendarwithwrongcategory = \calendar_information
::create($time, $course->id
, $wrongcategory->id
);
360 $this->assertEquals($calendar, $calendarwithwrongcategory);
364 * Given a user has an enrolment in a single course.
365 * And I ask for the category information for the category my course is in.
366 * Then I should see that category.
367 * And I should see the category parents.
368 * And I should see the category descendants.
369 * And I should see the site.
370 * And I should see my course.
371 * And I should see no other courses.
372 * And I should see no categories.
374 public function test_site_visibility_single_course_category() {
375 $this->resetAfterTest();
376 list ($courses, $categories) = $this->mock_structure();
378 $generator = $this->getDataGenerator();
379 $user = $generator->create_user();
380 $course = $courses['A1.1'];
381 $category = \coursecat
::get($course->category
);
382 $generator->enrol_user($user->id
, $course->id
);
384 $this->setUser($user);
386 // Viewing the category calendar.
387 // Should see all courses that this user is enrolled in within this
388 // category, plus the site course, plus the category that course is
389 // in and it's parents, and it's children.
390 $calendar = \calendar_information
::create(time(), SITEID
, $category->id
);
392 $this->assertCount(2, $calendar->courses
);
393 $this->assertCount(4, $calendar->categories
);
394 $this->assertEquals(SITEID
, $calendar->courseid
);
395 $this->assertArrayHasKey($course->id
, array_flip($calendar->courses
));
396 $this->assertArrayHasKey(SITEID
, array_flip($calendar->courses
));
397 $this->assertArrayHasKey($categories['A']->id
, array_flip($calendar->categories
));
398 $this->assertArrayHasKey($categories['A1']->id
, array_flip($calendar->categories
));
399 $this->assertArrayHasKey($categories['A1i']->id
, array_flip($calendar->categories
));
400 $this->assertArrayHasKey($categories['A1ii']->id
, array_flip($calendar->categories
));
401 $this->assertEquals(\context_coursecat
::instance($category->id
), $calendar->context
);
405 * Given a user has an enrolment in a single course.
406 * And I ask for the category information for the parent of the category my course is in.
407 * Then I should see that category.
408 * And I should see the category parents.
409 * And I should see the category descendants.
410 * And I should see the site.
411 * And I should see my course.
412 * And I should see no other courses.
413 * And I should see no categories.
415 public function test_site_visibility_single_course_parent_category() {
416 $this->resetAfterTest();
417 list ($courses, $categories) = $this->mock_structure();
419 $generator = $this->getDataGenerator();
420 $user = $generator->create_user();
421 $course = $courses['A1.1'];
422 $category = \coursecat
::get($course->category
);
423 $generator->enrol_user($user->id
, $course->id
);
425 $this->setUser($user);
427 // Viewing the category calendar.
428 // Should see all courses that this user is enrolled in within this
429 // category, plus the site course, plus the category that course is
430 // in and it's parents, and it's children.
431 $calendar = \calendar_information
::create(time(), SITEID
, $category->parent
);
433 $this->assertCount(2, $calendar->courses
);
434 $this->assertCount(7, $calendar->categories
);
435 $this->assertEquals(SITEID
, $calendar->courseid
);
436 $this->assertArrayHasKey($course->id
, array_flip($calendar->courses
));
437 $this->assertArrayHasKey(SITEID
, array_flip($calendar->courses
));
438 $this->assertArrayHasKey($categories['A']->id
, array_flip($calendar->categories
));
439 $this->assertArrayHasKey($categories['A1']->id
, array_flip($calendar->categories
));
440 $this->assertArrayHasKey($categories['A1i']->id
, array_flip($calendar->categories
));
441 $this->assertArrayHasKey($categories['A1ii']->id
, array_flip($calendar->categories
));
442 $this->assertArrayHasKey($categories['A2']->id
, array_flip($calendar->categories
));
443 $this->assertArrayHasKey($categories['A2i']->id
, array_flip($calendar->categories
));
444 $this->assertArrayHasKey($categories['A2ii']->id
, array_flip($calendar->categories
));
445 $this->assertEquals(\context_coursecat
::instance($category->parent
), $calendar->context
);
449 * Given a user has an enrolment in a single course.
450 * And I ask for the category information for the sibling of the category my course is in.
451 * Then I should see that category.
452 * And I should see the category parents.
453 * And I should see the category descendants.
454 * And I should see the site.
455 * And I should see my course.
456 * And I should see no other courses.
457 * And I should see no categories.
459 public function test_site_visibility_single_course_sibling_category() {
460 $this->resetAfterTest();
461 list ($courses, $categories) = $this->mock_structure();
463 $generator = $this->getDataGenerator();
464 $user = $generator->create_user();
465 $course = $courses['A1.1'];
466 $category = $categories['A2'];
467 $generator->enrol_user($user->id
, $course->id
);
469 $this->setUser($user);
471 // Viewing the category calendar.
472 // Should see all courses that this user is enrolled in within this
473 // category, plus the site course, plus the category that course is
474 // in and it's parents, and it's children.
475 $calendar = \calendar_information
::create(time(), SITEID
, $category->id
);
477 $this->assertCount(1, $calendar->courses
);
478 $this->assertCount(4, $calendar->categories
);
479 $this->assertEquals(SITEID
, $calendar->courseid
);
480 $this->assertArrayHasKey(SITEID
, array_flip($calendar->courses
));
481 $this->assertArrayHasKey($categories['A']->id
, array_flip($calendar->categories
));
482 $this->assertArrayHasKey($categories['A2']->id
, array_flip($calendar->categories
));
483 $this->assertArrayHasKey($categories['A2i']->id
, array_flip($calendar->categories
));
484 $this->assertArrayHasKey($categories['A2ii']->id
, array_flip($calendar->categories
));
485 $this->assertEquals(\context_coursecat
::instance($category->id
), $calendar->context
);
489 * Given a user has an enrolment in a single course.
490 * And I ask for the category information for a different category to the one my course is in.
491 * Then I should see that category.
492 * And I should see the category parents.
493 * And I should see the category descendants.
494 * And I should see the site.
495 * And I should see not see my course.
496 * And I should see no other courses.
497 * And I should see no categories.
499 public function test_site_visibility_single_course_different_category() {
500 $this->resetAfterTest();
501 list ($courses, $categories) = $this->mock_structure();
503 $generator = $this->getDataGenerator();
504 $user = $generator->create_user();
505 $course = $courses['A1.1'];
506 $category = \coursecat
::get($course->category
);
507 $wrongcategory = $categories['B1'];
508 $generator->enrol_user($user->id
, $course->id
);
510 $this->setUser($user);
512 // Viewing the category calendar for a category the user doesn't have any enrolments in.
513 // Should see that category, and all categories underneath it.
514 $calendar = \calendar_information
::create(time(), SITEID
, $wrongcategory->id
);
516 $this->assertCount(1, $calendar->courses
);
517 $this->assertCount(4, $calendar->categories
);
518 $this->assertEquals(SITEID
, $calendar->courseid
);
519 $this->assertArrayHasKey(SITEID
, array_flip($calendar->courses
));
520 $this->assertArrayHasKey($categories['B']->id
, array_flip($calendar->categories
));
521 $this->assertArrayHasKey($categories['B1']->id
, array_flip($calendar->categories
));
522 $this->assertArrayHasKey($categories['B1i']->id
, array_flip($calendar->categories
));
523 $this->assertArrayHasKey($categories['B1ii']->id
, array_flip($calendar->categories
));
524 $this->assertEquals(\context_coursecat
::instance($wrongcategory->id
), $calendar->context
);
528 * Given a user has an enrolment in two courses in the same category.
529 * And I ask for the site information.
530 * Then I should see the site.
531 * And I should see the course I am enrolled in.
532 * And I should see the category that my enrolled course is in.
533 * And I should see the parents of the category that my enrolled course is in.
535 public function test_site_visibility_two_courses_one_category_site() {
536 $this->resetAfterTest();
537 list ($courses, $categories) = $this->mock_structure();
539 $generator = $this->getDataGenerator();
540 $user = $generator->create_user();
541 $coursea = $courses['A.1'];
542 $courseb = $courses['A.2'];
543 $category = \coursecat
::get($coursea->category
);
544 $wrongcategory = $categories['B1'];
545 $generator->enrol_user($user->id
, $coursea->id
);
546 $generator->enrol_user($user->id
, $courseb->id
);
548 $this->setUser($user);
550 // Viewing the site azs a whole.
551 // Should see all courses that this user is enrolled in.
552 $calendar = \calendar_information
::create(time(), SITEID
, null);
554 $this->assertCount(3, $calendar->courses
);
555 $this->assertCount(1, $calendar->categories
);
556 $this->assertEquals(SITEID
, $calendar->courseid
);
557 $this->assertArrayHasKey(SITEID
, array_flip($calendar->courses
));
558 $this->assertArrayHasKey($coursea->id
, array_flip($calendar->courses
));
559 $this->assertArrayHasKey($courseb->id
, array_flip($calendar->courses
));
560 $this->assertEquals(\context_system
::instance(), $calendar->context
);
564 * Given a user has an enrolment in two courses in the same category.
565 * And I ask for the course information.
566 * Then I should see the site.
567 * And I should see that course.
568 * And I should see the category that my enrolled courses are in.
569 * And I should see the parents of the category that my enrolled course are in.
571 public function test_site_visibility_two_courses_one_category_course() {
572 $this->resetAfterTest();
573 list ($courses, $categories) = $this->mock_structure();
575 $generator = $this->getDataGenerator();
576 $user = $generator->create_user();
577 $coursea = $courses['A.1'];
578 $courseb = $courses['A.2'];
579 $category = \coursecat
::get($coursea->category
);
580 $wrongcategory = $categories['B1'];
581 $generator->enrol_user($user->id
, $coursea->id
);
582 $generator->enrol_user($user->id
, $courseb->id
);
584 $this->setUser($user);
587 // Viewing the course calendar.
588 // Should see all courses that this user is enrolled in.
589 $calendar = \calendar_information
::create($time, $coursea->id
, null);
591 $this->assertCount(2, $calendar->courses
);
592 $this->assertCount(1, $calendar->categories
);
593 $this->assertEquals($coursea->id
, $calendar->courseid
);
594 $this->assertArrayHasKey($coursea->id
, array_flip($calendar->courses
));
595 $this->assertArrayHasKey(SITEID
, array_flip($calendar->courses
));
596 $this->assertEquals(\context_course
::instance($coursea->id
), $calendar->context
);
598 // Viewing the course calendar while specifying the category too.
599 // The category is essentially ignored. No change expected.
600 $calendarwithcategory = \calendar_information
::create($time, $coursea->id
, $category->id
);
601 $this->assertEquals($calendar, $calendarwithcategory);
603 // Viewing the course calendar while specifying the wrong category.
604 // The category is essentially ignored. No change expected.
605 $calendarwithwrongcategory = \calendar_information
::create($time, $coursea->id
, $wrongcategory->id
);
606 $this->assertEquals($calendar, $calendarwithwrongcategory);
610 * Given a user has an enrolment in two courses in the same category.
611 * And I ask for the course information of the second course.
612 * Then I should see the site.
613 * And I should see that course.
614 * And I should see the category that my enrolled courses are in.
615 * And I should see the parents of the category that my enrolled course are in.
617 public function test_site_visibility_two_courses_one_category_courseb() {
618 $this->resetAfterTest();
619 list ($courses, $categories) = $this->mock_structure();
621 $generator = $this->getDataGenerator();
622 $user = $generator->create_user();
623 $coursea = $courses['A.1'];
624 $courseb = $courses['A.2'];
625 $category = \coursecat
::get($coursea->category
);
626 $wrongcategory = $categories['B1'];
627 $generator->enrol_user($user->id
, $coursea->id
);
628 $generator->enrol_user($user->id
, $courseb->id
);
630 $this->setUser($user);
633 // Viewing the other course calendar.
634 // Should see all courses that this user is enrolled in.
635 $calendar = \calendar_information
::create($time, $courseb->id
, null);
637 $this->assertCount(2, $calendar->courses
);
638 $this->assertCount(1, $calendar->categories
);
639 $this->assertEquals($courseb->id
, $calendar->courseid
);
640 $this->assertArrayHasKey($courseb->id
, array_flip($calendar->courses
));
641 $this->assertArrayNotHasKey($coursea->id
, array_flip($calendar->courses
));
642 $this->assertArrayHasKey(SITEID
, array_flip($calendar->courses
));
643 $this->assertEquals(\context_course
::instance($courseb->id
), $calendar->context
);
645 // Viewing the course calendar while specifying the category too.
646 // The category is essentially ignored. No change expected.
647 $calendarwithcategory = \calendar_information
::create($time, $courseb->id
, $category->id
);
648 $this->assertEquals($calendar, $calendarwithcategory);
650 // Viewing the course calendar while specifying the wrong category.
651 // The category is essentially ignored. No change expected.
652 $calendarwithcategory = \calendar_information
::create($time, $courseb->id
, $wrongcategory->id
);
653 $this->assertEquals($calendar, $calendarwithcategory);
657 * Given a user has an enrolment in two courses in the same category.
658 * And I ask for the category information.
659 * Then I should see the site.
660 * And I should see that course.
661 * And I should see the category that my enrolled courses are in.
662 * And I should see the parents of the category that my enrolled course are in.
664 public function test_site_visibility_two_courses_one_category_category() {
665 $this->resetAfterTest();
666 list ($courses, $categories) = $this->mock_structure();
668 $generator = $this->getDataGenerator();
669 $user = $generator->create_user();
670 $coursea = $courses['A.1'];
671 $courseb = $courses['A.2'];
672 $category = \coursecat
::get($coursea->category
);
673 $wrongcategory = $categories['B1'];
674 $generator->enrol_user($user->id
, $coursea->id
);
675 $generator->enrol_user($user->id
, $courseb->id
);
677 $this->setUser($user);
679 // Viewing the category calendar.
680 // Should see all courses that this user is enrolled in.
681 $calendar = \calendar_information
::create(time(), SITEID
, $category->id
);
683 $this->assertCount(3, $calendar->courses
);
684 $this->assertCount(7, $calendar->categories
);
685 $this->assertEquals(SITEID
, $calendar->courseid
);
686 $this->assertArrayHasKey($coursea->id
, array_flip($calendar->courses
));
687 $this->assertArrayHasKey($courseb->id
, array_flip($calendar->courses
));
688 $this->assertArrayHasKey(SITEID
, array_flip($calendar->courses
));
689 $this->assertEquals(\context_coursecat
::instance($category->id
), $calendar->context
);
693 * Given a user has an enrolment in two courses in the same category.
694 * And I ask for the categoy information of a different course.
695 * Then I should see the site.
696 * And I should see that course.
697 * And I should see the category that my enrolled courses are in.
698 * And I should see the parents of the category that my enrolled course are in.
700 public function test_site_visibility_two_courses_one_category_othercategory() {
701 $this->resetAfterTest();
702 list ($courses, $categories) = $this->mock_structure();
704 $generator = $this->getDataGenerator();
705 $user = $generator->create_user();
706 $coursea = $courses['A.1'];
707 $courseb = $courses['A.2'];
708 $category = \coursecat
::get($coursea->category
);
709 $wrongcategory = $categories['B1'];
710 $generator->enrol_user($user->id
, $coursea->id
);
711 $generator->enrol_user($user->id
, $courseb->id
);
713 $this->setUser($user);
715 // Viewing the category calendar for a category the user doesn't have any enrolments in.
716 // Should see that category, and all categories underneath it.
717 $calendar = \calendar_information
::create(time(), SITEID
, $wrongcategory->id
);
719 $this->assertCount(1, $calendar->courses
);
720 $this->assertCount(4, $calendar->categories
);
721 $this->assertEquals(SITEID
, $calendar->courseid
);
722 $this->assertArrayHasKey(SITEID
, array_flip($calendar->courses
));
723 $this->assertEquals(\context_coursecat
::instance($wrongcategory->id
), $calendar->context
);
727 * Given a user has an enrolment in two courses in the separate category.
728 * And I ask for the site informatino.
729 * Then I should see the site.
730 * And I should see both course.
731 * And I should see the categories that my enrolled courses are in.
732 * And I should see the parents of those categories.
734 public function test_site_visibility_two_courses_two_categories_site() {
735 $this->resetAfterTest();
736 list ($courses, $categories) = $this->mock_structure();
738 $generator = $this->getDataGenerator();
739 $user = $generator->create_user();
740 $coursea = $courses['A.1'];
741 $courseb = $courses['B.1'];
742 $categorya = \coursecat
::get($coursea->category
);
743 $categoryb = \coursecat
::get($courseb->category
);
744 $wrongcategory = $categories['C'];
745 $generator->enrol_user($user->id
, $coursea->id
);
746 $generator->enrol_user($user->id
, $courseb->id
);
748 $this->setUser($user);
750 // Viewing the site azs a whole.
751 // Should see all courses that this user is enrolled in.
752 $calendar = \calendar_information
::create(time(), SITEID
, null);
754 $this->assertCount(3, $calendar->courses
);
755 $this->assertCount(2, $calendar->categories
);
756 $this->assertEquals(SITEID
, $calendar->courseid
);
757 $this->assertArrayHasKey($coursea->id
, array_flip($calendar->courses
));
758 $this->assertArrayHasKey($courseb->id
, array_flip($calendar->courses
));
759 $this->assertArrayHasKey(SITEID
, array_flip($calendar->courses
));
760 $this->assertEquals(\context_system
::instance(), $calendar->context
);
764 * Given a user has an enrolment in two courses in the separate category.
765 * And I ask for the course information for one of those courses.
766 * Then I should see the site.
767 * And I should see one of the courses.
768 * And I should see the categories that my enrolled courses are in.
769 * And I should see the parents of those categories.
771 public function test_site_visibility_two_courses_two_categories_coursea() {
772 $this->resetAfterTest();
773 list ($courses, $categories) = $this->mock_structure();
775 $generator = $this->getDataGenerator();
776 $user = $generator->create_user();
777 $coursea = $courses['A.1'];
778 $courseb = $courses['B.1'];
779 $categorya = \coursecat
::get($coursea->category
);
780 $categoryb = \coursecat
::get($courseb->category
);
781 $wrongcategory = $categories['C'];
782 $generator->enrol_user($user->id
, $coursea->id
);
783 $generator->enrol_user($user->id
, $courseb->id
);
785 $this->setUser($user);
788 // Viewing the course calendar.
789 // Should see all courses that this user is enrolled in.
790 $calendar = \calendar_information
::create($time, $coursea->id
, null);
792 $this->assertCount(2, $calendar->courses
);
793 $this->assertCount(1, $calendar->categories
);
794 $this->assertEquals($coursea->id
, $calendar->courseid
);
795 $this->assertArrayHasKey($coursea->id
, array_flip($calendar->courses
));
796 $this->assertArrayHasKey(SITEID
, array_flip($calendar->courses
));
797 $this->assertEquals(\context_course
::instance($coursea->id
), $calendar->context
);
799 // Viewing the course calendar while specifying the categorya too.
800 // The categorya is essentially ignored. No change expected.
801 $calendarwithcategory = \calendar_information
::create($time, $coursea->id
, $categorya->id
);
802 $this->assertEquals($calendar, $calendarwithcategory);
804 // Viewing the course calendar while specifying the wrong categorya.
805 // The categorya is essentially ignored. No change expected.
806 $calendarwithwrongcategory = \calendar_information
::create($time, $coursea->id
, $wrongcategory->id
);
807 $this->assertEquals($calendar, $calendarwithwrongcategory);
811 * Given a user has an enrolment in two courses in the separate category.
812 * And I ask for the course information for the second of those courses.
813 * Then I should see the site.
814 * And I should see one of the courses.
815 * And I should see the categories that my enrolled courses are in.
816 * And I should see the parents of those categories.
818 public function test_site_visibility_two_courses_two_categories_courseb() {
819 $this->resetAfterTest();
820 list ($courses, $categories) = $this->mock_structure();
822 $generator = $this->getDataGenerator();
823 $user = $generator->create_user();
824 $coursea = $courses['A.1'];
825 $courseb = $courses['B.1'];
826 $categorya = \coursecat
::get($coursea->category
);
827 $categoryb = \coursecat
::get($courseb->category
);
828 $wrongcategory = $categories['C'];
829 $generator->enrol_user($user->id
, $coursea->id
);
830 $generator->enrol_user($user->id
, $courseb->id
);
832 $this->setUser($user);
835 // Viewing the other course calendar.
836 // Should see all courses that this user is enrolled in.
837 $calendar = \calendar_information
::create($time, $courseb->id
, null);
839 $this->assertCount(2, $calendar->courses
);
840 $this->assertCount(1, $calendar->categories
);
841 $this->assertEquals($courseb->id
, $calendar->courseid
);
842 $this->assertArrayHasKey($courseb->id
, array_flip($calendar->courses
));
843 $this->assertArrayNotHasKey($coursea->id
, array_flip($calendar->courses
));
844 $this->assertArrayHasKey(SITEID
, array_flip($calendar->courses
));
845 $this->assertEquals(\context_course
::instance($courseb->id
), $calendar->context
);
847 // Viewing the other course calendar while specifying the categorya too.
848 // The categorya is essentially ignored. No change expected.
849 $calendarwithcategory = \calendar_information
::create($time, $courseb->id
, $categoryb->id
);
850 $this->assertEquals($calendar, $calendarwithcategory);
852 // Viewing the other course calendar while specifying the wrong categorya.
853 // The categorya is essentially ignored. No change expected.
854 $calendarwithwrongcategory = \calendar_information
::create($time, $courseb->id
, $wrongcategory->id
);
855 $this->assertEquals($calendar, $calendarwithwrongcategory);
859 * Given a user has an enrolment in two courses in separate categories.
860 * And I ask for the category information.
861 * Then I should see the site.
862 * And I should see one of the courses.
863 * And I should see the categories that my enrolled courses are in.
864 * And I should see the parents of those categories.
866 public function test_site_visibility_two_courses_two_categories_category() {
867 $this->resetAfterTest();
868 list ($courses, $categories) = $this->mock_structure();
870 $generator = $this->getDataGenerator();
871 $user = $generator->create_user();
872 $coursea = $courses['A.1'];
873 $courseb = $courses['B.1'];
874 $categorya = \coursecat
::get($coursea->category
);
875 $categoryb = \coursecat
::get($courseb->category
);
876 $wrongcategory = $categories['C'];
877 $generator->enrol_user($user->id
, $coursea->id
);
878 $generator->enrol_user($user->id
, $courseb->id
);
880 $this->setUser($user);
882 // Viewing the categorya calendar.
883 // Should see all courses that this user is enrolled in.
884 $calendar = \calendar_information
::create(time(), SITEID
, $categorya->id
);
886 $this->assertCount(2, $calendar->courses
);
887 $this->assertCount(7, $calendar->categories
);
888 $this->assertEquals(SITEID
, $calendar->courseid
);
889 $this->assertArrayHasKey($coursea->id
, array_flip($calendar->courses
));
890 $this->assertArrayHasKey(SITEID
, array_flip($calendar->courses
));
891 $this->assertArrayNotHasKey($courseb->id
, array_flip($calendar->courses
));
892 $this->assertArrayHasKey($categories['A']->id
, array_flip($calendar->categories
));
893 $this->assertArrayHasKey($categories['A1']->id
, array_flip($calendar->categories
));
894 $this->assertArrayHasKey($categories['A1i']->id
, array_flip($calendar->categories
));
895 $this->assertArrayHasKey($categories['A1ii']->id
, array_flip($calendar->categories
));
896 $this->assertEquals(\context_coursecat
::instance($categorya->id
), $calendar->context
);
900 * Given a user has an enrolment in two courses in the separate category.
901 * And I ask for the category information of a different category.
902 * Then I should see the site.
903 * And I should see one of the courses.
904 * And I should see the categories that my enrolled courses are in.
905 * And I should see the parents of those categories.
907 public function test_site_visibility_two_courses_two_categories_different_category() {
908 $this->resetAfterTest();
909 list ($courses, $categories) = $this->mock_structure();
911 $generator = $this->getDataGenerator();
912 $user = $generator->create_user();
913 $coursea = $courses['A.1'];
914 $courseb = $courses['B.1'];
915 $categorya = \coursecat
::get($coursea->category
);
916 $categoryb = \coursecat
::get($courseb->category
);
917 $wrongcategory = $categories['C'];
918 $generator->enrol_user($user->id
, $coursea->id
);
919 $generator->enrol_user($user->id
, $courseb->id
);
921 $this->setUser($user);
922 // Viewing the categorya calendar for a categorya the user doesn't have any enrolments in.
923 // Should see that categorya, and all categories underneath it.
924 $calendar = \calendar_information
::create(time(), SITEID
, $wrongcategory->id
);
926 $this->assertCount(1, $calendar->courses
);
927 $this->assertCount(1, $calendar->categories
);
928 $this->assertEquals(SITEID
, $calendar->courseid
);
929 $this->assertArrayHasKey(SITEID
, array_flip($calendar->courses
));
930 $this->assertEquals(\context_coursecat
::instance($wrongcategory->id
), $calendar->context
);
934 * Given an admin user with no enrolments.
935 * And I ask for the site information.
936 * Then I should see the site.
937 * And I should see no other courses.
938 * And I should see no categories.
940 public function test_site_visibility_admin_user() {
941 $this->resetAfterTest();
942 list ($courses, $categories) = $this->mock_structure();
944 $generator = $this->getDataGenerator();
945 $this->setAdminUser();
947 $calendar = \calendar_information
::create(time(), SITEID
, null);
949 $this->assertCount(1, $calendar->courses
);
950 $this->assertCount(0, $calendar->categories
);
951 $this->assertEquals(SITEID
, $calendar->courseid
);
952 $this->assertEquals(SITEID
, reset($calendar->courses
));
953 $this->assertEquals(\context_system
::instance(), $calendar->context
);
957 * Given an admin user with a single enrolments.
958 * And I ask for the site information.
959 * Then I should see the site.
960 * And I should see the course I am enrolled in
961 * And I should see the category of that course.
962 * And I should see the parents of that course category.
963 * And I should see no other courses.
964 * And I should see no other categories.
966 public function test_site_visibility_admin_user_with_enrolment_site() {
969 $this->resetAfterTest();
970 list ($courses, $categories) = $this->mock_structure();
972 $generator = $this->getDataGenerator();
973 $course = $courses['A1.1'];
974 $category = \coursecat
::get($course->category
);
975 $this->setAdminUser();
976 $generator->enrol_user($USER->id
, $course->id
);
978 $calendar = \calendar_information
::create(time(), SITEID
, null);
980 $this->assertCount(2, $calendar->courses
);
981 $this->assertCount(2, $calendar->categories
);
982 $this->assertArrayHasKey(SITEID
, array_flip($calendar->courses
));
983 $this->assertArrayHasKey($course->id
, array_flip($calendar->courses
));
984 $this->assertArrayHasKey($categories['A']->id
, array_flip($calendar->categories
));
985 $this->assertArrayHasKey($categories['A1']->id
, array_flip($calendar->categories
));
986 $this->assertEquals(\context_system
::instance(), $calendar->context
);
990 * Given an admin user with a single enrolments.
991 * And I ask for the course information.
992 * Then I should see the site.
993 * And I should see the course I am enrolled in
994 * And I should see the category of that course.
995 * And I should see the parents of that course category.
997 public function test_site_visibility_admin_user_with_enrolment_course() {
1000 $this->resetAfterTest();
1001 list ($courses, $categories) = $this->mock_structure();
1003 $generator = $this->getDataGenerator();
1004 $course = $courses['A1.1'];
1005 $category = \coursecat
::get($course->category
);
1006 $wrongcategory = $categories['B1'];
1007 $this->setAdminUser();
1008 $generator->enrol_user($USER->id
, $course->id
);
1012 $calendar = \calendar_information
::create($time, $course->id
, null);
1014 $this->assertCount(2, $calendar->courses
);
1015 $this->assertCount(2, $calendar->categories
);
1016 $this->assertArrayHasKey(SITEID
, array_flip($calendar->courses
));
1017 $this->assertArrayHasKey($course->id
, array_flip($calendar->courses
));
1018 $this->assertArrayHasKey($categories['A']->id
, array_flip($calendar->categories
));
1019 $this->assertArrayHasKey($categories['A1']->id
, array_flip($calendar->categories
));
1020 $this->assertEquals(\context_course
::instance($course->id
), $calendar->context
);
1022 // Viewing the course calendar while specifying the category too.
1023 // The category is essentially ignored. No change expected.
1024 $calendarwithcategory = \calendar_information
::create($time, $course->id
, $category->id
);
1025 $this->assertEquals($calendar, $calendarwithcategory);
1027 // Viewing the course calendar while specifying the wrong category.
1028 // The category is essentially ignored. No change expected.
1029 $calendarwithwrongcategory = \calendar_information
::create($time, $course->id
, $wrongcategory->id
);
1030 $this->assertEquals($calendar, $calendarwithwrongcategory);
1034 * Given an admin user with a single enrolments.
1035 * And I ask for the category information for the category my course is in.
1036 * Then I should see that category.
1037 * And I should see the category parents.
1038 * And I should see the category descendants.
1039 * And I should see the site.
1040 * And I should see my course.
1041 * And I should see no other courses.
1042 * And I should see no categories.
1044 public function test_site_visibility_admin_user_with_enrolment_category() {
1047 $this->resetAfterTest();
1048 list ($courses, $categories) = $this->mock_structure();
1050 $generator = $this->getDataGenerator();
1051 $course = $courses['A1.1'];
1052 $category = \coursecat
::get($course->category
);
1053 $wrongcategory = $categories['B1'];
1054 $this->setAdminUser();
1055 $generator->enrol_user($USER->id
, $course->id
);
1057 $calendar = \calendar_information
::create(time(), SITEID
, $category->id
);
1059 $this->assertCount(2, $calendar->courses
);
1060 $this->assertCount(4, $calendar->categories
);
1061 $this->assertArrayHasKey(SITEID
, array_flip($calendar->courses
));
1062 $this->assertArrayHasKey($course->id
, array_flip($calendar->courses
));
1063 $this->assertArrayHasKey($categories['A']->id
, array_flip($calendar->categories
));
1064 $this->assertArrayHasKey($categories['A1']->id
, array_flip($calendar->categories
));
1065 $this->assertArrayHasKey($categories['A1i']->id
, array_flip($calendar->categories
));
1066 $this->assertArrayHasKey($categories['A1ii']->id
, array_flip($calendar->categories
));
1067 $this->assertEquals(\context_coursecat
::instance($category->id
), $calendar->context
);