Password.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. class Password_Plugin extends PHPUnit_Framework_TestCase
  3. {
  4. function setUp()
  5. {
  6. include_once __DIR__ . '/../password.php';
  7. }
  8. /**
  9. * Plugin object construction test
  10. */
  11. function test_constructor()
  12. {
  13. $rcube = rcube::get_instance();
  14. $plugin = new password($rcube->api);
  15. $this->assertInstanceOf('password', $plugin);
  16. $this->assertInstanceOf('rcube_plugin', $plugin);
  17. }
  18. /**
  19. * cpanel_webmail driver test
  20. */
  21. function test_driver_cpanel_webmail()
  22. {
  23. $driver_class = $this->load_driver('cpanel_webmail');
  24. $error_result = $driver_class::decode_response(false);
  25. $this->assertEquals($error_result, PASSWORD_CONNECT_ERROR);
  26. $bad_result = $driver_class::decode_response(null);
  27. $this->assertEquals($bad_result, PASSWORD_CONNECT_ERROR);
  28. $null_result = $driver_class::decode_response('null');
  29. $this->assertEquals($null_result, PASSWORD_ERROR);
  30. $malformed_result = $driver_class::decode_response('random {string]!');
  31. $this->assertEquals($malformed_result, PASSWORD_ERROR);
  32. $other_result = $driver_class::decode_response('{"a":"b"}');
  33. $this->assertEquals($other_result, PASSWORD_ERROR);
  34. $fail_response = '{"data":null,"errors":["Execution of Email::passwdp'
  35. . 'op (api version:3) is not permitted inside of webmail"],"sta'
  36. . 'tus":0,"metadata":{},"messages":null}';
  37. $error_message = 'Execution of Email::passwdpop (api version:3) is no'
  38. . 't permitted inside of webmail';
  39. $expected_result = array(
  40. 'code' => PASSWORD_ERROR,
  41. 'message' => $error_message
  42. );
  43. $fail_result = $driver_class::decode_response($fail_response);
  44. $this->assertEquals($expected_result, $fail_result);
  45. $success_response = '{"metadata":{},"data":null,"messages":null,"errors'
  46. . '":null,"status":1}';
  47. $good_result = $driver_class::decode_response($success_response);
  48. $this->assertEquals($good_result, PASSWORD_SUCCESS);
  49. }
  50. /**
  51. * Loads a driver's source file, checks that its class exist and returns the
  52. * driver's class name.
  53. *
  54. * @param string $driver driver name, example: "chpasswd"
  55. * @return string driver's class name, example: "rcube_chpasswd_password"
  56. */
  57. function load_driver($driver)
  58. {
  59. include_once __DIR__ . "/../drivers/$driver.php";
  60. $driver_class = "rcube_${driver}_password";
  61. $this->assertTrue(class_exists($driver_class));
  62. return $driver_class;
  63. }
  64. }