mirror of
https://github.com/moodle/moodle.git
synced 2025-08-04 08:26:37 +02:00
MDL-75316 core: Set up MoodleNet API structure and utilities helper
Initial folder structure and declaring API. Created utilities class. Originally implemented as MDL-75932
This commit is contained in:
parent
cd097f117b
commit
d6c4c0fbbd
3 changed files with 198 additions and 0 deletions
|
@ -154,6 +154,11 @@
|
|||
"allowedlevel2": true,
|
||||
"allowedspread": true
|
||||
},
|
||||
"moodlenet": {
|
||||
"component": "core",
|
||||
"allowedlevel2": false,
|
||||
"allowedspread": false
|
||||
},
|
||||
"navigation": {
|
||||
"component": "core",
|
||||
"allowedlevel2": true,
|
||||
|
|
57
lib/classes/moodlenet/utilities.php
Normal file
57
lib/classes/moodlenet/utilities.php
Normal file
|
@ -0,0 +1,57 @@
|
|||
<?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\moodlenet;
|
||||
|
||||
use context_course;
|
||||
use core\oauth2\issuer;
|
||||
|
||||
/**
|
||||
* Class containing static utilities (such as various checks) required by the MoodleNet API.
|
||||
*
|
||||
* @package core
|
||||
* @copyright 2023 Michael Hawkins <michaelh@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class utilities {
|
||||
/**
|
||||
* Check whether the specified issuer is configured as a MoodleNet instance that can be shared to.
|
||||
*
|
||||
* @param \core\oauth2\issuer $issuer The OAuth 2 issuer being validated.
|
||||
* @return bool true if the issuer is enabled and available to share to.
|
||||
*/
|
||||
public static function is_valid_instance(issuer $issuer): bool {
|
||||
global $CFG;
|
||||
|
||||
$issuerid = $issuer->get('id');
|
||||
$allowedissuer = get_config('moodlenet', 'oauthservice');
|
||||
|
||||
return ($CFG->enablesharingtomoodlenet && $issuerid == $allowedissuer && $issuer->get('enabled') &&
|
||||
$issuer->get('servicetype') == 'moodlenet');
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a user has the capabilities required to share activities from a given course to MoodleNet.
|
||||
*
|
||||
* @param \context_course $coursecontext Course context where the activity would be shared from.
|
||||
* @param int $userid The user ID being checked.
|
||||
* @return boolean
|
||||
*/
|
||||
public static function can_user_share(context_course $coursecontext, int $userid): bool {
|
||||
return (has_capability('moodle/moodlenet:shareactivity', $coursecontext, $userid) &&
|
||||
has_capability('moodle/backup:backupactivity', $coursecontext, $userid));
|
||||
}
|
||||
}
|
136
lib/tests/moodlenet/utilities_test.php
Normal file
136
lib/tests/moodlenet/utilities_test.php
Normal file
|
@ -0,0 +1,136 @@
|
|||
<?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\moodlenet;
|
||||
|
||||
use context_course;
|
||||
use stdClass;
|
||||
use testing_data_generator;
|
||||
|
||||
/**
|
||||
* Unit tests for {@see utilities}.
|
||||
*
|
||||
* @coversDefaultClass \core\moodlenet\utilities
|
||||
* @package core
|
||||
* @copyright 2023 Huong Nguyen <huongnv13@gmail.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class utilities_test extends \advanced_testcase {
|
||||
|
||||
/** @var testing_data_generator Data generator. */
|
||||
private testing_data_generator $generator;
|
||||
|
||||
/** @var stdClass Activity object, */
|
||||
private stdClass $course;
|
||||
|
||||
/** @var context_course Course context instance. */
|
||||
private context_course $coursecontext;
|
||||
|
||||
/**
|
||||
* Set up function for tests.
|
||||
*/
|
||||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
|
||||
$this->resetAfterTest();
|
||||
$this->generator = $this->getDataGenerator();
|
||||
$this->course = $this->generator->create_course();
|
||||
$this->coursecontext = context_course::instance($this->course->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test is_valid_instance method.
|
||||
*
|
||||
* @covers ::is_valid_instance
|
||||
* @return void
|
||||
*/
|
||||
public function test_is_valid_instance() {
|
||||
global $CFG;
|
||||
$this->setAdminUser();
|
||||
|
||||
// Create dummy issuer.
|
||||
$issuer = new \core\oauth2\issuer(0);
|
||||
$issuer->set('enabled', 0);
|
||||
$issuer->set('servicetype', 'google');
|
||||
|
||||
// Can not share if the experimental flag it set to false.
|
||||
$CFG->enablesharingtomoodlenet = false;
|
||||
$this->assertFalse(utilities::is_valid_instance($issuer));
|
||||
|
||||
// Enable the experimental flag.
|
||||
$CFG->enablesharingtomoodlenet = true;
|
||||
|
||||
// Can not share if the OAuth 2 service in the outbound setting is not matched the given one.
|
||||
set_config('oauthservice', random_int(1, 30), 'moodlenet');
|
||||
$this->assertFalse(utilities::is_valid_instance($issuer));
|
||||
|
||||
// Can not share if the OAuth 2 service in the outbound setting is not enabled.
|
||||
set_config('oauthservice', $issuer->get('id'), 'moodlenet');
|
||||
$this->assertFalse(utilities::is_valid_instance($issuer));
|
||||
|
||||
// Can not share if the OAuth 2 service type is not moodlenet.
|
||||
$issuer->set('enabled', 1);
|
||||
$this->assertFalse(utilities::is_valid_instance($issuer));
|
||||
|
||||
// All good now.
|
||||
$issuer->set('servicetype', 'moodlenet');
|
||||
$this->assertTrue(utilities::is_valid_instance($issuer));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test can_user_share method.
|
||||
*
|
||||
* @covers ::can_user_share
|
||||
* @return void
|
||||
*/
|
||||
public function test_can_user_share() {
|
||||
global $DB;
|
||||
|
||||
// Generate data.
|
||||
$student1 = $this->generator->create_user();
|
||||
$teacher1 = $this->generator->create_user();
|
||||
$teacher2 = $this->generator->create_user();
|
||||
$manager1 = $this->generator->create_user();
|
||||
|
||||
// Enrol users.
|
||||
$this->generator->enrol_user($student1->id, $this->course->id, 'student');
|
||||
$this->generator->enrol_user($teacher1->id, $this->course->id, 'teacher');
|
||||
$this->generator->enrol_user($teacher2->id, $this->course->id, 'editingteacher');
|
||||
$this->generator->enrol_user($manager1->id, $this->course->id, 'manager');
|
||||
|
||||
// Get roles.
|
||||
$teacherrole = $DB->get_record('role', ['shortname' => 'teacher'], 'id', MUST_EXIST);
|
||||
$editingteacherrole = $DB->get_record('role', ['shortname' => 'editingteacher'], 'id', MUST_EXIST);
|
||||
|
||||
// Test with default settings.
|
||||
// Student and Teacher cannot share the activity.
|
||||
$this->assertFalse(utilities::can_user_share($this->coursecontext, $student1->id));
|
||||
$this->assertFalse(utilities::can_user_share($this->coursecontext, $teacher1->id));
|
||||
// Editing-teacher and Manager can share the activity.
|
||||
$this->assertTrue(utilities::can_user_share($this->coursecontext, $teacher2->id));
|
||||
$this->assertTrue(utilities::can_user_share($this->coursecontext, $manager1->id));
|
||||
|
||||
// Teacher who has the capabilities can share the activity.
|
||||
assign_capability('moodle/moodlenet:shareactivity', CAP_ALLOW, $teacherrole->id, $this->coursecontext);
|
||||
assign_capability('moodle/backup:backupactivity', CAP_ALLOW, $teacherrole->id, $this->coursecontext);
|
||||
$this->assertTrue(utilities::can_user_share($this->coursecontext, $teacher1->id));
|
||||
|
||||
// Editing-teacher who does not have the capabilities can not share the activity.
|
||||
assign_capability('moodle/moodlenet:shareactivity', CAP_PROHIBIT, $editingteacherrole->id, $this->coursecontext);
|
||||
$this->assertFalse(utilities::can_user_share($this->coursecontext, $teacher2->id));
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue