mirror of
https://github.com/moodle/moodle.git
synced 2025-04-13 12:32:08 +02:00
MDL-53832 enrol_lti: Unit tests for tool_provider
Original subtask: MDL-55647.
This commit is contained in:
parent
daebcd2801
commit
c314d6ed8e
@ -171,7 +171,7 @@ class tool_provider extends ToolProvider {
|
||||
|
||||
$this->errorOutput = '';
|
||||
|
||||
notification::error(get_string('failedregistration', 'enrol_lti', ['reason' => $message]));
|
||||
notification::error(get_string('failedrequest', 'enrol_lti', ['reason' => $message]));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -37,7 +37,7 @@ $string['enrolmentnotstarted'] = 'Enrolment has not started.';
|
||||
$string['enrolstartdate'] = 'Start date';
|
||||
$string['enrolstartdate_help'] = 'If enabled, users can access from this date onward only.';
|
||||
$string['frameembeddingnotenabled'] = 'To access the tool, please follow the link below.';
|
||||
$string['failedregistration'] = 'Failed registration. Reason: {$a->reason}';
|
||||
$string['failedrequest'] = 'Failed request. Reason: {$a->reason}';
|
||||
$string['gradesync'] = 'Grade synchronisation';
|
||||
$string['gradesync_help'] = 'Whether grades from the tool are sent to the remote system (LTI consumer).';
|
||||
$string['incorrecttoken'] = 'The token was incorrect. Please check the URL and try again, or contact the administrator of this tool.';
|
||||
|
27
enrol/lti/tests/fixtures/tool_consumer_profile.json
vendored
Normal file
27
enrol/lti/tests/fixtures/tool_consumer_profile.json
vendored
Normal file
@ -0,0 +1,27 @@
|
||||
{
|
||||
"@context" : ["http://purl.imsglobal.org/ctx/lti/v2/Profile"],
|
||||
"@type" : "ToolConsumerProfile",
|
||||
"lti_version" : "LTI-2p0",
|
||||
"@id" : "http://alpha.university.edu/resource/ToolConsumerProfile/12434",
|
||||
"guid" : "b6ffa601-ce1d-4549-9ccf-145670a964d4",
|
||||
"product_instance" : { },
|
||||
"service_offered" : [
|
||||
{
|
||||
"@type" : "RestService",
|
||||
"@id" : "http://www.example.com/moodle/lti/resource/ToolConsumerProfile/12434#Result.item",
|
||||
"endpoint" : "http://www.example.com/moodle/enpoint.php",
|
||||
"format" : ["application/vnd.ims.lti.v2.toolproxy+json"],
|
||||
"action" : ["POST"]
|
||||
}
|
||||
],
|
||||
"capability_offered" : [
|
||||
"Person.name.given",
|
||||
"Person.name.family",
|
||||
"Person.email.primary",
|
||||
"User.image",
|
||||
"Result.sourcedId",
|
||||
"basic-lti-launch-request",
|
||||
"Result.autocreate",
|
||||
"Result.url"
|
||||
]
|
||||
}
|
@ -93,7 +93,7 @@ class enrol_lti_helper_testcase extends advanced_testcase {
|
||||
// Set up the LTI enrolment tool.
|
||||
$data = new stdClass();
|
||||
$data->maxenrolled = 1;
|
||||
$tool = $this->create_tool($data);
|
||||
$tool = $this->getDataGenerator()->create_lti_tool($data);
|
||||
|
||||
// Now get all the information we need.
|
||||
$tool = \enrol_lti\helper::get_lti_tool($tool->id);
|
||||
@ -122,7 +122,7 @@ class enrol_lti_helper_testcase extends advanced_testcase {
|
||||
// Set up the LTI enrolment tool.
|
||||
$data = new stdClass();
|
||||
$data->enrolstartdate = time() + DAYSECS; // Make sure it is in the future.
|
||||
$tool = $this->create_tool($data);
|
||||
$tool = $this->getDataGenerator()->create_lti_tool($data);
|
||||
|
||||
// Now get all the information we need.
|
||||
$tool = \enrol_lti\helper::get_lti_tool($tool->id);
|
||||
@ -144,7 +144,7 @@ class enrol_lti_helper_testcase extends advanced_testcase {
|
||||
// Set up the LTI enrolment tool.
|
||||
$data = new stdClass();
|
||||
$data->enrolenddate = time() - DAYSECS; // Make sure it is in the past.
|
||||
$tool = $this->create_tool($data);
|
||||
$tool = $this->getDataGenerator()->create_lti_tool($data);
|
||||
|
||||
// Now get all the information we need.
|
||||
$tool = \enrol_lti\helper::get_lti_tool($tool->id);
|
||||
@ -161,22 +161,23 @@ class enrol_lti_helper_testcase extends advanced_testcase {
|
||||
* Test returning the number of available tools.
|
||||
*/
|
||||
public function test_count_lti_tools() {
|
||||
$generator = $this->getDataGenerator();
|
||||
// Create two tools belonging to the same course.
|
||||
$course1 = $this->getDataGenerator()->create_course();
|
||||
$course1 = $generator->create_course();
|
||||
$data = new stdClass();
|
||||
$data->courseid = $course1->id;
|
||||
$this->create_tool($data);
|
||||
$this->create_tool($data);
|
||||
$generator->create_lti_tool($data);
|
||||
$generator->create_lti_tool($data);
|
||||
|
||||
// Create two more tools in a separate course.
|
||||
$course2 = $this->getDataGenerator()->create_course();
|
||||
$data = new stdClass();
|
||||
$data->courseid = $course2->id;
|
||||
$this->create_tool($data);
|
||||
$generator->create_lti_tool($data);
|
||||
|
||||
// Set the next tool to disabled.
|
||||
$data->status = ENROL_INSTANCE_DISABLED;
|
||||
$this->create_tool($data);
|
||||
$generator->create_lti_tool($data);
|
||||
|
||||
// Count all the tools.
|
||||
$count = \enrol_lti\helper::count_lti_tools();
|
||||
@ -199,22 +200,23 @@ class enrol_lti_helper_testcase extends advanced_testcase {
|
||||
* Test returning the list of available tools.
|
||||
*/
|
||||
public function test_get_lti_tools() {
|
||||
$generator = $this->getDataGenerator();
|
||||
// Create two tools belonging to the same course.
|
||||
$course1 = $this->getDataGenerator()->create_course();
|
||||
$course1 = $generator->create_course();
|
||||
$data = new stdClass();
|
||||
$data->courseid = $course1->id;
|
||||
$tool1 = $this->create_tool($data);
|
||||
$tool2 = $this->create_tool($data);
|
||||
$tool1 = $generator->create_lti_tool($data);
|
||||
$tool2 = $generator->create_lti_tool($data);
|
||||
|
||||
// Create two more tools in a separate course.
|
||||
$course2 = $this->getDataGenerator()->create_course();
|
||||
$course2 = $generator->create_course();
|
||||
$data = new stdClass();
|
||||
$data->courseid = $course2->id;
|
||||
$tool3 = $this->create_tool($data);
|
||||
$tool3 = $generator->create_lti_tool($data);
|
||||
|
||||
// Set the next tool to disabled.
|
||||
$data->status = ENROL_INSTANCE_DISABLED;
|
||||
$tool4 = $this->create_tool($data);
|
||||
$tool4 = $generator->create_lti_tool($data);
|
||||
|
||||
// Get all the tools.
|
||||
$tools = \enrol_lti\helper::get_lti_tools();
|
||||
@ -254,7 +256,7 @@ class enrol_lti_helper_testcase extends advanced_testcase {
|
||||
$course1 = $this->getDataGenerator()->create_course();
|
||||
$data = new stdClass();
|
||||
$data->courseid = $course1->id;
|
||||
$tool1 = $this->create_tool($data);
|
||||
$tool1 = $this->getDataGenerator()->create_lti_tool($data);
|
||||
|
||||
$id = $tool1->id;
|
||||
$launchurl = \enrol_lti\helper::get_launch_url($id);
|
||||
@ -274,7 +276,7 @@ class enrol_lti_helper_testcase extends advanced_testcase {
|
||||
$course1 = $this->getDataGenerator()->create_course();
|
||||
$data = new stdClass();
|
||||
$data->courseid = $course1->id;
|
||||
$tool1 = $this->create_tool($data);
|
||||
$tool1 = $this->getDataGenerator()->create_lti_tool($data);
|
||||
|
||||
$id = $tool1->id;
|
||||
$token = \enrol_lti\helper::generate_cartridge_token($id);
|
||||
@ -304,7 +306,7 @@ class enrol_lti_helper_testcase extends advanced_testcase {
|
||||
$course1 = $this->getDataGenerator()->create_course();
|
||||
$data = new stdClass();
|
||||
$data->courseid = $course1->id;
|
||||
$tool1 = $this->create_tool($data);
|
||||
$tool1 = $this->getDataGenerator()->create_lti_tool($data);
|
||||
|
||||
$id = $tool1->id;
|
||||
$token = \enrol_lti\helper::generate_proxy_token($id);
|
||||
@ -328,7 +330,7 @@ class enrol_lti_helper_testcase extends advanced_testcase {
|
||||
$course1 = $this->getDataGenerator()->create_course();
|
||||
$data = new stdClass();
|
||||
$data->courseid = $course1->id;
|
||||
$tool1 = $this->create_tool($data);
|
||||
$tool1 = $this->getDataGenerator()->create_lti_tool($data);
|
||||
|
||||
$name = \enrol_lti\helper::get_name($tool1);
|
||||
$this->assertEquals('Course: Test course 1', $name);
|
||||
@ -342,20 +344,21 @@ class enrol_lti_helper_testcase extends advanced_testcase {
|
||||
* Test getting the description of a tool.
|
||||
*/
|
||||
public function test_get_description() {
|
||||
$course1 = $this->getDataGenerator()->create_course();
|
||||
$generator = $this->getDataGenerator();
|
||||
$course1 = $generator->create_course();
|
||||
$data = new stdClass();
|
||||
$data->courseid = $course1->id;
|
||||
$tool1 = $this->create_tool($data);
|
||||
$tool1 = $generator->create_lti_tool($data);
|
||||
|
||||
$description = \enrol_lti\helper::get_description($tool1);
|
||||
$this->assertContains('Test course 1 Lorem ipsum dolor sit amet', $description);
|
||||
|
||||
$module1 = $this->getDataGenerator()->create_module('assign', array(
|
||||
$module1 = $generator->create_module('assign', array(
|
||||
'course' => $course1->id
|
||||
));
|
||||
$data = new stdClass();
|
||||
$data->cmid = $module1->cmid;
|
||||
$tool2 = $this->create_tool($data);
|
||||
$tool2 = $generator->create_lti_tool($data);
|
||||
$description = \enrol_lti\helper::get_description($tool2);
|
||||
$this->assertContains('Test assign 1', $description);
|
||||
}
|
||||
@ -369,11 +372,11 @@ class enrol_lti_helper_testcase extends advanced_testcase {
|
||||
$course1 = $this->getDataGenerator()->create_course();
|
||||
$data = new stdClass();
|
||||
$data->courseid = $course1->id;
|
||||
$tool = $this->create_tool($data);
|
||||
$tool = $this->getDataGenerator()->create_lti_tool($data);
|
||||
|
||||
$icon = \enrol_lti\helper::get_icon($tool);
|
||||
$icon = $icon->out();
|
||||
// Only local icons are supported by the LTI framework
|
||||
// Only local icons are supported by the LTI framework.
|
||||
$this->assertContains($CFG->wwwroot, $icon);
|
||||
|
||||
}
|
||||
@ -385,7 +388,7 @@ class enrol_lti_helper_testcase extends advanced_testcase {
|
||||
$course1 = $this->getDataGenerator()->create_course();
|
||||
$data = new stdClass();
|
||||
$data->courseid = $course1->id;
|
||||
$tool1 = $this->create_tool($data);
|
||||
$tool1 = $this->getDataGenerator()->create_lti_tool($data);
|
||||
|
||||
$token = \enrol_lti\helper::generate_cartridge_token($tool1->id);
|
||||
$this->assertTrue(\enrol_lti\helper::verify_cartridge_token($tool1->id, $token));
|
||||
@ -399,7 +402,7 @@ class enrol_lti_helper_testcase extends advanced_testcase {
|
||||
$course1 = $this->getDataGenerator()->create_course();
|
||||
$data = new stdClass();
|
||||
$data->courseid = $course1->id;
|
||||
$tool1 = $this->create_tool($data);
|
||||
$tool1 = $this->getDataGenerator()->create_lti_tool($data);
|
||||
|
||||
$token = \enrol_lti\helper::generate_proxy_token($tool1->id);
|
||||
$this->assertTrue(\enrol_lti\helper::verify_proxy_token($tool1->id, $token));
|
||||
@ -526,55 +529,11 @@ class enrol_lti_helper_testcase extends advanced_testcase {
|
||||
$course1 = $this->getDataGenerator()->create_course();
|
||||
$data = new stdClass();
|
||||
$data->courseid = $course1->id;
|
||||
$tool1 = $this->create_tool($data);
|
||||
$tool1 = $this->getDataGenerator()->create_lti_tool($data);
|
||||
|
||||
$cartridge = \enrol_lti\helper::create_cartridge($tool1->id);
|
||||
$this->assertContains('<blti:title>Test LTI</blti:title>', $cartridge);
|
||||
$this->assertContains("<blti:icon>$CFG->wwwroot/theme/image.php/_s/clean/theme/1/favicon</blti:icon>", $cartridge);
|
||||
$this->assertContains("<blti:launch_url>$CFG->wwwroot/enrol/lti/tool.php?id=$tool1->id</blti:launch_url>", $cartridge);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function used to create a tool.
|
||||
*
|
||||
* @param array $data
|
||||
* @return stdClass the tool
|
||||
*/
|
||||
protected function create_tool($data = array()) {
|
||||
global $DB;
|
||||
|
||||
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
|
||||
$teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
|
||||
|
||||
// Create a course if no course id was specified.
|
||||
if (empty($data->courseid)) {
|
||||
$course = $this->getDataGenerator()->create_course();
|
||||
$data->courseid = $course->id;
|
||||
} else {
|
||||
$course = get_course($data->courseid);
|
||||
}
|
||||
|
||||
if (!empty($data->cmid)) {
|
||||
$data->contextid = context_module::instance($data->cmid)->id;
|
||||
} else {
|
||||
$data->contextid = context_course::instance($data->courseid)->id;
|
||||
}
|
||||
|
||||
// Set it to enabled if no status was specified.
|
||||
if (!isset($data->status)) {
|
||||
$data->status = ENROL_INSTANCE_ENABLED;
|
||||
}
|
||||
|
||||
// Add some extra necessary fields to the data.
|
||||
$data->name = 'Test LTI';
|
||||
$data->roleinstructor = $studentrole->id;
|
||||
$data->rolelearner = $teacherrole->id;
|
||||
|
||||
// Get the enrol LTI plugin.
|
||||
$enrolplugin = enrol_get_plugin('lti');
|
||||
$instanceid = $enrolplugin->add_instance($course, (array) $data);
|
||||
|
||||
// Get the tool associated with this instance.
|
||||
return $DB->get_record('enrol_lti_tools', array('enrolid' => $instanceid));
|
||||
}
|
||||
}
|
||||
|
622
enrol/lti/tests/tool_provider_test.php
Normal file
622
enrol/lti/tests/tool_provider_test.php
Normal file
@ -0,0 +1,622 @@
|
||||
<?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/>.
|
||||
|
||||
/**
|
||||
* Test the helper functionality.
|
||||
*
|
||||
* @package enrol_lti
|
||||
* @copyright 2016 Jun Pataleta <jun@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
use core\session\manager;
|
||||
use enrol_lti\data_connector;
|
||||
use enrol_lti\helper;
|
||||
use enrol_lti\tool_provider;
|
||||
use IMSGlobal\LTI\HTTPMessage;
|
||||
use IMSGlobal\LTI\ToolProvider\ResourceLink;
|
||||
use IMSGlobal\LTI\ToolProvider\ToolConsumer;
|
||||
use IMSGlobal\LTI\ToolProvider\ToolProvider;
|
||||
use IMSGlobal\LTI\ToolProvider\User;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Test the helper functionality.
|
||||
*
|
||||
* @package enrol_lti
|
||||
* @copyright 2016 Jun Pataleta <jun@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class tool_provider_testcase extends advanced_testcase {
|
||||
|
||||
/**
|
||||
* @var stdClass $tool The LTI tool.
|
||||
*/
|
||||
protected $tool;
|
||||
|
||||
/**
|
||||
* Test set up.
|
||||
*
|
||||
* This is executed before running any tests in this file.
|
||||
*/
|
||||
public function setUp() {
|
||||
global $SESSION;
|
||||
$this->resetAfterTest();
|
||||
|
||||
manager::init_empty_session();
|
||||
|
||||
// Set this user as the admin.
|
||||
$this->setAdminUser();
|
||||
|
||||
$data = new stdClass();
|
||||
$data->enrolstartdate = time();
|
||||
$data->secret = 'secret';
|
||||
$toolrecord = $this->getDataGenerator()->create_lti_tool($data);
|
||||
$this->tool = helper::get_lti_tool($toolrecord->id);
|
||||
$SESSION->notifications = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Passing non-existent tool ID.
|
||||
*/
|
||||
public function test_constructor_with_non_existent_tool() {
|
||||
$this->expectException('dml_exception');
|
||||
new tool_provider(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor test.
|
||||
*/
|
||||
public function test_constructor() {
|
||||
global $CFG, $SITE;
|
||||
|
||||
$tool = $this->tool;
|
||||
$tp = new tool_provider($tool->id);
|
||||
|
||||
$this->assertNull($tp->consumer);
|
||||
$this->assertNull($tp->returnUrl);
|
||||
$this->assertNull($tp->resourceLink);
|
||||
$this->assertNull($tp->context);
|
||||
$this->assertNotNull($tp->dataConnector);
|
||||
$this->assertEquals('', $tp->defaultEmail);
|
||||
$this->assertEquals(ToolProvider::ID_SCOPE_ID_ONLY, $tp->idScope);
|
||||
$this->assertFalse($tp->allowSharing);
|
||||
$this->assertEquals(ToolProvider::CONNECTION_ERROR_MESSAGE, $tp->message);
|
||||
$this->assertNull($tp->reason);
|
||||
$this->assertEmpty($tp->details);
|
||||
$this->assertEquals($CFG->wwwroot, $tp->baseUrl);
|
||||
|
||||
$this->assertNotNull($tp->vendor);
|
||||
$this->assertEquals($SITE->shortname, $tp->vendor->id);
|
||||
$this->assertEquals($SITE->fullname, $tp->vendor->name);
|
||||
$this->assertEquals($SITE->summary, $tp->vendor->description);
|
||||
|
||||
$token = helper::generate_proxy_token($tool->id);
|
||||
$name = helper::get_name($tool);
|
||||
$description = helper::get_description($tool);
|
||||
|
||||
$this->assertNotNull($tp->product);
|
||||
$this->assertEquals($token, $tp->product->id);
|
||||
$this->assertEquals($name, $tp->product->name);
|
||||
$this->assertEquals($description, $tp->product->description);
|
||||
|
||||
$this->assertNotNull($tp->requiredServices);
|
||||
$this->assertEmpty($tp->optionalServices);
|
||||
$this->assertNotNull($tp->resourceHandlers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for handle request.
|
||||
*/
|
||||
public function test_handle_request_no_request_data() {
|
||||
$tool = $this->tool;
|
||||
$tp = new tool_provider($tool->id);
|
||||
|
||||
// Tool provider object should have been created fine. OK flag should be fine for now.
|
||||
$this->assertTrue($tp->ok);
|
||||
|
||||
// There's basically no request data submitted so OK flag should turn out false.
|
||||
$tp->handleRequest();
|
||||
$this->assertFalse($tp->ok);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for tool_provider::onError().
|
||||
*/
|
||||
public function test_on_error() {
|
||||
global $SESSION;
|
||||
|
||||
$tool = $this->tool;
|
||||
$tp = new dummy_tool_provider($tool->id);
|
||||
$message = "THIS IS AN ERROR!";
|
||||
$tp->message = $message;
|
||||
$tp->onError();
|
||||
// Assert that a notification has been added.
|
||||
$this->assertCount(1, $SESSION->notifications);
|
||||
$notification = $SESSION->notifications[0];
|
||||
$errormessage = get_string('failedrequest', 'enrol_lti', ['reason' => $message]);
|
||||
$this->assertEquals($errormessage, $notification->message);
|
||||
$this->assertEquals('error', $notification->type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for tool_provider::onRegister() with no tool consumer set.
|
||||
*/
|
||||
public function test_on_register_no_consumer() {
|
||||
$tool = $this->tool;
|
||||
|
||||
$tp = new dummy_tool_provider($tool->id);
|
||||
$tp->onRegister();
|
||||
|
||||
$this->assertFalse($tp->ok);
|
||||
$this->assertEquals(get_string('invalidtoolconsumer', 'enrol_lti'), $tp->message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for tool_provider::onRegister() without return URL.
|
||||
*/
|
||||
public function test_on_register_no_return_url() {
|
||||
$tool = $this->tool;
|
||||
|
||||
$dataconnector = new data_connector();
|
||||
$consumer = new ToolConsumer('testkey', $dataconnector);
|
||||
$consumer->ltiVersion = ToolProvider::LTI_VERSION2;
|
||||
$consumer->secret = $tool->secret;
|
||||
$consumer->name = 'TEST CONSUMER NAME';
|
||||
$consumer->consumerName = 'TEST CONSUMER INSTANCE NAME';
|
||||
$consumer->consumerGuid = 'TEST CONSUMER INSTANCE GUID';
|
||||
$consumer->consumerVersion = 'TEST CONSUMER INFO VERSION';
|
||||
$consumer->enabled = true;
|
||||
$consumer->protected = true;
|
||||
$consumer->save();
|
||||
|
||||
$tp = new dummy_tool_provider($tool->id);
|
||||
$tp->consumer = $consumer;
|
||||
|
||||
$tp->onRegister();
|
||||
$this->assertFalse($tp->ok);
|
||||
$this->assertEquals(get_string('returnurlnotset', 'enrol_lti'), $tp->message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for tool_provider::onRegister() when registration fails.
|
||||
*/
|
||||
public function test_on_register_failed() {
|
||||
global $CFG;
|
||||
$tool = $this->tool;
|
||||
|
||||
$dataconnector = new data_connector();
|
||||
$consumer = new dummy_tool_consumer('testkey', $dataconnector);
|
||||
$consumer->ltiVersion = ToolProvider::LTI_VERSION2;
|
||||
$consumer->secret = $tool->secret;
|
||||
$consumer->name = 'TEST CONSUMER NAME';
|
||||
$consumer->consumerName = 'TEST CONSUMER INSTANCE NAME';
|
||||
$consumer->consumerGuid = 'TEST CONSUMER INSTANCE GUID';
|
||||
$consumer->consumerVersion = 'TEST CONSUMER INFO VERSION';
|
||||
$consumer->enabled = true;
|
||||
$consumer->protected = true;
|
||||
$profilejson = file_get_contents(__DIR__ . '/fixtures/tool_consumer_profile.json');
|
||||
$consumer->profile = json_decode($profilejson);
|
||||
$consumer->save();
|
||||
|
||||
$tp = new dummy_tool_provider($tool->id);
|
||||
$tp->consumer = $consumer;
|
||||
$tp->returnUrl = $CFG->wwwroot;
|
||||
|
||||
$tp->onRegister();
|
||||
|
||||
// The OK flag will be false.
|
||||
$this->assertFalse($tp->ok);
|
||||
// Check message.
|
||||
$this->assertEquals(get_string('couldnotestablishproxy', 'enrol_lti'), $tp->message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for tool_provider::onRegister() when registration succeeds.
|
||||
*/
|
||||
public function test_on_register() {
|
||||
global $CFG;
|
||||
$tool = $this->tool;
|
||||
|
||||
$dataconnector = new data_connector();
|
||||
$consumer = new dummy_tool_consumer('testkey', $dataconnector, false, true);
|
||||
$consumer->ltiVersion = ToolProvider::LTI_VERSION2;
|
||||
$consumer->secret = $tool->secret;
|
||||
$consumer->name = 'TEST CONSUMER NAME';
|
||||
$consumer->consumerName = 'TEST CONSUMER INSTANCE NAME';
|
||||
$consumer->consumerGuid = 'TEST CONSUMER INSTANCE GUID';
|
||||
$consumer->consumerVersion = 'TEST CONSUMER INFO VERSION';
|
||||
$consumer->enabled = true;
|
||||
$consumer->protected = true;
|
||||
$profilejson = file_get_contents(__DIR__ . '/fixtures/tool_consumer_profile.json');
|
||||
$consumer->profile = json_decode($profilejson);
|
||||
$consumer->save();
|
||||
|
||||
$tp = new dummy_tool_provider($tool->id);
|
||||
$tp->consumer = $consumer;
|
||||
$tp->returnUrl = $CFG->wwwroot;
|
||||
|
||||
// Capture output of onLaunch() method and save it as a string.
|
||||
ob_start();
|
||||
$tp->onRegister();
|
||||
$output = ob_get_clean();
|
||||
|
||||
$successmessage = get_string('successfulregistration', 'enrol_lti');
|
||||
|
||||
// Check output contents. Confirm that it has the success message and return URL.
|
||||
$this->assertContains($successmessage, $output);
|
||||
$this->assertContains($tp->returnUrl, $output);
|
||||
|
||||
// The OK flag will be true on successful registration.
|
||||
$this->assertTrue($tp->ok);
|
||||
|
||||
// Check tool provider message.
|
||||
$this->assertEquals($successmessage, $tp->message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for tool_provider::onLaunch().
|
||||
*/
|
||||
public function test_on_launch_no_frame_embedding() {
|
||||
$tp = $this->build_dummy_tp();
|
||||
|
||||
// Capture output of onLaunch() method and save it as a string.
|
||||
ob_start();
|
||||
// Suppress session header errors.
|
||||
@$tp->onLaunch();
|
||||
$output = ob_get_clean();
|
||||
|
||||
$this->assertContains(get_string('frameembeddingnotenabled', 'enrol_lti'), $output);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for tool_provider::onLaunch().
|
||||
*/
|
||||
public function test_on_launch_with_frame_embedding() {
|
||||
global $CFG;
|
||||
$CFG->allowframembedding = true;
|
||||
|
||||
$tp = $this->build_dummy_tp();
|
||||
|
||||
// If redirect was called here, we will encounter an 'unsupported redirect error'.
|
||||
// We just want to verify that redirect() was called if frame embedding is allowed.
|
||||
$this->expectException('moodle_exception');
|
||||
|
||||
// Suppress session header errors.
|
||||
@$tp->onLaunch();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for tool_provider::onLaunch() with invalid secret and no tool proxy (for LTI 1 launches).
|
||||
*/
|
||||
public function test_on_launch_with_invalid_secret_and_no_proxy() {
|
||||
$tp = $this->build_dummy_tp('badsecret');
|
||||
|
||||
// Suppress session header errors.
|
||||
@$tp->onLaunch();
|
||||
$this->assertFalse($tp->ok);
|
||||
$this->assertEquals(get_string('invalidrequest', 'enrol_lti'), $tp->message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for tool_provider::onLaunch() with invalid launch URL.
|
||||
*/
|
||||
public function test_on_launch_proxy_with_invalid_launch_url() {
|
||||
$proxy = [
|
||||
'tool_profile' => [
|
||||
'resource_handler' => [
|
||||
[
|
||||
'message' => [
|
||||
[
|
||||
'message_type' => 'basic-lti-launch-request',
|
||||
'path' => '/enrol/lti/tool.php'
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
];
|
||||
$tp = $this->build_dummy_tp($this->tool->secret, $proxy);
|
||||
// Suppress session header errors.
|
||||
@$tp->onLaunch();
|
||||
|
||||
$this->assertFalse($tp->ok);
|
||||
$this->assertEquals(get_string('invalidrequest', 'enrol_lti'), $tp->message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for tool_provider::onLaunch() with invalid launch URL.
|
||||
*/
|
||||
public function test_on_launch_proxy_with_valid_launch_url() {
|
||||
$tool = $this->tool;
|
||||
|
||||
$proxy = [
|
||||
'tool_profile' => [
|
||||
'resource_handler' => [
|
||||
[
|
||||
'message' => [
|
||||
[
|
||||
'message_type' => 'basic-lti-launch-request',
|
||||
'path' => '/enrol/lti/tool.php?id=' . $tool->id
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
];
|
||||
$tp = $this->build_dummy_tp($this->tool->secret, $proxy);
|
||||
|
||||
// Capture output of onLaunch() method and save it as a string.
|
||||
ob_start();
|
||||
// Suppress session header errors.
|
||||
@$tp->onLaunch();
|
||||
$output = ob_get_clean();
|
||||
|
||||
$this->assertTrue($tp->ok);
|
||||
$this->assertEquals(get_string('success'), $tp->message);
|
||||
$this->assertContains(get_string('frameembeddingnotenabled', 'enrol_lti'), $output);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for tool_provider::onLaunch() for a request with message type other than basic-lti-launch-request.
|
||||
*/
|
||||
public function test_on_launch_proxy_with_invalid_message_type() {
|
||||
$tool = $this->tool;
|
||||
|
||||
$proxy = [
|
||||
'tool_profile' => [
|
||||
'resource_handler' => [
|
||||
[
|
||||
'message' => [
|
||||
[
|
||||
'message_type' => 'ContentItemSelectionRequest',
|
||||
'path' => '/enrol/lti/tool.php?id=' . $tool->id
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
];
|
||||
$tp = $this->build_dummy_tp($this->tool->secret, $proxy);
|
||||
|
||||
// Suppress session header errors.
|
||||
@$tp->onLaunch();
|
||||
|
||||
$this->assertFalse($tp->ok);
|
||||
$this->assertEquals(get_string('invalidrequest', 'enrol_lti'), $tp->message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for tool_provider::onLaunch() to verify that a user image can be set from the resource link's custom_user_image setting.
|
||||
*/
|
||||
public function test_on_launch_with_user_image_from_resource_link() {
|
||||
global $DB;
|
||||
|
||||
$userimageurl = $this->getExternalTestFileUrl('test.jpg');
|
||||
$resourcelinksettings = [
|
||||
'custom_user_image' => $userimageurl
|
||||
];
|
||||
$tp = $this->build_dummy_tp($this->tool->secret, null, $resourcelinksettings);
|
||||
|
||||
// Suppress output and session header errors.
|
||||
ob_start();
|
||||
@$tp->onLaunch();
|
||||
ob_end_clean();
|
||||
|
||||
$this->assertEquals($userimageurl, $tp->resourceLink->getSetting('custom_user_image'));
|
||||
|
||||
$username = helper::create_username($tp->consumer->getKey(), $tp->user->ltiUserId);
|
||||
$user = $DB->get_record('user', ['username' => $username]);
|
||||
// User was found.
|
||||
$this->assertNotFalse($user);
|
||||
// User picture was set.
|
||||
$this->assertNotEmpty($user->picture);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for tool_provider::onLaunch() to verify that a LTI user has been enrolled.
|
||||
*/
|
||||
public function test_on_launch_user_enrolment() {
|
||||
global $DB;
|
||||
|
||||
$tp = $this->build_dummy_tp($this->tool->secret);
|
||||
|
||||
// Suppress output and session header errors.
|
||||
ob_start();
|
||||
@$tp->onLaunch();
|
||||
ob_end_clean();
|
||||
|
||||
$username = helper::create_username($tp->consumer->getKey(), $tp->user->ltiUserId);
|
||||
$user = $DB->get_record('user', ['username' => $username]);
|
||||
// User was found.
|
||||
$this->assertNotFalse($user);
|
||||
// User picture was not set.
|
||||
$this->assertEmpty($user->picture);
|
||||
|
||||
// Check user enrolment.
|
||||
$enrolled = $DB->record_exists('user_enrolments', ['enrolid' => $this->tool->enrolid, 'userid' => $user->id]);
|
||||
$this->assertTrue($enrolled);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for tool_provider::onLaunch() when the consumer object has not been set.
|
||||
*/
|
||||
public function test_on_launch_no_consumer() {
|
||||
$tool = $this->tool;
|
||||
|
||||
$tp = new dummy_tool_provider($tool->id);
|
||||
$tp->onLaunch();
|
||||
$this->assertFalse($tp->ok);
|
||||
$this->assertEquals(get_string('invalidtoolconsumer', 'enrol_lti'), $tp->message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for tool_provider::onLaunch() when we have a non-existent consumer data.
|
||||
*/
|
||||
public function test_on_launch_invalid_consumer() {
|
||||
$tool = $this->tool;
|
||||
|
||||
$dataconnector = new data_connector();
|
||||
// Build consumer object but don't save it.
|
||||
$consumer = new dummy_tool_consumer('testkey', $dataconnector);
|
||||
|
||||
$tp = new dummy_tool_provider($tool->id);
|
||||
$tp->consumer = $consumer;
|
||||
$tp->onLaunch();
|
||||
$this->assertFalse($tp->ok);
|
||||
$this->assertEquals(get_string('invalidtoolconsumer', 'enrol_lti'), $tp->message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a dummy tool provider object.
|
||||
*
|
||||
* @param string $secret Consumer secret.
|
||||
* @param array|stdClass $proxy Tool proxy data.
|
||||
* @param null $resourcelinksettings Key-value array for resource link settings.
|
||||
* @return dummy_tool_provider
|
||||
*/
|
||||
protected function build_dummy_tp($secret = null, $proxy = null, $resourcelinksettings = null) {
|
||||
$tool = $this->tool;
|
||||
|
||||
$dataconnector = new data_connector();
|
||||
$consumer = new ToolConsumer('testkey', $dataconnector);
|
||||
|
||||
$ltiversion = ToolProvider::LTI_VERSION2;
|
||||
if ($secret === null && $proxy === null) {
|
||||
$consumer->secret = $tool->secret;
|
||||
$ltiversion = ToolProvider::LTI_VERSION1;
|
||||
} else {
|
||||
$consumer->secret = $secret;
|
||||
}
|
||||
$consumer->ltiVersion = $ltiversion;
|
||||
|
||||
$consumer->name = 'TEST CONSUMER NAME';
|
||||
$consumer->consumerName = 'TEST CONSUMER INSTANCE NAME';
|
||||
$consumer->consumerGuid = 'TEST CONSUMER INSTANCE GUID';
|
||||
$consumer->consumerVersion = 'TEST CONSUMER INFO VERSION';
|
||||
$consumer->enabled = true;
|
||||
$consumer->protected = true;
|
||||
if ($proxy !== null) {
|
||||
$consumer->toolProxy = json_encode($proxy);
|
||||
}
|
||||
$consumer->save();
|
||||
|
||||
$resourcelink = ResourceLink::fromConsumer($consumer, 'testresourcelinkid');
|
||||
if (!empty($resourcelinksettings)) {
|
||||
foreach ($resourcelinksettings as $setting => $value) {
|
||||
$resourcelink->setSetting($setting, $value);
|
||||
}
|
||||
}
|
||||
$resourcelink->save();
|
||||
|
||||
$ltiuser = User::fromResourceLink($resourcelink, '');
|
||||
$ltiuser->ltiResultSourcedId = 'testLtiResultSourcedId';
|
||||
$ltiuser->ltiUserId = 'testuserid';
|
||||
$ltiuser->email = 'user1@example.com';
|
||||
$ltiuser->save();
|
||||
|
||||
$tp = new dummy_tool_provider($tool->id);
|
||||
$tp->user = $ltiuser;
|
||||
$tp->resourceLink = $resourcelink;
|
||||
$tp->consumer = $consumer;
|
||||
|
||||
return $tp;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Class dummy_tool_provider.
|
||||
*
|
||||
* A class that extends tool_provider so that we can expose the protected methods that we have overridden.
|
||||
*
|
||||
* @copyright 2016 Jun Pataleta <jun@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class dummy_tool_provider extends tool_provider {
|
||||
/**
|
||||
* Exposes tool_provider::onError().
|
||||
*/
|
||||
public function onError() {
|
||||
parent::onError();
|
||||
}
|
||||
|
||||
/**
|
||||
* Exposes tool_provider::onLaunch().
|
||||
*/
|
||||
public function onLaunch() {
|
||||
parent::onLaunch();
|
||||
}
|
||||
|
||||
/**
|
||||
* Exposes tool_provider::onRegister().
|
||||
*/
|
||||
public function onRegister() {
|
||||
parent::onRegister();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Class dummy_tool_consumer
|
||||
*
|
||||
* A class that extends ToolConsumer in order to override and simulate sending and receiving data to tool consumer endpoint.
|
||||
*
|
||||
* @copyright 2016 Jun Pataleta <jun@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class dummy_tool_consumer extends ToolConsumer {
|
||||
|
||||
/**
|
||||
* @var bool Flag to indicate whether to send an OK response or a failed response.
|
||||
*/
|
||||
protected $success = false;
|
||||
|
||||
/**
|
||||
* dummy_tool_consumer constructor.
|
||||
*
|
||||
* @param null|string $key
|
||||
* @param mixed|null $dataconnector
|
||||
* @param bool $autoenable
|
||||
* @param bool $success
|
||||
*/
|
||||
public function __construct($key = null, $dataconnector = null, $autoenable = false, $success = false) {
|
||||
parent::__construct($key, $dataconnector, $autoenable);
|
||||
$this->success = $success;
|
||||
}
|
||||
|
||||
/**
|
||||
* Override ToolConsumer::doServiceRequest() to simulate sending/receiving data to and from the tool consumer.
|
||||
*
|
||||
* @param object $service
|
||||
* @param string $method
|
||||
* @param string $format
|
||||
* @param mixed $data
|
||||
* @return HTTPMessage
|
||||
*/
|
||||
public function doServiceRequest($service, $method, $format, $data) {
|
||||
$response = (object)['tool_proxy_guid' => 1];
|
||||
$header = ToolConsumer::addSignature($service->endpoint, $this->getKey(), $this->secret, $data, $method, $format);
|
||||
$http = new HTTPMessage($service->endpoint, $method, $data, $header);
|
||||
|
||||
if ($this->success) {
|
||||
$http->responseJson = $response;
|
||||
$http->ok = true;
|
||||
$http->status = 201;
|
||||
}
|
||||
|
||||
return $http;
|
||||
}
|
||||
}
|
@ -1060,4 +1060,48 @@ EOD;
|
||||
$gradeoutcome->update_from_db();
|
||||
return $gradeoutcome->get_record_data();
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function used to create an LTI tool.
|
||||
*
|
||||
* @param array $data
|
||||
* @return stdClass the tool
|
||||
*/
|
||||
public function create_lti_tool($data = array()) {
|
||||
global $DB;
|
||||
|
||||
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
|
||||
$teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
|
||||
|
||||
// Create a course if no course id was specified.
|
||||
if (empty($data->courseid)) {
|
||||
$course = $this->create_course();
|
||||
$data->courseid = $course->id;
|
||||
} else {
|
||||
$course = get_course($data->courseid);
|
||||
}
|
||||
|
||||
if (!empty($data->cmid)) {
|
||||
$data->contextid = context_module::instance($data->cmid)->id;
|
||||
} else {
|
||||
$data->contextid = context_course::instance($data->courseid)->id;
|
||||
}
|
||||
|
||||
// Set it to enabled if no status was specified.
|
||||
if (!isset($data->status)) {
|
||||
$data->status = ENROL_INSTANCE_ENABLED;
|
||||
}
|
||||
|
||||
// Add some extra necessary fields to the data.
|
||||
$data->name = 'Test LTI';
|
||||
$data->roleinstructor = $studentrole->id;
|
||||
$data->rolelearner = $teacherrole->id;
|
||||
|
||||
// Get the enrol LTI plugin.
|
||||
$enrolplugin = enrol_get_plugin('lti');
|
||||
$instanceid = $enrolplugin->add_instance($course, (array) $data);
|
||||
|
||||
// Get the tool associated with this instance.
|
||||
return $DB->get_record('enrol_lti_tools', array('enrolid' => $instanceid));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user