diff --git a/plugins/password/drivers/cpanel_webmail.php b/plugins/password/drivers/cpanel_webmail.php index 77fa0409f..6671c13ed 100644 --- a/plugins/password/drivers/cpanel_webmail.php +++ b/plugins/password/drivers/cpanel_webmail.php @@ -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, diff --git a/plugins/password/tests/Password.php b/plugins/password/tests/Password.php index b64c6b889..690213d89 100644 --- a/plugins/password/tests/Password.php +++ b/plugins/password/tests/Password.php @@ -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); + } +}