76e6d65bff6ce63862f0c6f833c6526f7e4542d1
[moodle.git] / mod / url / tests / lib_test.php
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/>.
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  */
26 defined('MOODLE_INTERNAL') || die();
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  */
37 class mod_url_lib_testcase extends advanced_testcase {
39     /**
40      * Prepares things before this test case is initialised
41      * @return void
42      */
43     public static function setUpBeforeClass() {
44         global $CFG;
45         require_once($CFG->dirroot . '/mod/url/lib.php');
46         require_once($CFG->dirroot . '/mod/url/locallib.php');
47     }
49     /**
50      * Tests the url_appears_valid_url function
51      * @return void
52      */
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'));
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'));
74         $this->assertTrue(url_appears_valid_url('lalala://@:@/'));
75     }
77     /**
78      * Test url_view
79      * @return void
80      */
81     public function test_url_view() {
82         global $CFG;
84         $CFG->enablecompletion = 1;
85         $this->resetAfterTest();
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);
94         // Trigger and capture the event.
95         $sink = $this->redirectEvents();
97         $this->setAdminUser();
98         url_view($url, $course, $cm, $context);
100         $events = $sink->get_events();
101         // 2 additional events thanks to completion.
102         $this->assertCount(3, $events);
103         $event = array_shift($events);
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());
113         // Check completion status.
114         $completion = new completion_info($course);
115         $completiondata = $completion->get_data($cm);
116         $this->assertEquals(1, $completiondata->completionstate);
117     }
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;
125         $this->resetAfterTest();
126         $this->setAdminUser();
127         $user = $this->getDataGenerator()->create_user();
128         $CFG->enablecompletion = 1;
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));
135         // Get some additional data.
136         $cm = get_coursemodule_from_instance('url', $url->id);
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);
142         // Mark the activity as completed.
143         $completion = new completion_info($course);
144         $completion->set_module_viewed($cm);
146         // Create an action factory.
147         $factory = new \core_calendar\action_factory();
149         // Decorate action event.
150         $actionevent = mod_url_core_calendar_provide_event_action($event, $factory, $USER->id);
152         // Decorate action with a userid override.
153         $actionevent2 = mod_url_core_calendar_provide_event_action($event, $factory, $user->id);
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);
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     }
168     public function test_url_core_calendar_provide_event_action() {
169         $this->resetAfterTest();
170         $this->setAdminUser();
172         // Create the activity.
173         $course = $this->getDataGenerator()->create_course();
174         $url = $this->getDataGenerator()->create_module('url', array('course' => $course->id));
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);
180         // Create an action factory.
181         $factory = new \core_calendar\action_factory();
183         // Decorate action event.
184         $actionevent = mod_url_core_calendar_provide_event_action($event, $factory);
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     }
194     public function test_url_core_calendar_provide_event_action_already_completed() {
195         global $CFG;
197         $this->resetAfterTest();
198         $this->setAdminUser();
200         $CFG->enablecompletion = 1;
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));
207         // Get some additional data.
208         $cm = get_coursemodule_from_instance('url', $url->id);
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);
214         // Mark the activity as completed.
215         $completion = new completion_info($course);
216         $completion->set_module_viewed($cm);
218         // Create an action factory.
219         $factory = new \core_calendar\action_factory();
221         // Decorate action event.
222         $actionevent = mod_url_core_calendar_provide_event_action($event, $factory);
224         // Ensure result was null.
225         $this->assertNull($actionevent);
226     }
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.
234      * @return bool|calendar_event
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();
246         return calendar_event::create($event);
247     }