MDL-63150 mod_url: Accept a userid override in event action
[moodle.git] / mod / url / tests / lib_test.php
CommitLineData
5bd40408
PS
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 some mod URL lib stuff.
19 *
20 * @package mod_url
21 * @category phpunit
22 * @copyright 2012 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28
29/**
30 * mod_url tests
31 *
32 * @package mod_url
33 * @category phpunit
34 * @copyright 2011 Petr Skoda {@link http://skodak.org}
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
f3fe1b8b 37class mod_url_lib_testcase extends advanced_testcase {
5bd40408 38
7aea08e1
SH
39 /**
40 * Prepares things before this test case is initialised
41 * @return void
42 */
5bd40408
PS
43 public static function setUpBeforeClass() {
44 global $CFG;
f3fe1b8b 45 require_once($CFG->dirroot . '/mod/url/lib.php');
5bd40408
PS
46 require_once($CFG->dirroot . '/mod/url/locallib.php');
47 }
48
7aea08e1
SH
49 /**
50 * Tests the url_appears_valid_url function
51 * @return void
52 */
5bd40408
PS
53 public function test_url_appears_valid_url() {
54 $this->assertTrue(url_appears_valid_url('http://example'));
55 $this->assertTrue(url_appears_valid_url('http://www.example.com'));
56 $this->assertTrue(url_appears_valid_url('http://www.exa-mple2.com'));
57 $this->assertTrue(url_appears_valid_url('http://www.example.com/~nobody/index.html'));
58 $this->assertTrue(url_appears_valid_url('http://www.example.com#hmm'));
59 $this->assertTrue(url_appears_valid_url('http://www.example.com/#hmm'));
60 $this->assertTrue(url_appears_valid_url('http://www.example.com/žlutý koníček/lala.txt'));
61 $this->assertTrue(url_appears_valid_url('http://www.example.com/žlutý koníček/lala.txt#hmmmm'));
62 $this->assertTrue(url_appears_valid_url('http://www.example.com/index.php?xx=yy&zz=aa'));
63 $this->assertTrue(url_appears_valid_url('https://user:password@www.example.com/žlutý koníček/lala.txt'));
64 $this->assertTrue(url_appears_valid_url('ftp://user:password@www.example.com/žlutý koníček/lala.txt'));
65
66 $this->assertFalse(url_appears_valid_url('http:example.com'));
67 $this->assertFalse(url_appears_valid_url('http:/example.com'));
68 $this->assertFalse(url_appears_valid_url('http://'));
69 $this->assertFalse(url_appears_valid_url('http://www.exa mple.com'));
70 $this->assertFalse(url_appears_valid_url('http://www.examplé.com'));
71 $this->assertFalse(url_appears_valid_url('http://@www.example.com'));
72 $this->assertFalse(url_appears_valid_url('http://user:@www.example.com'));
73
74 $this->assertTrue(url_appears_valid_url('lalala://@:@/'));
75 }
f3fe1b8b
JL
76
77 /**
78 * Test url_view
79 * @return void
80 */
81 public function test_url_view() {
82 global $CFG;
83
84 $CFG->enablecompletion = 1;
85 $this->resetAfterTest();
86
87 // Setup test data.
88 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
89 $url = $this->getDataGenerator()->create_module('url', array('course' => $course->id),
90 array('completion' => 2, 'completionview' => 1));
91 $context = context_module::instance($url->cmid);
92 $cm = get_coursemodule_from_instance('url', $url->id);
93
94 // Trigger and capture the event.
95 $sink = $this->redirectEvents();
96
97 $this->setAdminUser();
98 url_view($url, $course, $cm, $context);
99
100 $events = $sink->get_events();
101 // 2 additional events thanks to completion.
102 $this->assertCount(3, $events);
103 $event = array_shift($events);
104
105 // Checking that the event contains the expected values.
106 $this->assertInstanceOf('\mod_url\event\course_module_viewed', $event);
107 $this->assertEquals($context, $event->get_context());
108 $url = new \moodle_url('/mod/url/view.php', array('id' => $cm->id));
109 $this->assertEquals($url, $event->get_url());
110 $this->assertEventContextNotUsed($event);
111 $this->assertNotEmpty($event->get_name());
112
113 // Check completion status.
114 $completion = new completion_info($course);
115 $completiondata = $completion->get_data($cm);
116 $this->assertEquals(1, $completiondata->completionstate);
6c83e659
MN
117 }
118
dcb7662e
P
119 /**
120 * Test mod_url_core_calendar_provide_event_action with user override
121 */
122 public function test_url_core_calendar_provide_event_action_user_override() {
123 global $CFG, $USER;
124
125 $this->resetAfterTest();
126 $this->setAdminUser();
127 $user = $this->getDataGenerator()->create_user();
128 $CFG->enablecompletion = 1;
129
130 // Create the activity.
131 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
132 $url = $this->getDataGenerator()->create_module('url', array('course' => $course->id),
133 array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
134
135 // Get some additional data.
136 $cm = get_coursemodule_from_instance('url', $url->id);
137
138 // Create a calendar event.
139 $event = $this->create_action_event($course->id, $url->id,
140 \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
141
142 // Mark the activity as completed.
143 $completion = new completion_info($course);
144 $completion->set_module_viewed($cm);
145
146 // Create an action factory.
147 $factory = new \core_calendar\action_factory();
148
149 // Decorate action event.
150 $actionevent = mod_url_core_calendar_provide_event_action($event, $factory, $USER->id);
151
152 // Decorate action with a userid override.
153 $actionevent2 = mod_url_core_calendar_provide_event_action($event, $factory, $user->id);
154
155 // Ensure result was null because it has been marked as completed for the associated user.
156 // Logic was brought across from the "_already_completed" function.
157 $this->assertNull($actionevent);
158
159 // Confirm the event was decorated.
160 $this->assertNotNull($actionevent2);
161 $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent2);
162 $this->assertEquals(get_string('view'), $actionevent2->get_name());
163 $this->assertInstanceOf('moodle_url', $actionevent2->get_url());
164 $this->assertEquals(1, $actionevent2->get_item_count());
165 $this->assertTrue($actionevent2->is_actionable());
166 }
167
6c83e659
MN
168 public function test_url_core_calendar_provide_event_action() {
169 $this->resetAfterTest();
170 $this->setAdminUser();
171
172 // Create the activity.
173 $course = $this->getDataGenerator()->create_course();
174 $url = $this->getDataGenerator()->create_module('url', array('course' => $course->id));
175
176 // Create a calendar event.
177 $event = $this->create_action_event($course->id, $url->id,
178 \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
179
180 // Create an action factory.
181 $factory = new \core_calendar\action_factory();
182
183 // Decorate action event.
184 $actionevent = mod_url_core_calendar_provide_event_action($event, $factory);
f3fe1b8b 185
6c83e659
MN
186 // Confirm the event was decorated.
187 $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
188 $this->assertEquals(get_string('view'), $actionevent->get_name());
189 $this->assertInstanceOf('moodle_url', $actionevent->get_url());
190 $this->assertEquals(1, $actionevent->get_item_count());
191 $this->assertTrue($actionevent->is_actionable());
192 }
193
194 public function test_url_core_calendar_provide_event_action_already_completed() {
195 global $CFG;
196
197 $this->resetAfterTest();
198 $this->setAdminUser();
199
200 $CFG->enablecompletion = 1;
201
202 // Create the activity.
203 $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
204 $url = $this->getDataGenerator()->create_module('url', array('course' => $course->id),
205 array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
206
207 // Get some additional data.
208 $cm = get_coursemodule_from_instance('url', $url->id);
209
210 // Create a calendar event.
211 $event = $this->create_action_event($course->id, $url->id,
212 \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
213
214 // Mark the activity as completed.
215 $completion = new completion_info($course);
216 $completion->set_module_viewed($cm);
217
218 // Create an action factory.
219 $factory = new \core_calendar\action_factory();
220
221 // Decorate action event.
222 $actionevent = mod_url_core_calendar_provide_event_action($event, $factory);
223
224 // Ensure result was null.
225 $this->assertNull($actionevent);
226 }
227
228 /**
229 * Creates an action event.
230 *
231 * @param int $courseid The course id.
232 * @param int $instanceid The instance id.
233 * @param string $eventtype The event type.
e1cd93ce 234 * @return bool|calendar_event
6c83e659
MN
235 */
236 private function create_action_event($courseid, $instanceid, $eventtype) {
237 $event = new stdClass();
238 $event->name = 'Calendar event';
239 $event->modulename = 'url';
240 $event->courseid = $courseid;
241 $event->instance = $instanceid;
242 $event->type = CALENDAR_EVENT_TYPE_ACTION;
243 $event->eventtype = $eventtype;
244 $event->timestart = time();
245
e1cd93ce 246 return calendar_event::create($event);
f3fe1b8b 247 }
5bd40408 248}