Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   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   */
  26  class client_test extends advanced_testcase {
  27      /**
  28       * Uses the static dataset as feed-in
  29       *
  30       * @return array
  31       */
  32      public function map_response_provider(): array {
  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       */
  86      public function test_map_userinfo_to_fields(array $mapping, array $expected) {
  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          $method->setAccessible(true);
 113  
 114          $issuer = new \core\oauth2\issuer(0);
 115          $mockbuilder = $this->getMockBuilder('core\oauth2\client');
 116          $mockbuilder->onlyMethods(['get_userinfo_mapping']);
 117          $mockbuilder->setConstructorArgs([$issuer, "", ""]);
 118  
 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));
 124      }
 125  }