MDL-79059 core: Use full name as alt text for user picture links
[moodle.git] / lib / tests / client_test.php
blob2f6a71838476a93c0a87c213da31c27cbe562e05
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.
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/>.
17 /**
18 * Unit test client_test.
20 * Unit test for testable functions in core/oauth2/client.php
22 * @copyright 2021 Peter Dias
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 * @package core
26 class client_test extends advanced_testcase {
27 /**
28 * Uses the static dataset as feed-in
30 * @return array
32 public function map_response_provider(): array {
33 return [
34 "Nested objects syntax a-b-c syntax " => [
36 "name-firstname" => "firstname",
37 "contact-phone-home" => "homenumber",
38 ], [
39 "firstname" => "John",
40 "homenumber" => "020000000",
43 "Nested objects syntax with array support a-b[0]-c syntax " => [
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",
54 "Nested objects syntax with array support a-b-0-c syntax " => [
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",
65 "Nested objects syntax with array support a-b-0-c syntax with non-existent nodes" => [
67 "name-firstname" => "firstname",
68 "contact-phone-home" => "homenumber",
69 "picture-0-url-url" => "urltest",
70 ], [
71 "firstname" => "John",
72 "homenumber" => "020000000",
78 /**
79 * Test the map_userinfo_to_fields function
81 * @dataProvider map_response_provider
82 * @param array $mapping
83 * @param array $expected
84 * @throws ReflectionException
86 public function test_map_userinfo_to_fields(array $mapping, array $expected) {
87 $dataset = [
88 "name" => (object) [
89 "firstname" => "John",
90 "lastname" => "Doe",
92 "contact" => (object) [
93 "email" => "john@example.com",
94 "phone" => (object) [
95 "mobile" => "010000000",
96 "home" => "020000000"
99 "picture" => [
101 "url" => "www.google.com",
102 "description" => "This is a URL",
105 "url" => "www.facebook.com",
106 "description" => "This is another URL",
111 $method = new ReflectionMethod("core\oauth2\client", "map_userinfo_to_fields");
112 $method->setAccessible(true);
114 $issuer = new \core\oauth2\issuer(0);
115 $mockbuilder = $this->getMockBuilder('core\oauth2\client');
116 $mockbuilder->onlyMethods(['get_userinfo_mapping']);
117 $mockbuilder->setConstructorArgs([$issuer, "", ""]);
119 $mock = $mockbuilder->getMock();
120 $mock->expects($this->once())
121 ->method('get_userinfo_mapping')
122 ->will($this->returnValue($mapping));
123 $this->assertSame($expected, $method->invoke($mock, (object) $dataset));