From de2b0565a72c0a2a2ae11c784e7b6f0daf6b891d Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Mon, 4 Oct 2021 21:48:25 +0100 Subject: [PATCH] MDL-71086 core: load only those properties defined within persistent. --- lib/classes/persistent.php | 3 ++- lib/tests/persistent_test.php | 17 ++++++++++++++++- lib/upgrade.txt | 1 + 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/classes/persistent.php b/lib/classes/persistent.php index e7b09a2f299..85de57777b9 100644 --- a/lib/classes/persistent.php +++ b/lib/classes/persistent.php @@ -403,7 +403,8 @@ abstract class persistent { * @return static */ final public function from_record(stdClass $record) { - $record = (array) $record; + $properties = static::properties_definition(); + $record = array_intersect_key((array) $record, $properties); foreach ($record as $property => $value) { $this->raw_set($property, $value); } diff --git a/lib/tests/persistent_test.php b/lib/tests/persistent_test.php index 66bdffef994..82601bae41f 100644 --- a/lib/tests/persistent_test.php +++ b/lib/tests/persistent_test.php @@ -207,11 +207,26 @@ class core_persistent_testcase extends advanced_testcase { public function test_from_record_invalid_param() { $p = new core_testable_persistent(); $data = (object) array( + 'shortname' => 'ddd', + 'idnumber' => 'abc', + 'description' => 'xyz', + 'descriptionformat' => FORMAT_PLAIN, + 'parentid' => 999, + 'path' => '/a/b/c', + 'sortorder' => 12, + 'id' => 1, + 'timecreated' => 2, + 'timemodified' => 3, + 'usermodified' => 4, + 'scaleid' => null, 'invalidparam' => 'abc' ); - $this->expectException(coding_exception::class); $p->from_record($data); + + // Previous call should succeed, assert we get back all data except invalid param. + unset($data->invalidparam); + $this->assertEquals($data, $p->to_record()); } public function test_validate() { diff --git a/lib/upgrade.txt b/lib/upgrade.txt index 38f10134294..16603e39b96 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -103,6 +103,7 @@ completely removed from Moodle core too. fixed units), to always include a non-breaking space between the number and unit, and to use consistent rounding (always 1 decimal place by default). * The persistent method get() now returns the correct type for each property defined in the persistent class. +* The persistent method from_record() now only attempts to load record properties defined in the persistent class. * Require pass grade criteria is now part of core. Refer to upgrade.php to see transitioning from similar plugin criteria to core Refer to completion/upgrade.txt for additional information.