1
0
mirror of https://github.com/moodle/moodle.git synced 2025-03-15 05:00:06 +01:00

Merge branch 'master-MDL-74993' of https://github.com/golenkovm/moodle

This commit is contained in:
Ilya Tregubov 2022-09-20 09:06:05 +04:00
commit e08cc53b37
3 changed files with 34 additions and 3 deletions

@ -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';

@ -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);
}

@ -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);
}
}