course.lib.test.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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('All main course function tests');
  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 testIsExistingCourseCode() {
  167. $wanted_course_code = 'COURSE1';
  168. $res = $this->tcourse->is_existing_course_code($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, __FILE__, __LINE__);
  186. $result = Database::fetch_array($sql_result);
  187. $virtual_course_list[] = $result;
  188. $res=$this->tcourse->get_virtual_course_list();
  189. if (!empty($res)) {
  190. $this->assertTrue(is_array($res));
  191. } else {
  192. $this->assertTrue(is_null($res));
  193. }
  194. }
  195. public function testGetRealCourseListOfUserAsCourseAdmin(){
  196. $user_id = 1;
  197. $res = $this->tcourse->get_real_course_list_of_user_as_course_admin($user_id);
  198. if(is_array($res)){
  199. $this->assertTrue(is_array($res));
  200. $this->assertTrue($res);
  201. } else {
  202. $this->assertNull($res);
  203. $this->assertTrue(is_null($res));
  204. }
  205. //var_dump($res);
  206. }
  207. public function testGetCourseListOfUserAsCourseAdmin(){
  208. $user_id = 1;
  209. $res = $this->tcourse->get_course_list_of_user_as_course_admin($user_id);
  210. if(is_array($res)){
  211. $this->assertTrue($res);
  212. $this->assertTrue(is_array($res));
  213. } else {
  214. $this->assertTrue(is_null($res));
  215. $this->assertNull($res);
  216. }
  217. //var_dump($res);
  218. }
  219. public function testDetermineCourseTitleFromCourseInfo(){
  220. $user_id = 1;
  221. $course_info = 'abcd123';
  222. $res = $this->tcourse->determine_course_title_from_course_info($user_id, $course_info);
  223. $this->assertTrue($res);
  224. $this->assertTrue(is_array($res));
  225. }
  226. public function testCreateCombinedName(){
  227. $complete_course_name = array();
  228. $user_is_registered_in_real_course = false;
  229. $real_course_name = 'COURSE1';
  230. $virtual_course_list = array();
  231. $res = CourseManager::create_combined_name($user_is_registered_in_real_course,
  232. $real_course_name,
  233. $virtual_course_list
  234. );
  235. $this->assertFalse($res);
  236. $this->assertTrue(is_string($res));
  237. //var_dump($res);
  238. }
  239. public function testCreateCombinedCode(){
  240. $complete_course_code = array();
  241. $user_is_registered_in_real_course = false;
  242. $real_course_code = 'COURSE1';
  243. $virtual_course_list = array();
  244. $res = CourseManager::create_combined_code($user_is_registered_in_real_course,
  245. $real_course_code,
  246. $virtual_course_list);
  247. if(is_array($res)){
  248. $this->assertFalse($res);
  249. $this->assertTrue(is_string($res));
  250. } else {
  251. $this->assertFalse(is_null($res));
  252. }
  253. //var_dump($res);
  254. }
  255. public function testGetVirtualCourseInfo(){
  256. $real_course_code = 'COURSE1';
  257. $res = $this->tcourse->get_virtual_course_info($real_course_code);
  258. if(is_array($res)){
  259. $this->assertTrue(is_array($res));
  260. } else {
  261. $this->assertTrue($res);
  262. }
  263. //var_dump($res);
  264. }
  265. public function testIsVirtualCourseFromSystemCode(){
  266. $system_code = 'COURSE1';
  267. $res = $this->tcourse->is_virtual_course_from_system_code($system_code);
  268. $this->assertFalse($res);
  269. $this->assertTrue(is_bool($res));
  270. $this->assertFalse($res,null);
  271. }
  272. public function testIsVirtualCourseFromVisualCode(){
  273. $system_code = 'COURSE1';
  274. $res = $this->tcourse->is_virtual_course_from_visual_code($system_code);
  275. $this->assertFalse($res);
  276. $this->assertTrue(is_bool($res));
  277. }
  278. public function testHasVirtualCourseFromCode(){
  279. $real_course_code = 'COURSE1';
  280. $user_id = 1;
  281. $res = CourseManager::has_virtual_courses_from_code($real_course_code, $user_id);
  282. if(is_bool($res)){
  283. $this->assertFalse($res);
  284. $this->assertTrue(is_bool($res));
  285. } else {
  286. $this->assertFalse(is_null($res));
  287. }
  288. //var_dump($res);
  289. }
  290. public function testGetVirtualCourseLinkedToRealCourse(){
  291. $real_course_code = 'COURSE1';
  292. $res = CourseManager::get_virtual_courses_linked_to_real_course($real_course_code);
  293. $this->assertFalse($res);
  294. $this->assertTrue(is_array($res));
  295. $this->assertFalse(is_null($res));
  296. }
  297. public function testGetTargetOfLinkedCourse(){
  298. $virtual_course_code = 'COURSE1';
  299. $res = CourseManager::get_target_of_linked_course($virtual_course_code);
  300. $this->assertFalse($res);
  301. $this->assertTrue(is_null($res));
  302. $this->assertNull($res,true);
  303. }
  304. public function testIsUserSubscribedInCourse(){
  305. $user_id = 1;
  306. $course_code = 'COURSE1';
  307. $in_a_session = false;
  308. $res = CourseManager::is_user_subscribed_in_course($user_id, $course_code, $in_a_session);
  309. $this->assertTrue(is_bool($res));
  310. //var_dump($res);
  311. }
  312. public function testIsCourseTeacher(){
  313. $user_id = 1;
  314. $course_code = 'COURSE1';
  315. $res = CourseManager::is_course_teacher($user_id, $course_code);
  316. $this->assertTrue(is_bool($res));
  317. $this->assertFalse($res);
  318. }
  319. public function testIsUserSubscribedInRealOrLinkedCourse(){
  320. $user_id = 1;
  321. $course_code = 'COURSE1';
  322. $session_id = '';
  323. $res = CourseManager::is_user_subscribed_in_real_or_linked_course($user_id, $course_code, $session_id);
  324. if(is_bool($res)){
  325. $this-> assertTrue(is_bool($res));
  326. $this->assertFalse($res);
  327. } else {
  328. $this->assertTrue($res);
  329. }
  330. //var_dump($res);
  331. }
  332. public function testGetUserListFromCourseCode(){
  333. $course_code = 'COURSE1';
  334. $with_session = true;
  335. $session_id = 0;
  336. $limit = '';
  337. $order_by = '';
  338. $res = CourseManager::get_user_list_from_course_code($course_code, $with_session, $session_id, $limit, $order_by);
  339. //$this->assertTrue($res);
  340. $this->assertTrue(is_array($res));
  341. //var_dump($res);
  342. }
  343. public function testGetCoachListFromCourseCode(){
  344. $course_code = 'COURSE1';
  345. $session_id = '';
  346. $res = CourseManager::get_coach_list_from_course_code($course_code, $session_id);
  347. $this->assertFalse($res);
  348. $this->assertTrue(is_array($res));
  349. }
  350. public function testGetStudentListFromCourseCode(){
  351. $course_code = 'COURSE1';
  352. $session_id = '001';
  353. $res = $this->tcourse->get_student_list_from_course_code($course_code, $session_id);
  354. //$this->assertTrue($res);
  355. $this->assertTrue(is_array($res));
  356. //var_dump($res);
  357. }
  358. public function testGetTeacherListFromCourseCode(){
  359. $course_code = 'COURSE1';
  360. $res = $this->tcourse->get_teacher_list_from_course_code($course_code);
  361. $this->assertFalse($res);
  362. $this->assertFalse(is_null($res));
  363. //var_dump($res);
  364. }
  365. public function testGetRealAndLinkedUserList(){
  366. $course_code = 'COURSE1';
  367. $with_sessions = true;
  368. $session_id = 0;
  369. $res = $this->tcourse->get_real_and_linked_user_list($course_code, $with_sessions, $session_id);
  370. $this->assertNull($res);
  371. $this->assertTrue(is_null($res));
  372. //$this->assertTrue($this->tcourse->get_real_and_linked_user_list()===null);
  373. //var_dump($res);
  374. }
  375. public function testGetListOfVirtualCoursesForSpecificUserAndRealCourse(){
  376. $result_array = array('user_id' => 1, 'real_course_code' => 'COURSE1');
  377. $res = CourseManager::get_list_of_virtual_courses_for_specific_user_and_real_course($result_array['user_id'],$result_array['real_course_code']);
  378. if(is_array($res)){
  379. $this->assertTrue(is_array($res));
  380. } else {
  381. $this->assertTrue($res);
  382. }
  383. //var_dump($res);
  384. }
  385. public function testGetGroupListOfCourse(){
  386. $course_code = 'COURSE1';
  387. $sql= "SELECT * FROM chamilo_COURSE1";
  388. $result = CourseManager::get_group_list_of_course($course_code);
  389. $this->assertTrue(is_array($result));
  390. $this->assertFalse($result);
  391. //var_dump($res);
  392. }
  393. public function testAttemptCreateVirtualCourse(){
  394. $real_course_code = 'COURSE1';
  395. $course_title = 'COURSE1';
  396. $wanted_course_code = 'COURSE1';
  397. $course_language = 'english';
  398. $course_category = 'LANG';
  399. ob_start();
  400. $res = CourseManager::attempt_create_virtual_course($real_course_code, $course_title, $wanted_course_code, $course_language, $course_category);
  401. ob_end_clean();
  402. $this->assertTrue(is_bool($res));
  403. }
  404. public function testCreateVirtualCourse(){
  405. $real_course_code = 'COURSE1';
  406. $course_title = 'COURSE1';
  407. $wanted_course_code = 'COURSE1';
  408. $course_language = 'english';
  409. $course_category = 'LANG';
  410. ob_start();
  411. $res = CourseManager::create_virtual_course($real_course_code, $course_title, $wanted_course_code, $course_language, $course_category);
  412. ob_end_clean();
  413. if(is_bool($res)){
  414. $this->assertTrue(is_bool($res));
  415. } else {
  416. $this->assertTrue($res);
  417. }
  418. //var_dump($res);
  419. }
  420. public function testCreateDatabaseDump(){
  421. global $_configuration;
  422. $course_code='COURSE1';
  423. $res = $this->tcourse->create_database_dump($course_code);
  424. $this->assertFalse($res);
  425. $this->assertTrue(is_null($res));
  426. }
  427. public function testUserCourseSort(){
  428. $user_id ='01';
  429. $course_code='COURSE1';
  430. $res = CourseManager::UserCourseSort($user_id,$course_code);
  431. $this->assertTrue($res);
  432. $this->assertTrue(is_numeric($res));
  433. }
  434. public function testSelectAndSortCategories(){
  435. $form = new FormValidator('add_course');
  436. $categories = array('name' => 'prueba');
  437. $categories_select = $form->addElement('select', 'category_code', get_lang('Fac'), $categories);
  438. $res = $this->tcourse->select_and_sort_categories($categories_select);
  439. $this->assertFalse($res);
  440. $this->assertTrue(is_null($res));
  441. }
  442. public function testCourseExists(){
  443. $course_code='COURSE1';
  444. $accept_virtual = false;
  445. $res=$this->tcourse->course_exists($course_code, $accept_virtual);
  446. $this->assertTrue(is_numeric($res));
  447. }
  448. public function testEmailToTutor() {
  449. $user_id= '01';
  450. $course_code= 'COURSE1';
  451. $res=CourseManager::email_to_tutor($course_code,$user_id);
  452. $this->assertTrue(is_string($course_code));
  453. //var_dump($res);
  454. }
  455. public function testGetCoursesListByUserId(){
  456. $user_id = '01';
  457. $res = $this->tcourse->get_courses_list_by_user_id($user_id);
  458. $this->assertTrue(is_array($res));
  459. //var_dump($res);
  460. }
  461. public function testGetCourseIdFromPath(){
  462. $path = '/var/www/path';
  463. $res = $this->tcourse->get_course_id_from_path($path);
  464. $this->assertTrue(is_bool($res));
  465. $this->assertTrue($res === false);
  466. }
  467. public function testGetCoursesInfoFromVisualCode(){
  468. $code = '0001';
  469. $res=$this->tcourse->get_courses_info_from_visual_code($code);
  470. $this->assertTrue(is_array($res));
  471. //var_dump($res);
  472. }
  473. public function testGetEmailsOfTutorsToCourse(){
  474. $code = '0001';
  475. $res= $this->tcourse->get_emails_of_tutors_to_course($code);
  476. $this->assertTrue(is_array($res));
  477. //var_dump($res);
  478. }
  479. public function testGetEmailOfTutorToSession(){
  480. $session_id = '01';
  481. $course_code = 'COURSE1';
  482. ob_start();
  483. $res = CourseManager::get_email_of_tutor_to_session($session_id,$course_code);
  484. ob_end_clean();
  485. $this->assertTrue(is_array($res));
  486. //var_dump($res);
  487. }
  488. public function testCreateCourseExtraField(){
  489. $fieldvarname = '';
  490. $fieldtype = '5';
  491. $fieldtitle = '';
  492. $res = $this->tcourse->create_course_extra_field($fieldvarname, $fieldtype, $fieldtitle);
  493. $this->assertTrue(is_numeric($res));
  494. $this->assertTrue($res);
  495. }
  496. public function testUpdateCourseExtraFieldValue(){
  497. $course_code = 'COURSE1';
  498. $fname = '';
  499. $fvalue= '';
  500. $res = $this->tcourse->update_course_extra_field_value($course_code,$fname,$fvalue='');
  501. $this->assertTrue($res);
  502. $this->assertTrue(is_bool($res));
  503. $this->assertFalse(is_null($res));
  504. }
  505. public function testDeleteCourse(){
  506. global $_configuration;
  507. $code = 'COURSE1';
  508. if (!empty($course_code)) {
  509. $code = $course_code;
  510. }
  511. $res = CourseManager::delete_course($code);
  512. $this->assertTrue(is_null($res));
  513. }
  514. }
  515. ?>