Skip to content

Commit

Permalink
MDL-53832 enrol_lti: Unit tests for tool_provider
Browse files Browse the repository at this point in the history
Original subtask: MDL-55647.
  • Loading branch information
junpataleta authored and John Okely committed Oct 19, 2016
1 parent daebcd2 commit c314d6e
Show file tree
Hide file tree
Showing 6 changed files with 725 additions and 73 deletions.
2 changes: 1 addition & 1 deletion enrol/lti/classes/tool_provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ protected function onError() {

$this->errorOutput = '';

notification::error(get_string('failedregistration', 'enrol_lti', ['reason' => $message]));
notification::error(get_string('failedrequest', 'enrol_lti', ['reason' => $message]));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion enrol/lti/lang/en/enrol_lti.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
$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.';
Expand Down
27 changes: 27 additions & 0 deletions enrol/lti/tests/fixtures/tool_consumer_profile.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
101 changes: 30 additions & 71 deletions enrol/lti/tests/helper_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function test_enrol_user_max_enrolled() {
// 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);
Expand Down Expand Up @@ -122,7 +122,7 @@ public function test_enrol_user_enrolment_not_started() {
// 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);
Expand All @@ -144,7 +144,7 @@ public function test_enrol_user_enrolment_finished() {
// 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);
Expand All @@ -161,22 +161,23 @@ public function test_enrol_user_enrolment_finished() {
* 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();
Expand All @@ -199,22 +200,23 @@ public function test_count_lti_tools() {
* 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();
Expand Down Expand Up @@ -254,7 +256,7 @@ public function test_get_launch_url() {
$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);
Expand All @@ -274,7 +276,7 @@ public function test_get_cartridge_url() {
$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);
Expand Down Expand Up @@ -304,7 +306,7 @@ public function test_get_proxy_url() {
$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);
Expand All @@ -328,7 +330,7 @@ public function test_get_name() {
$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);
Expand All @@ -342,20 +344,21 @@ public function test_get_name() {
* 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);
}
Expand All @@ -369,11 +372,11 @@ public function test_get_icon() {
$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);

}
Expand All @@ -385,7 +388,7 @@ public function test_verify_cartridge_token() {
$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));
Expand All @@ -399,7 +402,7 @@ public function test_verify_proxy_token() {
$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));
Expand Down Expand Up @@ -526,55 +529,11 @@ public function test_create_cartridge() {
$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));
}
}
Loading

0 comments on commit c314d6e

Please sign in to comment.