MDL-67585 core_course: add hook get_all_content_items
[moodle.git] / course / tests / services_content_item_service_test.php
CommitLineData
dd494a41
JD
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Contains the tests for the content_item_service class.
19 *
20 * @package core
21 * @subpackage course
22 * @copyright 2020 Jake Dallimore <jrhdallimore@gmail.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25namespace tests\course;
26
27defined('MOODLE_INTERNAL') || die();
28
29use \core_course\local\service\content_item_service;
30use \core_course\local\repository\content_item_readonly_repository;
31
32/**
33 * The tests for the content_item_service class.
34 *
35 * @copyright 2020 Jake Dallimore <jrhdallimore@gmail.com>
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 */
38class services_content_item_service_testcase extends \advanced_testcase {
39
40 /**
41 * Test confirming that content items are returned by the service.
42 */
43 public function test_get_content_items_for_user_in_course_basic() {
44 $this->resetAfterTest();
45
46 // Create a user in a course.
47 $course = $this->getDataGenerator()->create_course();
48 $user = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
49
50 $cis = new content_item_service(new content_item_readonly_repository());
51 $contentitems = $cis->get_content_items_for_user_in_course($user, $course);
52
53 foreach ($contentitems as $key => $contentitem) {
54 $this->assertObjectHasAttribute('id', $contentitem);
55 $this->assertObjectHasAttribute('name', $contentitem);
56 $this->assertObjectHasAttribute('title', $contentitem);
57 $this->assertObjectHasAttribute('link', $contentitem);
58 $this->assertObjectHasAttribute('icon', $contentitem);
59 $this->assertObjectHasAttribute('help', $contentitem);
60 $this->assertObjectHasAttribute('archetype', $contentitem);
61 $this->assertObjectHasAttribute('componentname', $contentitem);
62 }
63 }
64
65 /**
66 * Test confirming that access control is performed when asking the service to return content items for a user in a course.
67 */
68 public function test_get_content_items_for_user_in_course_permissions() {
69 $this->resetAfterTest();
70 global $DB;
71
72 // Create a user in a course.
73 $course = $this->getDataGenerator()->create_course();
74 $user = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
75
76 // No cap override, so assign should be returned.
77 $cis = new content_item_service(new content_item_readonly_repository());
78 $contentitems = $cis->get_content_items_for_user_in_course($user, $course);
79 $this->assertContains('assign', array_column($contentitems, 'name'));
80
81 // Override the capability 'mod/assign:addinstance' for the 'editing teacher' role.
82 $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
83 assign_capability('mod/assign:addinstance', CAP_PROHIBIT, $teacherrole->id, \context_course::instance($course->id));
84
85 $contentitems = $cis->get_content_items_for_user_in_course($user, $course);
86 $this->assertArrayNotHasKey('assign', $contentitems);
87 }
88
89 /**
90 * Test confirming that params can be added to the content item's link.
91 */
92 public function test_get_content_item_for_user_in_course_link_params() {
93 $this->resetAfterTest();
94
95 // Create a user in a course.
96 $course = $this->getDataGenerator()->create_course();
97 $user = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
98
99 $cis = new content_item_service(new content_item_readonly_repository());
100 $contentitems = $cis->get_content_items_for_user_in_course($user, $course, ['sr' => 7]);
101
102 foreach ($contentitems as $item) {
103 $this->assertStringContainsString('sr=7', $item->link);
104 }
105 }
57dfcf95
JD
106
107 /**
108 * Test confirming that all content items can be fetched irrespective of permissions.
109 */
110 public function test_get_all_content_items() {
111 $this->resetAfterTest();
112 global $DB;
113
114 // Create a user in a course.
115 $course = $this->getDataGenerator()->create_course();
116 $user = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
117
118 $cis = new content_item_service(new content_item_readonly_repository());
119 $allcontentitems = $cis->get_all_content_items();
120 $coursecontentitems = $cis->get_content_items_for_user_in_course($user, $course);
121
122 // The call to get_all_content_items() should return the same items as for the course,
123 // given the user in an editing teacher and can add manual lti instances.
124 $this->assertEquals(array_column($allcontentitems, 'name'), array_column($coursecontentitems, 'name'));
125
126 // Now removing the cap 'mod/lti:addinstance'. This will restrict those items returned by the course-specific method.
127 $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
128 assign_capability('mod/lti:addinstance', CAP_PROHIBIT, $teacherrole->id, \context_course::instance($course->id));
129
130 // Verify that all items, including lti, are still returned by the get_all_content_items() call.
131 $allcontentitems = $cis->get_all_content_items();
132 $coursecontentitems = $cis->get_content_items_for_user_in_course($user, $course);
133 $this->assertNotContains('lti', array_column($coursecontentitems, 'name'));
134 $this->assertContains('lti', array_column($allcontentitems, 'name'));
135 }
dd494a41 136}