course.lib.test.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. <?php
  2. /*
  3. * To can run this test you need comment this line or "die(mysql_error())" in 1345 course.lib.php
  4. *
  5. */
  6. require_once(api_get_path(LIBRARY_PATH).'course.lib.php');
  7. require_once(api_get_path(LIBRARY_PATH).'mail.lib.inc.php');
  8. require_once(api_get_path(LIBRARY_PATH).'database.lib.php');
  9. require_once(api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  10. Mock::generate('Database');
  11. Mock::generate('CourseManager');
  12. Mock::generate('Display');
  13. class TestCourse extends UnitTestCase{
  14. public $tcourse;
  15. public function TestCourse(){
  16. $this->UnitTestCase('Courses library - main/inc/lib/course.lib.test.php');
  17. }
  18. public function setUp(){
  19. global $_configuration;
  20. $this->tcourse = new CourseManager();
  21. $course_datos = array(
  22. 'wanted_code'=> 'COURSE1',
  23. 'title'=>'COURSE1',
  24. 'tutor_name'=>'R. J. Wolfagan',
  25. 'category_code'=>'2121',
  26. 'course_language'=>'english',
  27. 'course_admin_id'=>'1211',
  28. 'db_prefix'=> $_configuration['db_prefix'],
  29. 'firstExpirationDelay'=>'112'
  30. );
  31. $res = create_course($course_datos['wanted_code'], $course_datos['title'],
  32. $course_datos['tutor_name'], $course_datos['category_code'],
  33. $course_datos['course_language'],$course_datos['course_admin_id'],
  34. $course_datos['db_prefix'], $course_datos['firstExpirationDelay']);
  35. }
  36. public function tearDown(){
  37. $this->tcourse = null;
  38. $this->dbase = null;
  39. $code = 'COURSE1';
  40. $res = CourseManager::delete_course($code);
  41. $path = api_get_path(SYS_PATH).'archive';
  42. if ($handle = opendir($path)) {
  43. while (false !== ($file = readdir($handle))) {
  44. if (strpos($file,$code)!==false) {
  45. if (is_dir($path.'/'.$file)) {
  46. rmdirr($path.'/'.$file);
  47. }
  48. }
  49. }
  50. closedir($handle);
  51. }
  52. }
  53. /*
  54. *todo public function testGetCourseInformation()
  55. *todo public function testGetCoursesList()
  56. *todo public function testGetAccessSettings()
  57. *todo public function testGetUserInCourseStatus()
  58. *todo public function testUnsubcribeUser()
  59. *todo public function testSubscribeUser()
  60. *todo public function testAddUserToCourse()
  61. *todo public function testGetRealCourseCodeSelectHtml()
  62. *todo public function testCheckParameter()
  63. *todo public function testCheckParameterOrFail()
  64. *todo public function testIsExistingCourseCode()
  65. *todo public function testGetRealCourseList()
  66. *todo public function testGetVirtualCourseList()
  67. *todo public function testGetRealCourseListOfUserAsCourseAdmin()
  68. *todo public function testGetCourseListOfUserAsCourseAdmin()
  69. *todo public function testDetermineCourseTitleFromCourseInfo()
  70. *todo public function testCreateCombinedName()
  71. *todo public function testCreateCombinedCode()
  72. *todo public function testGetVirtualCourseInfo()
  73. *todo public function testIsVirtualCourseFromSystemCode()
  74. *todo public function testIsVirtualCourseFromVisualCode()
  75. *todo public function testHasVirtualCoursesFromCode()
  76. *todo public function testGetVirtualCoursesLinkedToRealCourse()
  77. *todo public function testGetTargetOfLinkedCourse()
  78. *todo public function testIsCourseTeacher()
  79. *todo public function testIsUserSubscribedInRealOrLinkedCourse()
  80. *todo public function testGetUserListFromCourseCode()
  81. *todo public function testGetCoachListFromCourseCode()
  82. *todo public function testGetStudentListFromCourseCode()
  83. *todo public function testGetTeacherListFromCourseCode()
  84. *todo public function testGetRealAndLinkedUserList()
  85. *todo public function testGetListOfVirtualCoursesForSpecificUserAndRealCourse()
  86. *todo public function testGetGroupListOfCourse()
  87. *todo public function testAttemptCreateVirtualCourse()
  88. *todo public function testCreateVirtualCourse()
  89. *todo public function testDeleteCourse()
  90. *todo public function testCreateDatabaseDump()
  91. *todo public function testUserCourseSort()
  92. *todo public function testSelectAndSortCategories()
  93. *todo public function testCourseExists()
  94. *todo public function testEmailToTutor()
  95. *todo public function testGetCourseListByUserId()
  96. *todo public function testGetCourseIdFromPath()
  97. *todo public function testGetCourseInfoVisualCode()
  98. *todo public function testGetEmailOfTutorsToCourse()
  99. *todo public function testGetEmailOfTutorToSession()
  100. *todo public function testCreateCourseExtrField()
  101. *todo public function testUpdateCourseExtraFieldValue()
  102. */
  103. public function testGetCourseInformation(){
  104. $res = $this->tcourse->get_course_information(1211);
  105. $this->assertFalse($res);
  106. $this->assertTrue(is_bool($res));
  107. $this->assertTrue($this->tcourse->get_course_information(1211)=== is_array($res));
  108. }
  109. public function testGetCoursesList(){
  110. $res = $this->tcourse->get_courses_list();
  111. $this->assertTrue(is_array($res));
  112. //var_dump($res);
  113. }
  114. public function testGetAccessSettings(){
  115. $res = $this->tcourse->get_access_settings(0001);
  116. $this->assertFalse($res);
  117. $this->assertTrue($this->tcourse->get_access_settings(0001)===is_array($res));
  118. $this->assertFalse(is_null($res));
  119. }
  120. public function testGetUserInCourseStatus(){
  121. $res = $this->tcourse->get_user_in_course_status(01,0001);
  122. $this->assertFalse($res);
  123. $this->assertTrue($this->tcourse->get_user_in_course_status(01,0001)===null);
  124. $this->assertTrue(is_null($res));
  125. }
  126. public function testUnsubscribeUser(){
  127. $user_id = 1;
  128. $course_code = 'COURSE1';
  129. $res = CourseManager::unsubscribe_user($user_id, $course_code);
  130. $this->assertNull($res);
  131. $this->assertFalse(is_string($res));
  132. }
  133. public function testSubscribeUser(){
  134. $user_id = 1;
  135. $course_code = 'COURSE1';
  136. $status = STUDENT;
  137. $res = CourseManager::subscribe_user($user_id, $course_code, $status);
  138. $this->assertTrue(is_bool($res));
  139. }
  140. public function testAddUserToCourse(){
  141. $user_id = 1;
  142. $course_code = 'COURSE1';
  143. $status = STUDENT;
  144. $res = CourseManager::add_user_to_course($user_id, $course_code, $status);
  145. $this->assertTrue($res);
  146. $this->assertTrue(is_bool($res));
  147. //var_dump($res);
  148. }
  149. //function deprecated public function testGetRealCourseCodeSelectHtml(){}
  150. public function testCheckParameter(){
  151. $parameter = '123';
  152. $error_message = 'oops!!';
  153. $res = $this->tcourse->check_parameter($parameter, $error_message);
  154. $this->assertTrue($res);
  155. $this->assertTrue(is_bool($res));
  156. }
  157. public function testCheckParameterOrFail(){
  158. $parameter = 'course';
  159. $error_message = 'upps';
  160. $res = $this->tcourse->check_parameter_or_fail($parameter,$error_message); //ob_get_contents();
  161. $this->assertTrue(is_null($res));
  162. $this->assertFalse($res);
  163. $this->assertTrue($res=== null);
  164. $this->assertEqual($res,null);
  165. }
  166. public function testCourseCodeExists() {
  167. $wanted_course_code = 'COURSE1';
  168. $res = $this->tcourse->course_code_exists($wanted_course_code);
  169. $this->assertTrue(is_bool($res));
  170. $this->assertTrue($res);
  171. }
  172. /** Return a array() but now its empty, with this test is cheking is get the list course
  173. * @author Arthur Portugal <arthur.portugal@dokeos.com> -
  174. * doesn't work correctly refactoring by Ricardo Rodriguez <ricardo.rodriguez@beeznest.com>
  175. **/
  176. public function testGetRealCourseList(){
  177. $res = CourseManager::get_real_course_list();
  178. $this->assertTrue(is_array($res));
  179. $this->assertTrue($res);
  180. //var_dump($res);
  181. }
  182. public function testGetVirtualCourseList(){
  183. $course_table = Database :: get_main_table(TABLE_MAIN_COURSE);
  184. $sql_query = "SELECT * FROM $course_table WHERE target_course_code IS NOT NULL";
  185. $sql_result = Database::query($sql_query);
  186. $num = Database::num_rows($sql_result);
  187. $res = $this->tcourse->get_virtual_course_list();
  188. $this->assertEqual($num,count($res));
  189. }
  190. public function testGetRealCourseListOfUserAsCourseAdmin(){
  191. $user_id = 1;
  192. $res = $this->tcourse->get_real_course_list_of_user_as_course_admin($user_id);
  193. $this->assertTrue(is_array($res));
  194. }
  195. public function testGetCourseListOfUserAsCourseAdmin(){
  196. $user_id = 1;
  197. $res = $this->tcourse->get_course_list_of_user_as_course_admin($user_id);
  198. $this->assertTrue(is_array($res));
  199. }
  200. public function testDetermineCourseTitleFromCourseInfo(){
  201. $user_id = 1;
  202. $course_info = 'abcd123';
  203. $res = $this->tcourse->determine_course_title_from_course_info($user_id, $course_info);
  204. $this->assertTrue($res);
  205. $this->assertTrue(is_array($res));
  206. }
  207. public function testCreateCombinedName(){
  208. $complete_course_name = array();
  209. $user_is_registered_in_real_course = false;
  210. $real_course_name = 'COURSE1';
  211. $virtual_course_list = array();
  212. $res = CourseManager::create_combined_name($user_is_registered_in_real_course,
  213. $real_course_name,
  214. $virtual_course_list
  215. );
  216. $this->assertFalse($res);
  217. $this->assertTrue(is_string($res));
  218. //var_dump($res);
  219. }
  220. public function testCreateCombinedCode(){
  221. $complete_course_code = array();
  222. $user_is_registered_in_real_course = false;
  223. $real_course_code = 'COURSE1';
  224. $virtual_course_list = array();
  225. $res = CourseManager::create_combined_code($user_is_registered_in_real_course,
  226. $real_course_code,
  227. $virtual_course_list);
  228. if(is_array($res)){
  229. $this->assertFalse($res);
  230. $this->assertTrue(is_string($res));
  231. } else {
  232. $this->assertFalse(is_null($res));
  233. }
  234. //var_dump($res);
  235. }
  236. public function testGetVirtualCourseInfo(){
  237. $real_course_code = 'COURSE1';
  238. $res = $this->tcourse->get_virtual_course_info($real_course_code);
  239. if(is_array($res)){
  240. $this->assertTrue(is_array($res));
  241. } else {
  242. $this->assertTrue($res);
  243. }
  244. //var_dump($res);
  245. }
  246. public function testIsVirtualCourseFromSystemCode(){
  247. $system_code = 'COURSE1';
  248. $res = $this->tcourse->is_virtual_course_from_system_code($system_code);
  249. $this->assertFalse($res);
  250. $this->assertTrue(is_bool($res));
  251. $this->assertFalse($res,null);
  252. }
  253. public function testIsVirtualCourseFromVisualCode(){
  254. $system_code = 'COURSE1';
  255. $res = $this->tcourse->is_virtual_course_from_visual_code($system_code);
  256. $this->assertFalse($res);
  257. $this->assertTrue(is_bool($res));
  258. }
  259. public function testHasVirtualCourseFromCode(){
  260. $real_course_code = 'COURSE1';
  261. $user_id = 1;
  262. $res = CourseManager::has_virtual_courses_from_code($real_course_code, $user_id);
  263. if(is_bool($res)){
  264. $this->assertFalse($res);
  265. $this->assertTrue(is_bool($res));
  266. } else {
  267. $this->assertFalse(is_null($res));
  268. }
  269. //var_dump($res);
  270. }
  271. public function testGetVirtualCourseLinkedToRealCourse(){
  272. $real_course_code = 'COURSE1';
  273. $res = CourseManager::get_virtual_courses_linked_to_real_course($real_course_code);
  274. $this->assertFalse($res);
  275. $this->assertTrue(is_array($res));
  276. $this->assertFalse(is_null($res));
  277. }
  278. public function testGetTargetOfLinkedCourse(){
  279. $virtual_course_code = 'COURSE1';
  280. $res = CourseManager::get_target_of_linked_course($virtual_course_code);
  281. $this->assertFalse($res);
  282. $this->assertTrue(is_null($res));
  283. $this->assertNull($res,true);
  284. }
  285. public function testIsUserSubscribedInCourse(){
  286. $user_id = 1;
  287. $course_code = 'COURSE1';
  288. $in_a_session = false;
  289. $res = CourseManager::is_user_subscribed_in_course($user_id, $course_code, $in_a_session);
  290. $this->assertTrue(is_bool($res));
  291. //var_dump($res);
  292. }
  293. public function testIsCourseTeacher(){
  294. $user_id = 1;
  295. $course_code = 'COURSE1';
  296. $res = CourseManager::is_course_teacher($user_id, $course_code);
  297. $this->assertTrue(is_bool($res));
  298. $this->assertFalse($res);
  299. }
  300. public function testIsUserSubscribedInRealOrLinkedCourse(){
  301. $user_id = 1;
  302. $course_code = 'COURSE1';
  303. $session_id = '';
  304. $res = CourseManager::is_user_subscribed_in_real_or_linked_course($user_id, $course_code, $session_id);
  305. if(is_bool($res)){
  306. $this-> assertTrue(is_bool($res));
  307. $this->assertFalse($res);
  308. } else {
  309. $this->assertTrue($res);
  310. }
  311. //var_dump($res);
  312. }
  313. public function testGetUserListFromCourseCode(){
  314. $course_code = 'COURSE1';
  315. $with_session = true;
  316. $session_id = 0;
  317. $limit = '';
  318. $order_by = '';
  319. $res = CourseManager::get_user_list_from_course_code($course_code, $with_session, $session_id, $limit, $order_by);
  320. //$this->assertTrue($res);
  321. $this->assertTrue(is_array($res));
  322. //var_dump($res);
  323. }
  324. public function testGetCoachListFromCourseCode(){
  325. $course_code = 'COURSE1';
  326. $session_id = '';
  327. $res = CourseManager::get_coach_list_from_course_code($course_code, $session_id);
  328. $this->assertFalse($res);
  329. $this->assertTrue(is_array($res));
  330. }
  331. public function testGetStudentListFromCourseCode(){
  332. $course_code = 'COURSE1';
  333. $session_id = '001';
  334. $res = $this->tcourse->get_student_list_from_course_code($course_code, $session_id);
  335. //$this->assertTrue($res);
  336. $this->assertTrue(is_array($res));
  337. //var_dump($res);
  338. }
  339. public function testGetTeacherListFromCourseCode(){
  340. $course_code = 'COURSE1';
  341. $res = $this->tcourse->get_teacher_list_from_course_code($course_code);
  342. $this->assertFalse($res);
  343. $this->assertFalse(is_null($res));
  344. //var_dump($res);
  345. }
  346. public function testGetRealAndLinkedUserList(){
  347. $course_code = 'COURSE1';
  348. $with_sessions = true;
  349. $session_id = 0;
  350. $res = $this->tcourse->get_real_and_linked_user_list($course_code, $with_sessions, $session_id);
  351. $this->assertNull($res);
  352. $this->assertTrue(is_null($res));
  353. //$this->assertTrue($this->tcourse->get_real_and_linked_user_list()===null);
  354. //var_dump($res);
  355. }
  356. public function testGetListOfVirtualCoursesForSpecificUserAndRealCourse(){
  357. $result_array = array('user_id' => 1, 'real_course_code' => 'COURSE1');
  358. $res = CourseManager::get_list_of_virtual_courses_for_specific_user_and_real_course($result_array['user_id'],$result_array['real_course_code']);
  359. if(is_array($res)){
  360. $this->assertTrue(is_array($res));
  361. } else {
  362. $this->assertTrue($res);
  363. }
  364. //var_dump($res);
  365. }
  366. public function testGetGroupListOfCourse(){
  367. $course_code = 'COURSE1';
  368. $sql= "SELECT * FROM chamilo_COURSE1";
  369. $result = CourseManager::get_group_list_of_course($course_code);
  370. $this->assertTrue(is_array($result));
  371. $this->assertFalse($result);
  372. //var_dump($res);
  373. }
  374. public function testAttemptCreateVirtualCourse(){
  375. $real_course_code = 'COURSE1';
  376. $course_title = 'COURSE1';
  377. $wanted_course_code = 'COURSE1';
  378. $course_language = 'english';
  379. $course_category = 'LANG';
  380. ob_start();
  381. $res = CourseManager::attempt_create_virtual_course($real_course_code, $course_title, $wanted_course_code, $course_language, $course_category);
  382. ob_end_clean();
  383. $this->assertTrue(is_bool($res));
  384. }
  385. public function testCreateVirtualCourse(){
  386. $real_course_code = 'COURSE1';
  387. $course_title = 'COURSE1';
  388. $wanted_course_code = 'COURSE1';
  389. $course_language = 'english';
  390. $course_category = 'LANG';
  391. ob_start();
  392. $res = CourseManager::create_virtual_course($real_course_code, $course_title, $wanted_course_code, $course_language, $course_category);
  393. ob_end_clean();
  394. if(is_bool($res)){
  395. $this->assertTrue(is_bool($res));
  396. } else {
  397. $this->assertTrue($res);
  398. }
  399. //var_dump($res);
  400. }
  401. public function testCreateDatabaseDump(){
  402. global $_configuration;
  403. $course_code='COURSE1';
  404. $res = $this->tcourse->create_database_dump($course_code);
  405. $this->assertFalse($res);
  406. $this->assertTrue(is_null($res));
  407. }
  408. public function testUserCourseSort(){
  409. $user_id ='01';
  410. $course_code='COURSE1';
  411. $res = CourseManager::UserCourseSort($user_id,$course_code);
  412. $this->assertTrue($res);
  413. $this->assertTrue(is_numeric($res));
  414. }
  415. public function testSelectAndSortCategories(){
  416. $form = new FormValidator('add_course');
  417. $categories = array('name' => 'prueba');
  418. $categories_select = $form->addElement('select', 'category_code', get_lang('Fac'), $categories);
  419. $res = $this->tcourse->select_and_sort_categories($categories_select);
  420. $this->assertFalse($res);
  421. $this->assertTrue(is_null($res));
  422. }
  423. public function testCourseExists(){
  424. $course_code='COURSE1';
  425. $accept_virtual = false;
  426. $res=$this->tcourse->course_exists($course_code, $accept_virtual);
  427. $this->assertTrue(is_numeric($res));
  428. }
  429. public function testEmailToTutor() {
  430. $user_id= '01';
  431. $course_code= 'COURSE1';
  432. $res=CourseManager::email_to_tutor($course_code,$user_id);
  433. $this->assertTrue(is_string($course_code));
  434. //var_dump($res);
  435. }
  436. public function testGetCoursesListByUserId(){
  437. $user_id = '01';
  438. $res = $this->tcourse->get_courses_list_by_user_id($user_id);
  439. $this->assertTrue(is_array($res));
  440. //var_dump($res);
  441. }
  442. public function testGetCourseIdFromPath(){
  443. $path = '/var/www/path';
  444. $res = $this->tcourse->get_course_id_from_path($path);
  445. $this->assertTrue(is_bool($res));
  446. $this->assertTrue($res === false);
  447. }
  448. public function testGetCoursesInfoFromVisualCode(){
  449. $code = '0001';
  450. $res=$this->tcourse->get_courses_info_from_visual_code($code);
  451. $this->assertTrue(is_array($res));
  452. //var_dump($res);
  453. }
  454. public function testGetEmailsOfTutorsToCourse(){
  455. $code = '0001';
  456. $res= $this->tcourse->get_emails_of_tutors_to_course($code);
  457. $this->assertTrue(is_array($res));
  458. //var_dump($res);
  459. }
  460. public function testGetEmailOfTutorToSession(){
  461. $session_id = '01';
  462. $course_code = 'COURSE1';
  463. ob_start();
  464. $res = CourseManager::get_email_of_tutor_to_session($session_id,$course_code);
  465. ob_end_clean();
  466. $this->assertTrue(is_array($res));
  467. //var_dump($res);
  468. }
  469. public function testCreateCourseExtraField(){
  470. $fieldvarname = '';
  471. $fieldtype = '5';
  472. $fieldtitle = '';
  473. $res = $this->tcourse->create_course_extra_field($fieldvarname, $fieldtype, $fieldtitle);
  474. $this->assertTrue(is_numeric($res));
  475. $this->assertTrue($res);
  476. }
  477. public function testUpdateCourseExtraFieldValue(){
  478. $course_code = 'COURSE1';
  479. $fname = '';
  480. $fvalue= '';
  481. $res = $this->tcourse->update_course_extra_field_value($course_code,$fname,$fvalue='');
  482. $this->assertTrue($res);
  483. $this->assertTrue(is_bool($res));
  484. $this->assertFalse(is_null($res));
  485. }
  486. public function testDeleteCourse(){
  487. global $_configuration;
  488. $code = 'COURSE1';
  489. if (!empty($course_code)) {
  490. $code = $course_code;
  491. }
  492. $res = CourseManager::delete_course($code);
  493. $this->assertTrue(is_null($res));
  494. }
  495. }
  496. ?>