MDL-58898 oauth: Cover orphaned linked logins in unit tests
[moodle.git] / auth / oauth2 / tests / api_test.php
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/>.
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  */
25 defined('MOODLE_INTERNAL') || die();
27 global $CFG;
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 {
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();
45         $issuer = \core\oauth2\api::create_standard_issuer('google');
46         \core\oauth2\api::create_standard_issuer('microsoft');
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);
54         \core\oauth2\api::delete_issuer($issuer->get('id'));
56         $linkedlogins = \auth_oauth2\api::get_linked_logins($user->id, $issuer);
57         $this->assertCount(1, $linkedlogins);
59         \auth_oauth2\api::clean_orphaned_linked_logins();
61         $linkedlogins = \auth_oauth2\api::get_linked_logins($user->id, $issuer);
62         $this->assertCount(0, $linkedlogins);
64         $match = \auth_oauth2\api::match_username_to_user('banana', $issuer);
65         $this->assertFalse($match);
66     }
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();
75         $issuer1 = \core\oauth2\api::create_standard_issuer('google');
76         $issuer2 = \core\oauth2\api::create_standard_issuer('microsoft');
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);
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);
90         \core\oauth2\api::delete_issuer($issuer1->get('id'));
92         \auth_oauth2\api::clean_orphaned_linked_logins($issuer1->get('id'));
94         $linkedlogins = \auth_oauth2\api::get_linked_logins($user1->id, $issuer1);
95         $this->assertCount(0, $linkedlogins);
97         $linkedlogins = \auth_oauth2\api::get_linked_logins($user2->id, $issuer2);
98         $this->assertCount(1, $linkedlogins);
99     }