test_webservices.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * Set of unit tests for the web services
  4. *
  5. * @author Guillaume Viguier <guillaume.viguier@beeznest.com>
  6. */
  7. ini_set('soap.wsdl_cache_enabled', 0);
  8. require_once(dirname(__FILE__).'/../main/inc/global.inc.php');
  9. require_once(dirname(__FILE__).'/simpletest/autorun.php');
  10. class TestSoapWebService extends UnitTestCase {
  11. protected $_secret_key;
  12. protected $_encrypt_method;
  13. protected $_client;
  14. public function __construct() {
  15. $configuration = $GLOBALS['_configuration'];
  16. $security_key = $configuration['security_key'];
  17. $ip_address = '::1';
  18. $this->_secret_key = sha1($ip_address.$security_key);
  19. $this->_encrypt_method = $_GLOBALS['userPasswordCrypted'];
  20. $this->_client = new SoapClient($configuration['root_web'].'main/webservices/soap.php?wsdl');
  21. }
  22. protected function getUserArray() {
  23. $user = array(
  24. 'firstname' => 'Guillaume',
  25. 'lastname' => 'Viguier',
  26. 'status' => 5,
  27. 'loginname' => 'guillaumev',
  28. 'password' => 'guillaume',
  29. 'encrypt_method' => '',
  30. 'user_id_field_name' => 'chamilo_user_id',
  31. 'user_id_field_value' => '',
  32. 'visibility' => 1,
  33. 'email' => 'guillaume.viguier@beeznest.com',
  34. 'language' => 'english',
  35. 'phone' => '123456',
  36. 'expiration_date' => '0000-00-00 00:00:00',
  37. 'extras' => array());
  38. return $user;
  39. }
  40. protected function soapCall($method, $arguments) {
  41. return $this->_client->__soapCall($method, $arguments);
  42. }
  43. public function testTest() {
  44. $result = $this->soapCall('WS.test', array());
  45. $this->assertEqual($result, "success");
  46. }
  47. public function testInvalidKey() {
  48. $secret_key = 'invalid';
  49. try {
  50. $this->soapCall('WS.DisableUser', array('secret_key' => $secret_key, 'user_id_field_name' => 'chamilo_user_id', 'user_id_value' => 3));
  51. $this->fail('Exception was expected');
  52. } catch(SOAPFault $f) {
  53. $this->pass();
  54. }
  55. }
  56. public function testCreateUser() {
  57. $user = $this->getUserArray();
  58. $result = $this->soapCall('WS.CreateUser', array_merge(array('secret_key' => $this->_secret_key), $user));
  59. $this->assertIsA($result, 'int');
  60. //Delete user created
  61. $this->soapCall('WS.DeleteUser', array('secret_key' => $this->_secret_key, 'user_id_field_name' => 'chamilo_user_id', 'user_id_value' => $result));
  62. }
  63. public function testCreateUserEncrypted() {
  64. $user = $this->getUserArray();
  65. $user['encrypt_method'] = $this->_encrypt_method;
  66. if($this->_encrypt_method == 'md5') {
  67. $user['password'] = md5('guillaume');
  68. } else if($this->_encrypt_method == 'sha1') {
  69. $user['password'] = sha1('guillaume');
  70. }
  71. $user['extras'] = array(array('field_name' => 'salt', 'field_value' => '1234'));
  72. $result = $this->soapCall('WS.CreateUser', array_merge(array('secret_key' => $this->_secret_key), $user));
  73. $this->assertIsA($result, 'int');
  74. //Delete user created
  75. $this->soapCall('WS.DeleteUser', array('secret_key' => $this->_secret_key, 'user_id_field_name' => 'chamilo_user_id', 'user_id_value' => $result));
  76. }
  77. }