Proyectos de Subversion Moodle

Rev

Rev 1 | | 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
namespace core_calendar;
18
 
19
use core_calendar\external\calendar_event_exporter;
20
use core_calendar\local\event\container;
21
 
22
defined('MOODLE_INTERNAL') || die();
23
require_once(__DIR__ . '/helpers.php');
24
 
25
/**
26
 * Calendar event exporter testcase.
27
 *
28
 * @package core_calendar
29
 * @copyright 2017 Ryan Wyllie <ryan@moodle.com>
30
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 */
32
class calendar_event_exporter_test extends \advanced_testcase {
33
    /**
34
     * Data provider for the timestamp min limit test case to confirm
35
     * that the minimum time limit is set correctly on the boundary cases.
36
     */
37
    public function get_timestamp_min_limit_test_cases() {
38
        $now = time();
39
        $todaymidnight = usergetmidnight($now);
40
        $tomorrowmidnight = $todaymidnight + DAYSECS;
41
        $eightam = $todaymidnight + (60 * 60 * 8);
42
        $starttime = (new \DateTime())->setTimestamp($eightam);
43
 
44
        return [
45
            'before min' => [
46
                $starttime,
47
                [
48
                    ($starttime->getTimestamp() + 1),
49
                    'some error'
50
                ],
51
                $tomorrowmidnight
52
            ],
53
            'equal min' => [
54
                $starttime,
55
                [
56
                    $starttime->getTimestamp(),
57
                    'some error'
58
                ],
59
                $todaymidnight
60
            ],
61
            'after min' => [
62
                $starttime,
63
                [
64
                    ($starttime->getTimestamp() - 1),
65
                    'some error'
66
                ],
67
                $todaymidnight
68
            ]
69
        ];
70
    }
71
 
72
    /**
73
     * @dataProvider get_timestamp_min_limit_test_cases()
74
     */
11 efrain 75
    public function test_get_timestamp_min_limit($starttime, $min, $expected): void {
1 efrain 76
        $class = calendar_event_exporter::class;
77
        $mock = $this->getMockBuilder($class)
78
            ->disableOriginalConstructor()
79
            ->onlyMethods([])
80
            ->getMock();
81
        $reflector = new \ReflectionClass($class);
82
        $method = $reflector->getMethod('get_timestamp_min_limit');
83
 
84
        $result = $method->invoke($mock, $starttime, $min);
85
        $this->assertEquals($expected, $result['mindaytimestamp']);
86
        $this->assertEquals($min[1], $result['mindayerror']);
87
    }
88
 
89
    /**
90
     * Data provider for the timestamp max limit test case to confirm
91
     * that the maximum time limit is set correctly on the boundary cases.
92
     */
93
    public function get_timestamp_max_limit_test_cases() {
94
        $now = time();
95
        $todaymidnight = usergetmidnight($now);
96
        $yesterdaymidnight = $todaymidnight - DAYSECS;
97
        $eightam = $todaymidnight + (60 * 60 * 8);
98
        $starttime = (new \DateTime())->setTimestamp($eightam);
99
 
100
        return [
101
            'before max' => [
102
                $starttime,
103
                [
104
                    ($starttime->getTimestamp() + 1),
105
                    'some error'
106
                ],
107
                $todaymidnight
108
            ],
109
            'equal max' => [
110
                $starttime,
111
                [
112
                    $starttime->getTimestamp(),
113
                    'some error'
114
                ],
115
                $todaymidnight
116
            ],
117
            'after max' => [
118
                $starttime,
119
                [
120
                    ($starttime->getTimestamp() - 1),
121
                    'some error'
122
                ],
123
                $yesterdaymidnight
124
            ]
125
        ];
126
    }
127
 
128
    /**
129
     * @dataProvider get_timestamp_max_limit_test_cases()
130
     */
11 efrain 131
    public function test_get_timestamp_max_limit($starttime, $max, $expected): void {
1 efrain 132
        $class = calendar_event_exporter::class;
133
        $mock = $this->getMockBuilder($class)
134
            ->disableOriginalConstructor()
135
            ->onlyMethods([])
136
            ->getMock();
137
        $reflector = new \ReflectionClass($class);
138
        $method = $reflector->getMethod('get_timestamp_max_limit');
139
 
140
        $result = $method->invoke($mock, $starttime, $max);
141
        $this->assertEquals($expected, $result['maxdaytimestamp']);
142
        $this->assertEquals($max[1], $result['maxdayerror']);
143
    }
144
 
145
    /**
146
     * Exporting a course event should generate the course URL.
147
     */
11 efrain 148
    public function test_calendar_event_exporter_course_url_course_event(): void {
1 efrain 149
        global $CFG, $PAGE;
150
        require_once($CFG->dirroot . '/course/lib.php');
151
 
152
        $this->resetAfterTest(true);
153
        $this->setAdminUser();
154
        $generator = $this->getDataGenerator();
155
        $user = $generator->create_user();
156
        $course = $generator->create_course();
157
        $context = \context_course::instance($course->id);
158
        $now = time();
159
        $mapper = container::get_event_mapper();
160
        $legacyevent = create_event([
161
            'courseid' => $course->id,
162
            'userid' => 1,
163
            'eventtype' => 'course',
164
            'timestart' => $now
165
        ]);
166
        $event = $mapper->from_legacy_event_to_event($legacyevent);
167
        $exporter = new calendar_event_exporter($event, [
168
            'context' => $context,
169
            'course' => $course,
170
            'moduleinstance' => null,
171
            'daylink' => new \moodle_url(''),
172
            'type' => type_factory::get_calendar_instance(),
173
            'today' => $now
174
        ]);
175
 
176
        $courseurl = course_get_url($course->id);
177
        $expected = $courseurl->out(false);
178
        $renderer = $PAGE->get_renderer('core_calendar');
179
        $exportedevent = $exporter->export($renderer);
180
 
181
        // The exported URL should be for the course.
182
        $this->assertEquals($expected, $exportedevent->url);
183
    }
184
 
185
    /**
186
     * Exporting a user event should generate the site course URL.
187
     */
11 efrain 188
    public function test_calendar_event_exporter_course_url_user_event(): void {
1 efrain 189
        global $CFG, $PAGE;
190
        require_once($CFG->dirroot . '/course/lib.php');
191
 
192
        $this->resetAfterTest(true);
193
        $this->setAdminUser();
194
        $generator = $this->getDataGenerator();
195
        $user = $generator->create_user();
196
        $context = \context_user::instance($user->id);
197
        $now = time();
198
        $mapper = container::get_event_mapper();
199
        $legacyevent = create_event([
200
            'courseid' => 0,
201
            'userid' => $user->id,
202
            'eventtype' => 'user',
203
            'timestart' => $now
204
        ]);
205
        $event = $mapper->from_legacy_event_to_event($legacyevent);
206
        $exporter = new calendar_event_exporter($event, [
207
            'context' => $context,
208
            'course' => null,
209
            'moduleinstance' => null,
210
            'daylink' => new \moodle_url(''),
211
            'type' => type_factory::get_calendar_instance(),
212
            'today' => $now
213
        ]);
214
 
215
        $courseurl = course_get_url(SITEID);
216
        $expected = $courseurl->out(false);
217
        $renderer = $PAGE->get_renderer('core_calendar');
218
        $exportedevent = $exporter->export($renderer);
219
 
220
        // The exported URL should be for the site course.
221
        $this->assertEquals($expected, $exportedevent->url);
222
    }
223
 
224
    /**
225
     * Popup name respects filters for course shortname.
226
     */
11 efrain 227
    public function test_calendar_event_exporter_popupname_course_shortname_strips_links(): void {
1 efrain 228
        global $CFG, $PAGE;
229
 
230
        $this->resetAfterTest(true);
231
        $this->setAdminUser();
232
        $generator = $this->getDataGenerator();
233
        $user = $generator->create_user();
234
        $rawshortname = 'Shortname <a href="#">link</a>';
235
        $nolinkshortname = strip_links($rawshortname);
236
        $course = $generator->create_course(['shortname' => $rawshortname]);
237
        $coursecontext = \context_course::instance($course->id);
238
        $now = time();
239
        $mapper = container::get_event_mapper();
240
        $renderer = $PAGE->get_renderer('core_calendar');
241
        $legacyevent = create_event([
242
            'courseid' => $course->id,
243
            'userid' => 1,
244
            'eventtype' => 'course',
245
            'timestart' => $now
246
        ]);
247
        $event = $mapper->from_legacy_event_to_event($legacyevent);
248
        $exporter = new calendar_event_exporter($event, [
249
            'context' => $coursecontext,
250
            'course' => $course,
251
            'moduleinstance' => null,
252
            'daylink' => new \moodle_url(''),
253
            'type' => type_factory::get_calendar_instance(),
254
            'today' => $now
255
        ]);
256
 
257
        $exportedevent = $exporter->export($renderer);
258
        // Links should always be stripped from the course short name.
259
        $this->assertMatchesRegularExpression("/$nolinkshortname/", $exportedevent->popupname);
260
    }
261
 
262
    /**
263
     * Exported event contains the exported course.
264
     */
11 efrain 265
    public function test_calendar_event_exporter_exports_course(): void {
1 efrain 266
        global $CFG, $PAGE;
267
 
268
        $this->resetAfterTest(true);
269
        $this->setAdminUser();
270
        $generator = $this->getDataGenerator();
271
        $user = $generator->create_user();
272
        $rawshortname = 'Shortname <a href="#">link</a>';
273
        $nolinkshortname = strip_links($rawshortname);
274
        $course = $generator->create_course(['shortname' => $rawshortname]);
275
        $coursecontext = \context_course::instance($course->id);
276
        $now = time();
277
        $mapper = container::get_event_mapper();
278
        $renderer = $PAGE->get_renderer('core_calendar');
279
        $legacyevent = create_event([
280
            'courseid' => $course->id,
281
            'userid' => 1,
282
            'eventtype' => 'course',
283
            'timestart' => $now
284
        ]);
285
        $event = $mapper->from_legacy_event_to_event($legacyevent);
286
        $exporter = new calendar_event_exporter($event, [
287
            'context' => $coursecontext,
288
            'course' => $course,
289
            'moduleinstance' => null,
290
            'daylink' => new \moodle_url(''),
291
            'type' => type_factory::get_calendar_instance(),
292
            'today' => $now
293
        ]);
294
 
295
        $exportedevent = $exporter->export($renderer);
296
        $courseexporter = new \core_course\external\course_summary_exporter($course, [
297
            'context' => $coursecontext
298
        ]);
299
        $exportedcourse = $courseexporter->export($renderer);
300
        $this->assertEquals($exportedevent->course, $exportedcourse);
301
    }
302
}