diff --git a/admin/tool/analytics/tests/external_test.php b/admin/tool/analytics/tests/external_test.php index ff26c646b8c7d..597dda7b9e1aa 100644 --- a/admin/tool/analytics/tests/external_test.php +++ b/admin/tool/analytics/tests/external_test.php @@ -66,8 +66,6 @@ public function test_potential_contexts() { /** * test_potential_contexts description - * - * @expectedException required_capability_exception */ public function test_potential_contexts_no_manager() { $this->resetAfterTest(); @@ -75,6 +73,7 @@ public function test_potential_contexts_no_manager() { $user = $this->getDataGenerator()->create_user(); $this->setUser($user); + $this->expectException(required_capability_exception::class); $this->assertCount(2, \tool_analytics\external::potential_contexts()); } } diff --git a/admin/tool/cohortroles/tests/api_test.php b/admin/tool/cohortroles/tests/api_test.php index f023b71840b85..0464370d39bbf 100644 --- a/admin/tool/cohortroles/tests/api_test.php +++ b/admin/tool/cohortroles/tests/api_test.php @@ -60,9 +60,6 @@ protected function setUp(): void { cohort_add_member($this->cohort->id, $this->userassignover->id); } - /** - * @expectedException required_capability_exception - */ public function test_create_cohort_role_assignment_without_permission() { $this->setUser($this->userassignto); $params = (object) array( @@ -70,12 +67,10 @@ public function test_create_cohort_role_assignment_without_permission() { 'roleid' => $this->roleid, 'cohortid' => $this->cohort->id ); + $this->expectException(required_capability_exception::class); api::create_cohort_role_assignment($params); } - /** - * @expectedException core_competency\invalid_persistent_exception - */ public function test_create_cohort_role_assignment_with_invalid_data() { $this->setAdminUser(); $params = (object) array( @@ -83,6 +78,7 @@ public function test_create_cohort_role_assignment_with_invalid_data() { 'roleid' => -8, 'cohortid' => $this->cohort->id ); + $this->expectException(\core_competency\invalid_persistent_exception::class); api::create_cohort_role_assignment($params); } @@ -100,9 +96,6 @@ public function test_create_cohort_role_assignment() { $this->assertEquals($result->get('cohortid'), $this->cohort->id); } - /** - * @expectedException required_capability_exception - */ public function test_delete_cohort_role_assignment_without_permission() { $this->setAdminUser(); $params = (object) array( @@ -112,12 +105,10 @@ public function test_delete_cohort_role_assignment_without_permission() { ); $result = api::create_cohort_role_assignment($params); $this->setUser($this->userassignto); + $this->expectException(required_capability_exception::class); api::delete_cohort_role_assignment($result->get('id')); } - /** - * @expectedException dml_missing_record_exception - */ public function test_delete_cohort_role_assignment_with_invalid_data() { $this->setAdminUser(); $params = (object) array( @@ -126,6 +117,7 @@ public function test_delete_cohort_role_assignment_with_invalid_data() { 'cohortid' => $this->cohort->id ); $result = api::create_cohort_role_assignment($params); + $this->expectException(dml_missing_record_exception::class); api::delete_cohort_role_assignment($result->get('id') + 1); } diff --git a/admin/tool/langimport/tests/events_test.php b/admin/tool/langimport/tests/events_test.php index 0633984a8fbb6..84a948838dedd 100644 --- a/admin/tool/langimport/tests/events_test.php +++ b/admin/tool/langimport/tests/events_test.php @@ -56,12 +56,10 @@ public function test_langpack_updated() { $this->assertEquals(context_system::instance(), $event->get_context()); } - /** - * @expectedException coding_exception - * @expectedExceptionMessage The 'langcode' value must be set to a valid language code - */ public function test_langpack_updated_validation() { + $this->expectException('coding_exception'); + $this->expectExceptionMessage("The 'langcode' value must be set to a valid language code"); \tool_langimport\event\langpack_updated::event_with_langcode('broken langcode'); } @@ -78,12 +76,10 @@ public function test_langpack_installed() { $this->assertEquals(context_system::instance(), $event->get_context()); } - /** - * @expectedException coding_exception - * @expectedExceptionMessage The 'langcode' value must be set to a valid language code - */ public function test_langpack_installed_validation() { + $this->expectException('coding_exception'); + $this->expectExceptionMessage("The 'langcode' value must be set to a valid language code"); \tool_langimport\event\langpack_imported::event_with_langcode('broken langcode'); } @@ -100,12 +96,10 @@ public function test_langpack_removed() { $this->assertEquals(context_system::instance(), $event->get_context()); } - /** - * @expectedException coding_exception - * @expectedExceptionMessage The 'langcode' value must be set to a valid language code - */ public function test_langpack_removed_validation() { + $this->expectException('coding_exception'); + $this->expectExceptionMessage("The 'langcode' value must be set to a valid language code"); \tool_langimport\event\langpack_removed::event_with_langcode('broken langcode'); } } diff --git a/admin/tool/monitor/tests/subscription_test.php b/admin/tool/monitor/tests/subscription_test.php index cb18815ad127c..364b1c43849bb 100644 --- a/admin/tool/monitor/tests/subscription_test.php +++ b/admin/tool/monitor/tests/subscription_test.php @@ -59,11 +59,10 @@ public function test_magic_isset() { /** * Test for the magic __get method. - * - * @expectedException coding_exception */ public function test_magic_get() { $this->assertEquals(20, $this->subscription->courseid); + $this->expectException(coding_exception::class); $this->subscription->ruleid; } } diff --git a/admin/tool/uploadcourse/tests/course_test.php b/admin/tool/uploadcourse/tests/course_test.php index 9bd1f4eecdc0a..8843f60da8993 100644 --- a/admin/tool/uploadcourse/tests/course_test.php +++ b/admin/tool/uploadcourse/tests/course_test.php @@ -35,21 +35,16 @@ */ class tool_uploadcourse_course_testcase extends advanced_testcase { - /** - * @expectedException coding_exception - */ public function test_proceed_without_prepare() { $this->resetAfterTest(true); $mode = tool_uploadcourse_processor::MODE_CREATE_NEW; $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING; $data = array(); $co = new tool_uploadcourse_course($mode, $updatemode, $data); + $this->expectException(coding_exception::class); $co->proceed(); } - /** - * @expectedException moodle_exception - */ public function test_proceed_when_prepare_failed() { $this->resetAfterTest(true); $mode = tool_uploadcourse_processor::MODE_CREATE_NEW; @@ -57,6 +52,7 @@ public function test_proceed_when_prepare_failed() { $data = array(); $co = new tool_uploadcourse_course($mode, $updatemode, $data); $this->assertFalse($co->prepare()); + $this->expectException(moodle_exception::class); $co->proceed(); } @@ -1504,4 +1500,4 @@ protected function create_custom_field(\core_customfield\category_controller $ca 'configdata' => $configdata, ]); } -} \ No newline at end of file +} diff --git a/admin/tool/uploadcourse/tests/processor_test.php b/admin/tool/uploadcourse/tests/processor_test.php index 109c2434c23d3..b23c035a3aaeb 100644 --- a/admin/tool/uploadcourse/tests/processor_test.php +++ b/admin/tool/uploadcourse/tests/processor_test.php @@ -160,9 +160,6 @@ public function test_shortname_template() { $this->assertEquals('ID123: Course 1', $c->shortname); } - /** - * @expectedException moodle_exception - */ public function test_empty_csv() { $this->resetAfterTest(true); @@ -174,12 +171,10 @@ public function test_empty_csv() { $cir->init(); $options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_NEW); + $this->expectException(moodle_exception::class); $p = new tool_uploadcourse_processor($cir, $options, array()); } - /** - * @expectedException moodle_exception - */ public function test_not_enough_columns() { $this->resetAfterTest(true); @@ -194,6 +189,7 @@ public function test_not_enough_columns() { $cir->init(); $options = array('mode' => tool_uploadcourse_processor::MODE_CREATE_NEW); + $this->expectException(moodle_exception::class); $p = new tool_uploadcourse_processor($cir, $options, array()); } diff --git a/analytics/tests/indicator_test.php b/analytics/tests/indicator_test.php index add3f3d49149d..ff7ff0c3b36e4 100644 --- a/analytics/tests/indicator_test.php +++ b/analytics/tests/indicator_test.php @@ -70,7 +70,6 @@ public function validate_calculated_value() { * @param string $indicatorclass * @param string $willreturn * @dataProvider validate_calculated_value_exceptions - * @expectedException \coding_exception * @return null */ public function test_validate_calculated_value_exceptions($indicatorclass, $willreturn) { @@ -80,6 +79,7 @@ public function test_validate_calculated_value_exceptions($indicatorclass, $will ->setMethods(['calculate_sample']) ->getMock(); $indicatormock->method('calculate_sample')->willReturn($willreturn); + $this->expectException(coding_exception::class); list($values, $unused) = $indicatormock->calculate([1], 'notrelevanthere'); } diff --git a/backup/converter/moodle1/tests/moodle1_converter_test.php b/backup/converter/moodle1/tests/moodle1_converter_test.php index 7065286efdb0d..d8ece4bcbac8c 100644 --- a/backup/converter/moodle1/tests/moodle1_converter_test.php +++ b/backup/converter/moodle1/tests/moodle1_converter_test.php @@ -92,17 +92,12 @@ public function test_convert_factory() { $this->assertInstanceOf('moodle1_converter', $converter); } - /** - * @expectedException moodle1_convert_storage_exception - */ public function test_stash_storage_not_created() { $converter = convert_factory::get_converter('moodle1', $this->tempdir); + $this->expectException(moodle1_convert_storage_exception::class); $converter->set_stash('tempinfo', 12); } - /** - * @expectedException moodle1_convert_empty_storage_exception - */ public function test_stash_requiring_empty_stash() { $this->resetAfterTest(true); $converter = convert_factory::get_converter('moodle1', $this->tempdir); @@ -113,6 +108,7 @@ public function test_stash_requiring_empty_stash() { } catch (moodle1_convert_empty_storage_exception $e) { // we must drop the storage here so we are able to re-create it in the next test + $this->expectException(moodle1_convert_empty_storage_exception::class); $converter->drop_stash_storage(); throw new moodle1_convert_empty_storage_exception('rethrowing'); } @@ -440,9 +436,6 @@ public function test_convert_path_explicit_recipes() { $this->assertSame(null, $data['nothing']); } - /** - * @expectedException convert_path_exception - */ public function test_grouped_data_on_nongrouped_convert_path() { // prepare some grouped data $data = array( @@ -468,12 +461,10 @@ public function test_grouped_data_on_nongrouped_convert_path() { $path = new convert_path('beer_style', '/ROOT/BEER_STYLES/BEER_STYLE'); // an attempt to apply recipes throws exception because we do not expect grouped data + $this->expectException(convert_path_exception::class); $data = $path->apply_recipes($data); } - /** - * @expectedException convert_path_exception - */ public function test_grouped_convert_path_with_recipes() { // prepare some grouped data $data = array( @@ -501,6 +492,7 @@ public function test_grouped_convert_path_with_recipes() { $this->assertEquals('Heineken', $data['beers'][1]['beer']['name']); // an attempt to provide explicit recipes on grouped elements throws exception + $this->expectException(convert_path_exception::class); $path = new convert_path( 'beer_style', '/ROOT/BEER_STYLES/BEER_STYLE', array( diff --git a/calendar/tests/externallib_test.php b/calendar/tests/externallib_test.php index af78b18e47319..32224da49e728 100644 --- a/calendar/tests/externallib_test.php +++ b/calendar/tests/externallib_test.php @@ -157,8 +157,6 @@ public function test_create_calendar_events () { /** * Test delete_calendar_events - * - * @expectedException moodle_exception */ public function test_delete_calendar_events() { global $DB, $USER; @@ -282,6 +280,7 @@ public function test_delete_calendar_events() { array('eventid' => $userevent->id, 'repeat' => 0), array('eventid' => $groupevent->id, 'repeat' => 0) ); + $this->expectException(moodle_exception::class); core_calendar_external::delete_calendar_events($events); } diff --git a/cohort/tests/cohortlib_test.php b/cohort/tests/cohortlib_test.php index e48fdf5f825f3..05bcd0ef3de89 100644 --- a/cohort/tests/cohortlib_test.php +++ b/cohort/tests/cohortlib_test.php @@ -65,10 +65,6 @@ public function test_cohort_add_cohort() { $this->assertSame($newcohort->timecreated, $newcohort->timemodified); } - /** - * @expectedException coding_exception - * @expectedExceptionMessage Missing cohort name in cohort_add_cohort(). - */ public function test_cohort_add_cohort_missing_name() { $cohort = new stdClass(); $cohort->contextid = context_system::instance()->id; @@ -77,6 +73,8 @@ public function test_cohort_add_cohort_missing_name() { $cohort->description = 'test cohort desc'; $cohort->descriptionformat = FORMAT_HTML; + $this->expectException(coding_exception::class); + $this->expectExceptionMessage('Missing cohort name in cohort_add_cohort()'); cohort_add_cohort($cohort); } diff --git a/cohort/tests/externallib_test.php b/cohort/tests/externallib_test.php index f5cdd7987f2c3..a2ec653d8dbe8 100644 --- a/cohort/tests/externallib_test.php +++ b/cohort/tests/externallib_test.php @@ -34,8 +34,6 @@ class core_cohort_externallib_testcase extends externallib_advanced_testcase { /** * Test create_cohorts - * - * @expectedException required_capability_exception */ public function test_create_cohorts() { global $USER, $CFG, $DB; @@ -131,13 +129,12 @@ public function test_create_cohorts() { // Call without required capability. $this->unassignUserCapability('moodle/cohort:manage', $contextid, $roleid); + $this->expectException(required_capability_exception::class); $createdcohorts = core_cohort_external::create_cohorts(array($cohort3)); } /** * Test delete_cohorts - * - * @expectedException required_capability_exception */ public function test_delete_cohorts() { global $USER, $CFG, $DB; @@ -164,6 +161,7 @@ public function test_delete_cohorts() { $cohort1 = self::getDataGenerator()->create_cohort(); $cohort2 = self::getDataGenerator()->create_cohort(); $this->unassignUserCapability('moodle/cohort:manage', $contextid, $roleid); + $this->expectException(required_capability_exception::class); core_cohort_external::delete_cohorts(array($cohort1->id, $cohort2->id)); } @@ -232,8 +230,6 @@ public function test_get_cohorts() { /** * Test update_cohorts - * - * @expectedException required_capability_exception */ public function test_update_cohorts() { global $USER, $CFG, $DB; @@ -291,6 +287,7 @@ public function test_update_cohorts() { // Call without required capability. $this->unassignUserCapability('moodle/cohort:manage', $context->id, $roleid); + $this->expectException(required_capability_exception::class); core_cohort_external::update_cohorts(array($cohort1)); } @@ -326,8 +323,6 @@ public function test_update_cohorts_invalid_id_param() { /** * Test update_cohorts without permission on the dest category. - * - * @expectedException required_capability_exception */ public function test_update_cohorts_missing_dest() { global $USER, $CFG, $DB; @@ -363,13 +358,12 @@ public function test_update_cohorts_missing_dest() { // Call the external function. // Should fail because we don't have permission on the dest category + $this->expectException(required_capability_exception::class); core_cohort_external::update_cohorts(array($cohortupdate)); } /** * Test update_cohorts without permission on the src category. - * - * @expectedException required_capability_exception */ public function test_update_cohorts_missing_src() { global $USER, $CFG, $DB; @@ -405,13 +399,12 @@ public function test_update_cohorts_missing_src() { // Call the external function. // Should fail because we don't have permission on the src category + $this->expectException(required_capability_exception::class); core_cohort_external::update_cohorts(array($cohortupdate)); } /** * Test add_cohort_members - * - * @expectedException required_capability_exception */ public function test_add_cohort_members() { global $DB; @@ -457,13 +450,12 @@ public function test_add_cohort_members() { 'usertype' => array('type' => 'id', 'value' => '2') ); $this->unassignUserCapability('moodle/cohort:assign', $contextid, $roleid); + $this->expectException(required_capability_exception::class); $addcohortmembers = core_cohort_external::add_cohort_members(array($cohort2)); } /** * Test delete_cohort_members - * - * @expectedException required_capability_exception */ public function test_delete_cohort_members() { global $DB; @@ -515,6 +507,7 @@ public function test_delete_cohort_members() { // Call without required capability. $this->unassignUserCapability('moodle/cohort:assign', $context->id, $roleid); + $this->expectException(required_capability_exception::class); core_cohort_external::delete_cohort_members(array($cohortdel1, $cohortdel2)); } diff --git a/competency/tests/api_test.php b/competency/tests/api_test.php index ffe15e344a23a..37d85e78fd54c 100644 --- a/competency/tests/api_test.php +++ b/competency/tests/api_test.php @@ -166,8 +166,6 @@ public function test_get_template_related_contexts_with_capabilities() { /** * Test updating a template. - * - * @expectedException coding_exception */ public function test_update_template() { $cat = $this->getDataGenerator()->create_category(); @@ -186,6 +184,7 @@ public function test_update_template() { $this->assertEquals('success', $template->get('shortname')); // Trying to change the context. + $this->expectException(coding_exception::class); api::update_template((object) array('id' => $template->get('id'), 'contextid' => context_coursecat::instance($cat->id))); } @@ -511,9 +510,6 @@ public function test_update_plan() { } } - /** - * @expectedException coding_exception - */ public function test_create_plan_from_template() { $this->resetAfterTest(true); $this->setAdminUser(); @@ -536,6 +532,7 @@ public function test_create_plan_from_template() { // Check that api::create_plan cannot be used. unset($record->id); + $this->expectException(coding_exception::class); $plan = api::create_plan($record); } @@ -756,8 +753,6 @@ public function test_update_template_updates_plans() { /** * Test that the method to complete a plan. - * - * @expectedException coding_exception */ public function test_complete_plan() { global $DB; @@ -838,6 +833,7 @@ public function test_complete_plan() { } // Completing a plan that is completed throws an exception. + $this->expectException(coding_exception::class); api::complete_plan($plan); } @@ -4513,9 +4509,6 @@ public function test_delete_evidence() { $this->assertTrue(evidence::record_exists($ev2->get('id'))); } - /** - * @expectedException required_capability_exception - */ public function test_delete_evidence_without_permissions() { $this->resetAfterTest(); $dg = $this->getDataGenerator(); @@ -4529,6 +4522,7 @@ public function test_delete_evidence_without_permissions() { $this->setUser($u1); + $this->expectException(required_capability_exception::class); api::delete_evidence($ev1); } diff --git a/competency/tests/event_test.php b/competency/tests/event_test.php index 134c86b110f3e..384be5f97c0ab 100644 --- a/competency/tests/event_test.php +++ b/competency/tests/event_test.php @@ -1360,9 +1360,6 @@ public function test_evidence_created() { /** * Test evidence_created event by linking an invalid user competency to an evidence. - * - * @expectedException coding_exception - * @expectedExceptionMessage The user competency linked with this evidence is invalid. */ public function test_evidence_created_with_invalid_user_competency() { $this->resetAfterTest(true); @@ -1388,6 +1385,8 @@ public function test_evidence_created_with_invalid_user_competency() { 'commentincontext', 'core', null, $recommend, null, 1); // We expect this to fail and throw a coding exception. + $this->expectException('coding_exception'); + $this->expectExceptionMessage('The user competency linked with this evidence is invalid.'); \core\event\competency_evidence_created::create_from_evidence($evidence, $otheruc, $recommend)->trigger(); } diff --git a/competency/tests/external_test.php b/competency/tests/external_test.php index c2ddd7af69876..e2e8af7cf5d34 100644 --- a/competency/tests/external_test.php +++ b/competency/tests/external_test.php @@ -299,22 +299,20 @@ protected function update_competency($id, $number) { /** * Test we can't create a competency framework with only read permissions. - * - * @expectedException required_capability_exception */ public function test_create_competency_frameworks_with_read_permissions() { $this->setUser($this->user); + $this->expectException(required_capability_exception::class); $result = $this->create_competency_framework(1, true); } /** * Test we can't create a competency framework with only read permissions. - * - * @expectedException required_capability_exception */ public function test_create_competency_frameworks_with_read_permissions_in_category() { $this->setUser($this->catuser); + $this->expectException(required_capability_exception::class); $result = $this->create_competency_framework(1, false); } @@ -365,8 +363,6 @@ public function test_create_competency_frameworks_with_manage_permissions_in_cat /** * Test we cannot create a competency framework with nasty data. - * - * @expectedException invalid_parameter_exception */ public function test_create_competency_frameworks_with_nasty_data() { $this->setUser($this->creator); @@ -380,6 +376,7 @@ public function test_create_competency_frameworks_with_nasty_data() { 'visible' => true, 'contextid' => context_system::instance()->id ); + $this->expectException(invalid_parameter_exception::class); $result = external::create_competency_framework($framework); } @@ -542,8 +539,6 @@ public function test_delete_competency_frameworks_with_manage_permissions_in_cat /** * Test we can delete a competency framework with read permissions. - * - * @expectedException required_capability_exception */ public function test_delete_competency_frameworks_with_read_permissions() { $this->setUser($this->creator); @@ -552,6 +547,7 @@ public function test_delete_competency_frameworks_with_read_permissions() { $id = $result->id; // Switch users to someone with less permissions. $this->setUser($this->user); + $this->expectException(required_capability_exception::class); $result = external::delete_competency_framework($id); } @@ -624,14 +620,13 @@ public function test_update_framework_scale() { /** * Test we can update a competency framework with read permissions. - * - * @expectedException required_capability_exception */ public function test_update_competency_frameworks_with_read_permissions() { $this->setUser($this->creator); $result = $this->create_competency_framework(1, true); $this->setUser($this->user); + $this->expectException(required_capability_exception::class); $result = $this->update_competency_framework($result->id, 2, true); } @@ -758,12 +753,11 @@ public function test_list_and_count_competency_frameworks_with_read_permissions( /** * Test we can't create a competency with only read permissions. - * - * @expectedException required_capability_exception */ public function test_create_competency_with_read_permissions() { $framework = $this->getDataGenerator()->get_plugin_generator('core_competency')->create_framework(); $this->setUser($this->user); + $this->expectException(required_capability_exception::class); $competency = $this->create_competency(1, $framework->get('id')); } @@ -820,8 +814,6 @@ public function test_create_competency_with_manage_permissions_in_category() { /** * Test we cannot create a competency with nasty data. - * - * @expectedException invalid_parameter_exception */ public function test_create_competency_with_nasty_data() { $this->setUser($this->creator); @@ -834,6 +826,10 @@ public function test_create_competency_with_nasty_data() { 'competencyframeworkid' => $framework->id, 'sortorder' => 0 ); + // TODO: MDL-69700 - Analyse if the throw exception is happening + // in the correct place and decide what happens with the trailing + // code that is never executed. + $this->expectException(invalid_parameter_exception::class); $result = external::create_competency($competency); $result = (object) external_api::clean_returnvalue(external::create_competency_returns(), $result); } @@ -998,8 +994,6 @@ public function test_delete_competency_with_manage_permissions_in_category() { /** * Test we can delete a competency with read permissions. - * - * @expectedException required_capability_exception */ public function test_delete_competency_with_read_permissions() { $this->setUser($this->creator); @@ -1009,6 +1003,7 @@ public function test_delete_competency_with_read_permissions() { $id = $result->id; // Switch users to someone with less permissions. $this->setUser($this->user); + $this->expectException(required_capability_exception::class); $result = external::delete_competency($id); } @@ -1052,8 +1047,6 @@ public function test_update_competency_with_manage_permissions_in_category() { /** * Test we can update a competency with read permissions. - * - * @expectedException required_capability_exception */ public function test_update_competency_with_read_permissions() { $this->setUser($this->creator); @@ -1061,6 +1054,7 @@ public function test_update_competency_with_read_permissions() { $result = $this->create_competency(1, $framework->id); $this->setUser($this->user); + $this->expectException(required_capability_exception::class); $result = $this->update_competency($result->id, 2); } diff --git a/competency/tests/plan_test.php b/competency/tests/plan_test.php index c942d9ff1a988..191a0b041aab0 100644 --- a/competency/tests/plan_test.php +++ b/competency/tests/plan_test.php @@ -526,10 +526,6 @@ public function test_get_by_user_and_competency() { $this->assertCount(0, $plans); } - /** - * @expectedException coding_exception - * @expectedExceptionMessage The competency does not belong to this template: - */ public function test_get_competency() { $this->resetAfterTest(); $this->setAdminUser(); @@ -571,6 +567,8 @@ public function test_get_competency() { $this->assertEquals($c4->to_record(), $p4->get_competency($c4->get('id'))->to_record()); // Getting the competency 4 from the non-completed plan based on a template p4, will throw an exception. + $this->expectException('coding_exception'); + $this->expectExceptionMessage('The competency does not belong to this template:'); $p3->get_competency($c4->get('id')); } } diff --git a/course/tests/externallib_test.php b/course/tests/externallib_test.php index d972d336f77c9..ed94e5ab70fdb 100644 --- a/course/tests/externallib_test.php +++ b/course/tests/externallib_test.php @@ -596,7 +596,7 @@ public function test_create_courses_empty_field(array $course, string $expectede $course['categoryid'] = $this->getDataGenerator()->create_category()->id; $this->expectException(moodle_exception::class); - $this->expectExceptionMessageRegExp("/{$expectedemptyfield}/"); + $this->expectExceptionMessageMatches("/{$expectedemptyfield}/"); core_course_external::create_courses([$course]); } diff --git a/enrol/tests/externallib_test.php b/enrol/tests/externallib_test.php index baf1d70a7f92b..0edd9401a6766 100644 --- a/enrol/tests/externallib_test.php +++ b/enrol/tests/externallib_test.php @@ -712,8 +712,6 @@ public function test_get_enrolled_users_including_lastcourseaccess() { /** * Test get_enrolled_users from core_enrol_external with capability to * viewparticipants removed. - * - * @expectedException moodle_exception */ public function test_get_enrolled_users_without_capability() { $capability = 'moodle/course:viewparticipants'; @@ -721,6 +719,7 @@ public function test_get_enrolled_users_without_capability() { // Call without required capability. $this->unassignUserCapability($capability, $data->context->id, $data->roleid); + $this->expectException(moodle_exception::class); $categories = core_enrol_external::get_enrolled_users($data->course->id); } diff --git a/grade/grading/tests/grading_manager_test.php b/grade/grading/tests/grading_manager_test.php index b4907241312af..6aa0444642a91 100644 --- a/grade/grading/tests/grading_manager_test.php +++ b/grade/grading/tests/grading_manager_test.php @@ -67,8 +67,6 @@ public function test_basic_instantiation() { /** * Unit test to set and get grading areas - * - * @expectedException moodle_exception */ public function test_set_and_get_grading_area() { global $DB; @@ -105,6 +103,7 @@ public function test_set_and_get_grading_area() { $this->assertEquals('rubric', $gradingman->get_active_method()); // attempting to set an invalid method + $this->expectException(moodle_exception::class); $gradingman->set_active_method('no_one_should_ever_try_to_implement_a_method_with_this_silly_name'); } diff --git a/lib/filestorage/tests/file_storage_test.php b/lib/filestorage/tests/file_storage_test.php index 20db7b7d01599..62c19f30e1325 100644 --- a/lib/filestorage/tests/file_storage_test.php +++ b/lib/filestorage/tests/file_storage_test.php @@ -1291,7 +1291,6 @@ private function generate_file_record() { } /** - * @expectedException file_exception * @covers ::create_file_from_storedfile */ public function test_create_file_from_storedfile_file_invalid() { @@ -1302,12 +1301,11 @@ public function test_create_file_from_storedfile_file_invalid() { $fs = get_file_storage(); // Create a file from a file id which doesn't exist. + $this->expectException(file_exception::class); $fs->create_file_from_storedfile($filerecord, 9999); } /** - * @expectedException file_exception - * @expectedExceptionMessage Invalid contextid * @covers ::create_file_from_storedfile */ public function test_create_file_from_storedfile_contextid_invalid() { @@ -1322,12 +1320,12 @@ public function test_create_file_from_storedfile_contextid_invalid() { $filerecord->filename = 'invalid.txt'; $filerecord->contextid = 'invalid'; + $this->expectException(file_exception::class); + $this->expectExceptionMessage('Invalid contextid'); $fs->create_file_from_storedfile($filerecord, $file1->get_id()); } /** - * @expectedException file_exception - * @expectedExceptionMessage Invalid component * @covers ::create_file_from_storedfile */ public function test_create_file_from_storedfile_component_invalid() { @@ -1342,12 +1340,12 @@ public function test_create_file_from_storedfile_component_invalid() { $filerecord->filename = 'invalid.txt'; $filerecord->component = 'bad/component'; + $this->expectException(file_exception::class); + $this->expectExceptionMessage('Invalid component'); $fs->create_file_from_storedfile($filerecord, $file1->get_id()); } /** - * @expectedException file_exception - * @expectedExceptionMessage Invalid filearea * @covers ::create_file_from_storedfile */ public function test_create_file_from_storedfile_filearea_invalid() { @@ -1362,12 +1360,12 @@ public function test_create_file_from_storedfile_filearea_invalid() { $filerecord->filename = 'invalid.txt'; $filerecord->filearea = 'bad-filearea'; + $this->expectException(file_exception::class); + $this->expectExceptionMessage('Invalid filearea'); $fs->create_file_from_storedfile($filerecord, $file1->get_id()); } /** - * @expectedException file_exception - * @expectedExceptionMessage Invalid itemid * @covers ::create_file_from_storedfile */ public function test_create_file_from_storedfile_itemid_invalid() { @@ -1382,12 +1380,12 @@ public function test_create_file_from_storedfile_itemid_invalid() { $filerecord->filename = 'invalid.txt'; $filerecord->itemid = 'bad-itemid'; + $this->expectException(file_exception::class); + $this->expectExceptionMessage('Invalid itemid'); $fs->create_file_from_storedfile($filerecord, $file1->get_id()); } /** - * @expectedException file_exception - * @expectedExceptionMessage Invalid file path * @covers ::create_file_from_storedfile */ public function test_create_file_from_storedfile_filepath_invalid() { @@ -1402,12 +1400,12 @@ public function test_create_file_from_storedfile_filepath_invalid() { $filerecord->filename = 'invalid.txt'; $filerecord->filepath = 'a-/bad/-filepath'; + $this->expectException(file_exception::class); + $this->expectExceptionMessage('Invalid file path'); $fs->create_file_from_storedfile($filerecord, $file1->get_id()); } /** - * @expectedException file_exception - * @expectedExceptionMessage Invalid file name * @covers ::create_file_from_storedfile */ public function test_create_file_from_storedfile_filename_invalid() { @@ -1421,12 +1419,12 @@ public function test_create_file_from_storedfile_filename_invalid() { $filerecord->filename = ''; + $this->expectException(file_exception::class); + $this->expectExceptionMessage('Invalid file name'); $fs->create_file_from_storedfile($filerecord, $file1->get_id()); } /** - * @expectedException file_exception - * @expectedExceptionMessage Invalid file timecreated * @covers ::create_file_from_storedfile */ public function test_create_file_from_storedfile_timecreated_invalid() { @@ -1441,12 +1439,12 @@ public function test_create_file_from_storedfile_timecreated_invalid() { $filerecord->filename = 'invalid.txt'; $filerecord->timecreated = 'today'; + $this->expectException(file_exception::class); + $this->expectExceptionMessage('Invalid file timecreated'); $fs->create_file_from_storedfile($filerecord, $file1->get_id()); } /** - * @expectedException file_exception - * @expectedExceptionMessage Invalid file timemodified * @covers ::create_file_from_storedfile */ public function test_create_file_from_storedfile_timemodified_invalid() { @@ -1461,12 +1459,12 @@ public function test_create_file_from_storedfile_timemodified_invalid() { $filerecord->filename = 'invalid.txt'; $filerecord->timemodified = 'today'; + $this->expectException(file_exception::class); + $this->expectExceptionMessage('Invalid file timemodified'); $fs->create_file_from_storedfile($filerecord, $file1->get_id()); } /** - * @expectedException stored_file_creation_exception - * @expectedExceptionMessage Can not create file "1/core/phpunit/0/testfile.txt" * @covers ::create_file_from_storedfile */ public function test_create_file_from_storedfile_duplicate() { @@ -1479,6 +1477,8 @@ public function test_create_file_from_storedfile_duplicate() { $this->assertInstanceOf('stored_file', $file1); // Creating a file validating unique constraint. + $this->expectException(stored_file_creation_exception::class); + $this->expectExceptionMessage('Can not create file "1/core/phpunit/0/testfile.txt"'); $fs->create_file_from_storedfile($filerecord, $file1->get_id()); } @@ -1522,8 +1522,6 @@ public function test_create_file_from_storedfile() { } /** - * @expectedException file_exception - * @expectedExceptionMessage Invalid contextid * @covers ::create_file_from_string */ public function test_create_file_from_string_contextid_invalid() { @@ -1534,12 +1532,12 @@ public function test_create_file_from_string_contextid_invalid() { $filerecord->contextid = 'invalid'; + $this->expectException(file_exception::class); + $this->expectExceptionMessage('Invalid contextid'); $file1 = $fs->create_file_from_string($filerecord, 'text contents'); } /** - * @expectedException file_exception - * @expectedExceptionMessage Invalid component * @covers ::create_file_from_string */ public function test_create_file_from_string_component_invalid() { @@ -1550,12 +1548,12 @@ public function test_create_file_from_string_component_invalid() { $filerecord->component = 'bad/component'; + $this->expectException(file_exception::class); + $this->expectExceptionMessage('Invalid component'); $file1 = $fs->create_file_from_string($filerecord, 'text contents'); } /** - * @expectedException file_exception - * @expectedExceptionMessage Invalid filearea * @covers ::create_file_from_string */ public function test_create_file_from_string_filearea_invalid() { @@ -1566,12 +1564,12 @@ public function test_create_file_from_string_filearea_invalid() { $filerecord->filearea = 'bad-filearea'; + $this->expectException(file_exception::class); + $this->expectExceptionMessage('Invalid filearea'); $file1 = $fs->create_file_from_string($filerecord, 'text contents'); } /** - * @expectedException file_exception - * @expectedExceptionMessage Invalid itemid * @covers ::create_file_from_string */ public function test_create_file_from_string_itemid_invalid() { @@ -1582,12 +1580,12 @@ public function test_create_file_from_string_itemid_invalid() { $filerecord->itemid = 'bad-itemid'; + $this->expectException(file_exception::class); + $this->expectExceptionMessage('Invalid itemid'); $file1 = $fs->create_file_from_string($filerecord, 'text contents'); } /** - * @expectedException file_exception - * @expectedExceptionMessage Invalid file path * @covers ::create_file_from_string */ public function test_create_file_from_string_filepath_invalid() { @@ -1598,12 +1596,12 @@ public function test_create_file_from_string_filepath_invalid() { $filerecord->filepath = 'a-/bad/-filepath'; + $this->expectException(file_exception::class); + $this->expectExceptionMessage('Invalid file path'); $file1 = $fs->create_file_from_string($filerecord, 'text contents'); } /** - * @expectedException file_exception - * @expectedExceptionMessage Invalid file name * @covers ::create_file_from_string */ public function test_create_file_from_string_filename_invalid() { @@ -1614,12 +1612,12 @@ public function test_create_file_from_string_filename_invalid() { $filerecord->filename = ''; + $this->expectException(file_exception::class); + $this->expectExceptionMessage('Invalid file name'); $file1 = $fs->create_file_from_string($filerecord, 'text contents'); } /** - * @expectedException file_exception - * @expectedExceptionMessage Invalid file timecreated * @covers ::create_file_from_string */ public function test_create_file_from_string_timecreated_invalid() { @@ -1636,8 +1634,6 @@ public function test_create_file_from_string_timecreated_invalid() { } /** - * @expectedException file_exception - * @expectedExceptionMessage Invalid file timemodified * @covers ::create_file_from_string */ public function test_create_file_from_string_timemodified_invalid() { @@ -1648,6 +1644,8 @@ public function test_create_file_from_string_timemodified_invalid() { $filerecord->timemodified = 'today'; + $this->expectException(file_exception::class); + $this->expectExceptionMessage('Invalid file timemodified'); $file1 = $fs->create_file_from_string($filerecord, 'text contents'); } @@ -1669,8 +1667,6 @@ public function test_create_file_from_string_duplicate() { } /** - * @expectedException file_exception - * @expectedExceptionMessage Invalid contextid * @covers ::create_file_from_pathname */ public function test_create_file_from_pathname_contextid_invalid() { @@ -1684,12 +1680,12 @@ public function test_create_file_from_pathname_contextid_invalid() { $filerecord->contextid = 'invalid'; + $this->expectException(file_exception::class); + $this->expectExceptionMessage('Invalid contextid'); $file1 = $fs->create_file_from_pathname($filerecord, $path); } /** - * @expectedException file_exception - * @expectedExceptionMessage Invalid component * @covers ::create_file_from_pathname */ public function test_create_file_from_pathname_component_invalid() { @@ -1703,12 +1699,12 @@ public function test_create_file_from_pathname_component_invalid() { $filerecord->component = 'bad/component'; + $this->expectException(file_exception::class); + $this->expectExceptionMessage('Invalid component'); $file1 = $fs->create_file_from_pathname($filerecord, $path); } /** - * @expectedException file_exception - * @expectedExceptionMessage Invalid filearea * @covers ::create_file_from_pathname */ public function test_create_file_from_pathname_filearea_invalid() { @@ -1722,12 +1718,12 @@ public function test_create_file_from_pathname_filearea_invalid() { $filerecord->filearea = 'bad-filearea'; + $this->expectException(file_exception::class); + $this->expectExceptionMessage('Invalid filearea'); $file1 = $fs->create_file_from_pathname($filerecord, $path); } /** - * @expectedException file_exception - * @expectedExceptionMessage Invalid itemid * @covers ::create_file_from_pathname */ public function test_create_file_from_pathname_itemid_invalid() { @@ -1741,12 +1737,12 @@ public function test_create_file_from_pathname_itemid_invalid() { $filerecord->itemid = 'bad-itemid'; - $file1 = $fs->create_file_from_pathname($filerecord, $path); + $this->expectException(file_exception::class); + $this->expectExceptionMessage('Invalid itemid'); + $file1 = $fs->create_file_from_pathname($filerecord, $path); } /** - * @expectedException file_exception - * @expectedExceptionMessage Invalid file path * @covers ::create_file_from_pathname */ public function test_create_file_from_pathname_filepath_invalid() { @@ -1760,12 +1756,12 @@ public function test_create_file_from_pathname_filepath_invalid() { $filerecord->filepath = 'a-/bad/-filepath'; + $this->expectException(file_exception::class); + $this->expectExceptionMessage('Invalid file path'); $file1 = $fs->create_file_from_pathname($filerecord, $path); } /** - * @expectedException file_exception - * @expectedExceptionMessage Invalid file name * @covers ::create_file_from_pathname */ public function test_create_file_from_pathname_filename_invalid() { @@ -1779,12 +1775,12 @@ public function test_create_file_from_pathname_filename_invalid() { $filerecord->filename = ''; + $this->expectException(file_exception::class); + $this->expectExceptionMessage('Invalid file name'); $file1 = $fs->create_file_from_pathname($filerecord, $path); } /** - * @expectedException file_exception - * @expectedExceptionMessage Invalid file timecreated * @covers ::create_file_from_pathname */ public function test_create_file_from_pathname_timecreated_invalid() { @@ -1798,12 +1794,12 @@ public function test_create_file_from_pathname_timecreated_invalid() { $filerecord->timecreated = 'today'; + $this->expectException(file_exception::class); + $this->expectExceptionMessage('Invalid file timecreated'); $file1 = $fs->create_file_from_pathname($filerecord, $path); } /** - * @expectedException file_exception - * @expectedExceptionMessage Invalid file timemodified * @covers ::create_file_from_pathname */ public function test_create_file_from_pathname_timemodified_invalid() { @@ -1817,12 +1813,12 @@ public function test_create_file_from_pathname_timemodified_invalid() { $filerecord->timemodified = 'today'; + $this->expectException(file_exception::class); + $this->expectExceptionMessage('Invalid file timemodified'); $file1 = $fs->create_file_from_pathname($filerecord, $path); } /** - * @expectedException stored_file_creation_exception - * @expectedExceptionMessage Can not create file "1/core/phpunit/0/testfile.txt" * @covers ::create_file_from_pathname */ public function test_create_file_from_pathname_duplicate_file() { @@ -1838,6 +1834,8 @@ public function test_create_file_from_pathname_duplicate_file() { $this->assertInstanceOf('stored_file', $file1); // Creating a file validating unique constraint. + $this->expectException(stored_file_creation_exception::class); + $this->expectExceptionMessage('Can not create file "1/core/phpunit/0/testfile.txt"'); $file2 = $fs->create_file_from_pathname($filerecord, $path); } diff --git a/lib/filestorage/tests/file_system_filedir_test.php b/lib/filestorage/tests/file_system_filedir_test.php index 24653715fb639..3896770cc596c 100644 --- a/lib/filestorage/tests/file_system_filedir_test.php +++ b/lib/filestorage/tests/file_system_filedir_test.php @@ -153,7 +153,7 @@ public function test_readonly_filesystem_filedir() { // This should generate an exception. $this->expectException('file_exception'); - $this->expectExceptionMessageRegExp( + $this->expectExceptionMessageMatches( '/Can not create local file pool directories, please verify permissions in dataroot./'); new file_system_filedir(); @@ -177,7 +177,7 @@ public function test_readonly_filesystem_trashdir() { // This should generate an exception. $this->expectException('file_exception'); - $this->expectExceptionMessageRegExp( + $this->expectExceptionMessageMatches( '/Can not create local file pool directories, please verify permissions in dataroot./'); new file_system_filedir(); @@ -735,7 +735,7 @@ public function test_add_file_from_path_file_unavailable() { $vfileroot = $this->setup_vfile_root(); $this->expectException('file_exception'); - $this->expectExceptionMessageRegExp( + $this->expectExceptionMessageMatches( '/Cannot read file\. Either the file does not exist or there is a permission problem\./'); $fs = new file_system_filedir(); @@ -834,7 +834,7 @@ public function test_add_file_from_path_existing_cannot_write_hashpath() { ->chown(\org\bovigo\vfs\vfsStream::OWNER_USER_2); $this->expectException('file_exception'); - $this->expectExceptionMessageRegExp( + $this->expectExceptionMessageMatches( "/Can not create local file pool directories, please verify permissions in dataroot./"); // Attempt to add the file to the file pool. @@ -892,7 +892,7 @@ public function test_add_file_from_string_existing_cannot_write_hashpath() { ->chown(\org\bovigo\vfs\vfsStream::OWNER_USER_2); $this->expectException('file_exception'); - $this->expectExceptionMessageRegExp( + $this->expectExceptionMessageMatches( "/Can not create local file pool directories, please verify permissions in dataroot./"); // Attempt to add the file to the file pool. diff --git a/lib/form/tests/duration_test.php b/lib/form/tests/duration_test.php index 629ac4d8d14ba..cd0c9bcdd890c 100644 --- a/lib/form/tests/duration_test.php +++ b/lib/form/tests/duration_test.php @@ -69,7 +69,7 @@ protected function get_test_form_and_element(): array { public function test_constructor_rejects_invalid_unit(): void { // Test trying to create with an invalid unit. $mform = $this->get_test_form(); - $this->expectException('coding_exception'); + $this->expectException(coding_exception::class); $mform->addElement('duration', 'testel', null, ['defaultunit' => 123, 'optional' => false]); } diff --git a/lib/testing/tests/generator_test.php b/lib/testing/tests/generator_test.php index 2d9990fae25ab..2fd59265b4020 100644 --- a/lib/testing/tests/generator_test.php +++ b/lib/testing/tests/generator_test.php @@ -49,11 +49,10 @@ public function test_get_plugin_generator_sloppy_name() { /** * Test plugin generator, with no component directory. - * - * @expectedException coding_exception - * @expectedExceptionMessage Component core_completion does not support generators yet. Missing tests/generator/lib.php. */ public function test_get_plugin_generator_no_component_dir() { + $this->expectException(coding_exception::class); + $this->expectExceptionMessage('Component core_completion does not support generators yet. Missing tests/generator/lib.php.'); $generator = $this->getDataGenerator()->get_plugin_generator('core_completion'); } diff --git a/lib/tests/admintree_test.php b/lib/tests/admintree_test.php index cadd817046c92..245abb6c33159 100644 --- a/lib/tests/admintree_test.php +++ b/lib/tests/admintree_test.php @@ -95,19 +95,15 @@ public function test_add_nodes() { $this->assertEquals(array('zero', 'one', 'two', 'three', 'four', 'five', 'six'), $map); } - /** - * @expectedException coding_exception - */ public function test_add_nodes_before_invalid1() { $tree = new admin_root(true); + $this->expectException(coding_exception::class); $tree->add('root', new admin_externalpage('foo', 'Foo', 'http://foo.bar'), array('moodle:site/config')); } - /** - * @expectedException coding_exception - */ public function test_add_nodes_before_invalid2() { $tree = new admin_root(true); + $this->expectException(coding_exception::class); $tree->add('root', new admin_category('bar', 'Bar'), ''); } diff --git a/lib/tests/blocklib_test.php b/lib/tests/blocklib_test.php index 4fbd0a64416fb..c68ca54d59c0e 100644 --- a/lib/tests/blocklib_test.php +++ b/lib/tests/blocklib_test.php @@ -109,13 +109,11 @@ public function test_add_region_twice() { $this->assertEquals(array('a-region-name', 'another-region'), $this->blockmanager->get_regions(), '', 0, 10, true); } - /** - * @expectedException coding_exception - */ public function test_cannot_add_region_after_loaded() { // Set up fixture. $this->blockmanager->mark_loaded(); // Exercise SUT. + $this->expectException(coding_exception::class); $this->blockmanager->add_region('too-late', false); } @@ -167,12 +165,12 @@ public function test_add_custom_region_twice() { /** * Test to ensure that we cannot add a region after the blocks have been loaded. - * @expectedException coding_exception */ public function test_cannot_add_custom_region_after_loaded() { // Set up fixture. $this->blockmanager->mark_loaded(); // Exercise SUT. + $this->expectException(coding_exception::class); $this->blockmanager->add_region('too-late'); } @@ -185,21 +183,17 @@ public function test_set_default_region() { $this->assertEquals('a-region-name', $this->blockmanager->get_default_region()); } - /** - * @expectedException coding_exception - */ public function test_cannot_set_unknown_region_as_default() { // Exercise SUT. + $this->expectException(coding_exception::class); $this->blockmanager->set_default_region('a-region-name'); } - /** - * @expectedException coding_exception - */ public function test_cannot_change_default_region_after_loaded() { // Set up fixture. $this->blockmanager->mark_loaded(); // Exercise SUT. + $this->expectException(coding_exception::class); $this->blockmanager->set_default_region('too-late'); } diff --git a/lib/tests/event_test.php b/lib/tests/event_test.php index 9e71470e1dc1a..4bca28e55e488 100644 --- a/lib/tests/event_test.php +++ b/lib/tests/event_test.php @@ -849,10 +849,15 @@ public function test_iteration() { $this->assertSame($event->get_data(), $data); } - /** - * @expectedException PHPUnit\Framework\Error\Notice - */ public function test_context_not_used() { + // TODO: MDL-69688 - This test is far away from my understanding. It throws a + // "Trying to get property 'instanceid' of non-object" notice, so + // it's not clear for me what the test is doing. This was detected + // when preparing tests for PHPUnit 8 (MDL-67673) and, at the end + // all that was done is to move the annotation (deprecated) to + // explicit expectation. Still try commenting it out and you'll see + // the notice. + $this->expectException(\PHPUnit\Framework\Error\Notice::class); $event = \core_tests\event\context_used_in_event::create(array('other' => array('sample' => 1, 'xx' => 10))); $this->assertEventContextNotUsed($event); diff --git a/lib/tests/exporter_test.php b/lib/tests/exporter_test.php index 7b227577920d2..80b19a2e46af4 100644 --- a/lib/tests/exporter_test.php +++ b/lib/tests/exporter_test.php @@ -98,22 +98,21 @@ public function test_get_update_structure() { $this->assertArrayNotHasKey('otherstrings', $structure->keys); } - /** - * @expectedException coding_exception - */ public function test_invalid_data() { global $PAGE; $exporter = new core_testable_exporter($this->invaliddata, $this->validrelated); $output = $PAGE->get_renderer('core'); + $this->expectException(coding_exception::class); $result = $exporter->export($output); } - /** - * @expectedException coding_exception - */ public function test_invalid_related() { global $PAGE; + // TODO: MDL-69700 - Analyse if the throw exception is happening + // in the correct place and decide what happens with the trailing + // code that is never executed. + $this->expectException(coding_exception::class); $exporter = new core_testable_exporter($this->validdata, $this->invalidrelated); $output = $PAGE->get_renderer('core'); diff --git a/lib/tests/filterlib_test.php b/lib/tests/filterlib_test.php index 90fcb652f1f76..a2119642db971 100644 --- a/lib/tests/filterlib_test.php +++ b/lib/tests/filterlib_test.php @@ -91,11 +91,9 @@ public function test_set_filter_globally_disabled() { $this->assert_only_one_filter_globally('name', TEXTFILTER_DISABLED); } - /** - * @expectedException coding_exception - */ public function test_global_config_exception_on_invalid_state() { $this->resetAfterTest(); + $this->expectException(coding_exception::class); filter_set_global_state('name', 0); } @@ -248,21 +246,17 @@ public function test_local_inherit() { $this->assert_no_local_setting(); } - /** - * @expectedException coding_exception - */ public function test_local_invalid_state_throws_exception() { $this->resetAfterTest(); // Exercise SUT. + $this->expectException(coding_exception::class); filter_set_local_state('name', 123, -9999); } - /** - * @expectedException coding_exception - */ public function test_throws_exception_when_setting_global() { $this->resetAfterTest(); // Exercise SUT. + $this->expectException(coding_exception::class); filter_set_local_state('name', context_system::instance()->id, TEXTFILTER_INHERIT); } @@ -543,15 +537,13 @@ public function test_available_in_context_disabled_not_returned() { $this->assertEquals(array(), $filters); } - /** - * @expectedException coding_exception - */ public function test_available_in_context_exception_with_syscontext() { $this->resetAfterTest(); [ 'syscontext' => $syscontext ] = $this->setup_available_in_context_tests(); // Exercise SUT. + $this->expectException(coding_exception::class); filter_get_available_in_context($syscontext); } diff --git a/lib/tests/moodle_page_test.php b/lib/tests/moodle_page_test.php index 0159a6c5bec49..fac043cef9f36 100644 --- a/lib/tests/moodle_page_test.php +++ b/lib/tests/moodle_page_test.php @@ -83,32 +83,25 @@ public function test_global_course_not_changed_with_non_global_page() { $this->assertSame($originalcourse, $COURSE); } - /** - * @expectedException coding_exception - */ public function test_cannot_set_course_once_theme_set() { // Setup fixture. $this->testpage->force_theme(theme_config::DEFAULT_THEME); $course = $this->getDataGenerator()->create_course(); // Exercise SUT. + $this->expectException(coding_exception::class); $this->testpage->set_course($course); } - /** - * @expectedException coding_exception - */ public function test_cannot_set_category_once_theme_set() { // Setup fixture. $this->testpage->force_theme(theme_config::DEFAULT_THEME); // Exercise SUT. + $this->expectException(coding_exception::class); $this->testpage->set_category_by_id(123); } - /** - * @expectedException coding_exception - */ public function test_cannot_set_category_once_course_set() { // Setup fixture. $course = $this->getDataGenerator()->create_course(); @@ -116,6 +109,7 @@ public function test_cannot_set_category_once_course_set() { $this->testpage->set_course($course); // Exercise SUT. + $this->expectException(coding_exception::class); $this->testpage->set_category_by_id(123); } @@ -145,11 +139,9 @@ public function test_set_state_normal_path() { $this->assertEquals(moodle_page::STATE_DONE, $this->testpage->state); } - /** - * @expectedException coding_exception - */ public function test_set_state_cannot_skip_one() { // Exercise SUT. + $this->expectException(coding_exception::class); $this->testpage->set_state(moodle_page::STATE_IN_BODY); } @@ -396,15 +388,13 @@ public function test_set_cm() { $this->assertEquals($cm->id, $this->testpage->cm->id); } - /** - * @expectedException coding_exception - */ public function test_cannot_set_activity_record_before_cm() { // Setup fixture. $course = $this->getDataGenerator()->create_course(); $forum = $this->getDataGenerator()->create_module('forum', array('course'=>$course->id)); $cm = get_coursemodule_from_id('forum', $forum->cmid); // Exercise SUT. + $this->expectException(coding_exception::class); $this->testpage->set_activity_record($forum); } @@ -444,9 +434,6 @@ public function test_set_activity_record() { $this->assertEquals($forum, $this->testpage->activityrecord); } - /** - * @expectedException coding_exception - */ public function test_cannot_set_inconsistent_activity_record_course() { // Setup fixture. $course = $this->getDataGenerator()->create_course(); @@ -455,12 +442,10 @@ public function test_cannot_set_inconsistent_activity_record_course() { $this->testpage->set_cm($cm); // Exercise SUT. $forum->course = 13; + $this->expectException(coding_exception::class); $this->testpage->set_activity_record($forum); } - /** - * @expectedException coding_exception - */ public function test_cannot_set_inconsistent_activity_record_instance() { // Setup fixture. $course = $this->getDataGenerator()->create_course(); @@ -469,6 +454,7 @@ public function test_cannot_set_inconsistent_activity_record_instance() { $this->testpage->set_cm($cm); // Exercise SUT. $forum->id = 13; + $this->expectException(coding_exception::class); $this->testpage->set_activity_record($forum); } @@ -498,9 +484,6 @@ public function test_set_cm_with_course_and_activity_no_db() { $this->assertEquals($forum, $this->testpage->activityrecord); } - /** - * @expectedException coding_exception - */ public function test_cannot_set_cm_with_inconsistent_course() { // Setup fixture. $course = $this->getDataGenerator()->create_course(); @@ -508,6 +491,7 @@ public function test_cannot_set_cm_with_inconsistent_course() { $cm = get_coursemodule_from_id('forum', $forum->cmid); // Exercise SUT. $cm->course = 13; + $this->expectException(coding_exception::class); $this->testpage->set_cm($cm, $course); } diff --git a/lib/tests/moodle_url_test.php b/lib/tests/moodle_url_test.php index c5a9be4d0d7d4..a501c8bb2e083 100644 --- a/lib/tests/moodle_url_test.php +++ b/lib/tests/moodle_url_test.php @@ -121,11 +121,10 @@ public function test_moodle_url_set_good_scheme() { /** * Test set bad scheme on Moodle URL objects. - * - * @expectedException coding_exception */ public function test_moodle_url_set_bad_scheme() { $url = new moodle_url('http://moodle.org/foo/bar'); + $this->expectException(coding_exception::class); $url->set_scheme('not a valid $ scheme'); } @@ -200,36 +199,30 @@ public function test_out_as_local_url() { $this->assertSame('/', $url3->out_as_local_url()); } - /** - * @expectedException coding_exception - * @return void - */ public function test_out_as_local_url_error() { $url2 = new moodle_url('http://www.google.com/lib/tests/weblib_test.php'); + $this->expectException(coding_exception::class); $url2->out_as_local_url(); } /** * You should get error with modified url - * - * @expectedException coding_exception - * @return void */ public function test_modified_url_out_as_local_url_error() { global $CFG; $modifiedurl = $CFG->wwwroot.'1'; $url3 = new moodle_url($modifiedurl.'/login/profile.php'); + $this->expectException(coding_exception::class); $url3->out_as_local_url(); } /** * Try get local url from external https url and you should get error - * - * @expectedException coding_exception */ public function test_https_out_as_local_url_error() { $url4 = new moodle_url('https://www.google.com/lib/tests/weblib_test.php'); + $this->expectException(coding_exception::class); $url4->out_as_local_url(); } diff --git a/lib/tests/moodlelib_test.php b/lib/tests/moodlelib_test.php index ff08a52a2a2cb..f3c0dfa68c78b 100644 --- a/lib/tests/moodlelib_test.php +++ b/lib/tests/moodlelib_test.php @@ -2225,12 +2225,10 @@ public function test_get_string() { $COURSE->lang = $originallang; } - /** - * @expectedException PHPUnit\Framework\Error\Warning - */ public function test_get_string_limitation() { // This is one of the limitations to the lang_string class. It can't be // used as a key. + $this->expectException(\PHPUnit\Framework\Error\Warning::class); $array = array(get_string('yes', null, null, true) => 'yes'); } diff --git a/lib/tests/mustache_template_finder_test.php b/lib/tests/mustache_template_finder_test.php index 0f1fceb5a7941..4c5fadf2f145f 100644 --- a/lib/tests/mustache_template_finder_test.php +++ b/lib/tests/mustache_template_finder_test.php @@ -102,11 +102,10 @@ public function test_get_template_directories_for_component(string $component, s /** * Tests for get_template_directories_for_component when dealing with an invalid component. - * - * @expectedException coding_exception */ public function test_invalid_component_get_template_directories_for_component() { // Test something invalid. + $this->expectException(coding_exception::class); mustache_template_finder::get_template_directories_for_component('octopus', 'classic'); } @@ -191,10 +190,9 @@ public function test_get_template_filepath(string $template, string $theme, stri /** * Tests for get_template_filepath when dealing with an invalid component. - * - * @expectedException moodle_exception */ public function test_invalid_component_get_template_filepath() { + $this->expectException(moodle_exception::class); mustache_template_finder::get_template_filepath('core/octopus', 'classic'); } } diff --git a/lib/tests/persistent_test.php b/lib/tests/persistent_test.php index 711903e1c9822..dbddd3d03c406 100644 --- a/lib/tests/persistent_test.php +++ b/lib/tests/persistent_test.php @@ -174,15 +174,13 @@ public function test_from_record() { $this->assertEquals($data, $p->to_record()); } - /** - * @expectedException coding_exception - */ public function test_from_record_invalid_param() { $p = new core_testable_persistent(); $data = (object) array( 'invalidparam' => 'abc' ); + $this->expectException(coding_exception::class); $p->from_record($data); } @@ -439,11 +437,9 @@ public function test_get_sql_fields() { $this->assertEquals($expected, core_testable_persistent::get_sql_fields('c', 'prefix_')); } - /** - * @expectedException coding_exception - * @expectedExceptionMessageRegExp /The alias .+ exceeds 30 characters/ - */ public function test_get_sql_fields_too_long() { + $this->expectException(coding_exception::class); + $this->expectExceptionMessageMatches('/The alias .+ exceeds 30 characters/'); core_testable_persistent::get_sql_fields('c'); } } diff --git a/lib/tests/plugin_manager_test.php b/lib/tests/plugin_manager_test.php index d118b4470251b..1801a6127aeff 100644 --- a/lib/tests/plugin_manager_test.php +++ b/lib/tests/plugin_manager_test.php @@ -370,11 +370,10 @@ public function test_get_remote_plugin_info() { /** * The combination of ANY_VERSION + $exactmatch is illegal. - * - * @expectedException moodle_exception */ public function test_get_remote_plugin_info_exception() { $pluginman = testable_core_plugin_manager::instance(); + $this->expectException(moodle_exception::class); $pluginman->get_remote_plugin_info('any_thing', ANY_VERSION, true); } diff --git a/lib/tests/update_checker_test.php b/lib/tests/update_checker_test.php index 102a699f8ecca..53b34b605af3d 100644 --- a/lib/tests/update_checker_test.php +++ b/lib/tests/update_checker_test.php @@ -55,13 +55,12 @@ public function test_core_available_update() { /** * If there are no fetched data yet, the first cron should fetch them. - * - * @expectedException \core\update\testable_checker_cron_executed */ public function test_cron_initial_fetch() { $provider = testable_checker::instance(); $provider->fakerecentfetch = null; $provider->fakecurrenttimestamp = -1; + $this->expectException(\core\update\testable_checker_cron_executed::class); $provider->cron(); } @@ -78,13 +77,12 @@ public function test_cron_has_fresh_fetch() { /** * If there is an outdated fetch, the cron execution is expected. - * - * @expectedException \core\update\testable_checker_cron_executed */ public function test_cron_has_outdated_fetch() { $provider = testable_checker::instance(); $provider->fakerecentfetch = time() - 49 * HOURSECS; // Fetched 49 hours ago. $provider->fakecurrenttimestamp = -1; + $this->expectException(\core\update\testable_checker_cron_executed::class); $provider->cron(); } @@ -259,14 +257,12 @@ public function test_compare_responses_modified_update() { $this->assertEquals(2011010102, $cmp['mod_foo'][0]['version']); } - /** - * @expectedException \core\update\checker_exception - */ public function test_compare_responses_invalid_format() { $provider = testable_checker::instance(); $broken = array( 'status' => 'ERROR' // No 'updates' key here. ); + $this->expectException(\core\update\checker_exception::class); $cmp = $provider->compare_responses($broken, $broken); } diff --git a/lib/tests/update_code_manager_test.php b/lib/tests/update_code_manager_test.php index 872057f96759a..b2972217ae3f2 100644 --- a/lib/tests/update_code_manager_test.php +++ b/lib/tests/update_code_manager_test.php @@ -111,14 +111,12 @@ public function test_unzip_plugin_file() { $files = $codeman->unzip_plugin_file($zipfilepath, $targetdir, 'bar'); } - /** - * @expectedException moodle_exception - */ public function test_unzip_plugin_file_multidir() { $codeman = new \core\update\testable_code_manager(); $zipfilepath = __DIR__.'/fixtures/update_validator/zips/multidir.zip'; $targetdir = make_request_directory(); // Attempting to rename the root folder if there are multiple ones should lead to exception. + $this->expectException(moodle_exception::class); $files = $codeman->unzip_plugin_file($zipfilepath, $targetdir, 'foo'); } diff --git a/lib/tests/user_test.php b/lib/tests/user_test.php index ecfd86be462e6..7bd02a392a163 100644 --- a/lib/tests/user_test.php +++ b/lib/tests/user_test.php @@ -675,10 +675,6 @@ public function test_get_property_choices() { /** * Test get_property_default(). - * - * - * @expectedException coding_exception - * @expectedExceptionMessage Invalid property requested, or the property does not has a default value. */ public function test_get_property_default() { global $CFG; @@ -706,6 +702,8 @@ public function test_get_property_default() { $timezone = core_user::get_property_default('timezone'); $this->assertEquals('Pacific/Auckland', $timezone); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage('Invalid property requested, or the property does not has a default value.'); core_user::get_property_default('firstname'); } diff --git a/message/tests/externallib_test.php b/message/tests/externallib_test.php index 1aae295c9d7a1..70c8783fef3de 100644 --- a/message/tests/externallib_test.php +++ b/message/tests/externallib_test.php @@ -1188,7 +1188,6 @@ public function test_unblock_user_no_permission() { /** * Test search_contacts. - * @expectedException moodle_exception */ public function test_search_contacts() { global $DB; @@ -1250,6 +1249,7 @@ public function test_search_contacts() { $this->assertEquals($user5->id, $result['id']); // Empty query, will throw an exception. + $this->expectException(moodle_exception::class); $results = core_message_external::search_contacts(''); } diff --git a/mod/assign/tests/externallib_test.php b/mod/assign/tests/externallib_test.php index 2bd40007a2c02..4d734a8d1ff67 100644 --- a/mod/assign/tests/externallib_test.php +++ b/mod/assign/tests/externallib_test.php @@ -630,8 +630,6 @@ public function test_get_user_mappings() { /** * Test lock_submissions - * - * @expectedException moodle_exception */ public function test_lock_submissions() { global $DB, $USER; @@ -691,6 +689,7 @@ public function test_lock_submissions() { 'text'=>'Submission text', 'format'=>FORMAT_MOODLE); $notices = array(); + $this->expectException(moodle_exception::class); $assign->save_submission($data, $notices); } @@ -895,8 +894,6 @@ public function test_save_user_extensions() { /** * Test reveal_identities - * - * @expectedException required_capability_exception */ public function test_reveal_identities() { global $DB, $USER; @@ -930,6 +927,7 @@ public function test_reveal_identities() { $studentrole->id); $this->setUser($student1); + $this->expectException(required_capability_exception::class); $result = mod_assign_external::reveal_identities($instance->id); $result = external_api::clean_returnvalue(mod_assign_external::reveal_identities_returns(), $result); $this->assertEquals(1, count($result)); @@ -1344,8 +1342,6 @@ public function test_save_grades_with_advanced_grading() { /** * Test save grades for a team submission - * - * @expectedException invalid_parameter_exception */ public function test_save_grades_with_group_submission() { global $DB, $USER, $CFG; @@ -1439,6 +1435,7 @@ public function test_save_grades_with_group_submission() { $grades1[] = $student2gradeinfo; // Expect an exception since 2 grades have been submitted for the same team. + $this->expectException(invalid_parameter_exception::class); $result = mod_assign_external::save_grades($instance->id, true, $grades1); $result = external_api::clean_returnvalue(mod_assign_external::save_grades_returns(), $result); @@ -1649,8 +1646,6 @@ public function test_set_user_flags() { /** * Test view_grading_table - * - * @expectedException dml_missing_record_exception */ public function test_view_grading_table_invalid_instance() { global $DB; @@ -1664,13 +1659,12 @@ public function test_view_grading_table_invalid_instance() { $cm = get_coursemodule_from_instance('assign', $assign->id); // Test invalid instance id. + $this->expectException(dml_missing_record_exception::class); mod_assign_external::view_grading_table(0); } /** * Test view_grading_table - * - * @expectedException require_login_exception */ public function test_view_grading_table_not_enrolled() { global $DB; @@ -1687,6 +1681,7 @@ public function test_view_grading_table_not_enrolled() { $user = self::getDataGenerator()->create_user(); $this->setUser($user); + $this->expectException(require_login_exception::class); mod_assign_external::view_grading_table($assign->id); } @@ -1731,9 +1726,6 @@ public function test_view_grading_table_correct() { /** * Test view_grading_table - * - * @expectedException require_login_exception - * @expectedExceptionMessage Course or activity not accessible. (Activity is hidden) */ public function test_view_grading_table_without_capability() { global $DB; @@ -1759,6 +1751,8 @@ public function test_view_grading_table_without_capability() { accesslib_clear_all_caches_for_unit_testing(); course_modinfo::clear_instance_cache(); + $this->expectException(require_login_exception::class); + $this->expectExceptionMessage('Course or activity not accessible. (Activity is hidden)'); mod_assign_external::view_grading_table($assign->id); } @@ -2204,8 +2198,6 @@ public function test_get_submission_status_in_reopened_status() { /** * Test access control for get_submission_status. - * - * @expectedException required_capability_exception */ public function test_get_submission_status_access_control() { $this->resetAfterTest(true); @@ -2215,6 +2207,7 @@ public function test_get_submission_status_access_control() { $this->setUser($student2); // Access control test. + $this->expectException(required_capability_exception::class); mod_assign_external::get_submission_status($assign->get_instance()->id, $student1->id); } @@ -2338,19 +2331,16 @@ public function test_get_submission_status_with_override() { /** * get_participant should throw an excaption if the requested assignment doesn't exist. - * - * @expectedException moodle_exception */ public function test_get_participant_no_assignment() { $this->resetAfterTest(true); + $this->expectException(moodle_exception::class); mod_assign_external::get_participant('-1', '-1', false); } /** * get_participant should throw a require_login_exception if the user doesn't have access * to view assignments. - * - * @expectedException require_login_exception */ public function test_get_participant_no_view_capability() { global $DB; @@ -2366,14 +2356,13 @@ public function test_get_participant_no_view_capability() { $this->setUser($student); assign_capability('mod/assign:view', CAP_PROHIBIT, $studentrole->id, $context->id, true); + $this->expectException(require_login_exception::class); mod_assign_external::get_participant($assign->id, $student->id, false); } /** * get_participant should throw a required_capability_exception if the user doesn't have access * to view assignment grades. - * - * @expectedException required_capability_exception */ public function test_get_participant_no_grade_capability() { global $DB; @@ -2392,13 +2381,12 @@ public function test_get_participant_no_grade_capability() { assign_capability('mod/assign:grade', CAP_PROHIBIT, $teacherrole->id, $context->id, true); accesslib_clear_all_caches_for_unit_testing(); + $this->expectException(required_capability_exception::class); mod_assign_external::get_participant($assign->id, $student->id, false); } /** * get_participant should throw an exception if the user isn't enrolled in the course. - * - * @expectedException moodle_exception */ public function test_get_participant_no_participant() { global $DB; @@ -2411,6 +2399,7 @@ public function test_get_participant_no_participant() { $this->setUser($teacher); + $this->expectException(moodle_exception::class); $result = mod_assign_external::get_participant($assign->id, $student->id, false); $result = external_api::clean_returnvalue(mod_assign_external::get_participant_returns(), $result); } diff --git a/mod/choice/tests/events_test.php b/mod/choice/tests/events_test.php index c12258208b465..da42a2a1b78fd 100644 --- a/mod/choice/tests/events_test.php +++ b/mod/choice/tests/events_test.php @@ -168,8 +168,6 @@ public function test_answer_created_multiple() { /** * Test custom validations. - * - * @expectedException coding_exception */ public function test_answer_created_other_exception() { // Generate user data. @@ -183,6 +181,7 @@ public function test_answer_created_other_exception() { $eventdata['other'] = array(); // Make sure content identifier is always set. + $this->expectException(coding_exception::class); $event = \mod_choice\event\answer_created::create($eventdata); $event->trigger(); $this->assertEventContextNotUsed($event); diff --git a/mod/choice/tests/lib_test.php b/mod/choice/tests/lib_test.php index ff4b5ba8b22d8..49ab210c1c21d 100644 --- a/mod/choice/tests/lib_test.php +++ b/mod/choice/tests/lib_test.php @@ -138,9 +138,6 @@ public function test_choice_can_view_results() { } - /** - * @expectedException moodle_exception - */ public function test_choice_user_submit_response_validation() { global $USER; @@ -159,6 +156,7 @@ public function test_choice_user_submit_response_validation() { $optionids2 = array_keys($choicewithoptions2->option); // Make sure we cannot submit options from a different choice instance. + $this->expectException(moodle_exception::class); choice_user_submit_response($optionids2[0], $choice1, $USER->id, $course, $cm); } diff --git a/mod/forum/tests/events_test.php b/mod/forum/tests/events_test.php index 01b203ae8e74d..4177d19878f70 100644 --- a/mod/forum/tests/events_test.php +++ b/mod/forum/tests/events_test.php @@ -54,9 +54,6 @@ public function tearDown(): void { /** * Ensure course_searched event validates that searchterm is set. - * - * @expectedException coding_exception - * @expectedExceptionMessage The 'searchterm' value must be set in other. */ public function test_course_searched_searchterm_validation() { $course = $this->getDataGenerator()->create_course(); @@ -65,14 +62,13 @@ public function test_course_searched_searchterm_validation() { 'context' => $coursectx, ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage("The 'searchterm' value must be set in other."); \mod_forum\event\course_searched::create($params); } /** * Ensure course_searched event validates that context is the correct level. - * - * @expectedException coding_exception - * @expectedExceptionMessage Context level must be CONTEXT_COURSE. */ public function test_course_searched_context_validation() { $course = $this->getDataGenerator()->create_course(); @@ -83,6 +79,8 @@ public function test_course_searched_context_validation() { 'other' => array('searchterm' => 'testing'), ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage('Context level must be CONTEXT_COURSE.'); \mod_forum\event\course_searched::create($params); } @@ -123,9 +121,6 @@ public function test_course_searched() { /** * Ensure discussion_created event validates that forumid is set. - * - * @expectedException coding_exception - * @expectedExceptionMessage The 'forumid' value must be set in other. */ public function test_discussion_created_forumid_validation() { $course = $this->getDataGenerator()->create_course(); @@ -136,14 +131,13 @@ public function test_discussion_created_forumid_validation() { 'context' => $context, ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage("The 'forumid' value must be set in other."); \mod_forum\event\discussion_created::create($params); } /** * Ensure discussion_created event validates that the context is the correct level. - * - * @expectedException coding_exception - * @expectedExceptionMessage Context level must be CONTEXT_MODULE. */ public function test_discussion_created_context_validation() { $course = $this->getDataGenerator()->create_course(); @@ -154,6 +148,8 @@ public function test_discussion_created_context_validation() { 'other' => array('forumid' => $forum->id), ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage('Context level must be CONTEXT_MODULE.'); \mod_forum\event\discussion_created::create($params); } @@ -204,9 +200,6 @@ public function test_discussion_created() { /** * Ensure discussion_updated event validates that forumid is set. - * - * @expectedException coding_exception - * @expectedExceptionMessage The 'forumid' value must be set in other. */ public function test_discussion_updated_forumid_validation() { $course = $this->getDataGenerator()->create_course(); @@ -217,14 +210,13 @@ public function test_discussion_updated_forumid_validation() { 'context' => $context, ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage("The 'forumid' value must be set in other."); \mod_forum\event\discussion_updated::create($params); } /** * Ensure discussion_created event validates that the context is the correct level. - * - * @expectedException coding_exception - * @expectedExceptionMessage Context level must be CONTEXT_MODULE. */ public function test_discussion_updated_context_validation() { $course = $this->getDataGenerator()->create_course(); @@ -235,6 +227,8 @@ public function test_discussion_updated_context_validation() { 'other' => array('forumid' => $forum->id), ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage('Context level must be CONTEXT_MODULE.'); \mod_forum\event\discussion_updated::create($params); } @@ -283,9 +277,6 @@ public function test_discussion_updated() { /** * Ensure discussion_deleted event validates that forumid is set. - * - * @expectedException coding_exception - * @expectedExceptionMessage The 'forumid' value must be set in other. */ public function test_discussion_deleted_forumid_validation() { $course = $this->getDataGenerator()->create_course(); @@ -296,14 +287,13 @@ public function test_discussion_deleted_forumid_validation() { 'context' => $context, ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage("The 'forumid' value must be set in other."); \mod_forum\event\discussion_deleted::create($params); } /** * Ensure discussion_deleted event validates that context is of the correct level. - * - * @expectedException coding_exception - * @expectedExceptionMessage Context level must be CONTEXT_MODULE. */ public function test_discussion_deleted_context_validation() { $course = $this->getDataGenerator()->create_course(); @@ -314,6 +304,8 @@ public function test_discussion_deleted_context_validation() { 'other' => array('forumid' => $forum->id), ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage('Context level must be CONTEXT_MODULE.'); \mod_forum\event\discussion_deleted::create($params); } @@ -363,9 +355,6 @@ public function test_discussion_deleted() { /** * Ensure discussion_moved event validates that fromforumid is set. - * - * @expectedException coding_exception - * @expectedExceptionMessage The 'fromforumid' value must be set in other. */ public function test_discussion_moved_fromforumid_validation() { $course = $this->getDataGenerator()->create_course(); @@ -378,14 +367,13 @@ public function test_discussion_moved_fromforumid_validation() { 'other' => array('toforumid' => $toforum->id) ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage("The 'fromforumid' value must be set in other."); \mod_forum\event\discussion_moved::create($params); } /** * Ensure discussion_moved event validates that toforumid is set. - * - * @expectedException coding_exception - * @expectedExceptionMessage The 'toforumid' value must be set in other. */ public function test_discussion_moved_toforumid_validation() { $course = $this->getDataGenerator()->create_course(); @@ -398,14 +386,13 @@ public function test_discussion_moved_toforumid_validation() { 'other' => array('fromforumid' => $fromforum->id) ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage("The 'toforumid' value must be set in other."); \mod_forum\event\discussion_moved::create($params); } /** * Ensure discussion_moved event validates that the context level is correct. - * - * @expectedException coding_exception - * @expectedExceptionMessage Context level must be CONTEXT_MODULE. */ public function test_discussion_moved_context_validation() { $course = $this->getDataGenerator()->create_course(); @@ -426,6 +413,8 @@ public function test_discussion_moved_context_validation() { 'other' => array('fromforumid' => $fromforum->id, 'toforumid' => $toforum->id) ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage('Context level must be CONTEXT_MODULE.'); \mod_forum\event\discussion_moved::create($params); } @@ -477,9 +466,6 @@ public function test_discussion_moved() { /** * Ensure discussion_viewed event validates that the contextlevel is correct. - * - * @expectedException coding_exception - * @expectedExceptionMessage Context level must be CONTEXT_MODULE. */ public function test_discussion_viewed_context_validation() { $course = $this->getDataGenerator()->create_course(); @@ -498,6 +484,8 @@ public function test_discussion_viewed_context_validation() { 'objectid' => $discussion->id, ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage('Context level must be CONTEXT_MODULE.'); \mod_forum\event\discussion_viewed::create($params); } @@ -546,9 +534,6 @@ public function test_discussion_viewed() { /** * Ensure course_module_viewed event validates that the contextlevel is correct. - * - * @expectedException coding_exception - * @expectedExceptionMessage Context level must be CONTEXT_MODULE. */ public function test_course_module_viewed_context_validation() { $course = $this->getDataGenerator()->create_course(); @@ -559,6 +544,8 @@ public function test_course_module_viewed_context_validation() { 'objectid' => $forum->id, ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage('Context level must be CONTEXT_MODULE.'); \mod_forum\event\course_module_viewed::create($params); } @@ -600,9 +587,6 @@ public function test_course_module_viewed() { /** * Ensure subscription_created event validates that the forumid is set. - * - * @expectedException coding_exception - * @expectedExceptionMessage The 'forumid' value must be set in other. */ public function test_subscription_created_forumid_validation() { $user = $this->getDataGenerator()->create_user(); @@ -614,14 +598,13 @@ public function test_subscription_created_forumid_validation() { 'relateduserid' => $user->id, ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage("The 'forumid' value must be set in other."); \mod_forum\event\subscription_created::create($params); } /** * Ensure subscription_created event validates that the relateduserid is set. - * - * @expectedException coding_exception - * @expectedExceptionMessage The 'relateduserid' must be set. */ public function test_subscription_created_relateduserid_validation() { $course = $this->getDataGenerator()->create_course(); @@ -632,14 +615,13 @@ public function test_subscription_created_relateduserid_validation() { 'objectid' => $forum->id, ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage("The 'relateduserid' must be set."); \mod_forum\event\subscription_created::create($params); } /** * Ensure subscription_created event validates that the contextlevel is correct. - * - * @expectedException coding_exception - * @expectedExceptionMessage Context level must be CONTEXT_MODULE. */ public function test_subscription_created_contextlevel_validation() { $user = $this->getDataGenerator()->create_user(); @@ -652,6 +634,8 @@ public function test_subscription_created_contextlevel_validation() { 'relateduserid' => $user->id, ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage('Context level must be CONTEXT_MODULE.'); \mod_forum\event\subscription_created::create($params); } @@ -703,9 +687,6 @@ public function test_subscription_created() { /** * Ensure subscription_deleted event validates that the forumid is set. - * - * @expectedException coding_exception - * @expectedExceptionMessage The 'forumid' value must be set in other. */ public function test_subscription_deleted_forumid_validation() { $user = $this->getDataGenerator()->create_user(); @@ -717,14 +698,13 @@ public function test_subscription_deleted_forumid_validation() { 'relateduserid' => $user->id, ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage("The 'forumid' value must be set in other."); \mod_forum\event\subscription_deleted::create($params); } /** * Ensure subscription_deleted event validates that the relateduserid is set. - * - * @expectedException coding_exception - * @expectedExceptionMessage The 'relateduserid' must be set. */ public function test_subscription_deleted_relateduserid_validation() { $course = $this->getDataGenerator()->create_course(); @@ -735,14 +715,13 @@ public function test_subscription_deleted_relateduserid_validation() { 'objectid' => $forum->id, ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage("The 'relateduserid' must be set."); \mod_forum\event\subscription_deleted::create($params); } /** * Ensure subscription_deleted event validates that the contextlevel is correct. - * - * @expectedException coding_exception - * @expectedExceptionMessage Context level must be CONTEXT_MODULE. */ public function test_subscription_deleted_contextlevel_validation() { $user = $this->getDataGenerator()->create_user(); @@ -755,6 +734,8 @@ public function test_subscription_deleted_contextlevel_validation() { 'relateduserid' => $user->id, ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage('Context level must be CONTEXT_MODULE.'); \mod_forum\event\subscription_deleted::create($params); } @@ -806,9 +787,6 @@ public function test_subscription_deleted() { /** * Ensure readtracking_enabled event validates that the forumid is set. - * - * @expectedException coding_exception - * @expectedExceptionMessage The 'forumid' value must be set in other. */ public function test_readtracking_enabled_forumid_validation() { $user = $this->getDataGenerator()->create_user(); @@ -820,14 +798,13 @@ public function test_readtracking_enabled_forumid_validation() { 'relateduserid' => $user->id, ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage("The 'forumid' value must be set in other."); \mod_forum\event\readtracking_enabled::create($params); } /** * Ensure readtracking_enabled event validates that the relateduserid is set. - * - * @expectedException coding_exception - * @expectedExceptionMessage The 'relateduserid' must be set. */ public function test_readtracking_enabled_relateduserid_validation() { $course = $this->getDataGenerator()->create_course(); @@ -838,14 +815,13 @@ public function test_readtracking_enabled_relateduserid_validation() { 'objectid' => $forum->id, ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage("The 'relateduserid' must be set."); \mod_forum\event\readtracking_enabled::create($params); } /** * Ensure readtracking_enabled event validates that the contextlevel is correct. - * - * @expectedException coding_exception - * @expectedExceptionMessage Context level must be CONTEXT_MODULE. */ public function test_readtracking_enabled_contextlevel_validation() { $user = $this->getDataGenerator()->create_user(); @@ -858,6 +834,8 @@ public function test_readtracking_enabled_contextlevel_validation() { 'relateduserid' => $user->id, ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage('Context level must be CONTEXT_MODULE.'); \mod_forum\event\readtracking_enabled::create($params); } @@ -900,9 +878,6 @@ public function test_readtracking_enabled() { /** * Ensure readtracking_disabled event validates that the forumid is set. - * - * @expectedException coding_exception - * @expectedExceptionMessage The 'forumid' value must be set in other. */ public function test_readtracking_disabled_forumid_validation() { $user = $this->getDataGenerator()->create_user(); @@ -914,14 +889,13 @@ public function test_readtracking_disabled_forumid_validation() { 'relateduserid' => $user->id, ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage("The 'forumid' value must be set in other."); \mod_forum\event\readtracking_disabled::create($params); } /** * Ensure readtracking_disabled event validates that the relateduserid is set. - * - * @expectedException coding_exception - * @expectedExceptionMessage The 'relateduserid' must be set. */ public function test_readtracking_disabled_relateduserid_validation() { $course = $this->getDataGenerator()->create_course(); @@ -932,14 +906,13 @@ public function test_readtracking_disabled_relateduserid_validation() { 'objectid' => $forum->id, ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage("The 'relateduserid' must be set."); \mod_forum\event\readtracking_disabled::create($params); } /** * Ensure readtracking_disabled event validates that the contextlevel is correct - * - * @expectedException coding_exception - * @expectedExceptionMessage Context level must be CONTEXT_MODULE. */ public function test_readtracking_disabled_contextlevel_validation() { $user = $this->getDataGenerator()->create_user(); @@ -952,6 +925,8 @@ public function test_readtracking_disabled_contextlevel_validation() { 'relateduserid' => $user->id, ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage('Context level must be CONTEXT_MODULE.'); \mod_forum\event\readtracking_disabled::create($params); } @@ -994,9 +969,6 @@ public function test_readtracking_disabled() { /** * Ensure subscribers_viewed event validates that the forumid is set. - * - * @expectedException coding_exception - * @expectedExceptionMessage The 'forumid' value must be set in other. */ public function test_subscribers_viewed_forumid_validation() { $user = $this->getDataGenerator()->create_user(); @@ -1008,14 +980,13 @@ public function test_subscribers_viewed_forumid_validation() { 'relateduserid' => $user->id, ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage("The 'forumid' value must be set in other."); \mod_forum\event\subscribers_viewed::create($params); } /** * Ensure subscribers_viewed event validates that the contextlevel is correct. - * - * @expectedException coding_exception - * @expectedExceptionMessage Context level must be CONTEXT_MODULE. */ public function test_subscribers_viewed_contextlevel_validation() { $user = $this->getDataGenerator()->create_user(); @@ -1028,6 +999,8 @@ public function test_subscribers_viewed_contextlevel_validation() { 'relateduserid' => $user->id, ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage('Context level must be CONTEXT_MODULE.'); \mod_forum\event\subscribers_viewed::create($params); } @@ -1066,9 +1039,6 @@ public function test_subscribers_viewed() { /** * Ensure user_report_viewed event validates that the reportmode is set. - * - * @expectedException coding_exception - * @expectedExceptionMessage The 'reportmode' value must be set in other. */ public function test_user_report_viewed_reportmode_validation() { $user = $this->getDataGenerator()->create_user(); @@ -1079,14 +1049,13 @@ public function test_user_report_viewed_reportmode_validation() { 'relateduserid' => $user->id, ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage("The 'reportmode' value must be set in other."); \mod_forum\event\user_report_viewed::create($params); } /** * Ensure user_report_viewed event validates that the contextlevel is correct. - * - * @expectedException coding_exception - * @expectedExceptionMessage Context level must be either CONTEXT_SYSTEM, CONTEXT_COURSE or CONTEXT_USER. */ public function test_user_report_viewed_contextlevel_validation() { $user = $this->getDataGenerator()->create_user(); @@ -1099,14 +1068,13 @@ public function test_user_report_viewed_contextlevel_validation() { 'relateduserid' => $user->id, ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage('Context level must be either CONTEXT_SYSTEM, CONTEXT_COURSE or CONTEXT_USER.'); \mod_forum\event\user_report_viewed::create($params); } /** * Ensure user_report_viewed event validates that the relateduserid is set. - * - * @expectedException coding_exception - * @expectedExceptionMessage The 'relateduserid' must be set. */ public function test_user_report_viewed_relateduserid_validation() { @@ -1115,6 +1083,8 @@ public function test_user_report_viewed_relateduserid_validation() { 'other' => array('reportmode' => 'posts'), ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage("The 'relateduserid' must be set."); \mod_forum\event\user_report_viewed::create($params); } @@ -1178,9 +1148,6 @@ public function test_post_created_postid_validation() { /** * Ensure post_created event validates that the discussionid is set. - * - * @expectedException coding_exception - * @expectedExceptionMessage The 'discussionid' value must be set in other. */ public function test_post_created_discussionid_validation() { $course = $this->getDataGenerator()->create_course(); @@ -1206,14 +1173,13 @@ public function test_post_created_discussionid_validation() { 'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type) ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage("The 'discussionid' value must be set in other."); \mod_forum\event\post_created::create($params); } /** * Ensure post_created event validates that the forumid is set. - * - * @expectedException coding_exception - * @expectedExceptionMessage The 'forumid' value must be set in other. */ public function test_post_created_forumid_validation() { $course = $this->getDataGenerator()->create_course(); @@ -1239,14 +1205,13 @@ public function test_post_created_forumid_validation() { 'other' => array('discussionid' => $discussion->id, 'forumtype' => $forum->type) ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage("The 'forumid' value must be set in other."); \mod_forum\event\post_created::create($params); } /** * Ensure post_created event validates that the forumtype is set. - * - * @expectedException coding_exception - * @expectedExceptionMessage The 'forumtype' value must be set in other. */ public function test_post_created_forumtype_validation() { $course = $this->getDataGenerator()->create_course(); @@ -1272,14 +1237,13 @@ public function test_post_created_forumtype_validation() { 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id) ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage("The 'forumtype' value must be set in other."); \mod_forum\event\post_created::create($params); } /** * Ensure post_created event validates that the contextlevel is correct. - * - * @expectedException coding_exception - * @expectedExceptionMessage Context level must be CONTEXT_MODULE. */ public function test_post_created_context_validation() { $course = $this->getDataGenerator()->create_course(); @@ -1305,6 +1269,8 @@ public function test_post_created_context_validation() { 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type) ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage('Context level must be CONTEXT_MODULE.'); \mod_forum\event\post_created::create($params); } @@ -1439,9 +1405,6 @@ public function test_post_deleted_postid_validation() { /** * Ensure post_deleted event validates that the discussionid is set. - * - * @expectedException coding_exception - * @expectedExceptionMessage The 'discussionid' value must be set in other. */ public function test_post_deleted_discussionid_validation() { $course = $this->getDataGenerator()->create_course(); @@ -1467,14 +1430,13 @@ public function test_post_deleted_discussionid_validation() { 'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type) ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage("The 'discussionid' value must be set in other."); \mod_forum\event\post_deleted::create($params); } /** * Ensure post_deleted event validates that the forumid is set. - * - * @expectedException coding_exception - * @expectedExceptionMessage The 'forumid' value must be set in other. */ public function test_post_deleted_forumid_validation() { $course = $this->getDataGenerator()->create_course(); @@ -1500,14 +1462,13 @@ public function test_post_deleted_forumid_validation() { 'other' => array('discussionid' => $discussion->id, 'forumtype' => $forum->type) ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage("The 'forumid' value must be set in other."); \mod_forum\event\post_deleted::create($params); } /** * Ensure post_deleted event validates that the forumtype is set. - * - * @expectedException coding_exception - * @expectedExceptionMessage The 'forumtype' value must be set in other. */ public function test_post_deleted_forumtype_validation() { $course = $this->getDataGenerator()->create_course(); @@ -1533,14 +1494,13 @@ public function test_post_deleted_forumtype_validation() { 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id) ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage("The 'forumtype' value must be set in other."); \mod_forum\event\post_deleted::create($params); } /** * Ensure post_deleted event validates that the contextlevel is correct. - * - * @expectedException coding_exception - * @expectedExceptionMessage Context level must be CONTEXT_MODULE. */ public function test_post_deleted_context_validation() { $course = $this->getDataGenerator()->create_course(); @@ -1566,6 +1526,8 @@ public function test_post_deleted_context_validation() { 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type) ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage('Context level must be CONTEXT_MODULE.'); \mod_forum\event\post_deleted::create($params); } @@ -1711,9 +1673,6 @@ public function test_post_deleted_single() { /** * Ensure post_updated event validates that the discussionid is set. - * - * @expectedException coding_exception - * @expectedExceptionMessage The 'discussionid' value must be set in other. */ public function test_post_updated_discussionid_validation() { $course = $this->getDataGenerator()->create_course(); @@ -1739,14 +1698,13 @@ public function test_post_updated_discussionid_validation() { 'other' => array('forumid' => $forum->id, 'forumtype' => $forum->type) ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage("The 'discussionid' value must be set in other."); \mod_forum\event\post_updated::create($params); } /** * Ensure post_updated event validates that the forumid is set. - * - * @expectedException coding_exception - * @expectedExceptionMessage The 'forumid' value must be set in other. */ public function test_post_updated_forumid_validation() { $course = $this->getDataGenerator()->create_course(); @@ -1772,14 +1730,13 @@ public function test_post_updated_forumid_validation() { 'other' => array('discussionid' => $discussion->id, 'forumtype' => $forum->type) ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage("The 'forumid' value must be set in other."); \mod_forum\event\post_updated::create($params); } /** * Ensure post_updated event validates that the forumtype is set. - * - * @expectedException coding_exception - * @expectedExceptionMessage The 'forumtype' value must be set in other. */ public function test_post_updated_forumtype_validation() { $course = $this->getDataGenerator()->create_course(); @@ -1805,14 +1762,13 @@ public function test_post_updated_forumtype_validation() { 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id) ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage("The 'forumtype' value must be set in other."); \mod_forum\event\post_updated::create($params); } /** * Ensure post_updated event validates that the contextlevel is correct. - * - * @expectedException coding_exception - * @expectedExceptionMessage Context level must be CONTEXT_MODULE. */ public function test_post_updated_context_validation() { $course = $this->getDataGenerator()->create_course(); @@ -1838,6 +1794,8 @@ public function test_post_updated_context_validation() { 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type) ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage('Context level must be CONTEXT_MODULE.'); \mod_forum\event\post_updated::create($params); } @@ -1988,7 +1946,6 @@ public function test_discussion_subscription_created() { // Checking that the event contains the expected values. $this->assertInstanceOf('\mod_forum\event\discussion_subscription_created', $event); - $cm = get_coursemodule_from_instance('forum', $discussion->forum); $context = \context_module::instance($cm->id); $this->assertEquals($context, $event->get_context()); @@ -2064,9 +2021,6 @@ public function test_discussion_subscription_created_validation() { /** * Test contextlevel validation of discussion_subscription_created event. - * - * @expectedException coding_exception - * @expectedExceptionMessage Context level must be CONTEXT_MODULE. */ public function test_discussion_subscription_created_validation_contextlevel() { global $CFG, $DB; @@ -2115,14 +2069,13 @@ public function test_discussion_subscription_created_validation_contextlevel() { ); // Without an invalid context. + $this->expectException(coding_exception::class); + $this->expectExceptionMessage('Context level must be CONTEXT_MODULE.'); \mod_forum\event\discussion_subscription_created::create($params); } /** * Test discussion validation of discussion_subscription_created event. - * - * @expectedException coding_exception - * @expectedExceptionMessage The 'discussion' value must be set in other. */ public function test_discussion_subscription_created_validation_discussion() { global $CFG, $DB; @@ -2168,14 +2121,13 @@ public function test_discussion_subscription_created_validation_discussion() { ) ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage("The 'discussion' value must be set in other."); \mod_forum\event\discussion_subscription_created::create($params); } /** * Test forumid validation of discussion_subscription_created event. - * - * @expectedException coding_exception - * @expectedExceptionMessage The 'forumid' value must be set in other. */ public function test_discussion_subscription_created_validation_forumid() { global $CFG, $DB; @@ -2221,14 +2173,13 @@ public function test_discussion_subscription_created_validation_forumid() { ) ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage("The 'forumid' value must be set in other."); \mod_forum\event\discussion_subscription_created::create($params); } /** * Test relateduserid validation of discussion_subscription_created event. - * - * @expectedException coding_exception - * @expectedExceptionMessage The 'relateduserid' must be set. */ public function test_discussion_subscription_created_validation_relateduserid() { global $CFG, $DB; @@ -2276,6 +2227,8 @@ public function test_discussion_subscription_created_validation_relateduserid() ) ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage("The 'relateduserid' must be set."); \mod_forum\event\discussion_subscription_created::create($params); } @@ -2320,7 +2273,6 @@ public function test_discussion_subscription_deleted() { // Checking that the event contains the expected values. $this->assertInstanceOf('\mod_forum\event\discussion_subscription_deleted', $event); - $cm = get_coursemodule_from_instance('forum', $discussion->forum); $context = \context_module::instance($cm->id); $this->assertEquals($context, $event->get_context()); @@ -2420,9 +2372,6 @@ public function test_discussion_subscription_deleted_validation() { /** * Test contextlevel validation of discussion_subscription_deleted event. - * - * @expectedException coding_exception - * @expectedExceptionMessage Context level must be CONTEXT_MODULE. */ public function test_discussion_subscription_deleted_validation_contextlevel() { global $CFG, $DB; @@ -2471,14 +2420,13 @@ public function test_discussion_subscription_deleted_validation_contextlevel() { ); // Without an invalid context. + $this->expectException(coding_exception::class); + $this->expectExceptionMessage('Context level must be CONTEXT_MODULE.'); \mod_forum\event\discussion_subscription_deleted::create($params); } /** * Test discussion validation of discussion_subscription_deleted event. - * - * @expectedException coding_exception - * @expectedExceptionMessage The 'discussion' value must be set in other. */ public function test_discussion_subscription_deleted_validation_discussion() { global $CFG, $DB; @@ -2524,14 +2472,13 @@ public function test_discussion_subscription_deleted_validation_discussion() { ) ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage("The 'discussion' value must be set in other."); \mod_forum\event\discussion_subscription_deleted::create($params); } /** * Test forumid validation of discussion_subscription_deleted event. - * - * @expectedException coding_exception - * @expectedExceptionMessage The 'forumid' value must be set in other. */ public function test_discussion_subscription_deleted_validation_forumid() { global $CFG, $DB; @@ -2577,14 +2524,13 @@ public function test_discussion_subscription_deleted_validation_forumid() { ) ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage("The 'forumid' value must be set in other."); \mod_forum\event\discussion_subscription_deleted::create($params); } /** * Test relateduserid validation of discussion_subscription_deleted event. - * - * @expectedException coding_exception - * @expectedExceptionMessage The 'relateduserid' must be set. */ public function test_discussion_subscription_deleted_validation_relateduserid() { global $CFG, $DB; @@ -2632,6 +2578,8 @@ public function test_discussion_subscription_deleted_validation_relateduserid() ) ); + $this->expectException(coding_exception::class); + $this->expectExceptionMessage("The 'relateduserid' must be set."); \mod_forum\event\discussion_subscription_deleted::create($params); } diff --git a/mod/glossary/tests/external_test.php b/mod/glossary/tests/external_test.php index 18c3a4100420e..cee115dea1fb5 100644 --- a/mod/glossary/tests/external_test.php +++ b/mod/glossary/tests/external_test.php @@ -112,10 +112,6 @@ public function test_view_glossary() { $sink->close(); } - /** - * @expectedException require_login_exception - * @expectedExceptionMessage Activity is hidden - */ public function test_view_glossary_without_permission() { $this->resetAfterTest(true); @@ -134,13 +130,11 @@ public function test_view_glossary_without_permission() { // Assertion. $this->setUser($u1); + $this->expectException(require_login_exception::class); + $this->expectExceptionMessage('Activity is hidden'); mod_glossary_external::view_glossary($g1->id, 'letter'); } - /** - * @expectedException require_login_exception - * @expectedExceptionMessage Activity is hidden - */ public function test_view_entry() { $this->resetAfterTest(true); @@ -188,6 +182,8 @@ public function test_view_entry() { } // Test non-readable entry. + $this->expectException(require_login_exception::class); + $this->expectExceptionMessage('Activity is hidden'); mod_glossary_external::view_entry($e4->id); } diff --git a/mod/quiz/accessrule/seb/tests/link_generator_test.php b/mod/quiz/accessrule/seb/tests/link_generator_test.php index 6d61979a70626..1cb608ce7b43c 100644 --- a/mod/quiz/accessrule/seb/tests/link_generator_test.php +++ b/mod/quiz/accessrule/seb/tests/link_generator_test.php @@ -96,7 +96,7 @@ public function test_sebs_link_generated() { */ public function test_course_module_does_not_exist() { $this->expectException(dml_exception::class); - $this->expectExceptionMessageRegExp("/^Can't find data record in database.*/"); + $this->expectExceptionMessageMatches("/^Can't find data record in database.*/"); $generator = link_generator::get_link(123456, false); } } diff --git a/mod/quiz/tests/structure_test.php b/mod/quiz/tests/structure_test.php index 5388b9883a726..eb4fe10303807 100644 --- a/mod/quiz/tests/structure_test.php +++ b/mod/quiz/tests/structure_test.php @@ -259,9 +259,6 @@ public function test_remove_section_heading() { ), $structure); } - /** - * @expectedException coding_exception - */ public function test_cannot_remove_first_section() { $quizobj = $this->create_test_quiz(array( 'Heading 1', @@ -272,6 +269,7 @@ public function test_cannot_remove_first_section() { $sections = $structure->get_sections(); $section = reset($sections); + $this->expectException(coding_exception::class); $structure->remove_section_heading($section->id); } @@ -436,9 +434,6 @@ public function test_move_slot_emptying_a_page_renumbers_pages() { ), $structure); } - /** - * @expectedException coding_exception - */ public function test_move_slot_too_small_page_number_detected() { $quizobj = $this->create_test_quiz(array( array('TF1', 1, 'truefalse'), @@ -449,12 +444,10 @@ public function test_move_slot_too_small_page_number_detected() { $idtomove = $structure->get_question_in_slot(3)->slotid; $idmoveafter = $structure->get_question_in_slot(2)->slotid; + $this->expectException(coding_exception::class); $structure->move_slot($idtomove, $idmoveafter, '1'); } - /** - * @expectedException coding_exception - */ public function test_move_slot_too_large_page_number_detected() { $quizobj = $this->create_test_quiz(array( array('TF1', 1, 'truefalse'), @@ -465,6 +458,7 @@ public function test_move_slot_too_large_page_number_detected() { $idtomove = $structure->get_question_in_slot(1)->slotid; $idmoveafter = $structure->get_question_in_slot(2)->slotid; + $this->expectException(coding_exception::class); $structure->move_slot($idtomove, $idmoveafter, '4'); } @@ -722,8 +716,6 @@ public function test_quiz_removing_a_random_question_deletes_the_question() { /** * Unit test to make sue it is not possible to remove all slots in a section at once. - * - * @expectedException coding_exception */ public function test_cannot_remove_all_slots_in_a_section() { $quizobj = $this->create_test_quiz(array( @@ -735,12 +727,10 @@ public function test_cannot_remove_all_slots_in_a_section() { $structure = \mod_quiz\structure::create_for_quiz($quizobj); $structure->remove_slot(1); + $this->expectException(coding_exception::class); $structure->remove_slot(2); } - /** - * @expectedException coding_exception - */ public function test_cannot_remove_last_slot_in_a_section() { $quizobj = $this->create_test_quiz(array( array('TF1', 1, 'truefalse'), @@ -750,6 +740,7 @@ public function test_cannot_remove_last_slot_in_a_section() { )); $structure = \mod_quiz\structure::create_for_quiz($quizobj); + $this->expectException(coding_exception::class); $structure->remove_slot(3); } diff --git a/mod/scorm/tests/events_test.php b/mod/scorm/tests/events_test.php index 24e41b295a62f..b75627922666a 100644 --- a/mod/scorm/tests/events_test.php +++ b/mod/scorm/tests/events_test.php @@ -59,8 +59,6 @@ protected function setUp(): void { /** * Tests for attempt deleted event - * - * @expectedException coding_exception */ public function test_attempt_deleted_event() { @@ -87,11 +85,11 @@ public function test_attempt_deleted_event() { $this->assertEventContextNotUsed($event); // Test event validations. + $this->expectException(coding_exception::class); \mod_scorm\event\attempt_deleted::create(array( 'contextid' => 5, 'relateduserid' => 2 )); - $this->fail('event \\mod_scorm\\event\\attempt_deleted is not validating events properly'); } /** @@ -231,8 +229,6 @@ public function test_report_viewed_event() { /** Tests for sco launched event. * * There is no api involved so the best we can do is test legacy data and validations by triggering event manually. - * - * @expectedException coding_exception */ public function test_sco_launched_event() { $this->resetAfterTest(); @@ -256,12 +252,12 @@ public function test_sco_launched_event() { $this->assertEventContextNotUsed($event); // Test validations. + $this->expectException(coding_exception::class); \mod_scorm\event\sco_launched::create(array( 'objectid' => $this->eventscorm->id, 'context' => context_module::instance($this->eventcm->id), 'courseid' => $this->eventcourse->id, )); - $this->fail('Event \\mod_scorm\\event\\sco_launched is not validating "loadedcontent" properly'); } /** diff --git a/mod/scorm/tests/externallib_test.php b/mod/scorm/tests/externallib_test.php index 79d8f52975340..fe8a801ff07a4 100644 --- a/mod/scorm/tests/externallib_test.php +++ b/mod/scorm/tests/externallib_test.php @@ -174,9 +174,6 @@ public function test_mod_scorm_get_scorm_attempt_count_others_as_teacher() { $this->assertEquals(1, $result['attemptscount']); } - /** - * @expectedException required_capability_exception - */ public function test_mod_scorm_get_scorm_attempt_count_others_as_student() { // Create a second student. $student2 = self::getDataGenerator()->create_user(); @@ -186,27 +183,24 @@ public function test_mod_scorm_get_scorm_attempt_count_others_as_student() { self::setUser($student2); // I should not be able to view the attempts of another student. + $this->expectException(required_capability_exception::class); mod_scorm_external::get_scorm_attempt_count($this->scorm->id, $this->student->id); } - /** - * @expectedException moodle_exception - */ public function test_mod_scorm_get_scorm_attempt_count_invalid_instanceid() { // As student. self::setUser($this->student); // Test invalid instance id. + $this->expectException(moodle_exception::class); mod_scorm_external::get_scorm_attempt_count(0, $this->student->id); } - /** - * @expectedException moodle_exception - */ public function test_mod_scorm_get_scorm_attempt_count_invalid_userid() { // As student. self::setUser($this->student); + $this->expectException(moodle_exception::class); mod_scorm_external::get_scorm_attempt_count($this->scorm->id, -1); } diff --git a/mod/wiki/tests/externallib_test.php b/mod/wiki/tests/externallib_test.php index 6d543154ffa53..c91fd936c044f 100644 --- a/mod/wiki/tests/externallib_test.php +++ b/mod/wiki/tests/externallib_test.php @@ -459,30 +459,26 @@ public function test_get_subwikis() { /** * Test get_subwiki_pages using an invalid wiki instance. - * - * @expectedException moodle_exception */ public function test_get_subwiki_pages_invalid_instance() { + $this->expectException(moodle_exception::class); mod_wiki_external::get_subwiki_pages(0); } /** * Test get_subwiki_pages using a user not enrolled in the course. - * - * @expectedException require_login_exception */ public function test_get_subwiki_pages_unenrolled_user() { // Create and use the user. $usernotenrolled = self::getDataGenerator()->create_user(); $this->setUser($usernotenrolled); + $this->expectException(require_login_exception::class); mod_wiki_external::get_subwiki_pages($this->wiki->id); } /** * Test get_subwiki_pages using a hidden wiki as student. - * - * @expectedException require_login_exception */ public function test_get_subwiki_pages_hidden_wiki_as_student() { // Create a hidden wiki and try to get the list of pages. @@ -490,13 +486,12 @@ public function test_get_subwiki_pages_hidden_wiki_as_student() { array('course' => $this->course->id, 'visible' => false)); $this->setUser($this->student); + $this->expectException(require_login_exception::class); mod_wiki_external::get_subwiki_pages($hiddenwiki->id); } /** * Test get_subwiki_pages without the viewpage capability. - * - * @expectedException moodle_exception */ public function test_get_subwiki_pages_without_viewpage_capability() { // Prohibit capability = mod/wiki:viewpage on the course for students. @@ -505,38 +500,35 @@ public function test_get_subwiki_pages_without_viewpage_capability() { accesslib_clear_all_caches_for_unit_testing(); $this->setUser($this->student); + $this->expectException(moodle_exception::class); mod_wiki_external::get_subwiki_pages($this->wiki->id); } /** * Test get_subwiki_pages using an invalid userid. - * - * @expectedException moodle_exception */ public function test_get_subwiki_pages_invalid_userid() { // Create an individual wiki. $indwiki = $this->getDataGenerator()->create_module('wiki', array('course' => $this->course->id, 'wikimode' => 'individual')); + $this->expectException(moodle_exception::class); mod_wiki_external::get_subwiki_pages($indwiki->id, 0, -10); } /** * Test get_subwiki_pages using an invalid groupid. - * - * @expectedException moodle_exception */ public function test_get_subwiki_pages_invalid_groupid() { // Create testing data. $this->create_collaborative_wikis_with_groups(); + $this->expectException(moodle_exception::class); mod_wiki_external::get_subwiki_pages($this->wikisep->id, -111); } /** * Test get_subwiki_pages, check that a student can't see another user pages in an individual wiki without groups. - * - * @expectedException moodle_exception */ public function test_get_subwiki_pages_individual_student_see_other_user() { // Create an individual wiki. @@ -544,62 +536,59 @@ public function test_get_subwiki_pages_individual_student_see_other_user() { array('course' => $this->course->id, 'wikimode' => 'individual')); $this->setUser($this->student); + $this->expectException(moodle_exception::class); mod_wiki_external::get_subwiki_pages($indwiki->id, 0, $this->teacher->id); } /** * Test get_subwiki_pages, check that a student can't get the pages from another group in * a collaborative wiki using separate groups. - * - * @expectedException moodle_exception */ public function test_get_subwiki_pages_collaborative_separate_groups_student_see_other_group() { // Create testing data. $this->create_collaborative_wikis_with_groups(); $this->setUser($this->student); + $this->expectException(moodle_exception::class); mod_wiki_external::get_subwiki_pages($this->wikisep->id, $this->group2->id); } /** * Test get_subwiki_pages, check that a student can't get the pages from another group in * an individual wiki using separate groups. - * - * @expectedException moodle_exception */ public function test_get_subwiki_pages_individual_separate_groups_student_see_other_group() { // Create testing data. $this->create_individual_wikis_with_groups(); $this->setUser($this->student); + $this->expectException(moodle_exception::class); mod_wiki_external::get_subwiki_pages($this->wikisepind->id, $this->group2->id, $this->teacher->id); } /** * Test get_subwiki_pages, check that a student can't get the pages from all participants in * a collaborative wiki using separate groups. - * - * @expectedException moodle_exception */ public function test_get_subwiki_pages_collaborative_separate_groups_student_see_all_participants() { // Create testing data. $this->create_collaborative_wikis_with_groups(); $this->setUser($this->student); + $this->expectException(moodle_exception::class); mod_wiki_external::get_subwiki_pages($this->wikisep->id, 0); } /** * Test get_subwiki_pages, check that a student can't get the pages from all participants in * an individual wiki using separate groups. - * - * @expectedException moodle_exception */ public function test_get_subwiki_pages_individual_separate_groups_student_see_all_participants() { // Create testing data. $this->create_individual_wikis_with_groups(); $this->setUser($this->student); + $this->expectException(moodle_exception::class); mod_wiki_external::get_subwiki_pages($this->wikisepind->id, 0, $this->teacher->id); } @@ -921,30 +910,26 @@ public function test_get_subwiki_pages_visible_groups_individual() { /** * Test get_page_contents using an invalid pageid. - * - * @expectedException moodle_exception */ public function test_get_page_contents_invalid_pageid() { + $this->expectException(moodle_exception::class); mod_wiki_external::get_page_contents(0); } /** * Test get_page_contents using a user not enrolled in the course. - * - * @expectedException require_login_exception */ public function test_get_page_contents_unenrolled_user() { // Create and use the user. $usernotenrolled = self::getDataGenerator()->create_user(); $this->setUser($usernotenrolled); + $this->expectException(require_login_exception::class); mod_wiki_external::get_page_contents($this->firstpage->id); } /** * Test get_page_contents using a hidden wiki as student. - * - * @expectedException require_login_exception */ public function test_get_page_contents_hidden_wiki_as_student() { // Create a hidden wiki and try to get a page contents. @@ -953,13 +938,12 @@ public function test_get_page_contents_hidden_wiki_as_student() { $hiddenpage = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_page($hiddenwiki); $this->setUser($this->student); + $this->expectException(require_login_exception::class); mod_wiki_external::get_page_contents($hiddenpage->id); } /** * Test get_page_contents without the viewpage capability. - * - * @expectedException moodle_exception */ public function test_get_page_contents_without_viewpage_capability() { // Prohibit capability = mod/wiki:viewpage on the course for students. @@ -968,20 +952,20 @@ public function test_get_page_contents_without_viewpage_capability() { accesslib_clear_all_caches_for_unit_testing(); $this->setUser($this->student); + $this->expectException(moodle_exception::class); mod_wiki_external::get_page_contents($this->firstpage->id); } /** * Test get_page_contents, check that a student can't get a page from another group when * using separate groups. - * - * @expectedException moodle_exception */ public function test_get_page_contents_separate_groups_student_see_other_group() { // Create testing data. $this->create_individual_wikis_with_groups(); $this->setUser($this->student); + $this->expectException(moodle_exception::class); mod_wiki_external::get_page_contents($this->fpsepg2indt->id); } @@ -1079,14 +1063,13 @@ public function test_get_subwiki_files_no_files() { /** * Test get_subwiki_files, check that a student can't get files from another group's subwiki when * using separate groups. - * - * @expectedException moodle_exception */ public function test_get_subwiki_files_separate_groups_student_see_other_group() { // Create testing data. $this->create_collaborative_wikis_with_groups(); $this->setUser($this->student); + $this->expectException(moodle_exception::class); mod_wiki_external::get_subwiki_files($this->wikisep->id, $this->group2->id); } diff --git a/mod/workshop/allocation/random/tests/allocator_test.php b/mod/workshop/allocation/random/tests/allocator_test.php index f04fc98580ef2..ebbb2e58391ce 100644 --- a/mod/workshop/allocation/random/tests/allocator_test.php +++ b/mod/workshop/allocation/random/tests/allocator_test.php @@ -114,9 +114,6 @@ public function test_index_submissions_by_authors() { ), $submissions); } - /** - * @expectedException moodle_exception - */ public function test_index_submissions_by_authors_duplicate_author() { // fixture setup $submissions = array( @@ -124,6 +121,7 @@ public function test_index_submissions_by_authors_duplicate_author() { 87 => (object)array('id' => 121, 'authorid' => 3), ); // exercise SUT + $this->expectException(moodle_exception::class); $submissions = $this->allocator->index_submissions_by_authors($submissions); } diff --git a/mod/workshop/form/accumulative/tests/lib_test.php b/mod/workshop/form/accumulative/tests/lib_test.php index 42b23e13852ee..c5bd1748482a3 100644 --- a/mod/workshop/form/accumulative/tests/lib_test.php +++ b/mod/workshop/form/accumulative/tests/lib_test.php @@ -78,14 +78,12 @@ public function test_calculate_peer_grade_one_numerical() { $this->assertEquals(grade_floatval(5/20 * 100), $suggested); } - /** - * @expectedException coding_exception - */ public function test_calculate_peer_grade_negative_weight() { // fixture set-up $this->strategy->dimensions[1003] = (object)array('grade' => '20', 'weight' => '-1'); $grades[] = (object)array('dimensionid' => 1003, 'grade' => '20'); // exercise SUT + $this->expectException(coding_exception::class); $suggested = $this->strategy->calculate_peer_grade($grades); } @@ -178,9 +176,6 @@ public function test_calculate_peer_grade_two_scales_weighted() { $this->assertEquals(grade_floatval((1/2*2 + 4/6*3)/5 * 100), $suggested); } - /** - * @expectedException coding_exception - */ public function test_calculate_peer_grade_scale_exception() { $this->resetAfterTest(true); // fixture set-up @@ -189,6 +184,7 @@ public function test_calculate_peer_grade_scale_exception() { $grades[] = (object)array('dimensionid' => 1012, 'grade' => '4.00000'); // exceeds the number of scale items // Exercise SUT. + $this->expectException(coding_exception::class); $suggested = $this->strategy->calculate_peer_grade($grades); } } diff --git a/mod/workshop/tests/locallib_test.php b/mod/workshop/tests/locallib_test.php index 0e12a86c73e75..bc53127eddbb8 100644 --- a/mod/workshop/tests/locallib_test.php +++ b/mod/workshop/tests/locallib_test.php @@ -320,9 +320,6 @@ public function test_percent_to_value() { $this->assertEquals($part, $total * $percent / 100); } - /** - * @expectedException coding_exception - */ public function test_percent_to_value_negative() { $this->resetAfterTest(true); // fixture setup @@ -330,12 +327,10 @@ public function test_percent_to_value_negative() { $percent = -7.098; // exercise SUT + $this->expectException(coding_exception::class); $part = workshop::percent_to_value($percent, $total); } - /** - * @expectedException coding_exception - */ public function test_percent_to_value_over_hundred() { $this->resetAfterTest(true); // fixture setup @@ -343,6 +338,7 @@ public function test_percent_to_value_over_hundred() { $percent = 121.08; // exercise SUT + $this->expectException(coding_exception::class); $part = workshop::percent_to_value($percent, $total); } diff --git a/question/behaviour/missing/tests/missingbehaviour_test.php b/question/behaviour/missing/tests/missingbehaviour_test.php index 243bd1c8ab996..bdfb6786d1901 100644 --- a/question/behaviour/missing/tests/missingbehaviour_test.php +++ b/question/behaviour/missing/tests/missingbehaviour_test.php @@ -40,39 +40,31 @@ */ class qbehaviour_missing_test extends advanced_testcase { - /** - * @expectedException moodle_exception - */ public function test_missing_cannot_start() { $qa = new question_attempt(test_question_maker::make_question('truefalse', 'true'), 0); $behaviour = new qbehaviour_missing($qa, 'deferredfeedback'); + $this->expectException(moodle_exception::class); $behaviour->init_first_step(new question_attempt_step(array()), 1); } - /** - * @expectedException moodle_exception - */ public function test_missing_cannot_process() { $qa = new question_attempt(test_question_maker::make_question('truefalse', 'true'), 0); $behaviour = new qbehaviour_missing($qa, 'deferredfeedback'); + $this->expectException(moodle_exception::class); $behaviour->process_action(new question_attempt_pending_step(array())); } - /** - * @expectedException moodle_exception - */ public function test_missing_cannot_get_min_fraction() { $qa = new question_attempt(test_question_maker::make_question('truefalse', 'true'), 0); $behaviour = new qbehaviour_missing($qa, 'deferredfeedback'); + $this->expectException(moodle_exception::class); $behaviour->get_min_fraction(); } - /** - * @expectedException moodle_exception - */ public function test_missing_cannot_get_max_fraction() { $qa = new question_attempt(test_question_maker::make_question('truefalse', 'true'), 0); $behaviour = new qbehaviour_missing($qa, 'deferredfeedback'); + $this->expectException(moodle_exception::class); $behaviour->get_max_fraction(); } diff --git a/question/engine/tests/questionattempt_with_steps_test.php b/question/engine/tests/questionattempt_with_steps_test.php index 7849943591f1e..9627758baa553 100644 --- a/question/engine/tests/questionattempt_with_steps_test.php +++ b/question/engine/tests/questionattempt_with_steps_test.php @@ -57,10 +57,8 @@ protected function tearDown(): void { $this->qa = null; } - /** - * @expectedException moodle_exception - */ public function test_get_step_before_start() { + $this->expectException(moodle_exception::class); $step = $this->qa->get_step(-1); } @@ -74,10 +72,8 @@ public function test_get_step_at_end() { $this->assertEquals(2, $step->get_qt_var('i')); } - /** - * @expectedException moodle_exception - */ public function test_get_step_past_end() { + $this->expectException(moodle_exception::class); $step = $this->qa->get_step(3); } diff --git a/question/engine/tests/questionattemptiterator_test.php b/question/engine/tests/questionattemptiterator_test.php index 5453567e3b1cb..d4ea62b9252a8 100644 --- a/question/engine/tests/questionattemptiterator_test.php +++ b/question/engine/tests/questionattemptiterator_test.php @@ -87,10 +87,8 @@ public function test_offsetExists_past_end() { $this->assertFalse(isset($this->iterator[3])); } - /** - * @expectedException moodle_exception - */ public function test_offsetGet_before_start() { + $this->expectException(moodle_exception::class); $step = $this->iterator[0]; } @@ -102,24 +100,18 @@ public function test_offsetGet_at_end() { $this->assertSame($this->qas[2], $this->iterator[2]); } - /** - * @expectedException moodle_exception - */ public function test_offsetGet_past_end() { + $this->expectException(moodle_exception::class); $step = $this->iterator[3]; } - /** - * @expectedException moodle_exception - */ public function test_cannot_set() { + $this->expectException(moodle_exception::class); $this->iterator[0] = null; } - /** - * @expectedException moodle_exception - */ public function test_cannot_unset() { + $this->expectException(moodle_exception::class); unset($this->iterator[2]); } } diff --git a/question/engine/tests/questionattemptstepiterator_test.php b/question/engine/tests/questionattemptstepiterator_test.php index 543282c6db307..03f84d4442e8a 100644 --- a/question/engine/tests/questionattemptstepiterator_test.php +++ b/question/engine/tests/questionattemptstepiterator_test.php @@ -104,10 +104,8 @@ public function test_offsetExists_past_end() { $this->assertFalse(isset($this->iterator[3])); } - /** - * @expectedException moodle_exception - */ public function test_offsetGet_before_start() { + $this->expectException(moodle_exception::class); $step = $this->iterator[-1]; } @@ -121,24 +119,18 @@ public function test_offsetGet_at_end() { $this->assertEquals(2, $step->get_qt_var('i')); } - /** - * @expectedException moodle_exception - */ public function test_offsetGet_past_end() { + $this->expectException(moodle_exception::class); $step = $this->iterator[3]; } - /** - * @expectedException moodle_exception - */ public function test_cannot_set() { + $this->expectException(moodle_exception::class); $this->iterator[0] = null; } - /** - * @expectedException moodle_exception - */ public function test_cannot_unset() { + $this->expectException(moodle_exception::class); unset($this->iterator[2]); } } diff --git a/question/engine/tests/questionengine_test.php b/question/engine/tests/questionengine_test.php index db1f72e25217f..d313f8571d4db 100644 --- a/question/engine/tests/questionengine_test.php +++ b/question/engine/tests/questionengine_test.php @@ -45,11 +45,9 @@ public function test_load_behaviour_class() { $this->assertTrue(class_exists('qbehaviour_deferredfeedback')); } - /** - * @expectedException moodle_exception - */ public function test_load_behaviour_class_missing() { // Exercise SUT + $this->expectException(moodle_exception::class); question_engine::load_behaviour_class('nonexistantbehaviour'); } diff --git a/question/engine/tests/questionutils_test.php b/question/engine/tests/questionutils_test.php index e60960164f42a..be65ddc73dda6 100644 --- a/question/engine/tests/questionutils_test.php +++ b/question/engine/tests/questionutils_test.php @@ -206,24 +206,18 @@ public function test_int_to_letter() { $this->assertEquals('Z', question_utils::int_to_letter(26)); } - /** - * @expectedException moodle_exception - */ public function test_int_to_roman_too_small() { + $this->expectException(moodle_exception::class); question_utils::int_to_roman(0); } - /** - * @expectedException moodle_exception - */ public function test_int_to_roman_too_big() { + $this->expectException(moodle_exception::class); question_utils::int_to_roman(4000); } - /** - * @expectedException moodle_exception - */ public function test_int_to_roman_not_int() { + $this->expectException(moodle_exception::class); question_utils::int_to_roman(1.5); } diff --git a/question/type/calculated/tests/variablesubstituter_test.php b/question/type/calculated/tests/variablesubstituter_test.php index a42d426808015..aff4ca7aaeb66 100644 --- a/question/type/calculated/tests/variablesubstituter_test.php +++ b/question/type/calculated/tests/variablesubstituter_test.php @@ -48,32 +48,23 @@ public function test_simple_expression_negatives() { $this->assertEquals(1, $vs->calculate('{a}-{b}')); } - /** - * @expectedException moodle_exception - */ public function test_cannot_use_nonnumbers() { + $this->expectException(moodle_exception::class); $vs = new qtype_calculated_variable_substituter(array('a' => 'frog', 'b' => -2), '.'); } - /** - * @expectedException moodle_exception - */ public function test_invalid_expression() { $vs = new qtype_calculated_variable_substituter(array('a' => 1, 'b' => 2), '.'); + $this->expectException(moodle_exception::class); $vs->calculate('{a} + {b}?'); } - /** - * @expectedException moodle_exception - */ public function test_tricky_invalid_expression() { $vs = new qtype_calculated_variable_substituter(array('a' => 1, 'b' => 2), '.'); + $this->expectException(moodle_exception::class); $vs->calculate('{a}{b}'); // Have to make sure this does not just evaluate to 12. } - /** - * @expectedException moodle_exception - */ public function test_division_by_zero_expression() { if (intval(PHP_VERSION) < 7) { @@ -81,6 +72,7 @@ public function test_division_by_zero_expression() { } $vs = new qtype_calculated_variable_substituter(array('a' => 1, 'b' => 0), '.'); + $this->expectException(moodle_exception::class); $vs->calculate('{a} / {b}'); } diff --git a/question/type/missingtype/tests/missingtype_test.php b/question/type/missingtype/tests/missingtype_test.php index 600dad278c6ff..026ab403ca22f 100644 --- a/question/type/missingtype/tests/missingtype_test.php +++ b/question/type/missingtype/tests/missingtype_test.php @@ -67,18 +67,14 @@ protected function get_unknown_questiondata() { return $questiondata; } - /** - * @expectedException moodle_exception - */ public function test_cannot_grade() { $q = new qtype_missingtype_question(); + $this->expectException(moodle_exception::class); $q->grade_response(array()); } - /** - * @expectedException moodle_exception - */ public function test_load_qtype_strict() { + $this->expectException(moodle_exception::class); $qtype = question_bank::get_qtype('strange_unknown'); } diff --git a/user/tests/myprofile_test.php b/user/tests/myprofile_test.php index 6d94bc7a19a26..5315c3e21cd6c 100644 --- a/user/tests/myprofile_test.php +++ b/user/tests/myprofile_test.php @@ -88,9 +88,6 @@ public function test_category__construct() { $this->assertSame('class1 class2', $category->classes); } - /** - * @expectedException coding_exception - */ public function test_validate_after_order1() { $category = new \phpunit_fixture_myprofile_category('category', 'title', null); @@ -103,13 +100,11 @@ public function test_validate_after_order1() { $category->add_node($node2); $category->add_node($node1); + $this->expectException(coding_exception::class); $category->validate_after_order(); } - /** - * @expectedException coding_exception - */ public function test_validate_after_order2() { $category = new \phpunit_fixture_myprofile_category('category', 'title', null); @@ -122,6 +117,7 @@ public function test_validate_after_order2() { $category->add_node($node2); $category->add_node($node1); + $this->expectException(coding_exception::class); $category->validate_after_order(); } @@ -171,8 +167,6 @@ public function test_find_nodes_after() { /** * Test category::sort_nodes(). - * - * @expectedException coding_exception */ public function test_sort_nodes1() { $category = new \phpunit_fixture_myprofile_category('category', 'title', null); @@ -219,6 +213,7 @@ public function test_sort_nodes1() { // Add a node with invalid 'after' and make sure an exception is thrown. $node7 = new \core_user\output\myprofile\node('category', 'node7', 'nodetitle', 'noderandom'); $category->add_node($node7); + $this->expectException(coding_exception::class); $category->sort_nodes(); } @@ -264,8 +259,6 @@ public function test_sort_nodes2() { /** * Test tree::add_node(). - * - * @expectedException coding_exception */ public function test_tree_add_node() { $tree = new \phpunit_fixture_myprofile_tree(); @@ -276,13 +269,12 @@ public function test_tree_add_node() { $this->assertEquals($node1, $node); // Can't add node with same name. + $this->expectException(coding_exception::class); $tree->add_node($node1); } /** * Test tree::add_category(). - * - * @expectedException coding_exception */ public function test_tree_add_category() { $tree = new \phpunit_fixture_myprofile_tree(); @@ -293,6 +285,7 @@ public function test_tree_add_category() { $this->assertEquals($category1, $category); // Can't add node with same name. + $this->expectException(coding_exception::class); $tree->add_category($category1); } @@ -341,8 +334,6 @@ public function test_find_categories_after() { /** * Test tree::sort_categories(). - * - * @expectedException coding_exception */ public function test_sort_categories() { $tree = new \phpunit_fixture_myprofile_tree('category', 'title', null); @@ -381,6 +372,7 @@ public function test_sort_categories() { $this->assertEquals($category6, $category); // Can't add category with same name. + $this->expectException(coding_exception::class); $tree->add_category($category1); } }