Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
namespace mod_url;
26
 
27
defined('MOODLE_INTERNAL') || die();
28
 
29
 
30
/**
31
 * mod_url tests
32
 *
33
 * @package    mod_url
34
 * @category   phpunit
35
 * @copyright  2011 Petr Skoda {@link http://skodak.org}
36
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37
 */
1441 ariadna 38
final class lib_test extends \advanced_testcase {
1 efrain 39
 
40
    /**
41
     * Prepares things before this test case is initialised
42
     * @return void
43
     */
44
    public static function setUpBeforeClass(): void {
45
        global $CFG;
46
        require_once($CFG->dirroot . '/mod/url/lib.php');
47
        require_once($CFG->dirroot . '/mod/url/locallib.php');
1441 ariadna 48
        parent::setUpBeforeClass();
1 efrain 49
    }
50
 
51
    /**
52
     * Tests the url_appears_valid_url function
53
     * @return void
54
     */
11 efrain 55
    public function test_url_appears_valid_url(): void {
1 efrain 56
        $this->assertTrue(url_appears_valid_url('http://example'));
57
        $this->assertTrue(url_appears_valid_url('http://www.example.com'));
58
        $this->assertTrue(url_appears_valid_url('http://www.examplé.com'));
59
        $this->assertTrue(url_appears_valid_url('http://💩.la'));
60
        $this->assertTrue(url_appears_valid_url('http://香港大學.香港'));
61
        $this->assertTrue(url_appears_valid_url('http://وزارة-الأتصالات.مصر'));
62
        $this->assertTrue(url_appears_valid_url('http://www.теннис-алт.рф'));
63
        $this->assertTrue(url_appears_valid_url('http://имена.бг'));
64
        $this->assertTrue(url_appears_valid_url('http://straße.de'));
65
        $this->assertTrue(url_appears_valid_url('http://キース.コム'));
66
        $this->assertTrue(url_appears_valid_url('http://太亞.中国'));
67
        $this->assertTrue(url_appears_valid_url('http://www.რეგისტრაცია.გე'));
68
        $this->assertTrue(url_appears_valid_url('http://уміц.укр'));
69
        $this->assertTrue(url_appears_valid_url('http://현대.한국'));
70
        $this->assertTrue(url_appears_valid_url('http://мон.мон'));
71
        $this->assertTrue(url_appears_valid_url('http://тест.қаз'));
72
        $this->assertTrue(url_appears_valid_url('http://рнидс.срб'));
73
        $this->assertTrue(url_appears_valid_url('http://اسماء.شبكة'));
74
        $this->assertTrue(url_appears_valid_url('http://www.informationssäkerhet.se'));
75
        $this->assertTrue(url_appears_valid_url('http://москва.рф/services'));
76
        $this->assertTrue(url_appears_valid_url('http://detdumærker.dk'));
77
        $this->assertTrue(url_appears_valid_url('http://www.exa-mple2.com'));
78
        $this->assertTrue(url_appears_valid_url('http://www.example.com/~nobody/index.html'));
79
        $this->assertTrue(url_appears_valid_url('http://www.example.com#hmm'));
80
        $this->assertTrue(url_appears_valid_url('http://www.example.com/#hmm'));
81
        $this->assertTrue(url_appears_valid_url('http://www.example.com/žlutý koníček/lala.txt'));
82
        $this->assertTrue(url_appears_valid_url('http://www.example.com/žlutý koníček/lala.txt#hmmmm'));
83
        $this->assertTrue(url_appears_valid_url('http://www.example.com/index.php?xx=yy&zz=aa'));
84
        $this->assertTrue(url_appears_valid_url('http://www.example.com:80/index.php?xx=yy&zz=aa'));
85
        $this->assertTrue(url_appears_valid_url('https://user:password@www.example.com/žlutý koníček/lala.txt'));
86
        $this->assertTrue(url_appears_valid_url('ftp://user:password@www.example.com/žlutý koníček/lala.txt'));
87
 
88
        $this->assertFalse(url_appears_valid_url('http:example.com'));
89
        $this->assertFalse(url_appears_valid_url('http:/example.com'));
90
        $this->assertFalse(url_appears_valid_url('http://'));
91
        $this->assertFalse(url_appears_valid_url('http://www.exa mple.com'));
92
        $this->assertFalse(url_appears_valid_url('http://@www.example.com'));
93
        $this->assertFalse(url_appears_valid_url('http://user:@www.example.com'));
94
 
95
        $this->assertTrue(url_appears_valid_url('lalala://@:@/'));
96
    }
97
 
98
    /**
99
     * Test url_view
100
     * @return void
101
     */
11 efrain 102
    public function test_url_view(): void {
1 efrain 103
        global $CFG;
104
 
105
        $CFG->enablecompletion = 1;
106
        $this->resetAfterTest();
107
 
108
        // Setup test data.
109
        $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
110
        $url = $this->getDataGenerator()->create_module('url', array('course' => $course->id),
111
                                                            array('completion' => 2, 'completionview' => 1));
112
        $context = \context_module::instance($url->cmid);
113
        $cm = get_coursemodule_from_instance('url', $url->id);
114
 
115
        // Trigger and capture the event.
116
        $sink = $this->redirectEvents();
117
 
118
        $this->setAdminUser();
119
        url_view($url, $course, $cm, $context);
120
 
121
        $events = $sink->get_events();
122
        // 2 additional events thanks to completion.
123
        $this->assertCount(3, $events);
124
        $event = array_shift($events);
125
 
126
        // Checking that the event contains the expected values.
127
        $this->assertInstanceOf('\mod_url\event\course_module_viewed', $event);
128
        $this->assertEquals($context, $event->get_context());
129
        $url = new \moodle_url('/mod/url/view.php', array('id' => $cm->id));
130
        $this->assertEquals($url, $event->get_url());
131
        $this->assertEventContextNotUsed($event);
132
        $this->assertNotEmpty($event->get_name());
133
 
134
        // Check completion status.
135
        $completion = new \completion_info($course);
136
        $completiondata = $completion->get_data($cm);
137
        $this->assertEquals(1, $completiondata->completionstate);
138
    }
139
 
140
    /**
141
     * Test mod_url_core_calendar_provide_event_action with user override
142
     */
11 efrain 143
    public function test_url_core_calendar_provide_event_action_user_override(): void {
1 efrain 144
        global $CFG, $USER;
145
 
146
        $this->resetAfterTest();
147
        $this->setAdminUser();
148
        $user = $this->getDataGenerator()->create_user();
149
        $CFG->enablecompletion = 1;
150
 
151
        // Create the activity.
152
        $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
153
        $url = $this->getDataGenerator()->create_module('url', array('course' => $course->id),
154
            array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
155
 
156
        // Get some additional data.
157
        $cm = get_coursemodule_from_instance('url', $url->id);
158
 
159
        // Create a calendar event.
160
        $event = $this->create_action_event($course->id, $url->id,
161
            \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
162
 
163
        // Mark the activity as completed.
164
        $completion = new \completion_info($course);
165
        $completion->set_module_viewed($cm);
166
 
167
        // Create an action factory.
168
        $factory = new \core_calendar\action_factory();
169
 
170
        // Decorate action event.
171
        $actionevent = mod_url_core_calendar_provide_event_action($event, $factory, $USER->id);
172
 
173
        // Decorate action with a userid override.
174
        $actionevent2 = mod_url_core_calendar_provide_event_action($event, $factory, $user->id);
175
 
176
        // Ensure result was null because it has been marked as completed for the associated user.
177
        // Logic was brought across from the "_already_completed" function.
178
        $this->assertNull($actionevent);
179
 
180
        // Confirm the event was decorated.
181
        $this->assertNotNull($actionevent2);
182
        $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent2);
183
        $this->assertEquals(get_string('view'), $actionevent2->get_name());
184
        $this->assertInstanceOf('moodle_url', $actionevent2->get_url());
185
        $this->assertEquals(1, $actionevent2->get_item_count());
186
        $this->assertTrue($actionevent2->is_actionable());
187
    }
188
 
11 efrain 189
    public function test_url_core_calendar_provide_event_action(): void {
1 efrain 190
        $this->resetAfterTest();
191
        $this->setAdminUser();
192
 
193
        // Create the activity.
194
        $course = $this->getDataGenerator()->create_course();
195
        $url = $this->getDataGenerator()->create_module('url', array('course' => $course->id));
196
 
197
        // Create a calendar event.
198
        $event = $this->create_action_event($course->id, $url->id,
199
            \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
200
 
201
        // Create an action factory.
202
        $factory = new \core_calendar\action_factory();
203
 
204
        // Decorate action event.
205
        $actionevent = mod_url_core_calendar_provide_event_action($event, $factory);
206
 
207
        // Confirm the event was decorated.
208
        $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
209
        $this->assertEquals(get_string('view'), $actionevent->get_name());
210
        $this->assertInstanceOf('moodle_url', $actionevent->get_url());
211
        $this->assertEquals(1, $actionevent->get_item_count());
212
        $this->assertTrue($actionevent->is_actionable());
213
    }
214
 
11 efrain 215
    public function test_url_core_calendar_provide_event_action_already_completed(): void {
1 efrain 216
        global $CFG;
217
 
218
        $this->resetAfterTest();
219
        $this->setAdminUser();
220
 
221
        $CFG->enablecompletion = 1;
222
 
223
        // Create the activity.
224
        $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
225
        $url = $this->getDataGenerator()->create_module('url', array('course' => $course->id),
226
            array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
227
 
228
        // Get some additional data.
229
        $cm = get_coursemodule_from_instance('url', $url->id);
230
 
231
        // Create a calendar event.
232
        $event = $this->create_action_event($course->id, $url->id,
233
            \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
234
 
235
        // Mark the activity as completed.
236
        $completion = new \completion_info($course);
237
        $completion->set_module_viewed($cm);
238
 
239
        // Create an action factory.
240
        $factory = new \core_calendar\action_factory();
241
 
242
        // Decorate action event.
243
        $actionevent = mod_url_core_calendar_provide_event_action($event, $factory);
244
 
245
        // Ensure result was null.
246
        $this->assertNull($actionevent);
247
    }
248
 
249
    /**
250
     * Creates an action event.
251
     *
252
     * @param int $courseid The course id.
253
     * @param int $instanceid The instance id.
254
     * @param string $eventtype The event type.
255
     * @return bool|calendar_event
256
     */
257
    private function create_action_event($courseid, $instanceid, $eventtype) {
258
        $event = new \stdClass();
259
        $event->name = 'Calendar event';
260
        $event->modulename  = 'url';
261
        $event->courseid = $courseid;
262
        $event->instance = $instanceid;
263
        $event->type = CALENDAR_EVENT_TYPE_ACTION;
264
        $event->eventtype = $eventtype;
265
        $event->timestart = time();
266
 
267
        return \calendar_event::create($event);
268
    }
269
}