MDL-51624 mod_lti: New Web Service mod_lti_view_lti
[moodle.git] / mod / lti / tests / externallib_test.php
index 9943fe6..0fab226 100644 (file)
@@ -216,4 +216,63 @@ class mod_lti_external_testcase extends externallib_advanced_testcase {
         $this->assertFalse(isset($ltis['ltis'][0]['intro']));
     }
 
+    /**
+     * Test view_lti
+     */
+    public function test_view_lti() {
+        global $DB;
+
+        // 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($this->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);
+
+        // Trigger and capture the event.
+        $sink = $this->redirectEvents();
+
+        $result = mod_lti_external::view_lti($this->lti->id);
+        $result = external_api::clean_returnvalue(mod_lti_external::view_lti_returns(), $result);
+
+        $events = $sink->get_events();
+        $this->assertCount(1, $events);
+        $event = array_shift($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($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);
+        accesslib_clear_all_caches_for_unit_testing();
+
+        try {
+            mod_lti_external::view_lti($this->lti->id);
+            $this->fail('Exception expected due to missing capability.');
+        } catch (moodle_exception $e) {
+            $this->assertEquals('nopermissions', $e->errorcode);
+        }
+
+    }
+
 }