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
namespace enrol_lti;
18
 
19
/**
20
 * Test the helper functionality.
21
 *
22
 * @package enrol_lti
23
 * @copyright 2016 Mark Nelson <markn@moodle.com>
24
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 */
1441 ariadna 26
final class helper_test extends \advanced_testcase {
1 efrain 27
 
28
    /**
29
     * @var \stdClass $user1 A user.
30
     */
31
    public $user1;
32
 
33
    /**
34
     * @var \stdClass $user2 A user.
35
     */
36
    public $user2;
37
 
38
    /**
39
     * Test set up.
40
     *
41
     * This is executed before running any test in this file.
42
     */
43
    public function setUp(): void {
1441 ariadna 44
        parent::setUp();
1 efrain 45
        $this->resetAfterTest();
46
 
47
        // Set this user as the admin.
48
        $this->setAdminUser();
49
 
50
        // Get some of the information we need.
51
        $this->user1 = self::getDataGenerator()->create_user();
52
        $this->user2 = self::getDataGenerator()->create_user();
53
    }
54
 
55
    /**
56
     * Test the update user profile image function.
57
     */
11 efrain 58
    public function test_update_user_profile_image(): void {
1 efrain 59
        global $DB, $CFG;
60
 
61
        // Set the profile image.
62
        \enrol_lti\helper::update_user_profile_image($this->user1->id, $this->getExternalTestFileUrl('/test.jpg'));
63
 
64
        // Get the new user record.
65
        $this->user1 = $DB->get_record('user', array('id' => $this->user1->id));
66
 
67
        // Set the page details.
68
        $page = new \moodle_page();
69
        $page->set_url('/user/profile.php');
70
        $page->set_context(\context_system::instance());
71
        $renderer = $page->get_renderer('core');
72
        $usercontext = \context_user::instance($this->user1->id);
73
 
74
        // Get the user's profile picture and make sure it is correct.
75
        $userpicture = new \user_picture($this->user1);
76
        $this->assertSame($CFG->wwwroot . '/pluginfile.php/' . $usercontext->id . '/user/icon/boost/f2?rev=' .$this->user1->picture,
77
            $userpicture->get_url($page, $renderer)->out(false));
78
    }
79
 
80
    /**
81
     * Test that we can not enrol past the maximum number of users allowed.
82
     */
11 efrain 83
    public function test_enrol_user_max_enrolled(): void {
1 efrain 84
        global $DB;
85
 
86
        // Set up the LTI enrolment tool.
87
        $data = new \stdClass();
88
        $data->maxenrolled = 1;
89
        $tool = $this->getDataGenerator()->create_lti_tool($data);
90
 
91
        // Now get all the information we need.
92
        $tool = \enrol_lti\helper::get_lti_tool($tool->id);
93
 
94
        // Enrol a user.
95
        $result = \enrol_lti\helper::enrol_user($tool, $this->user1->id);
96
 
97
        // Check that the user was enrolled.
98
        $this->assertEquals(true, $result);
99
        $this->assertEquals(1, $DB->count_records('user_enrolments', array('enrolid' => $tool->enrolid)));
100
 
101
        // Try and enrol another user - should not happen.
102
        $result = \enrol_lti\helper::enrol_user($tool, $this->user2->id);
103
 
104
        // Check that this user was not enrolled and we are told why.
105
        $this->assertEquals(\enrol_lti\helper::ENROLMENT_MAX_ENROLLED, $result);
106
        $this->assertEquals(1, $DB->count_records('user_enrolments', array('enrolid' => $tool->enrolid)));
107
    }
108
 
109
    /**
110
     * Test that we can not enrol when the enrolment has not started.
111
     */
11 efrain 112
    public function test_enrol_user_enrolment_not_started(): void {
1 efrain 113
        global $DB;
114
 
115
        // Set up the LTI enrolment tool.
116
        $data = new \stdClass();
117
        $data->enrolstartdate = time() + DAYSECS; // Make sure it is in the future.
118
        $tool = $this->getDataGenerator()->create_lti_tool($data);
119
 
120
        // Now get all the information we need.
121
        $tool = \enrol_lti\helper::get_lti_tool($tool->id);
122
 
123
        // Try and enrol a user - should not happen.
124
        $result = \enrol_lti\helper::enrol_user($tool, $this->user1->id);
125
 
126
        // Check that this user was not enrolled and we are told why.
127
        $this->assertEquals(\enrol_lti\helper::ENROLMENT_NOT_STARTED, $result);
128
        $this->assertEquals(0, $DB->count_records('user_enrolments', array('enrolid' => $tool->enrolid)));
129
    }
130
 
131
    /**
132
     * Test that we can not enrol when the enrolment has finished.
133
     */
11 efrain 134
    public function test_enrol_user_enrolment_finished(): void {
1 efrain 135
        global $DB;
136
 
137
        // Set up the LTI enrolment tool.
138
        $data = new \stdClass();
139
        $data->enrolenddate = time() - DAYSECS; // Make sure it is in the past.
140
        $tool = $this->getDataGenerator()->create_lti_tool($data);
141
 
142
        // Now get all the information we need.
143
        $tool = \enrol_lti\helper::get_lti_tool($tool->id);
144
 
145
        // Try and enrol a user - should not happen.
146
        $result = \enrol_lti\helper::enrol_user($tool, $this->user1->id);
147
 
148
        // Check that this user was not enrolled and we are told why.
149
        $this->assertEquals(\enrol_lti\helper::ENROLMENT_FINISHED, $result);
150
        $this->assertEquals(0, $DB->count_records('user_enrolments', array('enrolid' => $tool->enrolid)));
151
    }
152
 
153
    /**
154
     * Test returning the number of available tools.
155
     */
11 efrain 156
    public function test_count_lti_tools(): void {
1 efrain 157
        $generator = $this->getDataGenerator();
158
        // Create two tools belonging to the same course.
159
        $course1 = $generator->create_course();
160
        $data = new \stdClass();
161
        $data->courseid = $course1->id;
162
        $generator->create_lti_tool($data);
163
        $generator->create_lti_tool($data);
164
 
165
        // Create two more tools in a separate course.
166
        $course2 = $this->getDataGenerator()->create_course();
167
        $data = new \stdClass();
168
        $data->courseid = $course2->id;
169
        $generator->create_lti_tool($data);
170
 
171
        // Set the next tool to disabled.
172
        $data->status = ENROL_INSTANCE_DISABLED;
173
        $generator->create_lti_tool($data);
174
 
175
        // Count all the tools.
176
        $count = \enrol_lti\helper::count_lti_tools();
177
        $this->assertEquals(4, $count);
178
 
179
        // Count all the tools in course 1.
180
        $count = \enrol_lti\helper::count_lti_tools(array('courseid' => $course1->id));
181
        $this->assertEquals(2, $count);
182
 
183
        // Count all the tools in course 2 that are disabled.
184
        $count = \enrol_lti\helper::count_lti_tools(array('courseid' => $course2->id, 'status' => ENROL_INSTANCE_DISABLED));
185
        $this->assertEquals(1, $count);
186
 
187
        // Count all the tools that are enabled.
188
        $count = \enrol_lti\helper::count_lti_tools(array('status' => ENROL_INSTANCE_ENABLED));
189
        $this->assertEquals(3, $count);
190
    }
191
 
192
    /**
193
     * Test returning the list of available tools.
194
     */
11 efrain 195
    public function test_get_lti_tools(): void {
1 efrain 196
        $generator = $this->getDataGenerator();
197
        // Create two tools belonging to the same course.
198
        $course1 = $generator->create_course();
199
        $data = new \stdClass();
200
        $data->courseid = $course1->id;
201
        $tool1 = $generator->create_lti_tool($data);
202
        $tool2 = $generator->create_lti_tool($data);
203
 
204
        // Create two more tools in a separate course.
205
        $course2 = $generator->create_course();
206
        $data = new \stdClass();
207
        $data->courseid = $course2->id;
208
        $tool3 = $generator->create_lti_tool($data);
209
 
210
        // Set the next tool to disabled.
211
        $data->status = ENROL_INSTANCE_DISABLED;
212
        $tool4 = $generator->create_lti_tool($data);
213
 
214
        // Get all the tools.
215
        $tools = \enrol_lti\helper::get_lti_tools();
216
 
217
        // Check that we got all the tools.
218
        $this->assertEquals(4, count($tools));
219
 
220
        // Get all the tools in course 1.
221
        $tools = \enrol_lti\helper::get_lti_tools(array('courseid' => $course1->id));
222
 
223
        // Check that we got all the tools in course 1.
224
        $this->assertEquals(2, count($tools));
225
        $this->assertTrue(isset($tools[$tool1->id]));
226
        $this->assertTrue(isset($tools[$tool2->id]));
227
 
228
        // Get all the tools in course 2 that are disabled.
229
        $tools = \enrol_lti\helper::get_lti_tools(array('courseid' => $course2->id, 'status' => ENROL_INSTANCE_DISABLED));
230
 
231
        // Check that we got all the tools in course 2 that are disabled.
232
        $this->assertEquals(1, count($tools));
233
        $this->assertTrue(isset($tools[$tool4->id]));
234
 
235
        // Get all the tools that are enabled.
236
        $tools = \enrol_lti\helper::get_lti_tools(array('status' => ENROL_INSTANCE_ENABLED));
237
 
238
        // Check that we got all the tools that are enabled.
239
        $this->assertEquals(3, count($tools));
240
        $this->assertTrue(isset($tools[$tool1->id]));
241
        $this->assertTrue(isset($tools[$tool2->id]));
242
        $this->assertTrue(isset($tools[$tool3->id]));
243
    }
244
 
245
    /**
246
     * Test getting the launch url of a tool.
247
     */
11 efrain 248
    public function test_get_launch_url(): void {
1 efrain 249
        $course1 = $this->getDataGenerator()->create_course();
250
        $data = new \stdClass();
251
        $data->courseid = $course1->id;
252
        $tool1 = $this->getDataGenerator()->create_lti_tool($data);
253
 
254
        $id = $tool1->id;
255
        $launchurl = \enrol_lti\helper::get_launch_url($id);
256
        $this->assertEquals('https://www.example.com/moodle/enrol/lti/tool.php?id=' . $id, $launchurl->out());
257
    }
258
 
259
    /**
260
     * Test getting the cartridge url of a tool.
261
     */
11 efrain 262
    public function test_get_cartridge_url(): void {
1 efrain 263
        global $CFG;
264
 
265
        $slasharguments = $CFG->slasharguments;
266
 
267
        $CFG->slasharguments = false;
268
 
269
        $course1 = $this->getDataGenerator()->create_course();
270
        $data = new \stdClass();
271
        $data->courseid = $course1->id;
272
        $tool1 = $this->getDataGenerator()->create_lti_tool($data);
273
 
274
        $id = $tool1->id;
275
        $token = \enrol_lti\helper::generate_cartridge_token($id);
276
        $launchurl = \enrol_lti\helper::get_cartridge_url($tool1);
277
        $this->assertEquals('https://www.example.com/moodle/enrol/lti/cartridge.php?id=' . $id . '&amp;token=' . $token,
278
                            $launchurl->out());
279
 
280
        $CFG->slasharguments = true;
281
 
282
        $launchurl = \enrol_lti\helper::get_cartridge_url($tool1);
283
        $this->assertEquals('https://www.example.com/moodle/enrol/lti/cartridge.php/' . $id . '/' . $token . '/cartridge.xml',
284
                            $launchurl->out());
285
 
286
        $CFG->slasharguments = $slasharguments;
287
    }
288
 
289
    /**
290
     * Test getting the cartridge url of a tool.
291
     */
11 efrain 292
    public function test_get_proxy_url(): void {
1 efrain 293
        global $CFG;
294
 
295
        $slasharguments = $CFG->slasharguments;
296
 
297
        $CFG->slasharguments = false;
298
 
299
        $course1 = $this->getDataGenerator()->create_course();
300
        $data = new \stdClass();
301
        $data->courseid = $course1->id;
302
        $tool1 = $this->getDataGenerator()->create_lti_tool($data);
303
 
304
        $id = $tool1->id;
305
        $token = \enrol_lti\helper::generate_proxy_token($id);
306
        $launchurl = \enrol_lti\helper::get_proxy_url($tool1);
307
        $this->assertEquals('https://www.example.com/moodle/enrol/lti/proxy.php?id=' . $id . '&amp;token=' . $token,
308
                            $launchurl->out());
309
 
310
        $CFG->slasharguments = true;
311
 
312
        $launchurl = \enrol_lti\helper::get_proxy_url($tool1);
313
        $this->assertEquals('https://www.example.com/moodle/enrol/lti/proxy.php/' . $id . '/' . $token . '/',
314
                            $launchurl->out());
315
 
316
        $CFG->slasharguments = $slasharguments;
317
    }
318
 
319
    /**
320
     * Test getting the name of a tool.
321
     */
11 efrain 322
    public function test_get_name(): void {
1 efrain 323
        $course1 = $this->getDataGenerator()->create_course();
324
        $data = new \stdClass();
325
        $data->courseid = $course1->id;
326
        $tool1 = $this->getDataGenerator()->create_lti_tool($data);
327
 
328
        $name = \enrol_lti\helper::get_name($tool1);
329
        $this->assertEquals('Course: Test course 1', $name);
330
 
331
        $tool1->name = 'Shared course';
332
        $name = \enrol_lti\helper::get_name($tool1);
333
        $this->assertEquals('Shared course', $name);
334
    }
335
 
336
    /**
337
     * Test getting the description of a tool.
338
     */
11 efrain 339
    public function test_get_description(): void {
1 efrain 340
        $generator = $this->getDataGenerator();
341
        $course1 = $generator->create_course();
342
        $data = new \stdClass();
343
        $data->courseid = $course1->id;
344
        $tool1 = $generator->create_lti_tool($data);
345
 
346
        $description = \enrol_lti\helper::get_description($tool1);
347
        $this->assertStringContainsString('Test course 1 Lorem ipsum dolor sit amet', $description);
348
 
349
        $module1 = $generator->create_module('assign', array(
350
                'course' => $course1->id
351
            ));
352
        $data = new \stdClass();
353
        $data->cmid = $module1->cmid;
354
        $tool2 = $generator->create_lti_tool($data);
355
        $description = \enrol_lti\helper::get_description($tool2);
356
        $this->assertStringContainsString('Test assign 1', $description);
357
    }
358
 
359
    /**
360
     * Test getting the icon of a tool.
361
     */
11 efrain 362
    public function test_get_icon(): void {
1 efrain 363
        global $CFG;
364
 
365
        $course1 = $this->getDataGenerator()->create_course();
366
        $data = new \stdClass();
367
        $data->courseid = $course1->id;
368
        $tool = $this->getDataGenerator()->create_lti_tool($data);
369
 
370
        $icon = \enrol_lti\helper::get_icon($tool);
371
        $icon = $icon->out();
372
        // Only local icons are supported by the LTI framework.
373
        $this->assertStringContainsString($CFG->wwwroot, $icon);
374
 
375
    }
376
 
377
    /**
378
     * Test verifying a cartridge token.
379
     */
11 efrain 380
    public function test_verify_cartridge_token(): void {
1 efrain 381
        $course1 = $this->getDataGenerator()->create_course();
382
        $data = new \stdClass();
383
        $data->courseid = $course1->id;
384
        $tool1 = $this->getDataGenerator()->create_lti_tool($data);
385
 
386
        $token = \enrol_lti\helper::generate_cartridge_token($tool1->id);
387
        $this->assertTrue(\enrol_lti\helper::verify_cartridge_token($tool1->id, $token));
388
        $this->assertFalse(\enrol_lti\helper::verify_cartridge_token($tool1->id, 'incorrect token!'));
389
    }
390
 
391
    /**
392
     * Test verifying a proxy token.
393
     */
11 efrain 394
    public function test_verify_proxy_token(): void {
1 efrain 395
        $course1 = $this->getDataGenerator()->create_course();
396
        $data = new \stdClass();
397
        $data->courseid = $course1->id;
398
        $tool1 = $this->getDataGenerator()->create_lti_tool($data);
399
 
400
        $token = \enrol_lti\helper::generate_proxy_token($tool1->id);
401
        $this->assertTrue(\enrol_lti\helper::verify_proxy_token($tool1->id, $token));
402
        $this->assertFalse(\enrol_lti\helper::verify_proxy_token($tool1->id, 'incorrect token!'));
403
    }
404
 
405
    /**
406
     * Data provider for the set_xpath test.
407
     */
1441 ariadna 408
    public static function set_xpath_provider(): array {
1 efrain 409
        return [
410
            "Correct structure" => [
411
                "parameters" => [
412
                    "/root" => [
413
                        "/firstnode" => "Content 1",
414
                        "/parentnode" => [
415
                            "/childnode" => "Content 2"
416
                        ]
417
                    ]
418
                ],
419
                "expected" => "test_correct_xpath-expected.xml"
420
            ],
421
            "A null value, but no node to remove" => [
422
                "parameters" => [
423
                    "/root" => [
424
                        "/nonexistant" => null,
425
                        "/firstnode" => "Content 1"
426
                    ]
427
                ],
428
                "expected" => "test_missing_node-expected.xml"
429
            ],
430
            "A string value, but no node existing to set" => [
431
                "parameters" => [
432
                    "/root" => [
433
                        "/nonexistant" => "This will not be set",
434
                        "/firstnode" => "Content 1"
435
                    ]
436
                ],
437
                "expected" => "test_missing_node-expected.xml"
438
            ],
439
            "Array but no children exist" => [
440
                "parameters" => [
441
                    "/root" => [
442
                        "/nonexistant" => [
443
                            "/alsononexistant" => "This will not be set"
444
                        ],
445
                        "/firstnode" => "Content 1"
446
                    ]
447
                ],
448
                "expected" => "test_missing_node-expected.xml"
449
            ],
450
            "Remove nodes" => [
451
                "parameters" => [
452
                    "/root" => [
453
                        "/parentnode" => [
454
                            "/childnode" => null
455
                        ],
456
                        "/firstnode" => null
457
                    ]
458
                ],
459
                "expected" => "test_nodes_removed-expected.xml"
460
            ],
461
            "Get by attribute" => [
462
                "parameters" => [
463
                    "/root" => [
464
                        "/ambiguous[@id='1']" => 'Content 1'
465
                    ]
466
                ],
467
                "expected" => "test_ambiguous_nodes-expected.xml"
468
            ]
469
        ];
470
    }
471
 
472
    /**
473
     * Test set_xpath.
474
     * @dataProvider set_xpath_provider
475
     * @param array $parameters A hash of parameters represented by a heirarchy of xpath expressions
476
     * @param string $expected The name of the fixture file containing the expected result.
477
     */
11 efrain 478
    public function test_set_xpath($parameters, $expected): void {
1 efrain 479
        $helper = new \ReflectionClass('enrol_lti\\helper');
480
        $function = $helper->getMethod('set_xpath');
481
 
482
        $document = new \DOMDocument();
1441 ariadna 483
        $document->load(realpath(self::get_fixture_path(__NAMESPACE__, 'input.xml')));
1 efrain 484
        $xpath = new \DOMXpath($document);
485
        $function->invokeArgs(null, [$xpath, $parameters]);
486
        $result = $document->saveXML();
1441 ariadna 487
        $expected = file_get_contents(realpath(self::get_fixture_path(__NAMESPACE__, $expected)));
1 efrain 488
        $this->assertEquals($expected, $result);
489
    }
490
 
491
    /**
492
     * Test set_xpath when an incorrect xpath expression is given.
493
     */
11 efrain 494
    public function test_set_xpath_incorrect_xpath(): void {
1 efrain 495
        $parameters = [
496
            "/root" => [
497
                "/firstnode" => null,
498
                "/parentnode*&#^*#(" => [
499
                    "/childnode" => null
500
                ],
501
            ]
502
        ];
503
        $helper = new \ReflectionClass('enrol_lti\\helper');
504
        $function = $helper->getMethod('set_xpath');
505
 
506
        $document = new \DOMDocument();
1441 ariadna 507
        $document->load(realpath(self::get_fixture_path(__NAMESPACE__, 'input.xml')));
1 efrain 508
        $xpath = new \DOMXpath($document);
509
 
510
        $this->expectException('coding_exception');
511
        $function->invokeArgs(null, [$xpath, $parameters]);
512
    }
513
 
514
    /**
515
     * Test create cartridge.
516
     */
11 efrain 517
    public function test_create_cartridge(): void {
1 efrain 518
        global $CFG;
519
 
520
        $course1 = $this->getDataGenerator()->create_course();
521
        $data = new \stdClass();
522
        $data->courseid = $course1->id;
523
        $tool1 = $this->getDataGenerator()->create_lti_tool($data);
524
 
525
        $cartridge = \enrol_lti\helper::create_cartridge($tool1->id);
526
        $this->assertStringContainsString('<blti:title>Test LTI</blti:title>', $cartridge);
527
        $this->assertStringContainsString("<blti:icon>$CFG->wwwroot/theme/image.php/boost/theme/1/favicon</blti:icon>", $cartridge);
528
        $this->assertStringContainsString("<blti:launch_url>$CFG->wwwroot/enrol/lti/tool.php?id=$tool1->id</blti:launch_url>", $cartridge);
529
    }
530
}