1 |
efrain |
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 core\external;
|
|
|
18 |
|
|
|
19 |
defined('MOODLE_INTERNAL') || die();
|
|
|
20 |
|
|
|
21 |
use core\oauth2\api;
|
|
|
22 |
use core_external\external_api;
|
|
|
23 |
use externallib_advanced_testcase;
|
|
|
24 |
|
|
|
25 |
global $CFG;
|
|
|
26 |
|
|
|
27 |
require_once($CFG->dirroot . '/lib/tests/moodlenet/helpers.php');
|
|
|
28 |
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* External functions test for moodlenet_auth_check.
|
|
|
32 |
*
|
|
|
33 |
* @package core
|
|
|
34 |
* @category test
|
|
|
35 |
* @copyright 2023 Huong Nguyen <huongnv13@gmail.com>
|
|
|
36 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
37 |
* @coversDefaultClass \core\external\moodlenet_auth_check
|
|
|
38 |
*/
|
|
|
39 |
class moodlenet_auth_check_test extends externallib_advanced_testcase {
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* Test the behaviour of moodlenet_auth_check().
|
|
|
43 |
*
|
|
|
44 |
* @covers ::execute
|
|
|
45 |
*/
|
11 |
efrain |
46 |
public function test_moodlenet_auth_check(): void {
|
1 |
efrain |
47 |
global $CFG;
|
|
|
48 |
$this->resetAfterTest();
|
|
|
49 |
$this->setAdminUser();
|
|
|
50 |
$CFG->enablesharingtomoodlenet = true;
|
|
|
51 |
|
|
|
52 |
// Generate data.
|
|
|
53 |
$generator = $this->getDataGenerator();
|
|
|
54 |
$course = $generator->create_course();
|
|
|
55 |
$user = $generator->create_user();
|
|
|
56 |
$generator->enrol_user($user->id, $course->id, 'student');
|
|
|
57 |
|
|
|
58 |
// Create dummy issuer.
|
|
|
59 |
$issuer = \core\moodlenet\helpers::get_mock_issuer(0);
|
|
|
60 |
|
|
|
61 |
// Test with the user does not have permission.
|
|
|
62 |
$this->setUser($user);
|
|
|
63 |
$result = moodlenet_auth_check::execute($issuer->get('id'), $course->id);
|
|
|
64 |
$result = external_api::clean_returnvalue(moodlenet_auth_check::execute_returns(), $result);
|
|
|
65 |
$this->assertFalse($result['status']);
|
|
|
66 |
$this->assertNotEmpty($result['warnings']);
|
|
|
67 |
$this->assertEquals('errorpermission', $result['warnings'][0]['warningcode']);
|
|
|
68 |
|
|
|
69 |
// Test with the issuer is not enabled.
|
|
|
70 |
$this->setAdminUser();
|
|
|
71 |
$result = moodlenet_auth_check::execute($issuer->get('id'), $course->id);
|
|
|
72 |
$result = external_api::clean_returnvalue(moodlenet_auth_check::execute_returns(), $result);
|
|
|
73 |
$this->assertFalse($result['status']);
|
|
|
74 |
$this->assertNotEmpty($result['warnings']);
|
|
|
75 |
$this->assertEquals('errorissuernotenabled', $result['warnings'][0]['warningcode']);
|
|
|
76 |
|
|
|
77 |
// Test with the issuer is enabled and not logged in.
|
|
|
78 |
$issuer->set('enabled', 1);
|
|
|
79 |
$irecord = $issuer->to_record();
|
|
|
80 |
api::update_issuer($irecord);
|
|
|
81 |
set_config('oauthservice', $issuer->get('id'), 'moodlenet');
|
|
|
82 |
$result = moodlenet_auth_check::execute($issuer->get('id'), $course->id);
|
|
|
83 |
$result = external_api::clean_returnvalue(moodlenet_auth_check::execute_returns(), $result);
|
|
|
84 |
$this->assertFalse($result['status']);
|
|
|
85 |
$this->assertNotEmpty($result['loginurl']);
|
|
|
86 |
}
|
|
|
87 |
}
|