From b07642e882daddb2fdd78e5cf83c8cc955ffd8ef Mon Sep 17 00:00:00 2001 From: Jake Dallimore Date: Mon, 20 Jul 2020 10:45:54 +0800 Subject: [PATCH] MDL-69319 mod_lti: replace try/catch with expectException in ext tests --- mod/lti/tests/externallib_test.php | 80 +++++++++++++++++------------- 1 file changed, 46 insertions(+), 34 deletions(-) diff --git a/mod/lti/tests/externallib_test.php b/mod/lti/tests/externallib_test.php index a3252cda99e..58cd7208a83 100644 --- a/mod/lti/tests/externallib_test.php +++ b/mod/lti/tests/externallib_test.php @@ -254,7 +254,52 @@ 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_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() { + $td = $this->setup_test_data(); + $lti = $td['lti']; + + // 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() { + $td = $this->setup_test_data(); + $lti = $td['lti']; + $student = $td['student']; + $studentrole = $td['studentrole']; + $context = $td['context']; + + $this->setUser($student); + + // 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() { $td = $this->setup_test_data(); @@ -263,24 +308,6 @@ class mod_lti_external_testcase extends externallib_advanced_testcase { $cm = $td['cm']; $student = $td['student']; - // 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); - } - - // 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); - } - // Test user with full capabilities. $this->setUser($student); @@ -302,21 +329,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); - } - } /*