From ae4a5864ad97e60713caec86227e92760f6ac28a Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Tue, 28 Nov 2023 13:10:58 +0000 Subject: [PATCH] MDL-80060 enrol_manual: correct exception constructor arguments. --- enrol/manual/externallib.php | 8 ++++---- enrol/manual/tests/externallib_test.php | 19 +++++++++++++++---- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/enrol/manual/externallib.php b/enrol/manual/externallib.php index 000ad30ca00..27ab3721beb 100644 --- a/enrol/manual/externallib.php +++ b/enrol/manual/externallib.php @@ -111,9 +111,9 @@ class enrol_manual_external extends external_api { } } if (empty($instance)) { - $errorparams = new stdClass(); - $errorparams->courseid = $enrolment['courseid']; - throw new moodle_exception('wsnoinstance', 'enrol_manual', $errorparams); + $errorparams = new stdClass(); + $errorparams->courseid = $enrolment['courseid']; + throw new moodle_exception('wsnoinstance', 'enrol_manual', '', $errorparams); } // Check that the plugin accept enrolment (it should always the case, it's hard coded in the plugin). @@ -195,7 +195,7 @@ class enrol_manual_external extends external_api { require_capability('enrol/manual:unenrol', $context); $instance = $DB->get_record('enrol', array('courseid' => $enrolment['courseid'], 'enrol' => 'manual')); if (!$instance) { - throw new moodle_exception('wsnoinstance', 'enrol_manual', $enrolment); + throw new moodle_exception('wsnoinstance', 'enrol_manual', '', $enrolment); } $user = $DB->get_record('user', array('id' => $enrolment['userid'])); if (!$user) { diff --git a/enrol/manual/tests/externallib_test.php b/enrol/manual/tests/externallib_test.php index 91b773d5004..5a2054f765c 100644 --- a/enrol/manual/tests/externallib_test.php +++ b/enrol/manual/tests/externallib_test.php @@ -116,6 +116,10 @@ class externallib_test extends externallib_advanced_testcase { $this->fail('Exception expected if course does not have manual instance'); } catch (\moodle_exception $e) { $this->assertSame('wsnoinstance', $e->errorcode); + $this->assertSame( + "Manual enrolment plugin instance doesn't exist or is disabled for the course (id = {$course2->id})", + $e->getMessage() + ); } } @@ -274,14 +278,21 @@ class externallib_test extends externallib_advanced_testcase { } catch (\Exception $ex) { $this->assertTrue($ex instanceof \invalid_parameter_exception); } - $DB->delete_records('enrol', array('id' => $enrolinstance->id)); + + // Call for course without manual instance. + $DB->delete_records('user_enrolments'); + $DB->delete_records('enrol', ['courseid' => $course->id]); try { enrol_manual_external::unenrol_users(array( array('userid' => $student->id + 1, 'courseid' => $course->id), )); - $this->fail('Exception expected: invalid student id'); - } catch (\Exception $ex) { - $this->assertTrue($ex instanceof \moodle_exception); + $this->fail('Exception expected if course does not have manual instance'); + } catch (\moodle_exception $e) { + $this->assertSame('wsnoinstance', $e->errorcode); + $this->assertSame( + "Manual enrolment plugin instance doesn't exist or is disabled for the course (id = {$course->id})", + $e->getMessage() + ); } } }