MDL-67353 course: Fix broken unit test

This commit is contained in:
Andrew Nicols 2019-11-10 20:53:25 +08:00 committed by Eloy Lafuente (stronk7)
parent 4e41ace0be
commit 1d44ac9ec6
2 changed files with 23 additions and 10 deletions

View File

@ -68,6 +68,17 @@ class course_handler extends \core_customfield\handler {
return self::$singleton;
}
/**
* 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.
*

View File

@ -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 = [