From cc21a73a4af96692c7ab83954698e71274061132 Mon Sep 17 00:00:00 2001 From: Stephan Robotta Date: Wed, 23 Oct 2024 10:55:57 +0200 Subject: [PATCH] Add test for the string to id conversion and make sure that strings do not change. --- .../model/localized_string_type_test.php | 134 ++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 tests/repository/model/localized_string_type_test.php diff --git a/tests/repository/model/localized_string_type_test.php b/tests/repository/model/localized_string_type_test.php new file mode 100644 index 0000000..effc99e --- /dev/null +++ b/tests/repository/model/localized_string_type_test.php @@ -0,0 +1,134 @@ +. + +/** + * Class for performing DB actions for the verbal feedback activity module. + * + * @package mod_verbalfeedback + * @copyright 2024 Stephan Robotta + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace mod_verbalfeedback; + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; + +use mod_verbalfeedback\repository\model\localized_string_type; + +/** + * Verbal feedback language repository test class + */ +final class localized_string_type_test extends \advanced_testcase { + + /** + * The string constants that are used in the verbal feedback module but are stored as ids in the database. + */ + private $stringConstants = [ + 'instance_criterion', + 'instance_category_header', + 'instance_subrating_title', + 'instance_subrating_description', + 'instance_subrating_verynegative', + 'instance_subrating_negative', + 'instance_subrating_positive', + 'instance_subrating_verypositive', + 'template_criterion', + 'template_category_header', + 'template_subrating_title', + 'template_subrating_description', + 'template_subrating_verynegative', + 'template_subrating_negative', + 'template_subrating_positive', + 'template_subrating_verypositive', + ]; + + /** + * Test that the contants are defined correctly. + */ + public function test_constants(): void { + $this->assertEquals('instance_criterion', localized_string_type::INSTANCE_CRITERION); + $this->assertEquals('instance_category_header', localized_string_type::INSTANCE_CATEGORY_HEADER); + $this->assertEquals('instance_subrating_title', localized_string_type::INSTANCE_SUBRATING_TITLE); + $this->assertEquals('instance_subrating_description', localized_string_type::INSTANCE_SUBRATING_DESCRIPTION); + $this->assertEquals('instance_subrating_verynegative', localized_string_type::INSTANCE_SUBRATING_VERY_NEGATIVE); + $this->assertEquals('instance_subrating_negative', localized_string_type::INSTANCE_SUBRATING_NEGATIVE); + $this->assertEquals('instance_subrating_positive', localized_string_type::INSTANCE_SUBRATING_POSITIVE); + $this->assertEquals('instance_subrating_verypositive', localized_string_type::INSTANCE_SUBRATING_VERY_POSITIVE); + $this->assertEquals('template_criterion', localized_string_type::TEMPLATE_CRITERION); + $this->assertEquals('template_category_header', localized_string_type::TEMPLATE_CATEGORY_HEADER); + $this->assertEquals('template_subrating_title', localized_string_type::TEMPLATE_SUBRATING_TITLE); + $this->assertEquals('template_subrating_description', localized_string_type::TEMPLATE_SUBRATING_DESCRIPTION); + $this->assertEquals('template_subrating_verynegative', localized_string_type::TEMPLATE_SUBRATING_VERY_NEGATIVE); + $this->assertEquals('template_subrating_negative', localized_string_type::TEMPLATE_SUBRATING_NEGATIVE); + $this->assertEquals('template_subrating_positive', localized_string_type::TEMPLATE_SUBRATING_POSITIVE); + $this->assertEquals('template_subrating_verypositive', localized_string_type::TEMPLATE_SUBRATING_VERY_POSITIVE); + } + + /** + * Test the string to id conversion. + * + * @covers \mod_verbalfeedback\repository\model\localized_string_type::str2id + */ + public function test_str2id(): void { + $i = 1; + foreach ($this->stringConstants as $string) { + $this->assertEquals($i, localized_string_type::str2id($string)); + $i++; + } + } + + /** + * Test the id to string conversion. + * + * @covers \mod_verbalfeedback\repository\model\localized_string_type::id2str + */ + public function test_id2str(): void { + $i = 1; + foreach ($this->stringConstants as $string) { + $this->assertEquals($string, localized_string_type::id2str($i)); + $i++; + } + $this->expectException(\InvalidArgumentException::class); + localized_string_type::id2str(17); + $this->expectException(\InvalidArgumentException::class); + localized_string_type::id2str(0); + } + + /** + * Test the getStringTypes method. + * + * @covers \mod_verbalfeedback\repository\model\localized_string_type::getStringTypes + */ + public function test_getStringTypes(): void { + $this->assertEquals($this->stringConstants, localized_string_type::getStringTypes()); + } + + /** + * Test the exists method. + * + * @covers \mod_verbalfeedback\repository\model\localized_string_type::exists + */ + public function test_exists(): void { + foreach ($this->stringConstants as $string) { + $this->assertTrue(localized_string_type::exists($string)); + $this->assertFalse(localized_string_type::exists(strtoupper($string))); + $this->assertFalse(localized_string_type::exists(ucfirst($string))); + } + $this->assertFalse(localized_string_type::exists('nonexisting')); + } +}