first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
+285
View File
@@ -0,0 +1,285 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core\external\check;
defined('MOODLE_INTERNAL') || die();
use admin_category;
use admin_root;
use admin_setting_check;
use admin_settingpage;
use core\check\result;
use externallib_advanced_testcase;
use required_capability_exception;
use context_system;
use core\check\access\guestrole;
use core\check\check;
use core\check\external\get_result_admintree;
use core\check\security\passwordpolicy;
use ReflectionMethod;
global $CFG;
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
require_once($CFG->libdir . '/adminlib.php');
/**
* Unit tests check API get_result webservice
*
* @package core
* @covers \core\check\external\get_result_admintree
* @author Matthew Hilton <matthewhilton@catalyst-au.net>
* @copyright Catalyst IT, 2023
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class get_result_admintree_test extends externallib_advanced_testcase {
/**
* Sets up admin tree for the given settings.
*
* @param array $settings array of admin_settings. Each will be placed into a single category, but each on their own page.
* @return admin_root admin root that was created.
*/
private function setup_admin_tree(array $settings) {
$root = new admin_root(true);
$category = new admin_category('testcategory', 'testcategory');
$root->add('root', $category);
foreach ($settings as $i => $setting) {
$page = new admin_settingpage('testpage_' . $i, 'testpage');
$page->add($setting);
$root->add('testcategory', $page);
}
return $root;
}
/**
* Provides values to execute_test
*
* @return array
*/
public static function execute_options_provider(): array {
return [
'get check result (ok, no details)' => [
'triggererror' => false,
'check' => new passwordpolicy(),
'includedetails' => false,
'expectedreturn' => [
'status' => result::OK,
'summary' => get_string('check_passwordpolicy_ok', 'report_security'),
// Note for details and html, null = not returned and anything else will simply check something was returned.
'details' => null,
'html' => '',
],
],
'get check result (ok, with details)' => [
'triggererror' => false,
'check' => new passwordpolicy(),
'includedetails' => true,
'expectedreturn' => [
'status' => result::OK,
'summary' => get_string('check_passwordpolicy_ok', 'report_security'),
// Note for details and html, null = not returned and anything else will simply check something was returned.
'details' => '',
'html' => '',
],
],
'get check result (error, no details)' => [
'triggererror' => true,
'check' => new passwordpolicy(),
'includedetails' => false,
'expectedreturn' => [
'status' => result::WARNING,
'summary' => get_string('check_passwordpolicy_error', 'report_security'),
// Note for details and html, null = not returned and anything else will simply check something was returned.
'details' => null,
'html' => '',
],
],
'get check result (error, with details)' => [
'triggererror' => true,
'check' => new passwordpolicy(),
'includedetails' => true,
'expectedreturn' => [
'status' => result::WARNING,
'summary' => get_string('check_passwordpolicy_error', 'report_security'),
// Note for details and html, null = not returned and anything else will simply check something was returned.
'details' => '',
'html' => '',
],
],
];
}
/**
* Tests the execute function
*
* @param bool $triggererror If the test should setup the conditions so that the check will fail
* @param check $check Check to use
* @param bool $includedetails if details are included
* @param array $expectedreturn an array of key value pairs. For each key, if the value is null it expects the
* webservice to not return it. If it has a value, it checks that that value was inside what was returned from the webservice.
* @dataProvider execute_options_provider
*/
public function test_execute_options(bool $triggererror, check $check, bool $includedetails, array $expectedreturn): void {
global $CFG;
$this->resetAfterTest(true);
$this->setAdminUser();
// This makes the check we test (password policy) warn or be OK depending on the test.
$CFG->passwordpolicy = $triggererror ? false : true;
// Add the admin setting.
$checksetting = new admin_setting_check('core/testcheck', $check);
$root = $this->setup_admin_tree([$checksetting]);
// Execute the ws function.
$this->setAdminUser();
$wsresult = (object) get_result_admintree::execute($checksetting->get_id(), $checksetting->name, $includedetails, $root);
foreach ($expectedreturn as $key => $expectedvalue) {
// If the expected result is null, ensure the return value was also null.
if (is_null($expectedvalue)) {
$this->assertTrue(empty($wsresult->$key));
}
// If the expected result is set, ensure it is contained in the return value.
if (!is_null($expectedvalue)) {
$this->assertTrue(!empty($wsresult->$key));
$this->assertStringContainsString($expectedvalue, $wsresult->$key);
}
}
}
/**
* Provides values to test_find_check_from_settings_tree
*
* @return array
*/
public static function find_check_from_setting_tree_provider(): array {
$testsetting1 = new admin_setting_check('testsetting', new passwordpolicy());
return [
'setting is in tree, correctly linked' => [
'settings' => [
$testsetting1,
new admin_setting_check('testsetting2', new guestrole()),
new admin_setting_check('testsetting3', new guestrole()),
],
'searchname' => $testsetting1->name,
'searchid' => $testsetting1->get_id(),
'expectedcheck' => passwordpolicy::class,
],
'setting is not in tree' => [
'settings' => [],
'searchname' => $testsetting1->name,
'searchid' => $testsetting1->get_id(),
'expectedcheck' => '',
],
'setting in tree, but name has conflict' => [
'settings' => [
$testsetting1,
new admin_setting_check('testsetting', new guestrole()),
],
'searchname' => $testsetting1->name,
'searchid' => $testsetting1->get_id(),
// Because the two settings have the same name + id, its impossible to tell them apart.
// So the check should not be returned.
'expectedcheck' => '',
],
];
}
/**
* Tests finding the check using the admin tree in various situations.
*
* @param array $settings array of admin_settings to setup
* @param string $searchname name of setting to search for
* @param string $searchid id of setting to search for
* @param string $expectedcheck class name of expected check to be found. If empty, expects that none was found.
* @dataProvider find_check_from_setting_tree_provider
*/
public function test_find_check_from_setting_tree(array $settings, string $searchname, string $searchid,
string $expectedcheck): void {
$this->resetAfterTest(true);
$this->setAdminUser();
$root = $this->setup_admin_tree($settings);
$method = new ReflectionMethod(get_result_admintree::class, 'get_check_from_setting');
$result = $method->invoke(new get_result_admintree(), $searchid, $searchname, $root);
if (!empty($expectedcheck)) {
$this->assertInstanceOf($expectedcheck, $result);
} else {
$this->assertEmpty($result);
}
}
/**
* Provides values to test_capability_check
*
* @return array
*/
public static function capability_check_provider(): array {
return [
'has permission' => [
'permission' => CAP_ALLOW,
'expectedexception' => null,
],
'does not have permission' => [
'permission' => CAP_PROHIBIT,
'expectedexception' => required_capability_exception::class,
],
];
}
/**
* Tests that capabilites are being checked correctly by the webservice.
*
* @param int $permission the permission level to assign the capability to the role for.
* @param string|null $expectedexception Exception class expected, or null if none is expected.
* @dataProvider capability_check_provider
*/
public function test_capability_check($permission, $expectedexception): void {
$this->resetAfterTest(true);
$user = $this->getDataGenerator()->create_user();
$role = $this->getDataGenerator()->create_role();
role_assign($role, $user->id, context_system::instance()->id);
role_change_permission($role, context_system::instance(), 'moodle/site:config', $permission);
if (!empty($expectedexception)) {
$this->expectException($expectedexception);
}
// Setup check setting as admin.
$this->setAdminUser();
$checksetting = new admin_setting_check('core/testcheck', new passwordpolicy());
$root = $this->setup_admin_tree([$checksetting]);
$this->setUser($user);
get_result_admintree::execute($checksetting->get_id(), $checksetting->name, false, $root);
}
}
+53
View File
@@ -0,0 +1,53 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
declare(strict_types=1);
namespace core\external;
use core_external\external_api;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
require_once($CFG->dirroot . '/lib/tests/fixtures/testeable_dynamic_tab.php');
/**
* Unit tests external dynamic tabs get content
*
* @package core
* @covers \core\external\dynamic_tabs_get_content
* @copyright 2021 David Matamoros <davidmc@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class dynamic_tabs_get_content_test extends \externallib_advanced_testcase {
/**
* Text execute method
*/
public function test_execute(): void {
$this->resetAfterTest();
$this->setAdminUser();
$result = dynamic_tabs_get_content::execute(testeable_dynamic_tab::class, json_encode([]));
$result = external_api::clean_returnvalue(dynamic_tabs_get_content::execute_returns(), $result);
$this->assertEquals('templates/tabs/mytab', $result['template']);
$this->assertEquals(json_encode(['content' => get_string('content')]), $result['content']);
$this->assertNotEmpty($result['javascript']);
$this->assertStringStartsWith('<script>', $result['javascript']);
}
}
+87
View File
@@ -0,0 +1,87 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core\external;
defined('MOODLE_INTERNAL') || die();
use core\oauth2\api;
use core_external\external_api;
use externallib_advanced_testcase;
global $CFG;
require_once($CFG->dirroot . '/lib/tests/moodlenet/helpers.php');
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
/**
* External functions test for moodlenet_auth_check.
*
* @package core
* @category test
* @copyright 2023 Huong Nguyen <huongnv13@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \core\external\moodlenet_auth_check
*/
class moodlenet_auth_check_test extends externallib_advanced_testcase {
/**
* Test the behaviour of moodlenet_auth_check().
*
* @covers ::execute
*/
public function test_moodlenet_auth_check(): void {
global $CFG;
$this->resetAfterTest();
$this->setAdminUser();
$CFG->enablesharingtomoodlenet = true;
// Generate data.
$generator = $this->getDataGenerator();
$course = $generator->create_course();
$user = $generator->create_user();
$generator->enrol_user($user->id, $course->id, 'student');
// Create dummy issuer.
$issuer = \core\moodlenet\helpers::get_mock_issuer(0);
// Test with the user does not have permission.
$this->setUser($user);
$result = moodlenet_auth_check::execute($issuer->get('id'), $course->id);
$result = external_api::clean_returnvalue(moodlenet_auth_check::execute_returns(), $result);
$this->assertFalse($result['status']);
$this->assertNotEmpty($result['warnings']);
$this->assertEquals('errorpermission', $result['warnings'][0]['warningcode']);
// Test with the issuer is not enabled.
$this->setAdminUser();
$result = moodlenet_auth_check::execute($issuer->get('id'), $course->id);
$result = external_api::clean_returnvalue(moodlenet_auth_check::execute_returns(), $result);
$this->assertFalse($result['status']);
$this->assertNotEmpty($result['warnings']);
$this->assertEquals('errorissuernotenabled', $result['warnings'][0]['warningcode']);
// Test with the issuer is enabled and not logged in.
$issuer->set('enabled', 1);
$irecord = $issuer->to_record();
api::update_issuer($irecord);
set_config('oauthservice', $issuer->get('id'), 'moodlenet');
$result = moodlenet_auth_check::execute($issuer->get('id'), $course->id);
$result = external_api::clean_returnvalue(moodlenet_auth_check::execute_returns(), $result);
$this->assertFalse($result['status']);
$this->assertNotEmpty($result['loginurl']);
}
}
@@ -0,0 +1,140 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core\external;
use core\oauth2\api;
use core_external\external_api;
use externallib_advanced_testcase;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/lib/tests/moodlenet/helpers.php');
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
/**
* External functions test for moodlenet_get_share_info_activity.
*
* @package core
* @category test
* @copyright 2023 Huong Nguyen <huongnv13@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \core\external\moodlenet_get_share_info_activity
*/
class moodlenet_get_share_info_activity_test extends externallib_advanced_testcase {
/**
* Test the behaviour of moodlenet_get_share_info_activity().
* @covers ::execute
*/
public function test_moodlenet_get_share_info_activity(): void {
global $CFG;
$this->resetAfterTest();
$this->setAdminUser();
$CFG->enablesharingtomoodlenet = true;
// Generate course and activities.
$course = $this->getDataGenerator()->create_course();
$activity1 = $this->getDataGenerator()->create_module('chat', ['course' => $course->id, 'name' => 'Chat activity']);
$activity2 = $this->getDataGenerator()->create_module('assign', ['course' => $course->id, 'name' => 'Assign activity']);
$activity3 = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id, 'name' => 'Quiz activity']);
// Create dummy enabled issuer.
$issuer = \core\moodlenet\helpers::get_mock_issuer(1);
// Test the 1st activity with no OAuth2 setup yet.
$result = moodlenet_get_share_info_activity::execute($activity1->cmid);
$result = external_api::clean_returnvalue(moodlenet_get_share_info_activity::execute_returns(), $result);
$this->assertFalse($result['status']);
$this->assertEmpty($result['name']);
$this->assertEmpty($result['type']);
$this->assertEmpty($result['server']);
$this->assertEmpty($result['supportpageurl']);
$this->assertNotEmpty($result['warnings']);
$this->assertEquals(0, $result['warnings'][0]['item']);
$this->assertEquals('errorissuernotset', $result['warnings'][0]['warningcode']);
$this->assertEquals(get_string('moodlenet:issuerisnotset', 'moodle'), $result['warnings'][0]['message']);
// Test the 1st activity with OAuth2 disabled.
set_config('oauthservice', $issuer->get('id'), 'moodlenet');
$issuer->set('enabled', 0);
$irecord = $issuer->to_record();
api::update_issuer($irecord);
$result = moodlenet_get_share_info_activity::execute($activity1->cmid);
$result = external_api::clean_returnvalue(moodlenet_get_share_info_activity::execute_returns(), $result);
$this->assertFalse($result['status']);
$this->assertEmpty($result['name']);
$this->assertEmpty($result['type']);
$this->assertEmpty($result['server']);
$this->assertEmpty($result['supportpageurl']);
$this->assertNotEmpty($result['warnings']);
$this->assertEquals($issuer->get('id'), $result['warnings'][0]['item']);
$this->assertEquals('errorissuernotenabled', $result['warnings'][0]['warningcode']);
$this->assertEquals(get_string('moodlenet:issuerisnotenabled', 'moodle'), $result['warnings'][0]['message']);
// Test the 1st activity with support url is set to the internal contact site support page.
$issuer->set('enabled', 1);
$irecord = $issuer->to_record();
api::update_issuer($irecord);
$expectedsupporturl = $CFG->wwwroot . '/user/contactsitesupport.php';
$result = moodlenet_get_share_info_activity::execute($activity1->cmid);
$result = external_api::clean_returnvalue(moodlenet_get_share_info_activity::execute_returns(), $result);
$this->assertTrue($result['status']);
$this->assertEquals($activity1->name, $result['name']);
$this->assertEquals(get_string('modulename', 'mod_chat'), $result['type']);
$this->assertEquals($issuer->get_display_name(), $result['server']);
$this->assertEquals($expectedsupporturl, $result['supportpageurl']);
// Test the 2nd activity with support url is set to the external contact site support page.
$expectedsupporturl = 'https://moodle.org/';
$CFG->supportpage = $expectedsupporturl;
$result = moodlenet_get_share_info_activity::execute($activity2->cmid);
$result = external_api::clean_returnvalue(moodlenet_get_share_info_activity::execute_returns(), $result);
$this->assertTrue($result['status']);
$this->assertEquals($activity2->name, $result['name']);
$this->assertEquals(get_string('modulename', 'mod_assign'), $result['type']);
$this->assertEquals($expectedsupporturl, $result['supportpageurl']);
// Test the 3rd activity with contact site support is disabled.
$CFG->supportavailability = CONTACT_SUPPORT_DISABLED;
$result = moodlenet_get_share_info_activity::execute($activity3->cmid);
$result = external_api::clean_returnvalue(moodlenet_get_share_info_activity::execute_returns(), $result);
$this->assertTrue($result['status']);
$this->assertEquals($activity3->name, $result['name']);
$this->assertEquals(get_string('modulename', 'mod_quiz'), $result['type']);
$this->assertEmpty($result['supportpageurl']);
// Test with an invalid activity.
// Get a random cmid that not in the created activity list.
$cmids = [$activity1->cmid, $activity2->cmid, $activity3->cmid];
do {
$randomcmid = random_int(5, 25);
} while (in_array($randomcmid, $cmids));
$result = moodlenet_get_share_info_activity::execute($randomcmid);
$result = external_api::clean_returnvalue(moodlenet_get_share_info_activity::execute_returns(), $result);
$this->assertFalse($result['status']);
$this->assertEmpty($result['name']);
$this->assertEmpty($result['type']);
$this->assertNotEmpty($result['warnings']);
$this->assertEquals($randomcmid, $result['warnings'][0]['item']);
$this->assertEquals('errorgettingactivityinformation', $result['warnings'][0]['warningcode']);
$this->assertEquals(get_string('invalidcoursemodule', 'error'), $result['warnings'][0]['message']);
}
}
@@ -0,0 +1,103 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core\external;
use core\oauth2\api;
use core_external\external_api;
use externallib_advanced_testcase;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/lib/tests/moodlenet/helpers.php');
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
/**
* External functions test for moodlenet_get_shared_course_info.
*
* @package core
* @category test
* @copyright 2023 Safat Shahin <safat.shahin@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \core\external\moodlenet_get_shared_course_info
*/
class moodlenet_get_shared_course_info_test extends externallib_advanced_testcase {
/**
* Test the behaviour of moodlenet_get_shared_course_info().
*
* @covers ::execute
*/
public function test_moodlenet_get_shared_course_info(): void {
global $CFG;
$this->resetAfterTest();
$this->setAdminUser();
$CFG->enablesharingtomoodlenet = true;
// Generate course and activities.
$course = $this->getDataGenerator()->create_course();
// Create dummy enabled issuer.
$issuer = \core\moodlenet\helpers::get_mock_issuer(1);
// Test the course with no OAuth2 setup yet.
$result = moodlenet_get_shared_course_info::execute($course->id);
$result = external_api::clean_returnvalue(moodlenet_get_shared_course_info::execute_returns(), $result);
$this->assertFalse($result['status']);
$this->assertEmpty($result['name']);
$this->assertEmpty($result['type']);
$this->assertEmpty($result['server']);
$this->assertEmpty($result['supportpageurl']);
$this->assertNotEmpty($result['warnings']);
$this->assertEquals(0, $result['warnings'][0]['item']);
$this->assertEquals('errorissuernotset', $result['warnings'][0]['warningcode']);
$this->assertEquals(get_string('moodlenet:issuerisnotset', 'moodle'), $result['warnings'][0]['message']);
// Test the course with OAuth2 disabled.
set_config('oauthservice', $issuer->get('id'), 'moodlenet');
$issuer->set('enabled', 0);
$irecord = $issuer->to_record();
api::update_issuer($irecord);
$result = moodlenet_get_shared_course_info::execute($course->id);
$result = external_api::clean_returnvalue(moodlenet_get_shared_course_info::execute_returns(), $result);
$this->assertFalse($result['status']);
$this->assertEmpty($result['name']);
$this->assertEmpty($result['type']);
$this->assertEmpty($result['server']);
$this->assertEmpty($result['supportpageurl']);
$this->assertNotEmpty($result['warnings']);
$this->assertEquals($issuer->get('id'), $result['warnings'][0]['item']);
$this->assertEquals('errorissuernotenabled', $result['warnings'][0]['warningcode']);
$this->assertEquals(get_string('moodlenet:issuerisnotenabled', 'moodle'), $result['warnings'][0]['message']);
// Test the course with support url is set to the internal contact site support page.
$issuer->set('enabled', 1);
$irecord = $issuer->to_record();
api::update_issuer($irecord);
$expectedsupporturl = $CFG->wwwroot . '/user/contactsitesupport.php';
$result = moodlenet_get_shared_course_info::execute($course->id);
$result = external_api::clean_returnvalue(moodlenet_get_shared_course_info::execute_returns(), $result);
$this->assertTrue($result['status']);
$this->assertEquals($course->fullname, $result['name']);
$this->assertEquals(get_string('course'), $result['type']);
$this->assertEquals($issuer->get_display_name(), $result['server']);
$this->assertEquals($expectedsupporturl, $result['supportpageurl']);
}
}
+171
View File
@@ -0,0 +1,171 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core\external;
use core\oauth2\api;
use core_external\external_api;
use externallib_advanced_testcase;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/lib/tests/moodlenet/helpers.php');
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
/**
* External functions test for moodlenet_send_activity.
*
* @package core
* @category test
* @copyright 2023 Huong Nguyen <huongnv13@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \core\external\moodlenet_send_activity
*/
class moodlenet_send_activity_test extends externallib_advanced_testcase {
/**
* Test the behaviour of moodlenet_send_activity().
*
* @covers ::execute
*/
public function test_moodlenet_send_activity(): void {
global $CFG;
$this->resetAfterTest();
$this->setAdminUser();
// Generate data.
$generator = $this->getDataGenerator();
$course = $generator->create_course();
$moduleinstance = $generator->create_module('assign', ['course' => $course->id]);
$user = $generator->create_user();
$generator->enrol_user($user->id, $course->id, 'student');
// Create dummy issuer.
$issuer = \core\moodlenet\helpers::get_mock_issuer(0);
// Test with the experimental flag off.
$result = moodlenet_send_activity::execute($issuer->get('id'), $moduleinstance->cmid, 0);
$result = external_api::clean_returnvalue(moodlenet_send_activity::execute_returns(), $result);
$this->assertFalse($result['status']);
$this->assertNotEmpty($result['warnings']);
$this->assertEquals('errorissuernotenabled', $result['warnings'][0]['warningcode']);
$CFG->enablesharingtomoodlenet = true;
// Test with invalid format.
$result = moodlenet_send_activity::execute($issuer->get('id'), $moduleinstance->cmid, 5);
$result = external_api::clean_returnvalue(moodlenet_send_activity::execute_returns(), $result);
$this->assertFalse($result['status']);
$this->assertNotEmpty($result['warnings']);
$this->assertEquals('errorinvalidformat', $result['warnings'][0]['warningcode']);
// Test with the user does not have permission.
$this->setUser($user);
$result = moodlenet_send_activity::execute($issuer->get('id'), $moduleinstance->cmid, 0);
$result = external_api::clean_returnvalue(moodlenet_send_activity::execute_returns(), $result);
$this->assertFalse($result['status']);
$this->assertNotEmpty($result['warnings']);
$this->assertEquals('errorpermission', $result['warnings'][0]['warningcode']);
$this->setAdminUser();
// Test with the issuer is not enabled.
$result = moodlenet_send_activity::execute($issuer->get('id'), $moduleinstance->cmid, 0);
$result = external_api::clean_returnvalue(moodlenet_send_activity::execute_returns(), $result);
$this->assertFalse($result['status']);
$this->assertNotEmpty($result['warnings']);
$this->assertEquals('errorissuernotenabled', $result['warnings'][0]['warningcode']);
// Test with the issuer is enabled but not set in the MN Outbound setting.
$issuer->set('enabled', 1);
$irecord = $issuer->to_record();
api::update_issuer($irecord);
$result = moodlenet_send_activity::execute($issuer->get('id'), $moduleinstance->cmid, 0);
$result = external_api::clean_returnvalue(moodlenet_send_activity::execute_returns(), $result);
$this->assertFalse($result['status']);
$this->assertNotEmpty($result['warnings']);
$this->assertEquals('errorissuernotenabled', $result['warnings'][0]['warningcode']);
set_config('oauthservice', $issuer->get('id'), 'moodlenet');
// Test with the issuer not yet authorized.
$result = moodlenet_send_activity::execute($issuer->get('id'), $moduleinstance->cmid, 0);
$result = external_api::clean_returnvalue(moodlenet_send_activity::execute_returns(), $result);
$this->assertFalse($result['status']);
$this->assertNotEmpty($result['warnings']);
$this->assertEquals('erroroauthclient', $result['warnings'][0]['warningcode']);
$this->assertEquals($issuer->get('id'), $result['warnings'][0]['item']);
$this->assertEquals(get_string('moodlenet:issuerisnotauthorized', 'moodle'), $result['warnings'][0]['message']);
}
/**
* Test execute_returns() method.
*
* @dataProvider return_resource_url_provider
* @covers ::execute_returns
*/
public function test_moodlenet_send_activity_return_resource_url(bool $state, string $resourceurl): void {
$this->resetAfterTest();
// Create dummy result with the resourceurl.
$result = [
'status' => true,
'resourceurl' => $resourceurl,
'warnings' => [],
];
if (!$state) {
$this->expectException(\invalid_response_exception::class);
}
$result = external_api::clean_returnvalue(moodlenet_send_activity::execute_returns(), $result);
if ($state) {
$this->assertEquals($resourceurl, $result['resourceurl']);
}
}
/**
* Provider for test_moodlenet_send_activity_return_resource_url().
*
* @return array Test data.
*/
public function return_resource_url_provider(): array {
return [
'Success 1' => [
true,
'https://moodlenet.example.com/drafts/view/testactivity_backup.mbz',
],
'Success 2' => [
true,
'https://moodlenet.example.com/drafts/view/testactivity_backup with spaces.mbz',
],
'Success 3' => [
true,
'https://moodlenet.example.com/drafts/view/testactivity_backup with " character.mbz',
],
'Success 4' => [
true,
"https://moodlenet.example.com/drafts/view/testactivity_backup with ' character.mbz",
],
'Success 5' => [
true,
'https://moodlenet.example.com/drafts/view/testactivity_backup with < and > characters.mbz',
],
'Fail 1' => [
false,
'https://moodlenet.example.com/drafts/view/testactivity_backupwith<lang lang="en">a<a</lang>html.mbz',
],
];
}
}
+177
View File
@@ -0,0 +1,177 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core\external;
use core\oauth2\api;
use core_external\external_api;
use externallib_advanced_testcase;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/lib/tests/moodlenet/helpers.php');
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
/**
* External functions test for moodlenet_send_course.
*
* @package core
* @category test
* @copyright 2023 Safat Shahin <safat.shahin@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \core\external\moodlenet_send_course
*/
class moodlenet_send_course_test extends externallib_advanced_testcase {
/**
* Test the behaviour of moodlenet_send_course().
*
* @covers ::execute
*/
public function test_moodlenet_send_course(): void {
global $CFG;
$this->resetAfterTest();
$this->setAdminUser();
// Generate data.
$generator = $this->getDataGenerator();
$course = $generator->create_course();
$user = $generator->create_user();
$generator->enrol_user($user->id, $course->id, 'student');
// Create dummy issuer.
$issuer = \core\moodlenet\helpers::get_mock_issuer(0);
// Test with the experimental flag off.
$result = moodlenet_send_course::execute($issuer->get('id'), $course->id, 0);
$result = external_api::clean_returnvalue(moodlenet_send_course::execute_returns(), $result);
$this->assertFalse($result['status']);
$this->assertNotEmpty($result['warnings']);
$this->assertEquals('errorissuernotenabled', $result['warnings'][0]['warningcode']);
$CFG->enablesharingtomoodlenet = true;
// Test with invalid format.
$result = moodlenet_send_course::execute($issuer->get('id'), $course->id, 5);
$result = external_api::clean_returnvalue(moodlenet_send_course::execute_returns(), $result);
$this->assertFalse($result['status']);
$this->assertNotEmpty($result['warnings']);
$this->assertEquals('errorinvalidformat', $result['warnings'][0]['warningcode']);
// Test with invalid course module id.
$result = moodlenet_send_course::execute($issuer->get('id'), $course->id, 0, [random_int(5, 30)]);
$result = external_api::clean_returnvalue(moodlenet_send_course::execute_returns(), $result);
$this->assertFalse($result['status']);
$this->assertNotEmpty($result['warnings']);
$this->assertEquals('errorinvalidcmids', $result['warnings'][0]['warningcode']);
// Test with the user does not have permission.
$this->setUser($user);
$result = moodlenet_send_course::execute($issuer->get('id'), $course->id, 0);
$result = external_api::clean_returnvalue(moodlenet_send_course::execute_returns(), $result);
$this->assertFalse($result['status']);
$this->assertNotEmpty($result['warnings']);
$this->assertEquals('errorpermission', $result['warnings'][0]['warningcode']);
$this->setAdminUser();
// Test with the issuer is not enabled.
$result = moodlenet_send_course::execute($issuer->get('id'), $course->id, 0);
$result = external_api::clean_returnvalue(moodlenet_send_course::execute_returns(), $result);
$this->assertFalse($result['status']);
$this->assertNotEmpty($result['warnings']);
$this->assertEquals('errorissuernotenabled', $result['warnings'][0]['warningcode']);
// Test with the issuer is enabled but not set in the MN Outbound setting.
$issuer->set('enabled', 1);
$irecord = $issuer->to_record();
api::update_issuer($irecord);
$result = moodlenet_send_course::execute($issuer->get('id'), $course->id, 0);
$result = external_api::clean_returnvalue(moodlenet_send_course::execute_returns(), $result);
$this->assertFalse($result['status']);
$this->assertNotEmpty($result['warnings']);
$this->assertEquals('errorissuernotenabled', $result['warnings'][0]['warningcode']);
set_config('oauthservice', $issuer->get('id'), 'moodlenet');
// Test with the issuer not yet authorized.
$result = moodlenet_send_course::execute($issuer->get('id'), $course->id, 0);
$result = external_api::clean_returnvalue(moodlenet_send_course::execute_returns(), $result);
$this->assertFalse($result['status']);
$this->assertNotEmpty($result['warnings']);
$this->assertEquals('erroroauthclient', $result['warnings'][0]['warningcode']);
$this->assertEquals($issuer->get('id'), $result['warnings'][0]['item']);
$this->assertEquals(get_string('moodlenet:issuerisnotauthorized', 'moodle'), $result['warnings'][0]['message']);
}
/**
* Test execute_returns() method.
*
* @dataProvider return_resource_url_provider
* @covers ::execute_returns
*/
public function test_moodlenet_send_course_return_resource_url(bool $state, string $resourceurl): void {
$this->resetAfterTest();
// Create dummy result with the resourceurl.
$result = [
'status' => true,
'resourceurl' => $resourceurl,
'warnings' => [],
];
if (!$state) {
$this->expectException(\invalid_response_exception::class);
}
$result = external_api::clean_returnvalue(moodlenet_send_course::execute_returns(), $result);
if ($state) {
$this->assertEquals($resourceurl, $result['resourceurl']);
}
}
/**
* Provider for test_moodlenet_send_course_return_resource_url().
*
* @return array Test data.
*/
public function return_resource_url_provider(): array {
return [
'Success 1' => [
true,
'https://moodlenet.example.com/drafts/view/testcourse_backup.mbz',
],
'Success 2' => [
true,
'https://moodlenet.example.com/drafts/view/testcourse_backup with spaces.mbz',
],
'Success 3' => [
true,
'https://moodlenet.example.com/drafts/view/testcourse_backup with " character.mbz',
],
'Success 4' => [
true,
"https://moodlenet.example.com/drafts/view/testcourse_backup with ' character.mbz",
],
'Success 5' => [
true,
'https://moodlenet.example.com/drafts/view/testcourse_backup with < and > characters.mbz',
],
'Fail 1' => [
false,
'https://moodlenet.example.com/drafts/view/testcourse_backupwith<lang lang="en">a<a</lang>html.mbz',
],
];
}
}
@@ -0,0 +1,86 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* External functions test for record_feedback_action.
*
* @package core
* @category test
* @copyright 2020 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core\external\output\icon_system;
use externallib_advanced_testcase;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
/**
* Class record_userfeedback_action_testcase
*
* @copyright 2020 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \core\external\output\icon_system\load_fontawesome_map
*/
class load_fontawesome_map_test extends externallib_advanced_testcase {
/**
* Ensure that a valid theme which uses fontawesome returns a map.
*
* @covers ::execute_parameters
* @covers ::execute
* @covers ::execute_returns
* @dataProvider valid_fontawesome_theme_provider
* @param string $themename
*/
public function test_execute(string $themename): void {
$result = load_fontawesome_map::execute($themename);
$this->assertIsArray($result);
foreach ($result as $value) {
$this->assertArrayHasKey('component', $value);
$this->assertArrayHasKey('pix', $value);
$this->assertArrayHasKey('to', $value);
}
}
/**
* Ensure that an invalid theme cannot be loaded.
*/
public function test_execute_invalid_themename(): void {
$result = load_fontawesome_map::execute('invalidtheme');
$this->assertDebuggingCalled(
'This page should be using theme invalidtheme which cannot be initialised. Falling back to the site theme boost'
);
$this->assertIsArray($result);
}
/**
* Data provider for valid themes to use with the execute function.
*
* @return array
*/
public function valid_fontawesome_theme_provider(): array {
return [
'Boost theme' => ['boost'],
'Classic theme (extends boost)' => ['classic'],
];
}
}
+88
View File
@@ -0,0 +1,88 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* External functions test for record_feedback_action.
*
* @package core
* @category test
* @copyright 2020 Shamim Rezaie <shamim@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core\external;
defined('MOODLE_INTERNAL') || die();
use externallib_advanced_testcase;
use context_system;
global $CFG;
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
/**
* Class record_userfeedback_action_testcase
*
* @copyright 2020 Shamim Rezaie <shamim@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \core\external\record_userfeedback_action
*/
class record_userfeedback_action_test extends externallib_advanced_testcase {
/**
* Data provider for test_record_userfeedback_action.
*
* @return array
*/
public function record_userfeedback_action_provider() {
return [
'give action' => ['give'],
'remind action' => ['remind'],
];
}
/**
* Test the behaviour of record_userfeedback_action().
*
* @dataProvider record_userfeedback_action_provider
* @param string $action The action taken by the user
*
* @covers ::execute
*/
public function test_record_userfeedback_action(string $action): void {
$this->resetAfterTest();
$context = context_system::instance();
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);
$eventsink = $this->redirectEvents();
$now = time();
// Call the WS and check the action is recorded as expected.
$result = record_userfeedback_action::execute($action, $context->id);
$this->assertNull($result);
$preference = get_user_preferences('core_userfeedback_' . $action);
$this->assertGreaterThanOrEqual($now, $preference);
$events = $eventsink->get_events();
$this->assertCount(1, $events);
$this->assertInstanceOf('\core\event\userfeedback_' . $action, $events[0]);
$eventsink->clear();
}
}