diff --git a/classes/assess_type.php b/classes/assess_type.php index e144c6f..622e1f1 100644 --- a/classes/assess_type.php +++ b/classes/assess_type.php @@ -74,7 +74,7 @@ public static function get_type_int(int $cmid): ?int { * @param int $cmid */ public static function get_type_name(int $cmid): ?string { - if ($typeint = self::get_type_int($cmid)) { + if (($typeint = self::get_type_int($cmid)) !== null) { switch ($typeint) { case self::ASSESS_TYPE_FORMATIVE: return get_string('formative', 'local_assess_type'); diff --git a/tests/assess_type_test.php b/tests/assess_type_test.php new file mode 100644 index 0000000..49adef0 --- /dev/null +++ b/tests/assess_type_test.php @@ -0,0 +1,70 @@ +. + +/** + * PHPUnit tests for local_assess_type. + * + * @package local_assess_type + * @copyright 2024 onwards University College London {@link https://www.ucl.ac.uk/} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @author Leon Stringer + */ + +namespace local_assess_type; + +class assess_type_test extends \advanced_testcase { + /** + * Test strings returned by assess_type::get_type_name() are the expected + * values. + * @dataProvider assess_type_strings + * @param int $assess_type ASSESS_TYPE_FORMATIVE, ASSESS_TYPE_SUMMATIVE, + * etc. + * @param string $name "Formative", "Summative", etc. + */ + public final function test_get_type_name(int $assess_type, string $name) { + $this->resetAfterTest(true); + + // Make a course. + $course = $this->getDataGenerator()->create_course(); + + // Make a quiz and add it to the course. + $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz'); + $quiz = $quizgenerator->create_instance(['course' => $course->id, 'questionsperpage' => 0, + 'grade' => 100.0, 'sumgrades' => 2, 'preferredbehaviour' => 'immediatefeedback']); + + $cm = get_coursemodule_from_instance('quiz', $quiz->id, $course->id); + assess_type::update_type($course->id, $assess_type, $cm->id); + $this->assertEquals(assess_type::get_type_name($cm->id), $name); + + } + + public final static function assess_type_strings(): array { + return [ + 'formative' => [ + 'assess_type' => assess_type::ASSESS_TYPE_FORMATIVE, + 'name' => get_string('formative', 'local_assess_type') + ], + 'summative' => [ + 'assess_type' => assess_type::ASSESS_TYPE_SUMMATIVE, + 'name' => get_string('summative', 'local_assess_type') + ], + 'dummy' => [ + 'assess_type' => assess_type::ASSESS_TYPE_DUMMY, + 'name' => get_string('dummy', 'local_assess_type') + ], + ]; + } +}