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
@@ -0,0 +1,139 @@
<?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 core\context\user;
/**
* Unit tests for {@see activity_packager}.
*
* @coversDefaultClass \core\moodlenet\activity_packager
* @package core
* @copyright 2023 Michael Hawkins <michaelh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class activity_packager_test extends \advanced_testcase {
/**
* Test fetching and overriding a backup task setting.
*
* @covers ::override_task_setting
* @covers ::get_all_task_settings
* @covers ::get_backup_controller
*/
public function test_override_task_setting(): void {
global $USER;
$this->resetAfterTest();
$this->setAdminUser();
$generator = $this->getDataGenerator();
$course = $generator->create_course();
$assigndata = [
'course' => $course->id,
'name' => 'Extremely interesting assignment',
'intro' => 'A great assignment to share',
];
$assign = $generator->create_module('assign', $assigndata);
$cminfo = get_fast_modinfo($course->id)->get_cm($assign->cmid);
$packager = new activity_packager($cminfo, $USER->id);
// Fetch all backup task settings.
$rc = new \ReflectionClass(activity_packager::class);
$rcmgetbackup = $rc->getMethod('get_backup_controller');
$controller = $rcmgetbackup->invoke($packager);
$rcmgetall = $rc->getMethod('get_all_task_settings');
$tasksettings = $rcmgetall->invoke($packager, $controller);
// Fetch the default settings and grab an example value (setting_root_users).
$rootsettings = $tasksettings[\backup_root_task::class];
$testsettingname = 'setting_root_users';
$oldvalue = 99;
foreach ($rootsettings as $setting) {
$name = $setting->get_ui_name();
if ($name == $testsettingname) {
$oldvalue = $setting->get_value();
break;
}
}
// Check we found the setting value (either 0 or 1 are valid).
$this->assertNotEquals(99, $oldvalue);
$this->assertLessThanOrEqual(1, $oldvalue);
// Override the setting_root_users value, then re-fetch the settings to check the change is reflected.
$overridevalue = ($oldvalue == 1) ? 0 : 1;
$rcmoverridesetting = $rc->getMethod('override_task_setting');
$rcmoverridesetting->invoke($packager, $tasksettings, $testsettingname, $overridevalue);
$tasksettings = $rcmgetall->invoke($packager, $controller);
$rootsettings = $tasksettings[\backup_root_task::class];
$newvalue = 99;
foreach ($rootsettings as $setting) {
$name = $setting->get_ui_name();
if ($name == $testsettingname) {
$newvalue = $setting->get_value();
break;
}
}
$this->assertEquals($overridevalue, $newvalue);
// We have finished with the backup controller, so destroy it.
$controller->destroy();
}
/**
* Test overriding a backup task setting.
*
* @covers ::get_package
* @covers ::package
*/
public function test_get_package(): void {
global $USER;
$this->resetAfterTest();
$this->setAdminUser();
$currenttime = time();
$generator = $this->getDataGenerator();
$course = $generator->create_course();
$assigndata = [
'course' => $course->id,
'name' => 'Extremely interesting assignment',
'intro' => 'A great assignment to share',
];
$assign = $generator->create_module('assign', $assigndata);
$cminfo = get_fast_modinfo($course->id)->get_cm($assign->cmid);
$packager = new activity_packager($cminfo, $USER->id);
$package = $packager->get_package();
// Confirm the expected stored_file object is returned.
$this->assertInstanceOf(\stored_file::class, $package);
// Check some known values in the returned stored_file object to confirm they match the file we have packaged.
$this->assertNotEmpty($package->get_contenthash());
$this->assertEquals(user::instance($USER->id)->id, $package->get_contextid());
$this->assertEquals('user', $package->get_component());
$this->assertEquals('draft', $package->get_filearea());
$this->assertEquals('assign_backup.mbz', $package->get_filename());
$this->assertGreaterThan(0, $package->get_filesize());
$timecreated = $package->get_timecreated();
$this->assertGreaterThanOrEqual($currenttime, $timecreated);
$this->assertEquals($timecreated, $package->get_timemodified());
}
}
@@ -0,0 +1,312 @@
<?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\http_client;
use core\oauth2\issuer;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Http\Message\ResponseInterface;
use ReflectionMethod;
use stdClass;
use testing_data_generator;
/**
* Unit tests for {@see activity_sender}.
*
* @coversDefaultClass \core\moodlenet\activity_sender
* @package core
* @copyright 2023 Huong Nguyen <huongnv13@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class activity_sender_test extends \advanced_testcase {
/** @var testing_data_generator Data generator. */
private testing_data_generator $generator;
/** @var stdClass Course object. */
private stdClass $course;
/** @var stdClass Activity object, */
private stdClass $moduleinstance;
/** @var context_course Course context instance. */
private context_course $coursecontext;
/** @var issuer $issuer Dummy issuer. */
private issuer $issuer;
/** @var MockObject $mockoauthclient Mock OAuth client. */
private MockObject $mockoauthclient;
public static function setUpBeforeClass(): void {
parent::setUpBeforeClass();
require_once(__DIR__ . '/helpers.php');
}
/**
* Set up function for tests.
*/
protected function setUp(): void {
parent::setUp();
$this->resetAfterTest();
// Get data generator.
$this->generator = $this->getDataGenerator();
// Create course.
$this->course = $this->generator->create_course();
$this->moduleinstance = $this->generator->create_module('assign', ['course' => $this->course->id]);
$this->coursecontext = context_course::instance($this->course->id);
// Create mock issuer.
$this->issuer = helpers::get_mock_issuer(1);
// Create mock builder for OAuth2 client.
$mockbuilder = $this->getMockBuilder('core\oauth2\client');
$mockbuilder->onlyMethods(['get_issuer', 'is_logged_in', 'get_accesstoken']);
$mockbuilder->setConstructorArgs([$this->issuer, '', '']);
// Get the OAuth2 client mock.
$this->mockoauthclient = $mockbuilder->getMock();
}
/**
* Test prepare_share_contents method.
*
* @covers ::prepare_share_contents
*/
public function test_prepare_share_contents(): void {
global $USER;
$this->setAdminUser();
$httpclient = new http_client();
$moodlenetclient = new moodlenet_client($httpclient, $this->mockoauthclient);
// Set get_file method accessibility.
$method = new ReflectionMethod(activity_sender::class, 'prepare_share_contents');
// Test with invalid share format.
$this->expectException(\moodle_exception::class);
$this->expectExceptionMessage(get_string('moodlenet:invalidshareformat', 'error'));
$package = $method->invoke(new activity_sender(
$this->moduleinstance->cmid,
$USER->id,
$moodlenetclient,
$this->mockoauthclient,
random_int(5, 30)
));
// Test with valid share format and invalid course module.
$package = $method->invoke(new activity_sender(
random_int(5, 30),
$USER->id,
$moodlenetclient,
$this->mockoauthclient,
resource_sender::SHARE_FORMAT_BACKUP
));
$this->assertEmpty($package);
// Test with valid share format and valid course module.
$package = $method->invoke(new activity_sender(
$this->moduleinstance->cmid,
$USER->id,
$moodlenetclient,
$this->mockoauthclient,
resource_sender::SHARE_FORMAT_BACKUP
));
$this->assertNotEmpty($package);
// Confirm the expected stored_file object is returned.
$this->assertInstanceOf(\stored_file::class, $package);
}
/**
* Test get_resource_description method.
*
* @covers ::get_resource_description
*/
public function test_get_resource_description(): void {
global $USER;
$this->setAdminUser();
$activity = $this->generator->create_module('assign', [
'course' => $this->course->id,
'intro' => '<p>This is an example Moodle activity description.</p>
<p>&nbsp;</p>
<p>This is a formatted intro</p>
<p>&nbsp;</p>
<p>This thing has many lines.</p>
<p>&nbsp;</p>
<p>The last word of this sentence is in <strong>bold</strong></p>'
]);
$httpclient = new http_client();
$moodlenetclient = new moodlenet_client($httpclient, $this->mockoauthclient);
// Set get_resource_description method accessibility.
$method = new ReflectionMethod(activity_sender::class, 'get_resource_description');
// Test the processed description.
$processeddescription = $method->invoke(new activity_sender(
$activity->cmid,
$USER->id,
$moodlenetclient,
$this->mockoauthclient,
resource_sender::SHARE_FORMAT_BACKUP
), $this->coursecontext);
$this->assertEquals('This is an example Moodle activity description.
 
This is a formatted intro
 
This thing has many lines.
 
The last word of this sentence is in bold', $processeddescription);
}
/**
* Test share_resource() method.
*
* @dataProvider share_resource_provider
* @covers ::share_resource
* @covers ::log_event
* @covers \core\moodlenet\moodlenet_client::create_resource_from_stored_file
* @covers \core\moodlenet\moodlenet_client::prepare_file_share_request_data
* @param ResponseInterface $httpresponse
* @param array $expected
*/
public function test_share_resource(ResponseInterface $httpresponse, array $expected): void {
global $CFG, $USER;
$this->setAdminUser();
// Enable the experimental flag.
$CFG->enablesharingtomoodlenet = true;
// Set OAuth 2 service in the outbound setting to the dummy issuer.
set_config('oauthservice', $this->issuer->get('id'), 'moodlenet');
// Generate access token for the mock.
$accesstoken = new stdClass();
$accesstoken->token = random_string(64);
// Get the OAuth2 client mock and set the return value for necessary methods.
$this->mockoauthclient->method('get_issuer')->will($this->returnValue($this->issuer));
$this->mockoauthclient->method('is_logged_in')->will($this->returnValue(true));
$this->mockoauthclient->method('get_accesstoken')->will($this->returnValue($accesstoken));
// Create Guzzle mock.
$mockguzzlehandler = new MockHandler([$httpresponse]);
$handlerstack = HandlerStack::create($mockguzzlehandler);
$httpclient = new http_client(['handler' => $handlerstack]);
// Create events sink.
$sink = $this->redirectEvents();
// Create activity sender.
$moodlenetclient = new moodlenet_client($httpclient, $this->mockoauthclient);
$activitysender = new activity_sender(
$this->moduleinstance->cmid,
$USER->id,
$moodlenetclient,
$this->mockoauthclient,
resource_sender::SHARE_FORMAT_BACKUP
);
if (isset($expected['exception'])) {
$this->expectException(ClientException::class);
$this->expectExceptionMessage($expected['exception']);
}
// Call the API.
$result = $activitysender->share_resource();
// Verify the result.
$this->assertEquals($expected['response_code'], $result['responsecode']);
$this->assertEquals($expected['resource_url'], $result['drafturl']);
// Verify the events.
$events = $sink->get_events();
$event = reset($events);
$this->assertInstanceOf('\core\event\moodlenet_resource_exported', $event);
$this->assertEquals($USER->id, $event->userid);
if ($result['responsecode'] == 201) {
$description = "The user with id '{$USER->id}' successfully shared activities to MoodleNet with the " .
"following course module ids, from context with id '{$this->coursecontext->id}': '{$this->moduleinstance->cmid}'.";
} else {
$description = "The user with id '{$USER->id}' failed to share activities to MoodleNet with the " .
"following course module ids, from context with id '{$this->coursecontext->id}': '{$this->moduleinstance->cmid}'.";
}
$this->assertEquals($description, $event->get_description());
}
/**
* Provider for test share_resource().
*
* @return array Test data.
*/
public function share_resource_provider(): array {
return [
'Success' => [
'http_response' => new Response(
201,
['Content-Type' => 'application/json'],
json_encode([
'homepage' => 'https://moodlenet.example.com/drafts/view/activity_backup_1.mbz',
]),
),
'expected' => [
'response_code' => 201,
'resource_url' => 'https://moodlenet.example.com/drafts/view/activity_backup_1.mbz',
],
],
'Fail with 200 status code' => [
'http_response' => new Response(
200,
['Content-Type' => 'application/json'],
json_encode([
'homepage' => 'https://moodlenet.example.com/drafts/view/activity_backup_2.mbz',
]),
),
'expected' => [
'response_code' => 200,
'resource_url' => 'https://moodlenet.example.com/drafts/view/activity_backup_2.mbz',
],
],
'Fail with 401 status code' => [
'http_response' => new Response(
401,
),
'expected' => [
'response_code' => 401,
'resource_url' => '',
'exception' => 'Client error: ' .
'`POST https://moodlenet.example.com/.pkg/@moodlenet/ed-resource/basic/v1/create` ' .
'resulted in a `401 Unauthorized` response',
],
],
'Fail with 404 status code' => [
'http_response' => new Response(
404,
),
'expected' => [
'response_code' => 404,
'resource_url' => '',
'exception' => 'Client error: '.
'`POST https://moodlenet.example.com/.pkg/@moodlenet/ed-resource/basic/v1/create` ' .
'resulted in a `404 Not Found` response',
],
],
];
}
}
@@ -0,0 +1,128 @@
<?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 core\context\user;
/**
* Test coverage for moodlenet course packager.
*
* @package core
* @copyright 2023 Safat Shahin <safat.shahin@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \core\moodlenet\course_packager
*/
class course_packager_test extends \advanced_testcase {
/**
* Test fetching and overriding a backup task setting.
*
* @covers ::override_task_setting
* @covers ::get_all_task_settings
* @covers ::get_backup_controller
*/
public function test_override_task_setting(): void {
global $USER;
$this->resetAfterTest();
$this->setAdminUser();
$generator = $this->getDataGenerator();
$course = $generator->create_course();
// Load the course packager.
$packager = new course_packager($course, $USER->id);
// Fetch all backup task settings.
$rc = new \ReflectionClass(course_packager::class);
$rcmgetbackup = $rc->getMethod('get_backup_controller');
$controller = $rcmgetbackup->invoke($packager);
$rcmgetall = $rc->getMethod('get_all_task_settings');
$tasksettings = $rcmgetall->invoke($packager, $controller);
// Fetch the default settings and grab an example value (setting_root_users).
$rootsettings = $tasksettings[\backup_root_task::class];
$testsettingname = 'setting_root_users';
$oldvalue = 99;
foreach ($rootsettings as $setting) {
$name = $setting->get_ui_name();
if ($name == $testsettingname) {
$oldvalue = $setting->get_value();
break;
}
}
// Check we found the setting value (either 0 or 1 are valid).
$this->assertNotEquals(99, $oldvalue);
$this->assertLessThanOrEqual(1, $oldvalue);
// Override the setting_root_users value, then re-fetch the settings to check the change is reflected.
$overridevalue = ($oldvalue == 1) ? 0 : 1;
$rcmoverridesetting = $rc->getMethod('override_task_setting');
$rcmoverridesetting->invoke($packager, $tasksettings, $testsettingname, $overridevalue);
$tasksettings = $rcmgetall->invoke($packager, $controller);
$rootsettings = $tasksettings[\backup_root_task::class];
$newvalue = 99;
foreach ($rootsettings as $setting) {
$name = $setting->get_ui_name();
if ($name == $testsettingname) {
$newvalue = $setting->get_value();
break;
}
}
$this->assertEquals($overridevalue, $newvalue);
// We have finished with the backup controller, so destroy it.
$controller->destroy();
}
/**
* Test the course package file.
*
* @covers ::get_package
* @covers ::package
*/
public function test_get_package(): void {
global $USER;
$this->resetAfterTest();
$this->setAdminUser();
$currenttime = time();
$generator = $this->getDataGenerator();
$course = $generator->create_course();
// Load the course packager.
$packager = new course_packager($course, $USER->id);
$package = $packager->get_package();
// Confirm the expected stored_file object is returned.
$this->assertInstanceOf(\stored_file::class, $package);
// Check some known values in the returned stored_file object to confirm they match the file we have packaged.
$this->assertNotEmpty($package->get_contenthash());
$this->assertEquals(user::instance($USER->id)->id, $package->get_contextid());
$this->assertEquals('user', $package->get_component());
$this->assertEquals('draft', $package->get_filearea());
$this->assertEquals($course->shortname . '_backup.mbz', $package->get_filename());
$this->assertGreaterThan(0, $package->get_filesize());
$timecreated = $package->get_timecreated();
$this->assertGreaterThanOrEqual($currenttime, $timecreated);
$this->assertEquals($timecreated, $package->get_timemodified());
}
}
@@ -0,0 +1,92 @@
<?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 backup_activity_task;
/**
* Unit tests for {@see \core\moodlenet\course_partial_packager}.
*
* @coversDefaultClass \core\moodlenet\course_partial_packager
* @package core
* @copyright 2023 Huong Nguyen <huongnv13@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class course_partial_packager_test extends \advanced_testcase {
/**
* Test fetching task settings.
*
* @covers ::remove_unselected_activities
* @covers ::get_all_activity_tasks
* @covers ::get_backup_controller
*/
public function test_remove_unselected_activities(): void {
global $USER;
$this->resetAfterTest();
$this->setAdminUser();
$generator = $this->getDataGenerator();
$course = $generator->create_course();
$page1 = $generator->create_module('page', ['course' => $course->id]);
$page2 = $generator->create_module('page', ['course' => $course->id]);
// Load the course packager.
$packager = new course_partial_packager($course, [$page1->cmid], $USER->id);
// Fetch all backup task settings.
$rc = new \ReflectionClass(course_partial_packager::class);
$rcmgetbackup = $rc->getMethod('get_backup_controller');
$controller = $rcmgetbackup->invoke($packager);
$rcmremove = $rc->getMethod('remove_unselected_activities');
$rcmremove->invoke($packager, $controller);
// Fetch all backup task settings for asserting them.
$finalsetting = [];
foreach ($controller->get_plan()->get_tasks() as $task) {
if (! $task instanceof backup_activity_task) { // Only for activity tasks.
continue;
}
$tasksettings = $task->get_settings();
foreach ($tasksettings as $setting) {
if (in_array($task->get_moduleid(), [$page1->cmid, $page2->cmid]) &&
strpos($setting->get_name(), '_included') !== false) {
$finalsetting[$task->get_moduleid()] = [
'name' => $setting->get_name(),
'value' => $setting->get_value(),
];
}
}
}
// Check the number of partial sharing tasks.
// Expected 2, Page 1 and Page 2.
$this->assertCount(2, $finalsetting);
// Check the value of the task of Page 1. 1 mean enabled, the backup will include the Page 1 activity.
$this->assertEquals('page_' . $page1->cmid . '_included', $finalsetting[$page1->cmid]['name']);
$this->assertEquals(1, $finalsetting[$page1->cmid]['value']);
// Check the value of the task of Page 2. 0 mean disabled, the backup will not include the Page 2 activity.
$this->assertEquals('page_' . $page2->cmid . '_included', $finalsetting[$page2->cmid]['name']);
$this->assertEquals(0, $finalsetting[$page2->cmid]['value']);
// We have finished with the backup controller, so destroy it.
$controller->destroy();
}
}
@@ -0,0 +1,313 @@
<?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\http_client;
use core\oauth2\issuer;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use moodle_exception;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Http\Message\ResponseInterface;
use ReflectionMethod;
use stdClass;
use testing_data_generator;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/lib/tests/moodlenet/helpers.php');
/**
* Unit tests for {@see \core\moodlenet\course_partial_sender}.
*
* @coversDefaultClass \core\moodlenet\course_partial_sender
* @package core
* @copyright 2023 Huong Nguyen <huongnv13@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class course_partial_sender_test extends \advanced_testcase {
/** @var testing_data_generator Data generator. */
private testing_data_generator $generator;
/** @var stdClass Course object. */
private stdClass $course;
/** @var context_course Course context instance. */
private context_course $coursecontext;
/** @var array List of activities. */
private array $activities;
/** @var issuer $issuer Dummy issuer. */
private issuer $issuer;
/** @var MockObject $mockoauthclient Mock OAuth client. */
private MockObject $mockoauthclient;
/**
* Set up function for tests.
*/
protected function setUp(): void {
parent::setUp();
$this->resetAfterTest();
// Get data generator.
$this->generator = $this->getDataGenerator();
// Create course.
$this->course = $this->generator->create_course(['shortname' => 'testcourse']);
$this->coursecontext = context_course::instance($this->course->id);
// Create activities.
$this->activities[1] = $this->generator->create_module('page', ['course' => $this->course->id]);
$this->activities[2] = $this->generator->create_module('page', ['course' => $this->course->id]);
// Create mock issuer.
$this->issuer = helpers::get_mock_issuer(1);
// Create mock builder for OAuth2 client.
$mockbuilder = $this->getMockBuilder('core\oauth2\client');
$mockbuilder->onlyMethods(['get_issuer', 'is_logged_in', 'get_accesstoken']);
$mockbuilder->setConstructorArgs([$this->issuer, '', '']);
// Get the OAuth2 client mock.
$this->mockoauthclient = $mockbuilder->getMock();
}
/**
* Test prepare_share_contents method.
*
* @covers ::prepare_share_contents
*/
public function test_prepare_share_contents(): void {
global $USER;
$this->setAdminUser();
// Set get_file method accessibility.
$method = new ReflectionMethod(course_partial_sender::class, 'prepare_share_contents');
$httpclient = new http_client();
$moodlenetclient = new moodlenet_client($httpclient, $this->mockoauthclient);
$this->expectException(moodle_exception::class);
$this->expectExceptionMessage(get_string('invalidcoursemodule', 'error'));
// Test with valid share format and invalid course module id.
$package = $method->invoke(new course_partial_sender(
$this->course->id,
$USER->id,
$moodlenetclient,
$this->mockoauthclient,
[random_int(5, 30)],
resource_sender::SHARE_FORMAT_BACKUP
));
$this->assertEmpty($package);
// Test with valid share format and valid course module ids.
$package = $method->invoke(new course_partial_sender(
$this->course->id,
$USER->id,
$moodlenetclient,
$this->mockoauthclient,
[$this->activities[1]->cmid, $this->activities[2]->cmid],
resource_sender::SHARE_FORMAT_BACKUP
));
$this->assertNotEmpty($package);
// Confirm the expected stored_file object is returned.
$this->assertInstanceOf(\stored_file::class, $package);
}
/**
* Test get_resource_description method.
*
* @covers ::get_resource_description
*/
public function test_get_resource_description(): void {
global $USER;
$this->setAdminUser();
$course = $this->generator->create_course([
'summary' => '<p>This is an example Moodle course description.</p>
<p>&nbsp;</p>
<p>This is a formatted intro</p>
<p>&nbsp;</p>
<p>This thing has many lines.</p>
<p>&nbsp;</p>
<p>The last word of this sentence is in <strong>bold</strong></p>'
]);
$page = $this->generator->create_module('page', ['course' => $course->id]);
// Set get_resource_description method accessibility.
$method = new ReflectionMethod(course_partial_sender::class, 'get_resource_description');
// Test the processed description.
$httpclient = new http_client();
$moodlenetclient = new moodlenet_client($httpclient, $this->mockoauthclient);
$processeddescription = $method->invoke(new course_partial_sender(
$course->id,
$USER->id,
$moodlenetclient,
$this->mockoauthclient,
[$page->cmid],
resource_sender::SHARE_FORMAT_BACKUP
), $this->coursecontext);
$this->assertEquals('This is an example Moodle course description.
 
This is a formatted intro
 
This thing has many lines.
 
The last word of this sentence is in bold', $processeddescription);
}
/**
* Test share_resource() method.
*
* @dataProvider share_resource_provider
* @covers ::share_resource
* @covers ::log_event
* @covers \core\moodlenet\moodlenet_client::create_resource_from_stored_file
* @covers \core\moodlenet\moodlenet_client::prepare_file_share_request_data
* @param ResponseInterface $httpresponse
* @param array $expected
*/
public function test_share_resource(ResponseInterface $httpresponse, array $expected): void {
global $CFG, $USER;
$this->setAdminUser();
// Enable the experimental flag.
$CFG->enablesharingtomoodlenet = true;
// Set OAuth 2 service in the outbound setting to the dummy issuer.
set_config('oauthservice', $this->issuer->get('id'), 'moodlenet');
// Generate access token for the mock.
$accesstoken = new stdClass();
$accesstoken->token = random_string(64);
// Get the OAuth2 client mock and set the return value for necessary methods.
$this->mockoauthclient->method('get_issuer')->will($this->returnValue($this->issuer));
$this->mockoauthclient->method('is_logged_in')->will($this->returnValue(true));
$this->mockoauthclient->method('get_accesstoken')->will($this->returnValue($accesstoken));
// Create Guzzle mock.
$mockguzzlehandler = new MockHandler([$httpresponse]);
$handlerstack = HandlerStack::create($mockguzzlehandler);
$httpclient = new http_client(['handler' => $handlerstack]);
// Create events sink.
$sink = $this->redirectEvents();
// Create sender.
$moodlenetclient = new moodlenet_client($httpclient, $this->mockoauthclient);
$coursepartialsender = new course_partial_sender(
$this->course->id,
$USER->id,
$moodlenetclient,
$this->mockoauthclient,
[$this->activities[1]->cmid, $this->activities[2]->cmid],
resource_sender::SHARE_FORMAT_BACKUP
);
if (isset($expected['exception'])) {
$this->expectException(ClientException::class);
$this->expectExceptionMessage($expected['exception']);
}
// Call the API.
$result = $coursepartialsender->share_resource();
// Verify the result.
$this->assertEquals($expected['response_code'], $result['responsecode']);
$this->assertEquals($expected['resource_url'], $result['drafturl']);
// Verify the events.
$events = $sink->get_events();
$event = end($events);
$this->assertInstanceOf('\core\event\moodlenet_resource_exported', $event);
$this->assertEquals($USER->id, $event->userid);
$cmidslist = implode("', '", [$this->activities[1]->cmid, $this->activities[2]->cmid]);
if ($result['responsecode'] == 201) {
$description = "The user with id '{$USER->id}' successfully shared activities to MoodleNet with the " .
"following course module ids, from context with id '{$this->coursecontext->id}': '{$cmidslist}'.";
} else {
$description = "The user with id '{$USER->id}' failed to share activities to MoodleNet with the " .
"following course module ids, from context with id '{$this->coursecontext->id}': '{$cmidslist}'.";
}
$this->assertEquals($description, $event->get_description());
}
/**
* Provider for test share_resource().
*
* @return array Test data.
*/
public function share_resource_provider(): array {
return [
'Success' => [
'http_response' => new Response(
201,
['Content-Type' => 'application/json'],
json_encode([
'homepage' => 'https://moodlenet.example.com/drafts/view/testcourse_backup_1.mbz',
]),
),
'expected' => [
'response_code' => 201,
'resource_url' => 'https://moodlenet.example.com/drafts/view/testcourse_backup_1.mbz',
],
],
'Fail with 200 status code' => [
'http_response' => new Response(
200,
['Content-Type' => 'application/json'],
json_encode([
'homepage' => 'https://moodlenet.example.com/drafts/view/testcourse_backup_2.mbz',
]),
),
'expected' => [
'response_code' => 200,
'resource_url' => 'https://moodlenet.example.com/drafts/view/testcourse_backup_2.mbz',
],
],
'Fail with 401 status code' => [
'http_response' => new Response(
401,
),
'expected' => [
'response_code' => 401,
'resource_url' => '',
'exception' => 'Client error: ' .
'`POST https://moodlenet.example.com/.pkg/@moodlenet/ed-resource/basic/v1/create` ' .
'resulted in a `401 Unauthorized` response',
],
],
'Fail with 404 status code' => [
'http_response' => new Response(
404,
),
'expected' => [
'response_code' => 404,
'resource_url' => '',
'exception' => 'Client error: '.
'`POST https://moodlenet.example.com/.pkg/@moodlenet/ed-resource/basic/v1/create` ' .
'resulted in a `404 Not Found` response',
],
],
];
}
}
+318
View File
@@ -0,0 +1,318 @@
<?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\http_client;
use core\oauth2\issuer;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Http\Message\ResponseInterface;
use ReflectionMethod;
use stdClass;
use testing_data_generator;
/**
* Test coverage for moodlenet course sender.
*
* @package core
* @copyright 2023 Safat Shahin <safat.shahin@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \core\moodlenet\course_sender
*/
class course_sender_test extends \advanced_testcase {
/** @var testing_data_generator Data generator. */
private testing_data_generator $generator;
/** @var stdClass Course object. */
private stdClass $course;
/** @var context_course Course context instance. */
private context_course $coursecontext;
/** @var issuer $issuer Dummy issuer. */
private issuer $issuer;
/** @var MockObject $mockoauthclient Mock OAuth client. */
private MockObject $mockoauthclient;
public static function setUpBeforeClass(): void {
parent::setUpBeforeClass();
require_once(__DIR__ . '/helpers.php');
}
/**
* Set up function for tests.
*/
protected function setUp(): void {
parent::setUp();
$this->resetAfterTest();
// Get data generator.
$this->generator = $this->getDataGenerator();
// Create course.
$this->course = $this->generator->create_course(['shortname' => 'testcourse']);
$this->coursecontext = context_course::instance($this->course->id);
// Create mock issuer.
$this->issuer = helpers::get_mock_issuer(1);
// Create mock builder for OAuth2 client.
$mockbuilder = $this->getMockBuilder('core\oauth2\client');
$mockbuilder->onlyMethods(['get_issuer', 'is_logged_in', 'get_accesstoken']);
$mockbuilder->setConstructorArgs([$this->issuer, '', '']);
// Get the OAuth2 client mock.
$this->mockoauthclient = $mockbuilder->getMock();
}
/**
* Test prepare_share_contents method.
*
* @covers ::prepare_share_contents
*/
public function test_prepare_share_contents(): void {
global $USER;
$this->setAdminUser();
// Set get_file method accessibility.
$method = new ReflectionMethod(course_sender::class, 'prepare_share_contents');
// Test with invalid share format.
$this->expectException(\moodle_exception::class);
$this->expectExceptionMessage(get_string('moodlenet:invalidshareformat', 'error'));
$httpclient = new http_client();
$moodlenetclient = new moodlenet_client($httpclient, $this->mockoauthclient);
$coursesender = new course_sender(
random_int(5, 30),
$USER->id,
$moodlenetclient,
$this->mockoauthclient,
random_int(5, 30)
);
$coursesender = $method->invoke(new course_sender(
$this->course->id,
$USER->id,
$moodlenetclient,
$this->mockoauthclient,
resource_sender::SHARE_FORMAT_BACKUP
));
// Test with valid share format and invalid course module.
$package = $method->invoke(new course_sender(
$this->course->id,
$USER->id,
$moodlenetclient,
$this->mockoauthclient,
resource_sender::SHARE_FORMAT_BACKUP
));
$this->assertEmpty($package);
// Test with valid share format and valid course module.
$package = $method->invoke(new course_sender(
$this->course->id,
$USER->id,
$moodlenetclient,
$this->mockoauthclient,
resource_sender::SHARE_FORMAT_BACKUP
));
$this->assertNotEmpty($package);
// Confirm the expected stored_file object is returned.
$this->assertInstanceOf(\stored_file::class, $package);
}
/**
* Test get_resource_description method.
*
* @covers ::get_resource_description
*/
public function test_get_resource_description(): void {
global $USER;
$this->setAdminUser();
$course = $this->generator->create_course([
'summary' => '<p>This is an example Moodle course description.</p>
<p>&nbsp;</p>
<p>This is a formatted intro</p>
<p>&nbsp;</p>
<p>This thing has many lines.</p>
<p>&nbsp;</p>
<p>The last word of this sentence is in <strong>bold</strong></p>'
]);
// Set get_resource_description method accessibility.
$method = new ReflectionMethod(course_sender::class, 'get_resource_description');
// Test the processed description.
$httpclient = new http_client();
$moodlenetclient = new moodlenet_client($httpclient, $this->mockoauthclient);
$processeddescription = $method->invoke(new course_sender(
$course->id,
$USER->id,
$moodlenetclient,
$this->mockoauthclient,
resource_sender::SHARE_FORMAT_BACKUP
), $this->coursecontext);
$this->assertEquals('This is an example Moodle course description.
 
This is a formatted intro
 
This thing has many lines.
 
The last word of this sentence is in bold', $processeddescription);
}
/**
* Test share_resource() method.
*
* @dataProvider share_resource_provider
* @covers ::share_resource
* @covers ::log_event
* @covers \core\moodlenet\moodlenet_client::create_resource_from_stored_file
* @covers \core\moodlenet\moodlenet_client::prepare_file_share_request_data
* @param ResponseInterface $httpresponse
* @param array $expected
*/
public function test_share_resource(ResponseInterface $httpresponse, array $expected): void {
global $CFG, $USER;
$this->setAdminUser();
// Enable the experimental flag.
$CFG->enablesharingtomoodlenet = true;
// Set OAuth 2 service in the outbound setting to the dummy issuer.
set_config('oauthservice', $this->issuer->get('id'), 'moodlenet');
// Generate access token for the mock.
$accesstoken = new stdClass();
$accesstoken->token = random_string(64);
// Get the OAuth2 client mock and set the return value for necessary methods.
$this->mockoauthclient->method('get_issuer')->will($this->returnValue($this->issuer));
$this->mockoauthclient->method('is_logged_in')->will($this->returnValue(true));
$this->mockoauthclient->method('get_accesstoken')->will($this->returnValue($accesstoken));
// Create Guzzle mock.
$mockguzzlehandler = new MockHandler([$httpresponse]);
$handlerstack = HandlerStack::create($mockguzzlehandler);
$httpclient = new http_client(['handler' => $handlerstack]);
// Create events sink.
$sink = $this->redirectEvents();
// Create sender.
$moodlenetclient = new moodlenet_client($httpclient, $this->mockoauthclient);
$coursesender = new course_sender(
$this->course->id,
$USER->id,
$moodlenetclient,
$this->mockoauthclient,
resource_sender::SHARE_FORMAT_BACKUP
);
if (isset($expected['exception'])) {
$this->expectException(ClientException::class);
$this->expectExceptionMessage($expected['exception']);
}
// Call the API.
$result = $coursesender->share_resource();
// Verify the result.
$this->assertEquals($expected['response_code'], $result['responsecode']);
$this->assertEquals($expected['resource_url'], $result['drafturl']);
// Verify the events.
$events = $sink->get_events();
$event = end($events);
$this->assertInstanceOf('\core\event\moodlenet_resource_exported', $event);
$this->assertEquals($USER->id, $event->userid);
if ($result['responsecode'] == 201) {
$description = "The user with id '{$USER->id}' successfully shared course to MoodleNet with the " .
"following course id, from context with id '{$this->coursecontext->id}': '{$this->course->id}'.";
} else {
$description = "The user with id '{$USER->id}' failed to share course to MoodleNet with the " .
"following course id, from context with id '{$this->coursecontext->id}': '{$this->course->id}'.";
}
$this->assertEquals($description, $event->get_description());
}
/**
* Provider for test share_resource().
*
* @return array Test data.
*/
public function share_resource_provider(): array {
return [
'Success' => [
'http_response' => new Response(
201,
['Content-Type' => 'application/json'],
json_encode([
'homepage' => 'https://moodlenet.example.com/drafts/view/testcourse_backup_1.mbz',
]),
),
'expected' => [
'response_code' => 201,
'resource_url' => 'https://moodlenet.example.com/drafts/view/testcourse_backup_1.mbz',
],
],
'Fail with 200 status code' => [
'http_response' => new Response(
200,
['Content-Type' => 'application/json'],
json_encode([
'homepage' => 'https://moodlenet.example.com/drafts/view/testcourse_backup_2.mbz',
]),
),
'expected' => [
'response_code' => 200,
'resource_url' => 'https://moodlenet.example.com/drafts/view/testcourse_backup_2.mbz',
],
],
'Fail with 401 status code' => [
'http_response' => new Response(
401,
),
'expected' => [
'response_code' => 401,
'resource_url' => '',
'exception' => 'Client error: ' .
'`POST https://moodlenet.example.com/.pkg/@moodlenet/ed-resource/basic/v1/create` ' .
'resulted in a `401 Unauthorized` response',
],
],
'Fail with 404 status code' => [
'http_response' => new Response(
404,
),
'expected' => [
'response_code' => 404,
'resource_url' => '',
'exception' => 'Client error: '.
'`POST https://moodlenet.example.com/.pkg/@moodlenet/ed-resource/basic/v1/create` ' .
'resulted in a `404 Not Found` response',
],
],
];
}
}
+52
View File
@@ -0,0 +1,52 @@
<?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 core\oauth2\issuer;
/**
* Helper for tests related to MoodleNet integration.
*
* @package core
* @copyright 2023 Michael Hawkins <michaelh@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class helpers {
/**
* Create and return a mock MoodleNet issuer.
*
* @param int $enabled Whether the issuer is enabled.
* @return issuer The issuer that has been created.
*/
public static function get_mock_issuer(int $enabled): issuer {
$record = (object) [
'name' => 'MoodleNet',
'image' => 'https://moodle.net/favicon.ico',
'baseurl' => 'https://moodlenet.example.com',
'loginscopes' => '',
'loginscopesoffline' => '',
'loginparamsoffline' => '',
'showonloginpage' => issuer::SERVICEONLY,
'servicetype' => 'moodlenet',
'enabled' => $enabled,
];
$issuer = new issuer(0, $record);
$issuer->create();
return $issuer;
}
}
+134
View File
@@ -0,0 +1,134 @@
<?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 core\moodlenet\share_recorder;
/**
* Test coverage for moodlenet share recorder.
*
* @package core
* @copyright 2023 David Woloszyn <david.woloszyn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @coversDefaultClass \core\moodlenet\share_recorder
*/
class share_recorder_test extends \advanced_testcase {
/**
* Test inserting and updating an activity share progress to MoodleNet.
*
* @covers ::insert_share_progress
* @covers ::update_share_progress
*/
public function test_activity_share_progress(): void {
global $DB, $USER;
$this->resetAfterTest();
$courseid = 10;
$cmid = 20;
$resourceurl = 'https://moodlenet.test/files/testresource.mbz';
// Insert the activity share progress and test the returned id.
$shareid = share_recorder::insert_share_progress(share_recorder::TYPE_ACTIVITY, $USER->id, $courseid, $cmid);
$this->assertNotNull($shareid);
// Test we have set the fields correctly.
$record = $DB->get_record('moodlenet_share_progress', ['id' => $shareid]);
$this->assertEquals(share_recorder::TYPE_ACTIVITY, $record->type);
$this->assertEquals(share_recorder::STATUS_IN_PROGRESS, $record->status);
$this->assertEquals($courseid, $record->courseid);
$this->assertEquals($cmid, $record->cmid);
$this->assertTimeCurrent($record->timecreated);
$this->assertEquals($USER->id, $record->userid);
// Update the record with the returned data from MoodleNet.
share_recorder::update_share_progress($shareid, share_recorder::STATUS_SENT, $resourceurl);
// Test we have set the fields correctly.
$record = $DB->get_record('moodlenet_share_progress', ['id' => $shareid]);
$this->assertEquals($resourceurl, $record->resourceurl);
$this->assertEquals(share_recorder::STATUS_SENT, $record->status);
}
/**
* Test inserting and updating a course share progress to MoodleNet.
* We will also force an error status and test that too.
*
* @covers ::insert_share_progress
* @covers ::update_share_progress
*/
public function test_course_share_progress(): void {
global $DB, $USER;
$this->resetAfterTest();
$courseid = 10;
// Insert the course share progress and test the returned id.
$shareid = share_recorder::insert_share_progress(share_recorder::TYPE_COURSE, $USER->id, $courseid);
$this->assertNotNull($shareid);
// Test we have set the fields correctly (we expect cmid to be null for course shares).
$record = $DB->get_record('moodlenet_share_progress', ['id' => $shareid]);
$this->assertEquals(share_recorder::TYPE_COURSE, $record->type);
$this->assertEquals(share_recorder::STATUS_IN_PROGRESS, $record->status);
$this->assertEquals($courseid, $record->courseid);
$this->assertNull($record->cmid);
$this->assertTimeCurrent($record->timecreated);
$this->assertEquals($USER->id, $record->userid);
// Update the record, but let's test with an error status.
share_recorder::update_share_progress($shareid, share_recorder::STATUS_ERROR);
// Test we have set the field correctly.
$record = $DB->get_record('moodlenet_share_progress', ['id' => $shareid]);
$this->assertEquals(share_recorder::STATUS_ERROR, $record->status);
}
/**
* Tests the share type is one of the allowed values.
*
* @covers ::get_allowed_share_types
*/
public function test_invalid_share_type(): void {
global $USER;
$this->resetAfterTest();
$courseid = 10;
$invalidsharetype = 99;
$this->expectException(\moodle_exception::class);
share_recorder::insert_share_progress($invalidsharetype, $USER->id, $courseid);
}
/**
* Tests the share status is one of the allowed values.
*
* @covers ::get_allowed_share_statuses
*/
public function test_invalid_share_status(): void {
global $USER;
$this->resetAfterTest();
$courseid = 10;
$invalidsharestatus = 66;
$recordid = share_recorder::insert_share_progress(share_recorder::TYPE_COURSE, $USER->id, $courseid);
$this->expectException(\moodle_exception::class);
share_recorder::update_share_progress($recordid, $invalidsharestatus);
}
}
+193
View File
@@ -0,0 +1,193 @@
<?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(): void {
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(): void {
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));
// Test with default settings for course.
// Student and Teacher cannot share the course.
$this->assertFalse(utilities::can_user_share($this->coursecontext, $student1->id, 'course'));
$this->assertFalse(utilities::can_user_share($this->coursecontext, $teacher1->id, 'course'));
// Editing-teacher and Manager can share the course.
$this->assertTrue(utilities::can_user_share($this->coursecontext, $teacher2->id, 'course'));
$this->assertTrue(utilities::can_user_share($this->coursecontext, $manager1->id, 'course'));
// Teacher who has the capabilities can share the course.
assign_capability('moodle/moodlenet:sharecourse', CAP_ALLOW, $teacherrole->id, $this->coursecontext);
assign_capability('moodle/backup:backupcourse', CAP_ALLOW, $teacherrole->id, $this->coursecontext);
$this->assertTrue(utilities::can_user_share($this->coursecontext, $teacher1->id, 'course'));
// Editing-teacher who does not have the capabilities can not share the course.
assign_capability('moodle/moodlenet:sharecourse', CAP_PROHIBIT, $editingteacherrole->id, $this->coursecontext);
$this->assertFalse(utilities::can_user_share($this->coursecontext, $teacher2->id, 'course'));
}
/**
* Test does_user_have_capability_in_any_course method.
*
* @covers ::does_user_have_capability_in_any_course
* @return void
*/
public function test_does_user_have_capability_in_any_course(): void {
global $DB;
// Prepare data.
$teacher1 = $this->generator->create_user();
$student1 = $this->generator->create_user();
$teacherrole = $DB->get_record('role', ['shortname' => 'teacher'], 'id', MUST_EXIST);
$studentrole = $DB->get_record('role', ['shortname' => 'student'], 'id', MUST_EXIST);
// Enrol users,
$this->generator->enrol_user($teacher1->id, $this->course->id, $teacherrole->id);
$this->generator->enrol_user($student1->id, $this->course->id, $studentrole->id);
// Assign a valid capability to the teacher.
assign_capability('moodle/moodlenet:shareactivity', CAP_ALLOW, $teacherrole->id, $this->coursecontext);
// Check the method's results are as expected (this is cached).
$this->assertSame('yes', utilities::does_user_have_capability_in_any_course($teacher1->id));
$this->assertSame('no', utilities::does_user_have_capability_in_any_course($student1->id));
// Compare to cache.
$teachercachedvalue = \cache::make('core', 'moodlenet_usercanshare')->get($teacher1->id);
$this->assertSame('yes', $teachercachedvalue);
$studentcachedvalue = \cache::make('core', 'moodlenet_usercanshare')->get($student1->id);
$this->assertSame('no', $studentcachedvalue);
// Change the teacher's role and check the moodlenet_usercanshare cache is invalidated for everyone.
$this->generator->role_assign($studentrole->id, $teacher1->id, $this->coursecontext->id);
$teachercachedvalue = \cache::make('core', 'moodlenet_usercanshare')->get($teacher1->id);
$this->assertFalse($teachercachedvalue);
$studentcachedvalue = \cache::make('core', 'moodlenet_usercanshare')->get($student1->id);
$this->assertFalse($studentcachedvalue);
}
}