Skip to content

Commit

Permalink
MDL-74993 course: Output cm id when throwing Invalid course module id
Browse files Browse the repository at this point in the history
  • Loading branch information
golenkovm committed Sep 12, 2022
1 parent c6211a6 commit db04ed9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
1 change: 1 addition & 0 deletions lang/en/error.php
Expand Up @@ -330,6 +330,7 @@
$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';
Expand Down
4 changes: 2 additions & 2 deletions lib/modinfolib.php
Expand Up @@ -239,7 +239,7 @@ public function get_cms() {
*/
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];
}
Expand Down Expand Up @@ -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);
}
Expand Down
32 changes: 31 additions & 1 deletion lib/tests/modinfolib_test.php
Expand Up @@ -801,7 +801,7 @@ public function test_get_course_and_cm_from_cmid() {
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.
Expand Down Expand Up @@ -1096,4 +1096,34 @@ public function test_section_cache_by_number(): void {
// 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);
}
}

0 comments on commit db04ed9

Please sign in to comment.