From db04ed91ae41411dcda1c3c20988b85dd922031b Mon Sep 17 00:00:00 2001 From: Mikhail Golenkov Date: Thu, 16 Jun 2022 19:05:33 +1000 Subject: [PATCH] MDL-74993 course: Output cm id when throwing Invalid course module id --- lang/en/error.php | 1 + lib/modinfolib.php | 4 ++-- lib/tests/modinfolib_test.php | 32 +++++++++++++++++++++++++++++++- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/lang/en/error.php b/lang/en/error.php index 114fe7d1271..7bb16032ebc 100644 --- a/lang/en/error.php +++ b/lang/en/error.php @@ -330,6 +330,7 @@ $string['invalidcourseid'] = 'You are trying to use an invalid course ID'; $string['invalidcourselevel'] = 'Incorrect context level'; $string['invalidcourseformat'] = 'Invalid course format'; $string['invalidcoursemodule'] = 'Invalid course module ID'; +$string['invalidcoursemoduleid'] = 'Invalid course module id: {$a}'; $string['invalidcoursenameshort'] = 'Invalid short course name'; $string['invalidcountrycode'] = 'Invalid country code: {$a}'; $string['invaliddata'] = 'Data submitted is invalid'; diff --git a/lib/modinfolib.php b/lib/modinfolib.php index 0e2b5dd5d35..f0d5ce73a4b 100644 --- a/lib/modinfolib.php +++ b/lib/modinfolib.php @@ -239,7 +239,7 @@ class course_modinfo { */ public function get_cm($cmid) { if (empty($this->cms[$cmid])) { - throw new moodle_exception('invalidcoursemodule', 'error'); + throw new moodle_exception('invalidcoursemoduleid', 'error', '', $cmid); } return $this->cms[$cmid]; } @@ -2627,7 +2627,7 @@ function get_course_and_cm_from_cmid($cmorid, $modulename = '', $courseorid = 0, $modinfo = get_fast_modinfo($course, $userid); $cm = $modinfo->get_cm($cmid); if ($modulename && $cm->modname !== $modulename) { - throw new moodle_exception('invalidcoursemodule', 'error'); + throw new moodle_exception('invalidcoursemoduleid', 'error', '', $cmid); } return array($course, $cm); } diff --git a/lib/tests/modinfolib_test.php b/lib/tests/modinfolib_test.php index da19910af82..e908a53e510 100644 --- a/lib/tests/modinfolib_test.php +++ b/lib/tests/modinfolib_test.php @@ -801,7 +801,7 @@ class modinfolib_test extends advanced_testcase { get_course_and_cm_from_cmid($page->cmid, 'forum'); $this->fail(); } catch (moodle_exception $e) { - $this->assertEquals('invalidcoursemodule', $e->errorcode); + $this->assertEquals('invalidcoursemoduleid', $e->errorcode); } // Invalid module name. @@ -1096,4 +1096,34 @@ class modinfolib_test extends advanced_testcase { // Make sure that the cacherev will be reset. $this->assertEquals(-1, $coursemodinfo->cacherev); } + + /** + * Test get_cm() method to output course module id in the exception text. + * + * @covers \course_modinfo::get_cm + * @return void + */ + public function test_invalid_course_module_id(): void { + global $DB; + $this->resetAfterTest(); + + $course = $this->getDataGenerator()->create_course(); + $forum0 = $this->getDataGenerator()->create_module('assign', ['course' => $course->id], ['section' => 0]); + $forum1 = $this->getDataGenerator()->create_module('assign', ['course' => $course->id], ['section' => 0]); + $forum2 = $this->getDataGenerator()->create_module('assign', ['course' => $course->id], ['section' => 0]); + + // Break section sequence. + $modinfo = get_fast_modinfo($course->id); + $sectionid = $modinfo->get_section_info(0)->id; + $section = $DB->get_record('course_sections', ['id' => $sectionid]); + $sequence = explode(',', $section->sequence); + $sequence = array_diff($sequence, [$forum1->cmid]); + $section->sequence = implode(',', $sequence); + $DB->update_record('course_sections', $section); + + // Assert exception text. + $this->expectException(\moodle_exception::class); + $this->expectExceptionMessage('Invalid course module id: ' . $forum1->cmid); + delete_course($course, false); + } }