| 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 test client_test.
|
|
|
19 |
*
|
|
|
20 |
* Unit test for testable functions in core/oauth2/client.php
|
|
|
21 |
*
|
|
|
22 |
* @copyright 2021 Peter Dias
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
* @package core
|
|
|
25 |
*/
|
| 1441 |
ariadna |
26 |
final class client_test extends advanced_testcase {
|
| 1 |
efrain |
27 |
/**
|
|
|
28 |
* Uses the static dataset as feed-in
|
|
|
29 |
*
|
|
|
30 |
* @return array
|
|
|
31 |
*/
|
| 1441 |
ariadna |
32 |
public static function map_response_provider(): array {
|
| 1 |
efrain |
33 |
return [
|
|
|
34 |
"Nested objects syntax a-b-c syntax " => [
|
|
|
35 |
[
|
|
|
36 |
"name-firstname" => "firstname",
|
|
|
37 |
"contact-phone-home" => "homenumber",
|
|
|
38 |
], [
|
|
|
39 |
"firstname" => "John",
|
|
|
40 |
"homenumber" => "020000000",
|
|
|
41 |
]
|
|
|
42 |
],
|
|
|
43 |
"Nested objects syntax with array support a-b[0]-c syntax " => [
|
|
|
44 |
[
|
|
|
45 |
"name-firstname" => "firstname",
|
|
|
46 |
"contact-phone-home" => "homenumber",
|
|
|
47 |
"picture[0]-url" => "urltest",
|
|
|
48 |
], [
|
|
|
49 |
"firstname" => "John",
|
|
|
50 |
"homenumber" => "020000000",
|
|
|
51 |
"urltest" => "www.google.com",
|
|
|
52 |
]
|
|
|
53 |
],
|
|
|
54 |
"Nested objects syntax with array support a-b-0-c syntax " => [
|
|
|
55 |
[
|
|
|
56 |
"name-firstname" => "firstname",
|
|
|
57 |
"contact-phone-home" => "homenumber",
|
|
|
58 |
"picture-0-url" => "urltest",
|
|
|
59 |
], [
|
|
|
60 |
"firstname" => "John",
|
|
|
61 |
"homenumber" => "020000000",
|
|
|
62 |
"urltest" => "www.google.com",
|
|
|
63 |
]
|
|
|
64 |
],
|
|
|
65 |
"Nested objects syntax with array support a-b-0-c syntax with non-existent nodes" => [
|
|
|
66 |
[
|
|
|
67 |
"name-firstname" => "firstname",
|
|
|
68 |
"contact-phone-home" => "homenumber",
|
|
|
69 |
"picture-0-url-url" => "urltest",
|
|
|
70 |
], [
|
|
|
71 |
"firstname" => "John",
|
|
|
72 |
"homenumber" => "020000000",
|
|
|
73 |
]
|
|
|
74 |
],
|
|
|
75 |
];
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
/**
|
|
|
79 |
* Test the map_userinfo_to_fields function
|
|
|
80 |
*
|
|
|
81 |
* @dataProvider map_response_provider
|
|
|
82 |
* @param array $mapping
|
|
|
83 |
* @param array $expected
|
|
|
84 |
* @throws ReflectionException
|
|
|
85 |
*/
|
| 11 |
efrain |
86 |
public function test_map_userinfo_to_fields(array $mapping, array $expected): void {
|
| 1 |
efrain |
87 |
$dataset = [
|
|
|
88 |
"name" => (object) [
|
|
|
89 |
"firstname" => "John",
|
|
|
90 |
"lastname" => "Doe",
|
|
|
91 |
],
|
|
|
92 |
"contact" => (object) [
|
|
|
93 |
"email" => "john@example.com",
|
|
|
94 |
"phone" => (object) [
|
|
|
95 |
"mobile" => "010000000",
|
|
|
96 |
"home" => "020000000"
|
|
|
97 |
],
|
|
|
98 |
],
|
|
|
99 |
"picture" => [
|
|
|
100 |
[
|
|
|
101 |
"url" => "www.google.com",
|
|
|
102 |
"description" => "This is a URL",
|
|
|
103 |
],
|
|
|
104 |
[
|
|
|
105 |
"url" => "www.facebook.com",
|
|
|
106 |
"description" => "This is another URL",
|
|
|
107 |
]
|
|
|
108 |
]
|
|
|
109 |
];
|
|
|
110 |
|
|
|
111 |
$method = new ReflectionMethod("core\oauth2\client", "map_userinfo_to_fields");
|
|
|
112 |
|
|
|
113 |
$issuer = new \core\oauth2\issuer(0);
|
|
|
114 |
$mockbuilder = $this->getMockBuilder('core\oauth2\client');
|
|
|
115 |
$mockbuilder->onlyMethods(['get_userinfo_mapping']);
|
|
|
116 |
$mockbuilder->setConstructorArgs([$issuer, "", ""]);
|
|
|
117 |
|
|
|
118 |
$mock = $mockbuilder->getMock();
|
|
|
119 |
$mock->expects($this->once())
|
|
|
120 |
->method('get_userinfo_mapping')
|
|
|
121 |
->will($this->returnValue($mapping));
|
|
|
122 |
$this->assertSame($expected, $method->invoke($mock, (object) $dataset));
|
|
|
123 |
}
|
|
|
124 |
}
|