Commit | Line | Data |
---|---|---|
6c83e659 MN |
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 | * Unit tests for the activity label's lib. | |
19 | * | |
20 | * @package mod_label | |
21 | * @category test | |
22 | * @copyright 2017 Mark Nelson <markn@moodle.com> | |
23 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
24 | */ | |
25 | ||
26 | defined('MOODLE_INTERNAL') || die(); | |
27 | ||
28 | ||
29 | /** | |
30 | * Unit tests for the activity label's lib. | |
31 | * | |
32 | * @package mod_label | |
33 | * @category test | |
34 | * @copyright 2017 Mark Nelson <markn@moodle.com> | |
35 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
36 | */ | |
37 | class mod_label_lib_testcase extends advanced_testcase { | |
38 | ||
39 | /** | |
40 | * Set up. | |
41 | */ | |
42 | public function setUp() { | |
43 | $this->resetAfterTest(); | |
44 | $this->setAdminUser(); | |
45 | } | |
46 | ||
47 | public function test_label_core_calendar_provide_event_action() { | |
48 | // Create the activity. | |
49 | $course = $this->getDataGenerator()->create_course(); | |
50 | $label = $this->getDataGenerator()->create_module('label', array('course' => $course->id)); | |
51 | ||
52 | // Create a calendar event. | |
53 | $event = $this->create_action_event($course->id, $label->id, | |
54 | \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED); | |
55 | ||
56 | // Create an action factory. | |
57 | $factory = new \core_calendar\action_factory(); | |
58 | ||
59 | // Decorate action event. | |
60 | $actionevent = mod_label_core_calendar_provide_event_action($event, $factory); | |
61 | ||
62 | // Confirm the event was decorated. | |
63 | $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent); | |
64 | $this->assertEquals(get_string('view'), $actionevent->get_name()); | |
65 | $this->assertInstanceOf('moodle_url', $actionevent->get_url()); | |
66 | $this->assertEquals(1, $actionevent->get_item_count()); | |
67 | $this->assertTrue($actionevent->is_actionable()); | |
68 | } | |
69 | ||
cbeb3995 SR |
70 | public function test_label_core_calendar_provide_event_action_as_non_user() { |
71 | global $CFG; | |
72 | ||
73 | // Create the activity. | |
74 | $course = $this->getDataGenerator()->create_course(); | |
75 | $label = $this->getDataGenerator()->create_module('label', array('course' => $course->id)); | |
76 | ||
77 | // Create a calendar event. | |
78 | $event = $this->create_action_event($course->id, $label->id, | |
79 | \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED); | |
80 | ||
81 | // Now log out. | |
82 | $CFG->forcelogin = true; // We don't want to be logged in as guest, as guest users might still have some capabilities. | |
83 | $this->setUser(); | |
84 | ||
85 | // Create an action factory. | |
86 | $factory = new \core_calendar\action_factory(); | |
87 | ||
88 | // Decorate action event. | |
89 | $actionevent = mod_label_core_calendar_provide_event_action($event, $factory); | |
90 | ||
91 | // Confirm the event is not shown at all. | |
92 | $this->assertNull($actionevent); | |
93 | } | |
94 | ||
95 | public function test_label_core_calendar_provide_event_action_in_hidden_section() { | |
96 | // Create the activity. | |
97 | $course = $this->getDataGenerator()->create_course(); | |
98 | $label = $this->getDataGenerator()->create_module('label', array('course' => $course->id)); | |
99 | ||
100 | // Create a student. | |
101 | $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); | |
102 | ||
103 | // Create a calendar event. | |
104 | $event = $this->create_action_event($course->id, $label->id, | |
105 | \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED); | |
106 | ||
107 | // Set sections 0 as hidden. | |
108 | set_section_visible($course->id, 0, 0); | |
109 | ||
110 | // Create an action factory. | |
111 | $factory = new \core_calendar\action_factory(); | |
112 | ||
113 | // Decorate action event for the student. | |
114 | $actionevent = mod_label_core_calendar_provide_event_action($event, $factory, $student->id); | |
115 | ||
116 | // Confirm the event is not shown at all. | |
117 | $this->assertNull($actionevent); | |
118 | } | |
119 | ||
a7da4c5f | 120 | public function test_label_core_calendar_provide_event_action_for_user() { |
cbeb3995 SR |
121 | global $CFG; |
122 | ||
a7da4c5f SR |
123 | // Create the activity. |
124 | $course = $this->getDataGenerator()->create_course(); | |
125 | $label = $this->getDataGenerator()->create_module('label', array('course' => $course->id)); | |
126 | ||
127 | // Enrol a student in the course. | |
128 | $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); | |
129 | ||
130 | // Create a calendar event. | |
131 | $event = $this->create_action_event($course->id, $label->id, | |
132 | \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED); | |
133 | ||
134 | // Now, log out. | |
cbeb3995 | 135 | $CFG->forcelogin = true; // We don't want to be logged in as guest, as guest users might still have some capabilities. |
a7da4c5f SR |
136 | $this->setUser(); |
137 | ||
138 | // Create an action factory. | |
139 | $factory = new \core_calendar\action_factory(); | |
140 | ||
141 | // Decorate action event for the student. | |
142 | $actionevent = mod_label_core_calendar_provide_event_action($event, $factory, $student->id); | |
143 | ||
144 | // Confirm the event was decorated. | |
145 | $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent); | |
146 | $this->assertEquals(get_string('view'), $actionevent->get_name()); | |
147 | $this->assertInstanceOf('moodle_url', $actionevent->get_url()); | |
148 | $this->assertEquals(1, $actionevent->get_item_count()); | |
149 | $this->assertTrue($actionevent->is_actionable()); | |
150 | } | |
151 | ||
6c83e659 MN |
152 | public function test_label_core_calendar_provide_event_action_already_completed() { |
153 | global $CFG; | |
154 | ||
155 | $CFG->enablecompletion = 1; | |
156 | ||
157 | // Create the activity. | |
158 | $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1)); | |
159 | $label = $this->getDataGenerator()->create_module('label', array('course' => $course->id), | |
160 | array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS)); | |
161 | ||
162 | // Get some additional data. | |
163 | $cm = get_coursemodule_from_instance('label', $label->id); | |
164 | ||
165 | // Create a calendar event. | |
166 | $event = $this->create_action_event($course->id, $label->id, | |
167 | \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED); | |
168 | ||
169 | // Mark the activity as completed. | |
170 | $completion = new completion_info($course); | |
171 | $completion->set_module_viewed($cm); | |
172 | ||
173 | // Create an action factory. | |
174 | $factory = new \core_calendar\action_factory(); | |
175 | ||
176 | // Decorate action event. | |
177 | $actionevent = mod_label_core_calendar_provide_event_action($event, $factory); | |
178 | ||
179 | // Ensure result was null. | |
180 | $this->assertNull($actionevent); | |
181 | } | |
182 | ||
a7da4c5f SR |
183 | public function test_label_core_calendar_provide_event_action_already_completed_for_user() { |
184 | global $CFG; | |
185 | ||
186 | $CFG->enablecompletion = 1; | |
187 | ||
188 | // Create the activity. | |
189 | $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1)); | |
190 | $label = $this->getDataGenerator()->create_module('label', array('course' => $course->id), | |
191 | array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS)); | |
192 | ||
193 | // Enrol a student in the course. | |
194 | $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); | |
195 | ||
196 | // Get some additional data. | |
197 | $cm = get_coursemodule_from_instance('label', $label->id); | |
198 | ||
199 | // Create a calendar event. | |
200 | $event = $this->create_action_event($course->id, $label->id, | |
201 | \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED); | |
202 | ||
203 | // Mark the activity as completed for the student. | |
204 | $completion = new completion_info($course); | |
205 | $completion->set_module_viewed($cm, $student->id); | |
206 | ||
207 | // Create an action factory. | |
208 | $factory = new \core_calendar\action_factory(); | |
209 | ||
210 | // Decorate action event for the student. | |
211 | $actionevent = mod_label_core_calendar_provide_event_action($event, $factory, $student->id); | |
212 | ||
213 | // Ensure result was null. | |
214 | $this->assertNull($actionevent); | |
215 | } | |
216 | ||
6c83e659 MN |
217 | /** |
218 | * Creates an action event. | |
219 | * | |
220 | * @param int $courseid The course id. | |
221 | * @param int $instanceid The instance id. | |
222 | * @param string $eventtype The event type. | |
e1cd93ce | 223 | * @return bool|calendar_event |
6c83e659 MN |
224 | */ |
225 | private function create_action_event($courseid, $instanceid, $eventtype) { | |
226 | $event = new stdClass(); | |
227 | $event->name = 'Calendar event'; | |
228 | $event->modulename = 'label'; | |
229 | $event->courseid = $courseid; | |
230 | $event->instance = $instanceid; | |
231 | $event->type = CALENDAR_EVENT_TYPE_ACTION; | |
232 | $event->eventtype = $eventtype; | |
233 | $event->timestart = time(); | |
234 | ||
e1cd93ce | 235 | return calendar_event::create($event); |
6c83e659 MN |
236 | } |
237 | } |