MDL-78267 core: Backend API for partial sharing course for MoodleNet

This commit is contained in:
Huong Nguyen 2023-07-24 21:38:43 +07:00
parent b3ffe8432b
commit c6bf02a3b5
No known key found for this signature in database
GPG Key ID: 40D88AB693A3E72A
5 changed files with 606 additions and 1 deletions

View File

@ -0,0 +1,107 @@
<?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;
use backup_activity_task;
use backup_controller;
use stdClass;
use stored_file;
/**
* Packager to prepare appropriate backup of a number of activities in a course to share to MoodleNet.
*
* @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 extends resource_packager {
/** @var array $partialsharingtasks List of partial sharing tasks. */
private array $partialsharingtasks = [];
/**
* Constructor for course partial packager.
*
* @param stdClass $course The course to package
* @param array $cmids List of course module id of selected activities.
* @param int $userid The ID of the user performing the packaging
*/
public function __construct(
protected stdClass $course,
protected array $cmids,
protected int $userid,
) {
parent::__construct($course, $userid);
$this->controller = new backup_controller(
backup::TYPE_1COURSE,
$course->id,
backup::FORMAT_MOODLE,
backup::INTERACTIVE_NO,
backup::MODE_GENERAL,
$userid
);
$this->resourcefilename = $this->course->shortname;
}
/**
* Package the resource identified by resource id into a new stored_file.
*
* @return stored_file
*/
protected function package(): stored_file {
$this->remove_unselected_activities();
return parent::package();
}
/**
* Remove unselected activities in the course backup.
*/
protected function remove_unselected_activities(): void {
foreach ($this->partialsharingtasks as $task) {
foreach ($task->get_settings() as $setting) {
if (in_array($task->get_moduleid(), $this->cmids) &&
str_contains($setting->get_name(), '_included') !== false) {
$setting->set_value(1);
} else {
$setting->set_value(0);
}
}
}
}
/**
* Get all backup settings available for override.
*
* @return array the associative array of taskclass => settings instances.
*/
protected function get_all_task_settings(): array {
$tasksettings = [];
foreach ($this->controller->get_plan()->get_tasks() as $task) {
$taskclass = get_class($task);
$tasksettings[$taskclass] = $task->get_settings();
if ($task instanceof backup_activity_task) {
// Store partial sharing tasks.
$this->partialsharingtasks[] = $task;
}
}
return $tasksettings;
}
}

View File

@ -0,0 +1,100 @@
<?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\event\moodlenet_resource_exported;
use core\oauth2\client;
use moodle_exception;
/**
* API for sharing a number of Moodle LMS activities as a course backup to MoodleNet instances.
*
* @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 extends course_sender {
/**
* Constructor for course sender.
*
* @param int $courseid The course ID of the course being shared
* @param int $userid The user ID who is sharing the activity
* @param moodlenet_client $moodlenetclient The moodlenet_client object used to perform the share
* @param client $oauthclient The OAuth 2 client for the MoodleNet instance
* @param int $shareformat The data format to share in. Defaults to a Moodle backup (SHARE_FORMAT_BACKUP)
*/
public function __construct(
int $courseid,
protected int $userid,
protected moodlenet_client $moodlenetclient,
protected client $oauthclient,
protected array $cmids,
protected int $shareformat = self::SHARE_FORMAT_BACKUP,
) {
parent::__construct($courseid, $userid, $moodlenetclient, $oauthclient, $shareformat);
$this->validate_course_module_ids($this->course, $this->cmids);
$this->packager = new course_partial_packager($this->course, $this->cmids, $this->userid);
}
/**
* Log an event to the admin logs for an outbound share attempt.
*
* @param string $resourceurl The URL of the draft resource if it was created
* @param int $responsecode The HTTP response code describing the outcome of the attempt
* @return void
*/
protected function log_event(
string $resourceurl,
int $responsecode,
): void {
$event = moodlenet_resource_exported::create([
'context' => $this->coursecontext,
'other' => [
'cmids' => $this->cmids,
'courseid' => [$this->course->id],
'resourceurl' => $resourceurl,
'success' => ($responsecode === 201),
],
]);
$event->trigger();
}
/**
* Validate the course module ids.
*
* @param \stdClass $course Course object
* @param array $cmids List of course module ids to check
* @return void
*/
protected function validate_course_module_ids(
\stdClass $course,
array $cmids,
): void {
if (empty($cmids)) {
throw new moodle_exception('invalidcoursemodule');
}
$modinfo = get_fast_modinfo($course);
$cms = $modinfo->get_cms();
foreach ($cmids as $cmid) {
if (!array_key_exists($cmid, $cms)) {
throw new moodle_exception('invalidcoursemodule');
}
}
}
}

View File

@ -33,7 +33,7 @@ class course_sender extends resource_sender {
/**
* @var \core\context\course|false The course context.
*/
private \core\context\course|false $coursecontext;
protected \core\context\course|false $coursecontext;
/**
* Constructor for course sender.

View File

@ -0,0 +1,83 @@
<?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;
/**
* 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 ::get_all_task_settings
*/
public function test_get_all_task_settings(): 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);
$rcmgetall = $rc->getMethod('get_package');
$rcmgetall->setAccessible(true);
$rcmgetall->invoke($packager);
// Fetch the partial sharing tasks property.
$rcp = $rc->getProperty('partialsharingtasks');
$rcp->setAccessible(true);
$tasks = $rcp->getValue($packager);
$finalsetting = [];
foreach ($tasks as $task) {
foreach ($task->get_settings() 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']);
}
}

View File

@ -0,0 +1,315 @@
<?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');
$method->setAccessible(true);
$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');
$method->setAccessible(true);
// 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',
],
],
];
}
}