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