From 13fb78a4f64fc9cf2ee7a7cd94eb14f2ff47f660 Mon Sep 17 00:00:00 2001 From: Damyon Wiese Date: Wed, 1 Feb 2017 14:45:53 +0800 Subject: [PATCH] MDL-57841 exporter: Allow scalars as related objects --- lib/classes/external/exporter.php | 6 +++++- lib/tests/exporter_test.php | 21 ++++++++++++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/classes/external/exporter.php b/lib/classes/external/exporter.php index 9539e543a1e..a80d6a48efc 100644 --- a/lib/classes/external/exporter.php +++ b/lib/classes/external/exporter.php @@ -96,7 +96,11 @@ abstract class exporter { } } else { - if (array_key_exists($key, $related) && $related[$key] instanceof $classname) { + $scalartypes = ['string', 'int', 'bool', 'float']; + $scalarcheck = 'is_' . $classname; + if (array_key_exists($key, $related) && + ((in_array($classname, $scalartypes) && $scalarcheck($related[$key])) || + ($related[$key] instanceof $classname))) { $this->related[$key] = $related[$key]; } else { throw new coding_exception($missingdataerr . $key . ' => ' . $classname); diff --git a/lib/tests/exporter_test.php b/lib/tests/exporter_test.php index 3c126075d74..6d5b7756658 100644 --- a/lib/tests/exporter_test.php +++ b/lib/tests/exporter_test.php @@ -41,8 +41,22 @@ class core_exporter_testcase extends advanced_testcase { public function setUp() { $s = new stdClass(); - $this->validrelated = array('simplestdClass' => $s, 'arrayofstdClass' => array($s, $s), 'context' => null); - $this->invalidrelated = array('simplestdClass' => 'a string', 'arrayofstdClass' => 5, 'context' => null); + $this->validrelated = array( + 'simplestdClass' => $s, + 'arrayofstdClass' => array($s, $s), + 'context' => null, + 'aint' => 5, + 'astring' => 'valid string', + 'abool' => false + ); + $this->invalidrelated = array( + 'simplestdClass' => 'a string', + 'arrayofstdClass' => 5, + 'context' => null, + 'aint' => false, + 'astring' => 4, + 'abool' => 'not a boolean' + ); $this->validdata = array('stringA' => 'A string', 'stringAformat' => FORMAT_HTML, 'intB' => 4); @@ -166,7 +180,8 @@ class core_testable_exporter extends \core\external\exporter { protected static function define_related() { // We cache the context so it does not need to be retrieved from the course. - return array('simplestdClass' => 'stdClass', 'arrayofstdClass' => 'stdClass[]', 'context' => 'context?'); + return array('simplestdClass' => 'stdClass', 'arrayofstdClass' => 'stdClass[]', 'context' => 'context?', + 'astring' => 'string', 'abool' => 'bool', 'aint' => 'int'); } protected function get_other_values(renderer_base $output) {