MDL-70891 core: fix the lang_string::__set_state()

The function must be static and actually has to do what it is supposed to. It also breaks PHP 8.0
This commit is contained in:
Marina Glancy 2021-02-15 13:32:16 +01:00
parent af0040445a
commit 19d1a2ee61
2 changed files with 40 additions and 3 deletions

View file

@ -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;
}
/**

View file

@ -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 = <<<EOF
lang_string::__set_state(array(
'identifier' => '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.