571ee2a2dc71cf320776ba4d3fdfd42a805027b4
[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     public function test_url_core_calendar_provide_event_action() {
120         $this->resetAfterTest();
121         $this->setAdminUser();
123         // Create the activity.
124         $course = $this->getDataGenerator()->create_course();
125         $url = $this->getDataGenerator()->create_module('url', array('course' => $course->id));
127         // Create a calendar event.
128         $event = $this->create_action_event($course->id, $url->id,
129             \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
131         // Create an action factory.
132         $factory = new \core_calendar\action_factory();
134         // Decorate action event.
135         $actionevent = mod_url_core_calendar_provide_event_action($event, $factory);
137         // Confirm the event was decorated.
138         $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
139         $this->assertEquals(get_string('view'), $actionevent->get_name());
140         $this->assertInstanceOf('moodle_url', $actionevent->get_url());
141         $this->assertEquals(1, $actionevent->get_item_count());
142         $this->assertTrue($actionevent->is_actionable());
143     }
145     public function test_url_core_calendar_provide_event_action_already_completed() {
146         global $CFG;
148         $this->resetAfterTest();
149         $this->setAdminUser();
151         $CFG->enablecompletion = 1;
153         // Create the activity.
154         $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
155         $url = $this->getDataGenerator()->create_module('url', array('course' => $course->id),
156             array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
158         // Get some additional data.
159         $cm = get_coursemodule_from_instance('url', $url->id);
161         // Create a calendar event.
162         $event = $this->create_action_event($course->id, $url->id,
163             \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
165         // Mark the activity as completed.
166         $completion = new completion_info($course);
167         $completion->set_module_viewed($cm);
169         // Create an action factory.
170         $factory = new \core_calendar\action_factory();
172         // Decorate action event.
173         $actionevent = mod_url_core_calendar_provide_event_action($event, $factory);
175         // Ensure result was null.
176         $this->assertNull($actionevent);
177     }
179     /**
180      * Creates an action event.
181      *
182      * @param int $courseid The course id.
183      * @param int $instanceid The instance id.
184      * @param string $eventtype The event type.
185      * @return bool|calendar_event
186      */
187     private function create_action_event($courseid, $instanceid, $eventtype) {
188         $event = new stdClass();
189         $event->name = 'Calendar event';
190         $event->modulename  = 'url';
191         $event->courseid = $courseid;
192         $event->instance = $instanceid;
193         $event->type = CALENDAR_EVENT_TYPE_ACTION;
194         $event->eventtype = $eventtype;
195         $event->timestart = time();
197         return calendar_event::create($event);
198     }