Proyectos de Subversion Moodle

Rev

| 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 tool_usertours;
18
 
19
defined('MOODLE_INTERNAL') || die();
20
 
21
global $CFG;
22
require_once($CFG->libdir . '/formslib.php');
23
require_once(__DIR__ . '/helper_trait.php');
24
 
25
/**
26
 * Tests for step.
27
 *
28
 * @package    tool_usertours
29
 * @copyright  2016 Andrew Nicols <andrew@nicols.co.uk>
30
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 * @covers \tool_usertours\manager
32
 */
33
class manager_test extends \advanced_testcase {
34
    // There are shared helpers for these tests in the helper trait.
35
    use \tool_usertours_helper_trait;
36
 
37
    /**
38
     * @var moodle_database
39
     */
40
    protected $db;
41
 
42
    /**
43
     * Setup to store the DB reference.
44
     */
45
    public function setUp(): void {
46
        global $DB;
47
 
48
        $this->db = $DB;
49
    }
50
 
51
    /**
52
     * Tear down to restore the original DB reference.
53
     */
54
    public function tearDown(): void {
55
        global $DB;
56
 
57
        $DB = $this->db;
58
    }
59
 
60
    /**
61
     * Helper to mock the database.
62
     *
63
     * @return moodle_database
64
     */
65
    public function mock_database() {
66
        global $DB;
67
 
68
        $DB = $this->getMockBuilder('moodle_database')->getMock();
69
 
70
        return $DB;
71
    }
72
 
73
    /**
74
     * Data provider to ensure that all modification actions require the session key.
75
     *
76
     * @return array
77
     */
78
    public static function sesskey_required_provider(): array {
79
        $tourid = rand(1, 100);
80
        $stepid = rand(1, 100);
81
 
82
        return [
83
            'Tour removal' => [
84
                'delete_tour',
85
                [$tourid],
86
            ],
87
            'Step removal' => [
88
                'delete_step',
89
                [$stepid],
90
            ],
91
            'Tour visibility' => [
92
                'show_hide_tour',
93
                [$tourid, true],
94
            ],
95
            'Move step' => [
96
                'move_step',
97
                [$stepid],
98
            ],
99
        ];
100
    }
101
 
102
    /**
103
     * Ensure that all modification actions require the session key.
104
     *
105
     * @dataProvider sesskey_required_provider
106
     * @param   string  $function   The function to test
107
     * @param   array   $arguments  The arguments to pass with it
108
     */
109
    public function test_sesskey_required($function, $arguments): void {
110
        $manager = new \tool_usertours\manager();
111
 
112
        $rc = new \ReflectionClass('\tool_usertours\manager');
113
        $rcm = $rc->getMethod($function);
114
 
115
        $this->expectException('moodle_exception');
116
        $rcm->invokeArgs($manager, $arguments);
117
    }
118
 
119
    /**
120
     * Data provider for test_move_tour
121
     *
122
     * @return array
123
     */
124
    public static function move_tour_provider(): array {
125
        $alltours = [
126
            ['name' => 'Tour 1'],
127
            ['name' => 'Tour 2'],
128
            ['name' => 'Tour 3'],
129
        ];
130
 
131
        return [
132
            'Move up' => [
133
                $alltours,
134
                'Tour 2',
135
                \tool_usertours\helper::MOVE_UP,
136
                0,
137
            ],
138
            'Move down' => [
139
                $alltours,
140
                'Tour 2',
141
                \tool_usertours\helper::MOVE_DOWN,
142
                2,
143
            ],
144
            'Move up (first)' => [
145
                $alltours,
146
                'Tour 1',
147
                \tool_usertours\helper::MOVE_UP,
148
                0,
149
            ],
150
            'Move down (last)' => [
151
                $alltours,
152
                'Tour 3',
153
                \tool_usertours\helper::MOVE_DOWN,
154
                2,
155
            ],
156
        ];
157
    }
158
 
159
    /**
160
     * Test moving tours (changing sortorder)
161
     *
162
     * @dataProvider move_tour_provider
163
     *
164
     * @param array $alltours
165
     * @param string $movetourname
166
     * @param int $direction
167
     * @param int $expectedsortorder
168
     * @return void
169
     */
170
    public function test_move_tour($alltours, $movetourname, $direction, $expectedsortorder): void {
171
        global $DB;
172
 
173
        $this->resetAfterTest();
174
 
175
        // Clear out existing tours so ours are the only ones, otherwise we can't predict the sortorder.
176
        $DB->delete_records('tool_usertours_tours');
177
 
178
        foreach ($alltours as $tourconfig) {
179
            $this->helper_create_tour((object) $tourconfig);
180
        }
181
 
182
        // Load our tour to move.
183
        $record = $DB->get_record('tool_usertours_tours', ['name' => $movetourname]);
184
        $tour = \tool_usertours\tour::load_from_record($record);
185
 
186
        // Call protected method via reflection.
187
        $class = new \ReflectionClass(\tool_usertours\manager::class);
188
        $method = $class->getMethod('_move_tour');
189
        $method->invokeArgs(null, [$tour, $direction]);
190
 
191
        // Assert expected sortorder.
192
        $this->assertEquals($expectedsortorder, $tour->get_sortorder());
193
    }
194
 
195
    /**
196
     * Data Provider for get_matching_tours tests.
197
     *
198
     * @return array
199
     */
200
    public static function get_matching_tours_provider(): array {
201
        global $CFG;
202
 
203
        $alltours = [
204
            [
205
                'pathmatch'     => '/my/%',
206
                'enabled'       => false,
207
                'name'          => 'Failure',
208
                'description'   => '',
209
                'configdata'    => '',
210
            ],
211
            [
212
                'pathmatch'     => '/my/%',
213
                'enabled'       => true,
214
                'name'          => 'My tour enabled',
215
                'description'   => '',
216
                'configdata'    => '',
217
            ],
218
            [
219
                'pathmatch'     => '/my/%',
220
                'enabled'       => true,
221
                'name'          => 'My tour enabled 2',
222
                'description'   => '',
223
                'configdata'    => '',
224
            ],
225
            [
226
                'pathmatch'     => '/my/%',
227
                'enabled'       => false,
228
                'name'          => 'Failure',
229
                'description'   => '',
230
                'configdata'    => '',
231
            ],
232
            [
233
                'pathmatch'     => '/course/?id=%foo=bar',
234
                'enabled'       => false,
235
                'name'          => 'Failure',
236
                'description'   => '',
237
                'configdata'    => '',
238
            ],
239
            [
240
                'pathmatch'     => '/course/?id=%foo=bar',
241
                'enabled'       => true,
242
                'name'          => 'course tour with additional params enabled',
243
                'description'   => '',
244
                'configdata'    => '',
245
            ],
246
            [
247
                'pathmatch'     => '/course/?id=%foo=bar',
248
                'enabled'       => false,
249
                'name'          => 'Failure',
250
                'description'   => '',
251
                'configdata'    => '',
252
            ],
253
            [
254
                'pathmatch'     => '/course/?id=%',
255
                'enabled'       => false,
256
                'name'          => 'Failure',
257
                'description'   => '',
258
                'configdata'    => '',
259
            ],
260
            [
261
                'pathmatch'     => '/course/?id=%',
262
                'enabled'       => true,
263
                'name'          => 'course tour enabled',
264
                'description'   => '',
265
                'configdata'    => '',
266
            ],
267
            [
268
                'pathmatch'     => '/course/?id=%',
269
                'enabled'       => false,
270
                'name'          => 'Failure',
271
                'description'   => '',
272
                'configdata'    => '',
273
            ],
274
        ];
275
 
276
        return
277
        [
278
            'No matches found' => [
279
                $alltours,
280
                $CFG->wwwroot . '/some/invalid/value',
281
                [],
282
            ],
283
            'Never return a disabled tour' => [
284
                $alltours,
285
                $CFG->wwwroot . '/my/index.php',
286
                ['My tour enabled', 'My tour enabled 2'],
287
            ],
288
            'My not course' => [
289
                $alltours,
290
                $CFG->wwwroot . '/my/index.php',
291
                ['My tour enabled', 'My tour enabled 2'],
292
            ],
293
            'My with params' => [
294
                $alltours,
295
                $CFG->wwwroot . '/my/index.php?id=42',
296
                ['My tour enabled', 'My tour enabled 2'],
297
            ],
298
            'Course with params' => [
299
                $alltours,
300
                $CFG->wwwroot . '/course/?id=42',
301
                ['course tour enabled'],
302
            ],
303
            'Course with params and trailing content' => [
304
                $alltours,
305
                $CFG->wwwroot . '/course/?id=42&foo=bar',
306
                ['course tour with additional params enabled', 'course tour enabled'],
307
            ],
308
        ];
309
    }
310
 
311
    /**
312
     * Tests for the get_matching_tours function.
313
     *
314
     * @dataProvider get_matching_tours_provider
315
     * @param   array   $alltours   The list of tours to insert.
316
     * @param   string  $url        The URL to test.
317
     * @param   array   $expected   List of names of the expected matching tours.
318
     */
319
    public function test_get_matching_tours(array $alltours, string $url, array $expected): void {
320
        $this->resetAfterTest();
321
 
322
        $this->setGuestUser();
323
 
324
        foreach ($alltours as $tourconfig) {
325
            $tour = $this->helper_create_tour((object) $tourconfig);
326
            $this->helper_create_step((object) ['tourid' => $tour->get_id()]);
327
        }
328
 
329
        $matches = \tool_usertours\manager::get_matching_tours(new \moodle_url($url));
330
        $this->assertEquals(count($expected), count($matches));
331
        for ($i = 0; $i < count($matches); $i++) {
332
            $this->assertEquals($expected[$i], $matches[$i]->get_name());
333
        }
334
    }
335
 
336
    /**
337
     * Test that no matching tours are returned if there is pending site policy agreement.
338
     */
339
    public function test_get_matching_tours_for_user_without_site_policy_agreed(): void {
340
        global $CFG;
341
 
342
        $this->resetAfterTest();
343
        $this->setGuestUser();
344
 
345
        $tour = $this->helper_create_tour((object) [
346
            'pathmatch' => '/%',
347
            'enabled' => true,
348
            'name' => 'Test tour',
349
            'description' => '',
350
            'configdata' => '',
351
        ]);
352
 
353
        $this->helper_create_step((object) [
354
            'tourid' => $tour->get_id(),
355
        ]);
356
 
357
        $matches = \tool_usertours\manager::get_matching_tours(new \moodle_url('/'));
358
        $this->assertEquals(1, count($matches));
359
 
360
        $CFG->sitepolicyguest = 'https://example.com';
361
 
362
        $matches = \tool_usertours\manager::get_matching_tours(new \moodle_url('/'));
363
        $this->assertEmpty($matches);
364
    }
365
}