database.lib.test.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 testGetCourseTableFromCode() {
  134. $course_code='AYDD';
  135. $table='course';
  136. $ret = NULL;
  137. $res=$this->dbase->get_course_table_from_code($course_code, $table);
  138. $this->assertTrue(is_string($res));
  139. }
  140. function testGetCourseTablePrefix() {
  141. global $_configuration;
  142. $res=$this->dbase->get_course_table_prefix($_configuration);
  143. $this->assertTrue(is_string($res));
  144. }
  145. function testGetCurrentCourseDatabase() {
  146. $res=$this->dbase->get_current_course_database();
  147. if (empty($GLOBALS['_course']['dbName'])) {
  148. $this->assertFalse($res);
  149. } else {
  150. $this->assertTrue(is_string($res));
  151. }
  152. $res=$this->dbase->get_current_course_database('___');
  153. $this->assertFalse($res);
  154. }
  155. function testGetCurrentCourseGluedDatabase() {
  156. global $_course;
  157. $res=$this->dbase->get_current_course_glued_database();
  158. if (empty($_course['dbNameGlu'])) {
  159. $this->assertFalse($res);
  160. } else {
  161. $this->assertTrue(is_string($res));
  162. }
  163. }
  164. function testGetDatabaseGlue() {
  165. global $_configuration;
  166. $res=$this->dbase->get_database_glue($_configuration);
  167. $this->assertTrue(is_string($res));
  168. }
  169. function testGetDatabaseNamePrefix() {
  170. global $_configuration;
  171. $res=$this->dbase->get_database_name_prefix($_configuration);
  172. $this->assertTrue(is_string($res));
  173. }
  174. function testGetMainDatabase() {
  175. global $_configuration;
  176. $res=$this->dbase->get_main_database();
  177. $this->assertTrue(is_string($res));
  178. }
  179. function testGetMainTable() {
  180. $short_table_name='';
  181. $res=$this->dbase->get_main_table($short_table_name);
  182. $this->assertTrue(is_string($res));
  183. }
  184. function testGetScormDatabase() {
  185. global $_configuration;
  186. $res=$this->dbase->get_scorm_database();
  187. $this->assertTrue(is_string($res));
  188. }
  189. function testGetScorm_table() {
  190. $short_table_name='';
  191. $res=$this->dbase->get_scorm_table($short_table_name);
  192. $this->assertTrue(is_string($res));
  193. $this->assertTrue($res);
  194. }
  195. function testGetStatisticDatabase() {
  196. global $_configuration;
  197. $res=$this->dbase->get_statistic_database($_configuration);
  198. $this->assertTrue(is_string($res));
  199. $this->assertTrue($res);
  200. }
  201. function testGetStatisticTable() {
  202. $short_table_name='';
  203. $res=$this->dbase->get_statistic_table($short_table_name);
  204. $this->assertTrue(is_string($res));
  205. $this->assertTrue($res);
  206. }
  207. function testGetUserInfoFromIdOneIsArray() {
  208. $res=$this->dbase->get_user_info_from_id(1);
  209. $this->assertTrue(is_array($res));
  210. }
  211. /* Fails for some reason on automated tests server
  212. function testGetUserInfoFromIdNullIsFalse() {
  213. // should be returning GLOBALS[_user] (=null) if param is null (in testing context)
  214. $res=$this->dbase->get_user_info_from_id(null);
  215. $this->assertFalse($res);
  216. }
  217. */
  218. function testGetUserInfoFromIdHighValueIsArray() {
  219. // should be returning array with empty values if user doesn't exist
  220. $res=$this->dbase->get_user_info_from_id(5000000);
  221. $this->assertTrue(is_array($res));
  222. }
  223. function testGetUserPersonalDatabase() {
  224. global $_configuration;
  225. $res=$this->dbase->get_user_personal_database($_configuration);
  226. $this->assertTrue(is_string($res));
  227. $this->assertTrue($res);
  228. }
  229. function testGetUserPersonalTable(){
  230. $short_table_name='';
  231. $res=$this->dbase-> get_user_personal_table($short_table_name);
  232. $this->assertTrue(is_string($res));
  233. $this->assertTrue($res);
  234. }
  235. /* // Contains a private unaccessible method, Database::glue_course_database_name().
  236. function testGlueCourseDatabaseName() {
  237. $database_name='';
  238. $res=$this->dbase->glue_course_database_name($database_name);
  239. $this->assertTrue(is_string($res));
  240. $this->assertTrue($res);
  241. }
  242. */
  243. function testInsertId() {
  244. $res = $this->dbase->insert_id();
  245. $this->assertTrue(is_numeric($res));
  246. }
  247. function testNumRows() {
  248. $sql = 'SELECT * FROM user';
  249. $res = Database::query($sql);
  250. $resul=Database::num_rows($res);
  251. $this->assertTrue(is_numeric($resul));
  252. }
  253. function testQuery() {
  254. $sql = 'SELECT 1';
  255. $res = Database::query($sql);
  256. $this->assertTrue(is_resource($res));
  257. }
  258. function testResult() {
  259. $sql = 'SELECT email FROM user';
  260. $resource = Database::query($sql);
  261. $res = Database::result($resource, 1);
  262. $this->assertTrue(is_string($res));
  263. }
  264. function testStoreResult(){
  265. $sql = 'SELECT 1';
  266. $resource = $this->dbase->query($sql);
  267. $res = $this->dbase->store_result($resource);
  268. $this->assertTrue(is_array($res));
  269. }
  270. }
  271. ?>