diff --git a/lib/moodlelib.php b/lib/moodlelib.php index e9947ad1bad..269a435d42f 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -10685,10 +10685,14 @@ class lang_string { /** * Magic __set_state method used for var_export * - * @return string + * @param array $array + * @return self */ - public function __set_state() { - return $this->get_string(); + public static function __set_state(array $array): self { + $tmp = new lang_string($array['identifier'], $array['component'], $array['a'], $array['lang']); + $tmp->string = $array['string']; + $tmp->forcedstring = $array['forcedstring']; + return $tmp; } /** diff --git a/lib/tests/moodlelib_test.php b/lib/tests/moodlelib_test.php index 504128ca514..3d36659f5c3 100644 --- a/lib/tests/moodlelib_test.php +++ b/lib/tests/moodlelib_test.php @@ -2225,6 +2225,39 @@ class core_moodlelib_testcase extends advanced_testcase { $COURSE->lang = $originallang; } + public function test_lang_string_var_export() { + + // Call var_export() on a newly generated lang_string. + $str = new lang_string('no'); + + $expected1 = << 'no', + 'component' => 'moodle', + 'a' => NULL, + 'lang' => NULL, + 'string' => NULL, + 'forcedstring' => false, +)) +EOF; + + $v = var_export($str, true); + $this->assertEquals($expected1, $v); + + // Now execute the code that was returned - it should produce a correct string. + $str = lang_string::__set_state(array( + 'identifier' => 'no', + 'component' => 'moodle', + 'a' => NULL, + 'lang' => NULL, + 'string' => NULL, + 'forcedstring' => false, + )); + + $this->assertInstanceOf(lang_string::class, $str); + $this->assertEquals('No', $str); + } + public function test_get_string_limitation() { // This is one of the limitations to the lang_string class. It can't be // used as a key.