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 the activity label's lib.
22 * @copyright 2017 Mark Nelson <markn@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
30 * Unit tests for the activity label's lib.
34 * @copyright 2017 Mark Nelson <markn@moodle.com>
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class mod_label_lib_testcase
extends advanced_testcase
{
42 public function setUp() {
43 $this->resetAfterTest();
44 $this->setAdminUser();
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
));
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
);
56 // Create an action factory.
57 $factory = new \core_calendar\action_factory
();
59 // Decorate action event.
60 $actionevent = mod_label_core_calendar_provide_event_action($event, $factory);
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());
70 public function test_label_core_calendar_provide_event_action_as_non_user() {
73 // Create the activity.
74 $course = $this->getDataGenerator()->create_course();
75 $label = $this->getDataGenerator()->create_module('label', array('course' => $course->id
));
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
);
82 $CFG->forcelogin
= true; // We don't want to be logged in as guest, as guest users might still have some capabilities.
85 // Create an action factory.
86 $factory = new \core_calendar\action_factory
();
88 // Decorate action event.
89 $actionevent = mod_label_core_calendar_provide_event_action($event, $factory);
91 // Confirm the event is not shown at all.
92 $this->assertNull($actionevent);
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
));
101 $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
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
);
107 // Set sections 0 as hidden.
108 set_section_visible($course->id
, 0, 0);
110 // Create an action factory.
111 $factory = new \core_calendar\action_factory
();
113 // Decorate action event for the student.
114 $actionevent = mod_label_core_calendar_provide_event_action($event, $factory, $student->id
);
116 // Confirm the event is not shown at all.
117 $this->assertNull($actionevent);
120 public function test_label_core_calendar_provide_event_action_for_user() {
123 // Create the activity.
124 $course = $this->getDataGenerator()->create_course();
125 $label = $this->getDataGenerator()->create_module('label', array('course' => $course->id
));
127 // Enrol a student in the course.
128 $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
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
);
135 $CFG->forcelogin
= true; // We don't want to be logged in as guest, as guest users might still have some capabilities.
138 // Create an action factory.
139 $factory = new \core_calendar\action_factory
();
141 // Decorate action event for the student.
142 $actionevent = mod_label_core_calendar_provide_event_action($event, $factory, $student->id
);
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());
152 public function test_label_core_calendar_provide_event_action_already_completed() {
155 $CFG->enablecompletion
= 1;
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
));
162 // Get some additional data.
163 $cm = get_coursemodule_from_instance('label', $label->id
);
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
);
169 // Mark the activity as completed.
170 $completion = new completion_info($course);
171 $completion->set_module_viewed($cm);
173 // Create an action factory.
174 $factory = new \core_calendar\action_factory
();
176 // Decorate action event.
177 $actionevent = mod_label_core_calendar_provide_event_action($event, $factory);
179 // Ensure result was null.
180 $this->assertNull($actionevent);
183 public function test_label_core_calendar_provide_event_action_already_completed_for_user() {
186 $CFG->enablecompletion
= 1;
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
));
193 // Enrol a student in the course.
194 $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
196 // Get some additional data.
197 $cm = get_coursemodule_from_instance('label', $label->id
);
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
);
203 // Mark the activity as completed for the student.
204 $completion = new completion_info($course);
205 $completion->set_module_viewed($cm, $student->id
);
207 // Create an action factory.
208 $factory = new \core_calendar\action_factory
();
210 // Decorate action event for the student.
211 $actionevent = mod_label_core_calendar_provide_event_action($event, $factory, $student->id
);
213 // Ensure result was null.
214 $this->assertNull($actionevent);
218 * Creates an action event.
220 * @param int $courseid The course id.
221 * @param int $instanceid The instance id.
222 * @param string $eventtype The event type.
223 * @return bool|calendar_event
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();
235 return calendar_event
::create($event);