MDL-53351 lti: Do not throw PHP notice when deleting LTI instance
authorDavid Mudrák <david@moodle.com>
Fri, 4 Mar 2016 15:42:05 +0000 (16:42 +0100)
committerDavid Mudrák <david@moodle.com>
Fri, 4 Mar 2016 15:42:05 +0000 (16:42 +0100)
The typeid=0 is a valid and common value representing "Automatic, based
on launch URL" type. In that case, no record in the lti_types table is
found. Thence attempting to access its toolproxyid property used to
throw PHP notice.

mod/lti/lib.php
mod/lti/tests/lib_test.php

index cf9df60..090ae80 100644 (file)
@@ -188,8 +188,10 @@ function lti_delete_instance($id) {
     lti_grade_item_delete($basiclti);
 
     $ltitype = $DB->get_record('lti_types', array('id' => $basiclti->typeid));
-    $DB->delete_records('lti_tool_settings',
-        array('toolproxyid' => $ltitype->toolproxyid, 'course' => $basiclti->course, 'coursemoduleid' => $id));
+    if ($ltitype) {
+        $DB->delete_records('lti_tool_settings',
+            array('toolproxyid' => $ltitype->toolproxyid, 'course' => $basiclti->course, 'coursemoduleid' => $id));
+    }
 
     return $DB->delete_records("lti", array("id" => $basiclti->id));
 }
index f154683..0473f78 100644 (file)
@@ -89,4 +89,19 @@ class mod_lti_lib_testcase extends advanced_testcase {
         $this->assertEquals(1, $completiondata->completionstate);
 
     }
+
+    /**
+     * Test deleting LTI instance.
+     */
+    public function test_lti_delete_instance() {
+        $this->resetAfterTest();
+
+        $this->setAdminUser();
+        $course = $this->getDataGenerator()->create_course(array());
+        $lti = $this->getDataGenerator()->create_module('lti', array('course' => $course->id));
+        $cm = get_coursemodule_from_instance('lti', $lti->id);
+
+        // Must not throw notices.
+        course_delete_module($cm->id);
+    }
 }