From 1d44ac9ec622ad70c5a4077ad5e432a236885f54 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Sun, 10 Nov 2019 20:53:25 +0800 Subject: [PATCH] MDL-67353 course: Fix broken unit test --- course/classes/customfield/course_handler.php | 11 +++++++++++ customfield/tests/api_test.php | 22 ++++++++++++---------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/course/classes/customfield/course_handler.php b/course/classes/customfield/course_handler.php index d7badc85bf7..5deeb4737ff 100644 --- a/course/classes/customfield/course_handler.php +++ b/course/classes/customfield/course_handler.php @@ -69,6 +69,17 @@ class course_handler extends \core_customfield\handler { } /** + * Run reset code after unit tests to reset the singleton usage. + */ + public static function reset_after_test(): void { + if (!PHPUNIT_TEST) { + throw new \coding_exception('This feature is only intended for use in unit tests'); + } + + static::$singleton = null; + } + + /** * The current user can configure custom fields on this component. * * @return bool true if the current can configure custom fields, false otherwise diff --git a/customfield/tests/api_test.php b/customfield/tests/api_test.php index 2aa96e1883b..4f8c3c26dbc 100644 --- a/customfield/tests/api_test.php +++ b/customfield/tests/api_test.php @@ -40,16 +40,8 @@ class core_customfield_api_testcase extends advanced_testcase { /** * This method is called after the last test of this test class is run. */ - public static function tearDownAfterClass() { - $handler = core_course\customfield\course_handler::create(); - $handler->delete_all(); - } - - /** - * Tests set up. - */ - public function setUp() { - $this->resetAfterTest(); + public function tearDown() { + core_course\customfield\course_handler::reset_after_test(); } /** @@ -80,6 +72,8 @@ class core_customfield_api_testcase extends advanced_testcase { * in the interface using drag-drop. */ public function test_move_category() { + $this->resetAfterTest(); + // Create the categories. $params = ['component' => 'core_course', 'area' => 'course', 'itemid' => 0]; $id0 = $this->get_generator()->create_category($params)->get('id'); @@ -129,6 +123,8 @@ class core_customfield_api_testcase extends advanced_testcase { * Tests for \core_customfield\api::get_categories_with_fields() behaviour. */ public function test_get_categories_with_fields() { + $this->resetAfterTest(); + // Create the categories. $options = [ 'component' => 'core_course', @@ -163,6 +159,8 @@ class core_customfield_api_testcase extends advanced_testcase { * Test for functions api::save_category() and rename_category) */ public function test_save_category() { + $this->resetAfterTest(); + $params = ['component' => 'core_course', 'area' => 'course', 'itemid' => 0, 'name' => 'Cat1', 'contextid' => context_system::instance()->id]; $c1 = category_controller::create(0, (object)$params); @@ -196,6 +194,8 @@ class core_customfield_api_testcase extends advanced_testcase { * Test for function handler::create_category */ public function test_create_category() { + $this->resetAfterTest(); + $handler = \core_course\customfield\course_handler::create(); $c1id = $handler->create_category(); $c1 = $handler->get_categories_with_fields()[$c1id]; @@ -218,6 +218,8 @@ class core_customfield_api_testcase extends advanced_testcase { * Tests for \core_customfield\api::delete_category() behaviour. */ public function test_delete_category_with_fields() { + $this->resetAfterTest(); + global $DB; // Create two categories with fields and data. $options = [ -- 2.11.4.GIT