* Set up for every test
*/
public function setUp() {
- global $DB;
$this->resetAfterTest();
+ }
+
+ /**
+ * Sets up some basic test data including course, users, roles, and an lti instance, for use in some tests.
+ * @return array
+ */
+ protected function setup_test_data() {
+ global $DB;
$this->setAdminUser();
// Setup test data.
- $this->course = $this->getDataGenerator()->create_course();
- $this->lti = $this->getDataGenerator()->create_module('lti',
- array('course' => $this->course->id, 'toolurl' => 'http://localhost/not/real/tool.php'));
- $this->context = context_module::instance($this->lti->cmid);
- $this->cm = get_coursemodule_from_instance('lti', $this->lti->id);
+ $course = $this->getDataGenerator()->create_course();
+ $lti = $this->getDataGenerator()->create_module(
+ 'lti',
+ ['course' => $course->id, 'toolurl' => 'http://localhost/not/real/tool.php']
+ );
+ $context = context_module::instance($lti->cmid);
+ $cm = get_coursemodule_from_instance('lti', $lti->id);
// Create users.
- $this->student = self::getDataGenerator()->create_user();
- $this->teacher = self::getDataGenerator()->create_user();
+ $student = self::getDataGenerator()->create_user();
+ $teacher = self::getDataGenerator()->create_user();
// Users enrolments.
- $this->studentrole = $DB->get_record('role', array('shortname' => 'student'));
- $this->teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
- $this->getDataGenerator()->enrol_user($this->student->id, $this->course->id, $this->studentrole->id, 'manual');
- $this->getDataGenerator()->enrol_user($this->teacher->id, $this->course->id, $this->teacherrole->id, 'manual');
+ $studentrole = $DB->get_record('role', array('shortname' => 'student'));
+ $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
+ $this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id, 'manual');
+ $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id, 'manual');
+
+ return [
+ 'course' => $course,
+ 'lti' => $lti,
+ 'context' => $context,
+ 'cm' => $cm,
+ 'student' => $student,
+ 'teacher' => $teacher,
+ 'studentrole' => $studentrole,
+ 'teacherrole' => $teacherrole
+ ];
}
/**
public function test_get_tool_launch_data() {
global $USER, $SITE;
- $result = mod_lti_external::get_tool_launch_data($this->lti->id);
+ [
+ 'course' => $course,
+ 'lti' => $lti
+ ] = $this->setup_test_data();
+
+ $result = mod_lti_external::get_tool_launch_data($lti->id);
$result = external_api::clean_returnvalue(mod_lti_external::get_tool_launch_data_returns(), $result);
// Basic test, the function returns what it's expected.
- self::assertEquals($this->lti->toolurl, $result['endpoint']);
+ self::assertEquals($lti->toolurl, $result['endpoint']);
self::assertCount(36, $result['parameters']);
// Check some parameters.
foreach ($result['parameters'] as $param) {
$parameters[$param['name']] = $param['value'];
}
- self::assertEquals($this->lti->resourcekey, $parameters['oauth_consumer_key']);
- self::assertEquals($this->course->fullname, $parameters['context_title']);
- self::assertEquals($this->course->shortname, $parameters['context_label']);
+ self::assertEquals($lti->resourcekey, $parameters['oauth_consumer_key']);
+ self::assertEquals($course->fullname, $parameters['context_title']);
+ self::assertEquals($course->shortname, $parameters['context_label']);
self::assertEquals($USER->id, $parameters['user_id']);
self::assertEquals($USER->firstname, $parameters['lis_person_name_given']);
self::assertEquals($USER->lastname, $parameters['lis_person_name_family']);
public function test_mod_lti_get_ltis_by_courses() {
global $DB;
+ [
+ 'course' => $course,
+ 'lti' => $lti,
+ 'student' => $student,
+ 'teacher' => $teacher,
+ 'studentrole' => $studentrole,
+ ] = $this->setup_test_data();
+
// Create additional course.
$course2 = self::getDataGenerator()->create_course();
break;
}
}
- $enrol->enrol_user($instance2, $this->student->id, $this->studentrole->id);
+ $enrol->enrol_user($instance2, $student->id, $studentrole->id);
- self::setUser($this->student);
+ self::setUser($student);
$returndescription = mod_lti_external::get_ltis_by_courses_returns();
'showtitlelaunch', 'showdescriptionlaunch', 'icon', 'secureicon');
// Add expected coursemodule and data.
- $lti1 = $this->lti;
+ $lti1 = $lti;
$lti1->coursemodule = $lti1->cmid;
$lti1->introformat = 1;
$lti1->section = 0;
$expectedltis = array($expected2, $expected1);
// Call the external function passing course ids.
- $result = mod_lti_external::get_ltis_by_courses(array($course2->id, $this->course->id));
+ $result = mod_lti_external::get_ltis_by_courses(array($course2->id, $course->id));
$result = external_api::clean_returnvalue($returndescription, $result);
$this->assertEquals($expectedltis, $result['ltis']);
$this->assertCount(0, $result['warnings']);
// Unenrol user from second course and alter expected ltis.
- $enrol->unenrol_user($instance2, $this->student->id);
+ $enrol->unenrol_user($instance2, $student->id);
array_shift($expectedltis);
// Call the external function without passing course id.
$this->assertEquals($course2->id, $result['warnings'][0]['itemid']);
// Now, try as a teacher for getting all the additional fields.
- self::setUser($this->teacher);
+ self::setUser($teacher);
$additionalfields = array('timecreated', 'timemodified', 'typeid', 'toolurl', 'securetoolurl',
'instructorchoicesendname', 'instructorchoicesendemailaddr', 'instructorchoiceallowroster',
// Admin also should get all the information.
self::setAdminUser();
- $result = mod_lti_external::get_ltis_by_courses(array($this->course->id));
+ $result = mod_lti_external::get_ltis_by_courses(array($course->id));
$result = external_api::clean_returnvalue($returndescription, $result);
$this->assertEquals($expectedltis, $result['ltis']);
// Now, prohibit capabilities.
- $this->setUser($this->student);
- $contextcourse1 = context_course::instance($this->course->id);
+ $this->setUser($student);
+ $contextcourse1 = context_course::instance($course->id);
// Prohibit capability = mod:lti:view on Course1 for students.
- assign_capability('mod/lti:view', CAP_PROHIBIT, $this->studentrole->id, $contextcourse1->id);
+ assign_capability('mod/lti:view', CAP_PROHIBIT, $studentrole->id, $contextcourse1->id);
// Empty all the caches that may be affected by this change.
accesslib_clear_all_caches_for_unit_testing();
course_modinfo::clear_instance_cache();
- $ltis = mod_lti_external::get_ltis_by_courses(array($this->course->id));
+ $ltis = mod_lti_external::get_ltis_by_courses(array($course->id));
$ltis = external_api::clean_returnvalue(mod_lti_external::get_ltis_by_courses_returns(), $ltis);
$this->assertCount(0, $ltis['ltis']);
}
public function test_view_lti() {
global $DB;
+ [
+ 'lti' => $lti,
+ 'context' => $context,
+ 'cm' => $cm,
+ 'student' => $student,
+ 'studentrole' => $studentrole,
+ ] = $this->setup_test_data();
+
// Test invalid instance id.
try {
mod_lti_external::view_lti(0);
$usernotenrolled = self::getDataGenerator()->create_user();
$this->setUser($usernotenrolled);
try {
- mod_lti_external::view_lti($this->lti->id);
+ mod_lti_external::view_lti($lti->id);
$this->fail('Exception expected due to not enrolled user.');
} catch (moodle_exception $e) {
$this->assertEquals('requireloginerror', $e->errorcode);
}
// Test user with full capabilities.
- $this->setUser($this->student);
+ $this->setUser($student);
// Trigger and capture the event.
$sink = $this->redirectEvents();
- $result = mod_lti_external::view_lti($this->lti->id);
+ $result = mod_lti_external::view_lti($lti->id);
$result = external_api::clean_returnvalue(mod_lti_external::view_lti_returns(), $result);
$events = $sink->get_events();
// Checking that the event contains the expected values.
$this->assertInstanceOf('\mod_lti\event\course_module_viewed', $event);
- $this->assertEquals($this->context, $event->get_context());
- $moodlelti = new \moodle_url('/mod/lti/view.php', array('id' => $this->cm->id));
+ $this->assertEquals($context, $event->get_context());
+ $moodlelti = new \moodle_url('/mod/lti/view.php', array('id' => $cm->id));
$this->assertEquals($moodlelti, $event->get_url());
$this->assertEventContextNotUsed($event);
$this->assertNotEmpty($event->get_name());
// Test user with no capabilities.
// We need a explicit prohibit since this capability is only defined in authenticated user and guest roles.
- assign_capability('mod/lti:view', CAP_PROHIBIT, $this->studentrole->id, $this->context->id);
+ assign_capability('mod/lti:view', CAP_PROHIBIT, $studentrole->id, $context->id);
// Empty all the caches that may be affected by this change.
accesslib_clear_all_caches_for_unit_testing();
course_modinfo::clear_instance_cache();
try {
- mod_lti_external::view_lti($this->lti->id);
+ mod_lti_external::view_lti($lti->id);
$this->fail('Exception expected due to missing capability.');
} catch (moodle_exception $e) {
$this->assertEquals('requireloginerror', $e->errorcode);
* Test create tool proxy
*/
public function test_mod_lti_create_tool_proxy() {
+ $this->setAdminUser();
$capabilities = ['AA', 'BB'];
$proxy = mod_lti_external::create_tool_proxy('Test proxy', $this->getExternalTestFileUrl('/test.html'), $capabilities, []);
$proxy = (object) external_api::clean_returnvalue(mod_lti_external::create_tool_proxy_returns(), $proxy);
* Test create tool proxy with duplicate url
*/
public function test_mod_lti_create_tool_proxy_duplicateurl() {
+ $this->setAdminUser();
$this->expectException('moodle_exception');
$proxy = mod_lti_external::create_tool_proxy('Test proxy 1', $this->getExternalTestFileUrl('/test.html'), array(), array());
$proxy = mod_lti_external::create_tool_proxy('Test proxy 2', $this->getExternalTestFileUrl('/test.html'), array(), array());
* Test create tool proxy without sufficient capability
*/
public function test_mod_lti_create_tool_proxy_without_capability() {
- self::setUser($this->teacher);
+ $course = $this->getDataGenerator()->create_course();
+ $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
+ $this->setUser($teacher);
$this->expectException('required_capability_exception');
$proxy = mod_lti_external::create_tool_proxy('Test proxy', $this->getExternalTestFileUrl('/test.html'), array(), array());
}
* Test delete tool proxy
*/
public function test_mod_lti_delete_tool_proxy() {
+ $this->setAdminUser();
$proxy = mod_lti_external::create_tool_proxy('Test proxy', $this->getExternalTestFileUrl('/test.html'), array(), array());
$proxy = (object) external_api::clean_returnvalue(mod_lti_external::create_tool_proxy_returns(), $proxy);
$this->assertNotEmpty(lti_get_tool_proxy($proxy->id));
* Test get tool proxy registration request
*/
public function test_mod_lti_get_tool_proxy_registration_request() {
+ $this->setAdminUser();
$proxy = mod_lti_external::create_tool_proxy('Test proxy', $this->getExternalTestFileUrl('/test.html'), array(), array());
$proxy = (object) external_api::clean_returnvalue(mod_lti_external::create_tool_proxy_returns(), $proxy);
*/
public function test_mod_lti_get_tool_types() {
// Create a tool proxy.
+ $this->setAdminUser();
$proxy = mod_lti_external::create_tool_proxy('Test proxy', $this->getExternalTestFileUrl('/test.html'), array(), array());
$proxy = (object) external_api::clean_returnvalue(mod_lti_external::create_tool_proxy_returns(), $proxy);
* Test create tool type
*/
public function test_mod_lti_create_tool_type() {
+ $this->setAdminUser();
$type = mod_lti_external::create_tool_type($this->getExternalTestFileUrl('/ims_cartridge_basic_lti_link.xml'), '', '');
$type = external_api::clean_returnvalue(mod_lti_external::create_tool_type_returns(), $type);
* Test creating of tool types without sufficient capability
*/
public function test_mod_lti_create_tool_type_without_capability() {
- self::setUser($this->teacher);
+ $course = $this->getDataGenerator()->create_course();
+ $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
+ $this->setUser($teacher);
$this->expectException('required_capability_exception');
$type = mod_lti_external::create_tool_type($this->getExternalTestFileUrl('/ims_cartridge_basic_lti_link.xml'), '', '');
}
* Test update tool type
*/
public function test_mod_lti_update_tool_type() {
+ $this->setAdminUser();
$type = mod_lti_external::create_tool_type($this->getExternalTestFileUrl('/ims_cartridge_basic_lti_link.xml'), '', '');
$type = external_api::clean_returnvalue(mod_lti_external::create_tool_type_returns(), $type);
* Test delete tool type
*/
public function test_mod_lti_delete_tool_type() {
+ $this->setAdminUser();
$type = mod_lti_external::create_tool_type($this->getExternalTestFileUrl('/ims_cartridge_basic_lti_link.xml'), '', '');
$type = external_api::clean_returnvalue(mod_lti_external::create_tool_type_returns(), $type);
$this->assertNotEmpty(lti_get_type($type['id']));
* Test delete tool type without sufficient capability
*/
public function test_mod_lti_delete_tool_type_without_capability() {
+ $this->setAdminUser();
$type = mod_lti_external::create_tool_type($this->getExternalTestFileUrl('/ims_cartridge_basic_lti_link.xml'), '', '');
$type = external_api::clean_returnvalue(mod_lti_external::create_tool_type_returns(), $type);
$this->assertNotEmpty(lti_get_type($type['id']));
$this->expectException('required_capability_exception');
- self::setUser($this->teacher);
+ $course = $this->getDataGenerator()->create_course();
+ $teacher = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
+ $this->setUser($teacher);
$type = mod_lti_external::delete_tool_type($type['id']);
}
* Test is cartridge
*/
public function test_mod_lti_is_cartridge() {
+ $this->setAdminUser();
$result = mod_lti_external::is_cartridge($this->getExternalTestFileUrl('/ims_cartridge_basic_lti_link.xml'));
$result = external_api::clean_returnvalue(mod_lti_external::is_cartridge_returns(), $result);
$this->assertTrue($result['iscartridge']);