test_webservices.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Set of unit tests for the web services
  5. *
  6. * @author Guillaume Viguier <guillaume.viguier@beeznest.com>
  7. * @package chamilo.tests
  8. */
  9. /**
  10. * Init
  11. */
  12. ini_set('soap.wsdl_cache_enabled', 0);
  13. require_once(dirname(__FILE__).'/../main/inc/global.inc.php');
  14. require_once(dirname(__FILE__).'/simpletest/autorun.php');
  15. /**
  16. * @package chamilo.tests
  17. */
  18. class TestSoapWebService extends UnitTestCase {
  19. protected $_secret_key;
  20. protected $_encrypt_method;
  21. protected $_client;
  22. public function __construct() {
  23. $configuration = $GLOBALS['_configuration'];
  24. $security_key = $configuration['security_key'];
  25. $ip_address = '::1';
  26. $this->_secret_key = sha1($ip_address.$security_key);
  27. $this->_encrypt_method = $configuration['password_encryption'];
  28. $this->_client = new SoapClient($configuration['root_web'].'main/webservices/soap.php?wsdl');
  29. }
  30. protected function getUserArray() {
  31. $user = array(
  32. 'firstname' => 'Guillaume',
  33. 'lastname' => 'Viguier',
  34. 'status' => 5,
  35. 'loginname' => 'guillaumev',
  36. 'password' => 'guillaume',
  37. 'encrypt_method' => '',
  38. 'user_id_field_name' => 'chamilo_user_id',
  39. 'user_id_field_value' => '',
  40. 'visibility' => 1,
  41. 'email' => 'guillaume.viguier@beeznest.com',
  42. 'language' => 'english',
  43. 'phone' => '123456',
  44. 'expiration_date' => '0000-00-00 00:00:00',
  45. 'extras' => array());
  46. return $user;
  47. }
  48. protected function getCourseArray() {
  49. $course = array(
  50. 'title' => 'My test course',
  51. 'category_code' => 'LANG',
  52. 'wanted_code' => '110',
  53. 'tutor_name' => 'Guillaume Viguier',
  54. 'course_admin_user_id_field_name' => 'chamilo_user_id',
  55. 'course_admin_user_id_value' => '1',
  56. 'language' => 'spanish',
  57. 'course_id_field_name' => 'chamilo_course_id',
  58. 'course_id_value' => '',
  59. 'extras' => array());
  60. return $course;
  61. }
  62. protected function getSessionArray() {
  63. $end_date = date('Y') + 1;
  64. $end_date .= '-'.date('m-d');
  65. $session = array(
  66. 'name' => 'My session',
  67. 'start_date' => date('Y-m-d'),
  68. 'end_date' => $end_date,
  69. 'nb_days_access_before' => 0,
  70. 'nb_days_access_after' => 0,
  71. 'nolimit' => 0,
  72. 'visibility' => 1,
  73. 'user_id_field_name' => 'chamilo_user_id',
  74. 'user_id_value' => '1',
  75. 'session_id_field_name' => 'chamilo_session_id',
  76. 'session_id_value' => '',
  77. 'extras' => array());
  78. return $session;
  79. }
  80. protected function soapCall($method, $arguments) {
  81. return $this->_client->__soapCall($method, $arguments);
  82. }
  83. protected function createUser() {
  84. $user = $this->getUserArray();
  85. $result = $this->soapCall('WSUser.CreateUser', array_merge(array('secret_key' => $this->_secret_key), $user));
  86. return $result;
  87. }
  88. protected function createCourse() {
  89. $course = $this->getCourseArray();
  90. $result = $this->soapCall('WSCourse.CreateCourse', array_merge(array('secret_key' => $this->_secret_key), $course));
  91. return $result;
  92. }
  93. public function testTest() {
  94. $result = $this->soapCall('WS.test', array());
  95. $this->assertEqual($result, "success");
  96. }
  97. public function testInvalidKey() {
  98. $secret_key = 'invalid';
  99. try {
  100. $this->soapCall('WSUser.DisableUser', array('secret_key' => $secret_key, 'user_id_field_name' => 'chamilo_user_id', 'user_id_value' => 3));
  101. $this->fail('Exception was expected');
  102. } catch(SOAPFault $f) {
  103. $this->pass();
  104. }
  105. }
  106. public function testCreateUser() {
  107. $user = $this->getUserArray();
  108. $result = $this->soapCall('WSUser.CreateUser', array_merge(array('secret_key' => $this->_secret_key), $user));
  109. $this->assertIsA($result, 'int');
  110. //Delete user created
  111. $this->soapCall('WSUser.DeleteUser', array('secret_key' => $this->_secret_key, 'user_id_field_name' => 'chamilo_user_id', 'user_id_value' => $result));
  112. }
  113. public function testCreateUserEncrypted() {
  114. $user = $this->getUserArray();
  115. $user['encrypt_method'] = $this->_encrypt_method;
  116. if($this->_encrypt_method == 'md5') {
  117. $user['password'] = md5('guillaume');
  118. } else if($this->_encrypt_method == 'sha1') {
  119. $user['password'] = sha1('guillaume');
  120. }
  121. $user['extras'] = array(array('field_name' => 'salt', 'field_value' => '1234'));
  122. $result = $this->soapCall('WSUser.CreateUser', array_merge(array('secret_key' => $this->_secret_key), $user));
  123. $this->assertIsA($result, 'int');
  124. //Delete user created
  125. $this->soapCall('WSUser.DeleteUser', array('secret_key' => $this->_secret_key, 'user_id_field_name' => 'chamilo_user_id', 'user_id_value' => $result));
  126. }
  127. public function testCourseCreation() {
  128. $course = $this->getCourseArray();
  129. $result = $this->soapCall('WSCourse.CreateCourse', array_merge(array('secret_key' => $this->_secret_key), $course));
  130. $this->assertIsA($result, 'int');
  131. // Delete course created
  132. $this->soapCall('WSCourse.DeleteCourse', array('secret_key' => $this->_secret_key, 'course_id_field_name' => 'chamilo_course_id', 'course_id_value' => $result));
  133. }
  134. /*public function testCourseSubscriptionAndUnsubscription() {
  135. //$course_id = $this->createCourse();
  136. //$user_id = $this->createUser();
  137. //echo $course_id.';'.$user_id;
  138. //$this->soapCall('WSCourse.SubscribeUserToCourse', array('secret_key' => $this->_secret_key, 'course_id_field_name' => 'chamilo_course_id', 'course_id_value' => 8, 'user_id_field_name' => 'chamilo_user_id', 'user_id_value' => 38, 'status' => 1));
  139. //$this->soapCall('WSCourse.UnsubscribeUserFromCourse', array('secret_key' => $this->_secret_key, 'course_id_field_name' => 'chamilo_course_id', 'course_id_value' => 8, 'user_id_field_name' => 'chamilo_user_id', 'user_id_value' => 38));
  140. }*/
  141. /*public function testCourseDescriptions() {
  142. //$this->soapCall('WSCourse.EditCourseDescription', array('secret_key' => $this->_secret_key, 'course_id_field_name' => 'chamilo_course_id', 'course_id_value' => 8, 'course_desc_id' => 1, 'course_desc_title' => 'My description', 'course_desc_content' => 'This is my new description'));
  143. //$result = $this->soapCall('WSCourse.GetCourseDescriptions', array('secret_key' => $this->_secret_key, 'course_id_field_name' => 'chamilo_course_id', 'course_id_value' => 8));
  144. //var_dump($result);
  145. $result = $this->soapCall('WSCourse.ListCourses', array('secret_key' => $this->_secret_key, 'course_id_field_name' => 'chamilo_course_id'));
  146. var_dump($result);
  147. }*/
  148. public function testSessionCreation() {
  149. $session = $this->getSessionArray();
  150. $result = $this->soapCall('WSSession.CreateSession', array_merge(array('secret_key' => $this->_secret_key), $session));
  151. $this->assertIsA($result, 'int');
  152. $this->soapCall('WSSession.DeleteSession', array('secret_key' => $this->_secret_key, 'session_id_field_name' => 'chamilo_session_id', 'session_id_value' => $result));
  153. }
  154. /*public function testUserSessionSubscriptionAndUnsubscription() {
  155. $this->soapCall('WSSession.UnsubscribeCourseFromSession', array('secret_key' => $this->_secret_key, 'course_id_field_name' => 'chamilo_course_id', 'course_id_value' => 8, 'session_id_field_name' => 'chamilo_session_id', 'session_id_value' => 3));
  156. //$this->soapCall('WSSession.UnsubscribeUserFromSession', array('secret_key' => $this->_secret_key, 'user_id_field_name' => 'chamilo_user_id', 'user_id_value' => 38, 'session_id_field_name' => 'chamilo_session_id', 'session_id_value' => 3));
  157. }*/
  158. }