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.

Differences Between: [Versions 310 and 401] [Versions 39 and 401]

   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 auth_oauth2;
  18  
  19  /**
  20   * External auth oauth2 API tests.
  21   *
  22   * @package     auth_oauth2
  23   * @copyright   2017 Damyon Wiese
  24   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  class api_test extends \advanced_testcase {
  27  
  28      /**
  29       * Test the cleaning of orphaned linked logins for all issuers.
  30       */
  31      public function test_clean_orphaned_linked_logins() {
  32          $this->resetAfterTest();
  33          $this->setAdminUser();
  34  
  35          $issuer = \core\oauth2\api::create_standard_issuer('google');
  36          \core\oauth2\api::create_standard_issuer('microsoft');
  37  
  38          $user = $this->getDataGenerator()->create_user();
  39          $info = [];
  40          $info['username'] = 'banana';
  41          $info['email'] = 'banana@example.com';
  42          \auth_oauth2\api::link_login($info, $issuer, $user->id, false);
  43  
  44          \core\oauth2\api::delete_issuer($issuer->get('id'));
  45  
  46          $linkedlogins = \auth_oauth2\api::get_linked_logins($user->id, $issuer);
  47          $this->assertCount(1, $linkedlogins);
  48  
  49          \auth_oauth2\api::clean_orphaned_linked_logins();
  50  
  51          $linkedlogins = \auth_oauth2\api::get_linked_logins($user->id, $issuer);
  52          $this->assertCount(0, $linkedlogins);
  53  
  54          $match = \auth_oauth2\api::match_username_to_user('banana', $issuer);
  55          $this->assertFalse($match);
  56      }
  57  
  58      /**
  59       * Test the cleaning of orphaned linked logins for a specific issuer.
  60       */
  61      public function test_clean_orphaned_linked_logins_with_issuer_id() {
  62          $this->resetAfterTest();
  63          $this->setAdminUser();
  64  
  65          $issuer1 = \core\oauth2\api::create_standard_issuer('google');
  66          $issuer2 = \core\oauth2\api::create_standard_issuer('microsoft');
  67  
  68          $user1 = $this->getDataGenerator()->create_user();
  69          $info = [];
  70          $info['username'] = 'banana';
  71          $info['email'] = 'banana@example.com';
  72          \auth_oauth2\api::link_login($info, $issuer1, $user1->id, false);
  73  
  74          $user2 = $this->getDataGenerator()->create_user();
  75          $info = [];
  76          $info['username'] = 'apple';
  77          $info['email'] = 'apple@example.com';
  78          \auth_oauth2\api::link_login($info, $issuer2, $user2->id, false);
  79  
  80          \core\oauth2\api::delete_issuer($issuer1->get('id'));
  81  
  82          \auth_oauth2\api::clean_orphaned_linked_logins($issuer1->get('id'));
  83  
  84          $linkedlogins = \auth_oauth2\api::get_linked_logins($user1->id, $issuer1);
  85          $this->assertCount(0, $linkedlogins);
  86  
  87          $linkedlogins = \auth_oauth2\api::get_linked_logins($user2->id, $issuer2);
  88          $this->assertCount(1, $linkedlogins);
  89      }
  90  
  91      /**
  92       * Test creating a new confirmed account.
  93       * Including testing that user profile fields are correctly set.
  94       *
  95       * @covers \auth_oauth2\api::create_new_confirmed_account
  96       */
  97      public function test_create_new_confirmed_account() {
  98          global $DB;
  99          $this->resetAfterTest();
 100          $this->setAdminUser();
 101  
 102          $issuer = \core\oauth2\api::create_standard_issuer('microsoft');
 103  
 104          $info = [];
 105          $info['username'] = 'apple';
 106          $info['email'] = 'apple@example.com';
 107          $info['firstname'] = 'Apple';
 108          $info['lastname'] = 'Fruit';
 109          $info['alternatename'] = 'Beatles';
 110          $info['idnumber'] = '123456';
 111          $info['city'] = 'Melbourne';
 112          $info['country'] = 'AU';
 113          $info['institution'] = 'ACME Inc';
 114          $info['department'] = 'Misc Explosives';
 115  
 116          $createduser = \auth_oauth2\api::create_new_confirmed_account($info, $issuer);
 117  
 118          // Get actual user record from DB to check.
 119          $userdata = $DB->get_record('user', ['id' => $createduser->id]);
 120  
 121          // Confirm each value supplied from issuers is saved into the user record.
 122          foreach ($info as $key => $value) {
 123              $this->assertEquals($value, $userdata->$key);
 124          }
 125  
 126          // Explicitly test the user is confirmed.
 127          $this->assertEquals(1, $userdata->confirmed);
 128      }
 129  
 130      /**
 131       * Test auto-confirming linked logins.
 132       */
 133      public function test_linked_logins() {
 134          $this->resetAfterTest();
 135  
 136          $this->setAdminUser();
 137          $issuer = \core\oauth2\api::create_standard_issuer('google');
 138  
 139          $user = $this->getDataGenerator()->create_user();
 140  
 141          $info = [];
 142          $info['username'] = 'banana';
 143          $info['email'] = 'banana@example.com';
 144  
 145          \auth_oauth2\api::link_login($info, $issuer, $user->id, false);
 146  
 147          // Try and match a user with a linked login.
 148          $match = \auth_oauth2\api::match_username_to_user('banana', $issuer);
 149  
 150          $this->assertEquals($user->id, $match->get('userid'));
 151          $linkedlogins = \auth_oauth2\api::get_linked_logins($user->id, $issuer);
 152          \auth_oauth2\api::delete_linked_login($linkedlogins[0]->get('id'));
 153  
 154          $match = \auth_oauth2\api::match_username_to_user('banana', $issuer);
 155          $this->assertFalse($match);
 156  
 157          $info = [];
 158          $info['username'] = 'apple';
 159          $info['email'] = 'apple@example.com';
 160          $info['firstname'] = 'Apple';
 161          $info['lastname'] = 'Fruit';
 162          $info['url'] = 'http://apple.com/';
 163          $info['alternamename'] = 'Beatles';
 164  
 165          $newuser = \auth_oauth2\api::create_new_confirmed_account($info, $issuer);
 166  
 167          $match = \auth_oauth2\api::match_username_to_user('apple', $issuer);
 168  
 169          $this->assertEquals($newuser->id, $match->get('userid'));
 170      }
 171  
 172      /**
 173       * Test that is_enabled correctly identifies when the plugin is enabled.
 174       */
 175      public function test_is_enabled() {
 176          $this->resetAfterTest();
 177  
 178          set_config('auth', 'manual,oauth2');
 179          $this->assertTrue(\auth_oauth2\api::is_enabled());
 180      }
 181  
 182      /**
 183       * Test that is_enabled correctly identifies when the plugin is disabled.
 184       */
 185      public function test_is_enabled_disabled() {
 186          $this->resetAfterTest();
 187  
 188          set_config('auth', 'manual');
 189          $this->assertFalse(\auth_oauth2\api::is_enabled());
 190      }
 191  
 192      /**
 193       * Test creating a user via the send confirm account email method.
 194       * Including testing that user profile fields are correctly set.
 195       *
 196       * @covers \auth_oauth2\api::send_confirm_account_email
 197       */
 198      public function test_send_confirm_account_email() {
 199          global $DB;
 200          $this->resetAfterTest();
 201          $this->setAdminUser();
 202  
 203          $issuer = \core\oauth2\api::create_standard_issuer('microsoft');
 204  
 205          $info = [];
 206          $info['username'] = 'apple';
 207          $info['email'] = 'apple@example.com';
 208          $info['firstname'] = 'Apple';
 209          $info['lastname'] = 'Fruit';
 210          $info['alternatename'] = 'Beatles';
 211          $info['idnumber'] = '123456';
 212          $info['city'] = 'Melbourne';
 213          $info['country'] = 'AU';
 214          $info['institution'] = 'ACME Inc';
 215          $info['department'] = 'Misc Explosives';
 216  
 217          $createduser = \auth_oauth2\api::send_confirm_account_email($info, $issuer);
 218  
 219          // Get actual user record from DB to check.
 220          $userdata = $DB->get_record('user', ['id' => $createduser->id]);
 221  
 222          // Confirm each value supplied from issuers is saved into the user record.
 223          foreach ($info as $key => $value) {
 224              $this->assertEquals($value, $userdata->$key);
 225          }
 226  
 227          // Explicitly test the user is not yet confirmed.
 228          $this->assertEquals(0, $userdata->confirmed);
 229      }
 230  }