Merge branch 'MDL-37355-master' of git://github.com/mouneyrac/moodle

This commit is contained in:
Dan Poltawski 2013-01-15 13:11:46 +08:00
commit cc1521f57b
2 changed files with 69 additions and 2 deletions

View File

@ -266,10 +266,16 @@ class external_api {
}
} else if ($description instanceof external_single_structure) {
if (!is_array($response)) {
throw new invalid_response_exception('Only arrays accepted. The bad value is: \'' .
if (!is_array($response) && !is_object($response)) {
throw new invalid_response_exception('Only arrays/objects accepted. The bad value is: \'' .
print_r($response, true) . '\'');
}
// Cast objects into arrays.
if (is_object($response)) {
$response = (array) $response;
}
$result = array();
foreach ($description->keys as $key=>$subdesc) {
if (!array_key_exists($key, $response)) {

View File

@ -74,4 +74,65 @@ class externallib_testcase extends basic_testcase {
$this->assertTrue($result['someid'] === 6);
$this->assertTrue($result['text'] === 'aaa');
}
/**
* Test for clean_returnvalue().
*/
public function test_clean_returnvalue() {
// Build some return value decription.
$returndesc = new external_multiple_structure(
new external_single_structure(
array(
'object' => new external_single_structure(
array('value1' => new external_value(PARAM_INT, 'this is a int'))),
'value2' => new external_value(PARAM_TEXT, 'some text', VALUE_OPTIONAL))
));
// Clean an object (it should be cast into an array).
$object = new stdClass();
$object->value1 = 1;
$singlestructure['object'] = $object;
$singlestructure['value2'] = 'Some text';
$testdata = array($singlestructure);
$cleanedvalue = external_api::clean_returnvalue($returndesc, $testdata);
$cleanedsinglestructure = array_pop($cleanedvalue);
$this->assertEquals($object->value1, $cleanedsinglestructure['object']['value1']);
$this->assertEquals($singlestructure['value2'], $cleanedsinglestructure['value2']);
// Missing VALUE_OPTIONAL.
$object = new stdClass();
$object->value1 = 1;
$singlestructure = new stdClass();
$singlestructure->object = $object;
$testdata = array($singlestructure);
$cleanedvalue = external_api::clean_returnvalue($returndesc, $testdata);
$cleanedsinglestructure = array_pop($cleanedvalue);
$this->assertEquals($object->value1, $cleanedsinglestructure['object']['value1']);
$this->assertEquals(false, array_key_exists('value2', $cleanedsinglestructure));
// Unknown attribut (the value should be ignored).
$object = array();
$object['value1'] = 1;
$singlestructure = array();
$singlestructure['object'] = $object;
$singlestructure['value2'] = 'Some text';
$singlestructure['unknownvalue'] = 'Some text to ignore';
$testdata = array($singlestructure);
$cleanedvalue = external_api::clean_returnvalue($returndesc, $testdata);
$cleanedsinglestructure = array_pop($cleanedvalue);
$this->assertEquals($object['value1'], $cleanedsinglestructure['object']['value1']);
$this->assertEquals($singlestructure['value2'], $cleanedsinglestructure['value2']);
$this->assertEquals(false, array_key_exists('unknownvalue', $cleanedsinglestructure));
// Missing required value (an exception is thrown).
$object = array();
$singlestructure = array();
$singlestructure['object'] = $object;
$singlestructure['value2'] = 'Some text';
$testdata = array($singlestructure);
$this->setExpectedException('invalid_response_exception');
$cleanedvalue = external_api::clean_returnvalue($returndesc, $testdata);
}
}