Correct decoding of JSON response

The old version assumed `json_decode` would return an associative array.
While it can do so if an extra option is specified, the default
behaviour is to return an object. Therefore, a successful password
change resulted in an error while parsing the response.

The new code is accessing the response as object instead. The method
is now covered by a unit test.
pull/5554/head
Maikel Linke 8 years ago
parent f351b48771
commit dd29ca1ee8

@ -70,13 +70,17 @@ class rcube_cpanel_webmail_password
*/
public static function decode_response($response)
{
if (!$response) {
return PASSWORD_CONNECT_ERROR;
}
$result = json_decode($response);
if ($result['status'] === 1) {
if ($result->status === 1) {
return PASSWORD_SUCCESS;
}
$errors = $result['errors'];
$errors = $result->errors;
if (is_array($errors) && count($errors) > 0) {
return array(
'code' => PASSWORD_ERROR,

@ -19,5 +19,28 @@ class Password_Plugin extends PHPUnit_Framework_TestCase
$this->assertInstanceOf('password', $plugin);
$this->assertInstanceOf('rcube_plugin', $plugin);
}
}
function test_driver_cpanel_webmail()
{
$driver = 'cpanel_webmail';
include_once __DIR__ . "/../drivers/$driver.php";
$driver_class = "rcube_${driver}_password";
$this->assertTrue(class_exists($driver_class));
$json_response_fail = '{"data":null,"errors":'
. '["Execution of Email::passwdpop (api version:3) is not '
. 'permitted inside of webmail"],"status":0,"metadata":{},'
. '"messages":null}';
$result = $driver_class::decode_response($json_response_fail);
$this->assertTrue(is_array($result));
$this->assertEquals($result['code'], PASSWORD_ERROR);
$expected_message = 'Execution of Email::passwdpop (api version:3) is'
. ' not permitted inside of webmail';
$this->assertEquals($result['message'], $expected_message);
$json_response_success = '{"metadata":{},"data":null,"messages":null,'
. '"errors":null,"status":1}';
$result = $driver_class::decode_response($json_response_success);
$this->assertEquals($result, PASSWORD_SUCCESS);
}
}

Loading…
Cancel
Save