database.lib.test.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. require_once(api_get_path(LIBRARY_PATH).'database.lib.php');
  3. require_once(api_get_path(LIBRARY_PATH).'add_course.lib.inc.php');
  4. require_once(api_get_path(LIBRARY_PATH).'course.lib.php');
  5. class TestDatabase extends UnitTestCase {
  6. public $dbase;
  7. public function TestDatabase() {
  8. $this->UnitTestCase('Database library - main/inc/lib/database.lib.test.php');
  9. }
  10. public function setUp() {
  11. global $_configuration;
  12. $this->dbase = new Database();
  13. }
  14. public function tearDown() {
  15. $this->dbase = null;
  16. }
  17. public function testAffectedRows() {
  18. $res=$this->dbase->affected_rows();
  19. $this->assertTrue(is_numeric($res));
  20. }
  21. public function testCountRows() {
  22. $table='class';
  23. $res=$this->dbase->count_rows($table);
  24. if(!is_string($res)){
  25. $this->assertTrue(is_numeric($res));
  26. }
  27. }
  28. public function testError() {
  29. $res=$this->dbase->error();
  30. $this->assertTrue(is_string($res));
  31. }
  32. public function testEscapeString() {
  33. $string='Lore"May';
  34. $res=$this->dbase->escape_string($string);
  35. $this->assertTrue(is_string($res));
  36. }
  37. public function testFetchArray() {
  38. $sql = 'select 1';
  39. $res=Database::query($sql);
  40. $resu=$this->dbase->fetch_array($res);
  41. $this->assertTrue(is_array($resu));
  42. $this->assertFalse(is_null($resu));
  43. }
  44. public function testFetchArrayError() {
  45. $sql = 'SELECT 1';
  46. $res=Database::query($sql);
  47. $resu=$this->dbase->fetch_array($res);
  48. $this->assertTrue(is_array($resu));
  49. }
  50. function testFetchObject() {
  51. $sql = 'SELECT 1';
  52. $res=Database::query($sql);
  53. $resu=$this->dbase->fetch_object($res);
  54. $this->assertTrue(is_object($resu));
  55. }
  56. function testFetchRow() {
  57. $sql = 'SELECT 1';
  58. $res=Database::query($sql);
  59. $resu=$this->dbase->fetch_row($res);
  60. $this->assertTrue(is_array($resu));
  61. }
  62. /* // Contains a private unaccessible method, Database::fix_database_parameter().
  63. function testFixDatabaseParameterEmpty() {
  64. $course_info = api_get_course_info();
  65. $database_name= $course_info[""];
  66. $res=$this->dbase->fix_database_parameter($database_name);
  67. if(!is_null($res)) :
  68. $this->assertTrue(is_string($res));
  69. endif;
  70. //var_dump($res);
  71. }
  72. */
  73. /* // Contains a private unaccessible method, Database::fix_database_parameter().
  74. function testFixDatabaseParameterReturnString() {
  75. $course_info = api_get_course_info();
  76. $database_name= $course_info["dbName"];
  77. $res=$this->dbase->fix_database_parameter($course_info);
  78. $this->assertTrue(is_string($res));
  79. }
  80. */
  81. /* // Contains a private unaccessible method, Database::format_glued_course_table_name().
  82. function testFormatGluedCourseTableName() {
  83. $database_name_with_glue='';
  84. $table='';
  85. $res=$this->dbase->format_glued_course_table_name($database_name_with_glue, $table);
  86. $this->assertTrue(is_string($res));
  87. }
  88. */
  89. /* // Contains a private unaccessible method, Database::format_table_name().
  90. function testFormatTableName() {
  91. $database='';
  92. $table='';
  93. $res=$this->dbase->format_table_name($database, $table);
  94. $this->assertTrue(is_string($res));
  95. }
  96. */
  97. function testGenerateAbstractCourseFieldNames() {
  98. $result_array='';
  99. $res=$this->dbase->generate_abstract_course_field_names($result_array);
  100. $this->assertTrue(is_array($res));
  101. }
  102. function testGenerateAbstractUserFieldNames() {
  103. $result_array='';
  104. $res=$this->dbase->generate_abstract_user_field_names($result_array);
  105. $this->assertTrue(is_array($res));
  106. }
  107. function get_course_by_category() {
  108. $category_id='1';
  109. $res=$this->dbase->get_course_by_category($category_id);
  110. $this->assertTrue(is_string($res));
  111. }
  112. function testGetCourseChatConnectedTable() {
  113. $database_name='dokeosla';
  114. $res=$this->dbase->get_course_chat_connected_table($database_name);
  115. $this->assertTrue(is_string($res));
  116. }
  117. function testGetCourseInfo() {
  118. $course_code='AYDD';
  119. $res=$this->dbase->get_course_info($course_code);
  120. $this->assertTrue(is_array($res));
  121. }
  122. function testGetCourseList() {
  123. $sql_query = "SELECT * FROM $table";
  124. $res=$this->dbase->get_course_list($sql_query);
  125. $this->assertTrue(is_array($res));
  126. }
  127. function testGetCourseTable() {
  128. $short_table_name='';
  129. $database_name='';
  130. $res=$this->dbase->get_course_table($short_table_name,$database_name);
  131. $this->assertTrue(is_string($res));
  132. }
  133. function testGetCourseTablePrefix() {
  134. global $_configuration;
  135. $res=$this->dbase->get_course_table_prefix($_configuration);
  136. $this->assertTrue(is_string($res));
  137. }
  138. function testGetCurrentCourseDatabase() {
  139. $res=$this->dbase->get_current_course_database();
  140. if (empty($GLOBALS['_course']['dbName'])) {
  141. $this->assertFalse($res);
  142. } else {
  143. $this->assertTrue(is_string($res));
  144. }
  145. $res=$this->dbase->get_current_course_database('___');
  146. $this->assertFalse($res);
  147. }
  148. function testGetCurrentCourseGluedDatabase() {
  149. global $_course;
  150. $res=$this->dbase->get_current_course_glued_database();
  151. if (empty($_course['dbNameGlu'])) {
  152. $this->assertFalse($res);
  153. } else {
  154. $this->assertTrue(is_string($res));
  155. }
  156. }
  157. function testGetDatabaseGlue() {
  158. global $_configuration;
  159. $res=$this->dbase->get_database_glue($_configuration);
  160. $this->assertTrue(is_string($res));
  161. }
  162. function testGetDatabaseNamePrefix() {
  163. global $_configuration;
  164. $res=$this->dbase->get_database_name_prefix($_configuration);
  165. $this->assertTrue(is_string($res));
  166. }
  167. function testGetMainDatabase() {
  168. global $_configuration;
  169. $res=$this->dbase->get_main_database();
  170. $this->assertTrue(is_string($res));
  171. }
  172. function testGetMainTable() {
  173. $short_table_name='';
  174. $res=$this->dbase->get_main_table($short_table_name);
  175. $this->assertTrue(is_string($res));
  176. }
  177. /* Fails for some reason on automated tests server
  178. function testGetUserInfoFromIdNullIsFalse() {
  179. // should be returning GLOBALS[_user] (=null) if param is null (in testing context)
  180. $res=$this->dbase->get_user_info_from_id(null);
  181. $this->assertFalse($res);
  182. }
  183. */
  184. function testGetUserInfoFromIdHighValueIsArray() {
  185. // should be returning array with empty values if user doesn't exist
  186. $res=$this->dbase->get_user_info_from_id(5000000);
  187. $this->assertTrue(is_array($res));
  188. }
  189. function testGetUserPersonalDatabase() {
  190. global $_configuration;
  191. $res=$this->dbase->get_user_personal_database($_configuration);
  192. $this->assertTrue(is_string($res));
  193. $this->assertTrue($res);
  194. }
  195. function testGetUserPersonalTable(){
  196. $short_table_name='';
  197. $res=$this->dbase-> get_user_personal_table($short_table_name);
  198. $this->assertTrue(is_string($res));
  199. $this->assertTrue($res);
  200. }
  201. /* // Contains a private unaccessible method, Database::glue_course_database_name().
  202. function testGlueCourseDatabaseName() {
  203. $database_name='';
  204. $res=$this->dbase->glue_course_database_name($database_name);
  205. $this->assertTrue(is_string($res));
  206. $this->assertTrue($res);
  207. }
  208. */
  209. function testInsertId() {
  210. $res = $this->dbase->insert_id();
  211. $this->assertTrue(is_numeric($res));
  212. }
  213. function testNumRows() {
  214. $sql = 'SELECT * FROM user';
  215. $res = Database::query($sql);
  216. $resul=Database::num_rows($res);
  217. $this->assertTrue(is_numeric($resul));
  218. }
  219. function testQuery() {
  220. $sql = 'SELECT 1';
  221. $res = Database::query($sql);
  222. $this->assertTrue(is_resource($res));
  223. }
  224. function testResult() {
  225. $sql = 'SELECT email FROM user';
  226. $resource = Database::query($sql);
  227. $res = Database::result($resource, 1);
  228. $this->assertTrue(is_string($res));
  229. }
  230. function testStoreResult(){
  231. $sql = 'SELECT 1';
  232. $resource = $this->dbase->query($sql);
  233. $res = $this->dbase->store_result($resource);
  234. $this->assertTrue(is_array($res));
  235. }
  236. }
  237. ?>