From cea2645a81d4302c2edd0e3de2ad7468c4e7648b Mon Sep 17 00:00:00 2001 From: Jake Dallimore Date: Mon, 20 Jul 2020 10:45:54 +0800 Subject: [PATCH 1/1] MDL-69319 mod_lti: replace try/catch with expectException in ext tests --- mod/lti/tests/externallib_test.php | 83 +++++++++++++++++------------- 1 file changed, 48 insertions(+), 35 deletions(-) diff --git a/mod/lti/tests/externallib_test.php b/mod/lti/tests/externallib_test.php index 046bdc56cf4..94e2c4c3674 100644 --- a/mod/lti/tests/externallib_test.php +++ b/mod/lti/tests/externallib_test.php @@ -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); - } - } /* -- 2.43.0