Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]
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 * Auth oauth2 api functions tests. 19 * 20 * @package auth_oauth2 21 * @copyright 2017 Damyon Wiese 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 defined('MOODLE_INTERNAL') || die(); 26 27 global $CFG; 28 29 /** 30 * External auth oauth2 API tests. 31 * 32 * @package auth_oauth2 33 * @copyright 2017 Damyon Wiese 34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 35 */ 36 class auth_oauth2_external_testcase extends advanced_testcase { 37 38 /** 39 * Test the cleaning of orphaned linked logins for all issuers. 40 */ 41 public function test_clean_orphaned_linked_logins() { 42 $this->resetAfterTest(); 43 $this->setAdminUser(); 44 45 $issuer = \core\oauth2\api::create_standard_issuer('google'); 46 \core\oauth2\api::create_standard_issuer('microsoft'); 47 48 $user = $this->getDataGenerator()->create_user(); 49 $info = []; 50 $info['username'] = 'banana'; 51 $info['email'] = 'banana@example.com'; 52 \auth_oauth2\api::link_login($info, $issuer, $user->id, false); 53 54 \core\oauth2\api::delete_issuer($issuer->get('id')); 55 56 $linkedlogins = \auth_oauth2\api::get_linked_logins($user->id, $issuer); 57 $this->assertCount(1, $linkedlogins); 58 59 \auth_oauth2\api::clean_orphaned_linked_logins(); 60 61 $linkedlogins = \auth_oauth2\api::get_linked_logins($user->id, $issuer); 62 $this->assertCount(0, $linkedlogins); 63 64 $match = \auth_oauth2\api::match_username_to_user('banana', $issuer); 65 $this->assertFalse($match); 66 } 67 68 /** 69 * Test the cleaning of orphaned linked logins for a specific issuer. 70 */ 71 public function test_clean_orphaned_linked_logins_with_issuer_id() { 72 $this->resetAfterTest(); 73 $this->setAdminUser(); 74 75 $issuer1 = \core\oauth2\api::create_standard_issuer('google'); 76 $issuer2 = \core\oauth2\api::create_standard_issuer('microsoft'); 77 78 $user1 = $this->getDataGenerator()->create_user(); 79 $info = []; 80 $info['username'] = 'banana'; 81 $info['email'] = 'banana@example.com'; 82 \auth_oauth2\api::link_login($info, $issuer1, $user1->id, false); 83 84 $user2 = $this->getDataGenerator()->create_user(); 85 $info = []; 86 $info['username'] = 'apple'; 87 $info['email'] = 'apple@example.com'; 88 \auth_oauth2\api::link_login($info, $issuer2, $user2->id, false); 89 90 \core\oauth2\api::delete_issuer($issuer1->get('id')); 91 92 \auth_oauth2\api::clean_orphaned_linked_logins($issuer1->get('id')); 93 94 $linkedlogins = \auth_oauth2\api::get_linked_logins($user1->id, $issuer1); 95 $this->assertCount(0, $linkedlogins); 96 97 $linkedlogins = \auth_oauth2\api::get_linked_logins($user2->id, $issuer2); 98 $this->assertCount(1, $linkedlogins); 99 } 100 101 /** 102 * Test auto-confirming linked logins. 103 */ 104 public function test_linked_logins() { 105 $this->resetAfterTest(); 106 107 $this->setAdminUser(); 108 $issuer = \core\oauth2\api::create_standard_issuer('google'); 109 110 $user = $this->getDataGenerator()->create_user(); 111 112 $info = []; 113 $info['username'] = 'banana'; 114 $info['email'] = 'banana@example.com'; 115 116 \auth_oauth2\api::link_login($info, $issuer, $user->id, false); 117 118 // Try and match a user with a linked login. 119 $match = \auth_oauth2\api::match_username_to_user('banana', $issuer); 120 121 $this->assertEquals($user->id, $match->get('userid')); 122 $linkedlogins = \auth_oauth2\api::get_linked_logins($user->id, $issuer); 123 \auth_oauth2\api::delete_linked_login($linkedlogins[0]->get('id')); 124 125 $match = \auth_oauth2\api::match_username_to_user('banana', $issuer); 126 $this->assertFalse($match); 127 128 $info = []; 129 $info['username'] = 'apple'; 130 $info['email'] = 'apple@example.com'; 131 $info['firstname'] = 'Apple'; 132 $info['lastname'] = 'Fruit'; 133 $info['url'] = 'http://apple.com/'; 134 $info['alternamename'] = 'Beatles'; 135 136 $newuser = \auth_oauth2\api::create_new_confirmed_account($info, $issuer); 137 138 $match = \auth_oauth2\api::match_username_to_user('apple', $issuer); 139 140 $this->assertEquals($newuser->id, $match->get('userid')); 141 } 142 143 /** 144 * Test that is_enabled correctly identifies when the plugin is enabled. 145 */ 146 public function test_is_enabled() { 147 $this->resetAfterTest(); 148 149 set_config('auth', 'manual,oauth2'); 150 $this->assertTrue(\auth_oauth2\api::is_enabled()); 151 } 152 153 /** 154 * Test that is_enabled correctly identifies when the plugin is disabled. 155 */ 156 public function test_is_enabled_disabled() { 157 $this->resetAfterTest(); 158 159 set_config('auth', 'manual'); 160 $this->assertFalse(\auth_oauth2\api::is_enabled()); 161 } 162 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body