Commit 54748014 authored by Jake Dallimore's avatar Jake Dallimore
Browse files

MDL-69319 mod_lti: replace try/catch with expectException in ext tests

parent 6baabaf7
......@@ -256,34 +256,62 @@ class mod_lti_external_testcase extends externallib_advanced_testcase {
}
/**
* Test view_lti
* Test view_lti with an invalid instance id.
*/
public function test_view_lti() {
public function test_view_lti_invalid_instanceid() {
$this->expectException(moodle_exception::class);
mod_lti_external::view_lti(0);
}
/**
* Test view_lti as a user who is not enrolled in the course.
*/
public function test_view_lti_no_enrolment() {
[
'lti' => $lti
] = $this->setup_test_data();
// Test not-enrolled user.
$usernotenrolled = self::getDataGenerator()->create_user();
$this->setUser($usernotenrolled);
$this->expectException(moodle_exception::class);
mod_lti_external::view_lti($lti->id);
}
/**
* Test view_lti for a user without the mod/lti:view capability.
*/
public function test_view_lti_no_capability() {
[
'lti' => $lti,
'context' => $context,
'cm' => $cm,
'student' => $student,
'studentrole' => $studentrole,
'context' => $context,
] = $this->setup_test_data();
// Test invalid instance id.
try {
mod_lti_external::view_lti(0);
$this->fail('Exception expected due to invalid mod_lti instance id.');
} catch (moodle_exception $e) {
$this->assertEquals('invalidrecord', $e->errorcode);
}
$this->setUser($student);
// Test not-enrolled user.
$usernotenrolled = self::getDataGenerator()->create_user();
$this->setUser($usernotenrolled);
try {
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);
}
// We need a explicit prohibit since this capability is only defined in authenticated user and guest roles.
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();
$this->expectException(moodle_exception::class);
mod_lti_external::view_lti($lti->id);
}
/**
* Test view_lti for a user with the mod/lti:view capability in the course.
*/
public function test_view_lti() {
[
'lti' => $lti,
'context' => $context,
'cm' => $cm,
'student' => $student,
] = $this->setup_test_data();
// Test user with full capabilities.
$this->setUser($student);
......@@ -306,21 +334,6 @@ class mod_lti_external_testcase extends externallib_advanced_testcase {
$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, $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($lti->id);
$this->fail('Exception expected due to missing capability.');
} catch (moodle_exception $e) {
$this->assertEquals('requireloginerror', $e->errorcode);
}
}
/*
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment