MDL-81919 customfield: Rewrite poor test assertion

This commit is contained in:
Andrew Nicols
2024-06-10 12:13:00 +08:00
parent 0abb2e6b9a
commit a128312ccb
3 changed files with 63 additions and 60 deletions
+18 -20
View File
@@ -25,9 +25,9 @@ use core_customfield_generator;
* @category test
* @copyright 2018 Toni Barbera <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \core_customfield\category_controller
*/
class category_controller_test extends \advanced_testcase {
final class category_controller_test extends \advanced_testcase {
/**
* Get generator.
*
@@ -40,7 +40,7 @@ class category_controller_test extends \advanced_testcase {
/**
* Test for the field_controller::__construct function.
*/
public function test_constructor() {
public function test_constructor(): void {
$this->resetAfterTest();
$c = category_controller::create(0, (object)['component' => 'core_course', 'area' => 'course', 'itemid' => 0]);
@@ -64,7 +64,7 @@ class category_controller_test extends \advanced_testcase {
/**
* Test for function \core_customfield\field_controller::create() in case of wrong parameters
*/
public function test_constructor_errors() {
public function test_constructor_errors(): void {
global $DB;
$this->resetAfterTest();
@@ -75,8 +75,10 @@ class category_controller_test extends \advanced_testcase {
$c = category_controller::create($catrecord->id, $catrecord);
$debugging = $this->getDebuggingMessages();
$this->assertEquals(1, count($debugging));
$this->assertEquals('Too many parameters, either id need to be specified or a record, but not both.',
$debugging[0]->message);
$this->assertEquals(
'Too many parameters, either id need to be specified or a record, but not both.',
$debugging[0]->message
);
$this->resetDebugging();
$this->assertTrue($c instanceof category_controller);
@@ -86,7 +88,6 @@ class category_controller_test extends \advanced_testcase {
$this->fail('Expected exception');
} catch (\moodle_exception $e) {
$this->assertEquals('Category not found', $e->getMessage());
$this->assertEquals(\moodle_exception::class, get_class($e));
}
// Missing required elements.
@@ -96,7 +97,6 @@ class category_controller_test extends \advanced_testcase {
} catch (\coding_exception $e) {
$this->assertEquals('Coding error detected, it must be fixed by a programmer: Not enough parameters ' .
'to initialise category_controller - unknown component', $e->getMessage());
$this->assertEquals(\coding_exception::class, get_class($e));
}
// Missing required elements.
@@ -106,7 +106,6 @@ class category_controller_test extends \advanced_testcase {
} catch (\coding_exception $e) {
$this->assertEquals('Coding error detected, it must be fixed by a programmer: Not enough parameters ' .
'to initialise category_controller - unknown area', $e->getMessage());
$this->assertEquals(\coding_exception::class, get_class($e));
}
// Missing required elements.
@@ -116,7 +115,6 @@ class category_controller_test extends \advanced_testcase {
} catch (\coding_exception $e) {
$this->assertEquals('Coding error detected, it must be fixed by a programmer: Not enough parameters ' .
'to initialise category_controller - unknown itemid', $e->getMessage());
$this->assertEquals(\coding_exception::class, get_class($e));
}
$handler = \core_course\customfield\course_handler::create();
@@ -127,7 +125,6 @@ class category_controller_test extends \advanced_testcase {
} catch (\coding_exception $e) {
$this->assertEquals('Coding error detected, it must be fixed by a programmer: Component of the handler ' .
'does not match the one from the record', $e->getMessage());
$this->assertEquals(\coding_exception::class, get_class($e));
}
try {
@@ -136,7 +133,6 @@ class category_controller_test extends \advanced_testcase {
} catch (\coding_exception $e) {
$this->assertEquals('Coding error detected, it must be fixed by a programmer: Area of the handler ' .
'does not match the one from the record', $e->getMessage());
$this->assertEquals(\coding_exception::class, get_class($e));
}
try {
@@ -145,18 +141,20 @@ class category_controller_test extends \advanced_testcase {
} catch (\coding_exception $e) {
$this->assertEquals('Coding error detected, it must be fixed by a programmer: Itemid of the ' .
'handler does not match the one from the record', $e->getMessage());
$this->assertEquals(\coding_exception::class, get_class($e));
}
try {
$user = $this->getDataGenerator()->create_user();
category_controller::create(0, (object)['component' => 'core_course', 'area' => 'course', 'itemid' => 0,
'contextid' => \context_user::instance($user->id)->id], $handler);
category_controller::create(0, (object) [
'component' => 'core_course',
'area' => 'course',
'itemid' => 0,
'contextid' => \context_user::instance($user->id)->id,
], $handler);
$this->fail('Expected exception');
} catch (\coding_exception $e) {
$this->assertEquals('Coding error detected, it must be fixed by a programmer: Context of the ' .
'handler does not match the one from the record', $e->getMessage());
$this->assertEquals(\coding_exception::class, get_class($e));
}
}
@@ -165,7 +163,7 @@ class category_controller_test extends \advanced_testcase {
* \core_customfield\category_controller::save()
* \core_customfield\category_controller::get()
*/
public function test_create_category() {
public function test_create_category(): void {
$this->resetAfterTest();
// Create the category.
@@ -194,12 +192,12 @@ class category_controller_test extends \advanced_testcase {
/**
* Tests for \core_customfield\category_controller::set() behaviour.
*/
public function test_rename_category() {
public function test_rename_category(): void {
$this->resetAfterTest();
// Create the category.
$params = ['component' => 'core_course', 'area' => 'course', 'itemid' => 0, 'name' => 'Cat1',
'contextid' => \context_system::instance()->id];
'contextid' => \context_system::instance()->id, ];
$c1 = category_controller::create(0, (object)$params);
$c1->save();
$this->assertNotEmpty($c1->get('id'));
@@ -219,7 +217,7 @@ class category_controller_test extends \advanced_testcase {
/**
* Tests for \core_customfield\category_controller::delete() behaviour.
*/
public function test_delete_category() {
public function test_delete_category(): void {
$this->resetAfterTest();
// Create the category.
+23 -19
View File
@@ -30,9 +30,9 @@ use customfield_textarea;
* @category test
* @copyright 2018 Toni Barbera <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \core_customfield\data_controller
*/
class data_controller_test extends \advanced_testcase {
final class data_controller_test extends \advanced_testcase {
/**
* Get generator.
*
@@ -45,7 +45,7 @@ class data_controller_test extends \advanced_testcase {
/**
* Test for function data_controller::create()
*/
public function test_constructor() {
public function test_constructor(): void {
global $DB;
$this->resetAfterTest();
@@ -76,11 +76,16 @@ class data_controller_test extends \advanced_testcase {
// Generate new data_controller records for these fields, specifying field controller or fieldid or both.
$data0 = data_controller::create(0, (object)$params, $field0);
$this->assertInstanceOf(customfield_checkbox\data_controller::class, $data0);
$data1 = data_controller::create(0,
(object)($params + ['fieldid' => $field1->get('id')]), $field1);
$data1 = data_controller::create(
0,
(object)($params + ['fieldid' => $field1->get('id')]),
$field1
);
$this->assertInstanceOf(customfield_date\data_controller::class, $data1);
$data2 = data_controller::create(0,
(object)($params + ['fieldid' => $field2->get('id')]));
$data2 = data_controller::create(
0,
(object)($params + ['fieldid' => $field2->get('id')])
);
$this->assertInstanceOf(customfield_select\data_controller::class, $data2);
$data3 = data_controller::create(0, (object)$params, $field3);
$this->assertInstanceOf(customfield_text\data_controller::class, $data3);
@@ -99,8 +104,10 @@ class data_controller_test extends \advanced_testcase {
$this->assertInstanceOf(customfield_date\data_controller::class, data_controller::create($data1->get('id')));
// Retrieve data by id and field.
$this->assertInstanceOf(customfield_select\data_controller::class,
data_controller::create($data2->get('id'), null, $field2));
$this->assertInstanceOf(
customfield_select\data_controller::class,
data_controller::create($data2->get('id'), null, $field2)
);
// Retrieve data by record without field.
$datarecord = $DB->get_record(\core_customfield\data::TABLE, ['id' => $data3->get('id')], '*', MUST_EXIST);
@@ -109,13 +116,12 @@ class data_controller_test extends \advanced_testcase {
// Retrieve data by record with field.
$datarecord = $DB->get_record(\core_customfield\data::TABLE, ['id' => $data4->get('id')], '*', MUST_EXIST);
$this->assertInstanceOf(customfield_textarea\data_controller::class, data_controller::create(0, $datarecord, $field4));
}
/**
* Test for function \core_customfield\field_controller::create() in case of wrong parameters
*/
public function test_constructor_errors() {
public function test_constructor_errors(): void {
global $DB;
$this->resetAfterTest();
@@ -124,7 +130,7 @@ class data_controller_test extends \advanced_testcase {
$field = $this->get_generator()->create_field(['categoryid' => $category->get('id')]);
$course = $this->getDataGenerator()->create_course();
$data = data_controller::create(0, (object)['instanceid' => $course->id,
'contextid' => \context_course::instance($course->id)->id], $field);
'contextid' => \context_course::instance($course->id)->id, ], $field);
$data->save();
$datarecord = $DB->get_record(\core_customfield\data::TABLE, ['id' => $data->get('id')], '*', MUST_EXIST);
@@ -133,9 +139,11 @@ class data_controller_test extends \advanced_testcase {
$d = data_controller::create($datarecord->id, $datarecord);
$debugging = $this->getDebuggingMessages();
$this->assertEquals(1, count($debugging));
$this->assertEquals('Too many parameters, either id need to be specified or a record, but not both.',
$debugging[0]->message);
$this->resetDebugging();
$this->assertEquals(
'Too many parameters, either id need to be specified or a record, but not both.',
$debugging[0]->message
);
$this->resetDebugging();
$this->assertInstanceOf(customfield_text\data_controller::class, $d);
// Retrieve non-existing data.
@@ -144,7 +152,6 @@ class data_controller_test extends \advanced_testcase {
$this->fail('Expected exception');
} catch (\dml_missing_record_exception $e) {
$this->assertStringMatchesFormat('Can\'t find data record in database table customfield_data%a', $e->getMessage());
$this->assertEquals(\dml_missing_record_exception::class, get_class($e));
}
// Missing field id.
@@ -154,7 +161,6 @@ class data_controller_test extends \advanced_testcase {
} catch (\coding_exception $e) {
$this->assertEquals('Coding error detected, it must be fixed by a programmer: Not enough parameters to ' .
'initialise data_controller - unknown field', $e->getMessage());
$this->assertEquals(\coding_exception::class, get_class($e));
}
// Mismatching field id.
@@ -164,7 +170,6 @@ class data_controller_test extends \advanced_testcase {
} catch (\coding_exception $e) {
$this->assertEquals('Coding error detected, it must be fixed by a programmer: Field id from the record ' .
'does not match field from the parameter', $e->getMessage());
$this->assertEquals(\coding_exception::class, get_class($e));
}
// Nonexisting class.
@@ -174,7 +179,6 @@ class data_controller_test extends \advanced_testcase {
$this->fail('Expected exception');
} catch (\moodle_exception $e) {
$this->assertEquals('Field type invalid not found', $e->getMessage());
$this->assertEquals(\moodle_exception::class, get_class($e));
}
}
}
+22 -21
View File
@@ -30,9 +30,9 @@ use customfield_textarea;
* @category test
* @copyright 2018 Ruslan Kabalin
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \core_customfield\field_controller
*/
class field_controller_test extends \advanced_testcase {
final class field_controller_test extends \advanced_testcase {
/**
* Get generator.
*
@@ -45,7 +45,7 @@ class field_controller_test extends \advanced_testcase {
/**
* Test for function \core_customfield\field_controller::create()
*/
public function test_constructor() {
public function test_constructor(): void {
global $DB;
$this->resetAfterTest();
@@ -76,8 +76,10 @@ class field_controller_test extends \advanced_testcase {
$this->assertInstanceOf(customfield_date\field_controller::class, field_controller::create($field1->get('id')));
// Retrieve field by id and category.
$this->assertInstanceOf(customfield_select\field_controller::class,
field_controller::create($field2->get('id'), null, $category0));
$this->assertInstanceOf(
customfield_select\field_controller::class,
field_controller::create($field2->get('id'), null, $category0)
);
// Retrieve fields by record without category.
$fieldrecord = $DB->get_record(\core_customfield\field::TABLE, ['id' => $field3->get('id')], '*', MUST_EXIST);
@@ -85,14 +87,16 @@ class field_controller_test extends \advanced_testcase {
// Retrieve fields by record with category.
$fieldrecord = $DB->get_record(\core_customfield\field::TABLE, ['id' => $field4->get('id')], '*', MUST_EXIST);
$this->assertInstanceOf(customfield_textarea\field_controller::class,
field_controller::create(0, $fieldrecord, $category0));
$this->assertInstanceOf(
customfield_textarea\field_controller::class,
field_controller::create(0, $fieldrecord, $category0)
);
}
/**
* Test for function \core_customfield\field_controller::create() in case of wrong parameters
*/
public function test_constructor_errors() {
public function test_constructor_errors(): void {
global $DB;
$this->resetAfterTest();
@@ -106,8 +110,10 @@ class field_controller_test extends \advanced_testcase {
$field = field_controller::create($fieldrecord->id, $fieldrecord);
$debugging = $this->getDebuggingMessages();
$this->assertEquals(1, count($debugging));
$this->assertEquals('Too many parameters, either id need to be specified or a record, but not both.',
$debugging[0]->message);
$this->assertEquals(
'Too many parameters, either id need to be specified or a record, but not both.',
$debugging[0]->message
);
$this->resetDebugging();
$this->assertInstanceOf(customfield_text\field_controller::class, $field);
@@ -117,7 +123,6 @@ class field_controller_test extends \advanced_testcase {
$this->fail('Expected exception');
} catch (\moodle_exception $e) {
$this->assertEquals('Field not found', $e->getMessage());
$this->assertEquals(\moodle_exception::class, get_class($e));
}
// Retrieve without id and without type.
@@ -127,7 +132,6 @@ class field_controller_test extends \advanced_testcase {
} catch (\coding_exception $e) {
$this->assertEquals('Coding error detected, it must be fixed by a programmer: Not enough parameters to ' .
'initialise field_controller - unknown field type', $e->getMessage());
$this->assertEquals(\coding_exception::class, get_class($e));
}
// Missing category id.
@@ -137,7 +141,6 @@ class field_controller_test extends \advanced_testcase {
} catch (\coding_exception $e) {
$this->assertEquals('Coding error detected, it must be fixed by a programmer: Not enough parameters ' .
'to initialise field_controller - unknown category', $e->getMessage());
$this->assertEquals(\coding_exception::class, get_class($e));
}
// Mismatching category id.
@@ -147,7 +150,6 @@ class field_controller_test extends \advanced_testcase {
} catch (\coding_exception $e) {
$this->assertEquals('Coding error detected, it must be fixed by a programmer: Category of the field ' .
'does not match category from the parameter', $e->getMessage());
$this->assertEquals(\coding_exception::class, get_class($e));
}
// Non-existing type.
@@ -156,7 +158,6 @@ class field_controller_test extends \advanced_testcase {
$this->fail('Expected exception');
} catch (\moodle_exception $e) {
$this->assertEquals('Field type nonexisting not found', $e->getMessage());
$this->assertEquals(\moodle_exception::class, get_class($e));
}
}
@@ -166,7 +167,7 @@ class field_controller_test extends \advanced_testcase {
* \core_customfield\field_controller::get()
* \core_customfield\field_controller::get_category()
*/
public function test_create_field() {
public function test_create_field(): void {
global $DB;
$this->resetAfterTest();
@@ -196,7 +197,7 @@ class field_controller_test extends \advanced_testcase {
/**
* Tests for \core_customfield\field_controller::delete() behaviour.
*/
public function test_delete_field() {
public function test_delete_field(): void {
global $DB;
$this->resetAfterTest();
@@ -206,8 +207,8 @@ class field_controller_test extends \advanced_testcase {
$this->assertCount(0, $fields);
// Create field using generator.
$field1 = $lpg->create_field(array('categoryid' => $category->get('id')));
$field2 = $lpg->create_field(array('categoryid' => $category->get('id')));
$field1 = $lpg->create_field(['categoryid' => $category->get('id')]);
$field2 = $lpg->create_field(['categoryid' => $category->get('id')]);
$fields = $DB->get_records(\core_customfield\field::TABLE, ['categoryid' => $category->get('id')]);
$this->assertCount(2, $fields);
@@ -225,14 +226,14 @@ class field_controller_test extends \advanced_testcase {
/**
* Tests for \core_customfield\field_controller::get_configdata_property() behaviour.
*/
public function test_get_configdata_property() {
public function test_get_configdata_property(): void {
$this->resetAfterTest();
$lpg = $this->get_generator();
$category = $lpg->create_category();
$configdata = ['a' => 'b', 'c' => ['d', 'e']];
$field = field_controller::create(0, (object)['type' => 'text',
'configdata' => json_encode($configdata), 'shortname' => 'a', 'name' => 'a'], $category);
'configdata' => json_encode($configdata), 'shortname' => 'a', 'name' => 'a', ], $category);
$field->save();
// Retrieve field and check configdata.