test_manager.inc.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. use ChamiloSession as Session;
  3. /* For licensing terms, see /license.txt */
  4. /**
  5. * Code library for load functions than are needed to test
  6. *
  7. * @author Arthur Portugal, Principal author
  8. * @package chamilo.tests
  9. */
  10. /**
  11. * @todo shouldn't these settings be moved to the test_suite.php
  12. * if these are really configuration then we can make require_once in each tests file.
  13. * @todo use this file to load in the setup in each file test.
  14. * @todo use this file to destroy in the teardown in each file test.
  15. * @todo check for duplication of "require_once" files with test_suite.php
  16. * @author Arthur Portugal
  17. */
  18. /*
  19. INIT SECTION
  20. */
  21. ini_set('memory_limit','256M');
  22. ini_set('max_execution_time','0');
  23. /*
  24. Included libraries
  25. */
  26. $maindir = dirname(__FILE__).'/../main/';
  27. $incdir = dirname(__FILE__).'/../main/inc/';
  28. $libdir = dirname(__FILE__).'/../main/inc/lib/';
  29. /**Files inside '/../main' */
  30. require_once $maindir . 'blog/permissions/permissions_functions.inc.php';
  31. require_once $maindir.'dropbox/dropbox_class.inc.php';
  32. require_once $maindir.'dropbox/dropbox_functions.inc.php';
  33. require_once $maindir.'survey/survey.lib.php';
  34. require_once $maindir.'exercice/export/scorm/scorm_classes.php';
  35. require_once $maindir.'exercice/export/qti2/qti2_classes.php';
  36. require_once $maindir.'exercice/export/exercise_import.inc.php';
  37. require_once $maindir.'exercice/exercise_result.class.php';
  38. require_once $maindir.'exercice/answer.class.php';
  39. require_once $maindir.'exercice/exercise.class.php';
  40. require_once $maindir.'exercice/fill_blanks.class.php';
  41. require_once $maindir.'exercice/freeanswer.class.php';
  42. require_once $maindir.'forum/forumfunction.inc.php';
  43. /*
  44. require_once $maindir.'gradebook/lib/be/gradebookitem.class.php';
  45. require_once $maindir.'gradebook/lib/be/abstractlink.class.php';
  46. require_once $maindir.'gradebook/lib/be/evallink.class.php';
  47. require_once $maindir.'gradebook/lib/be/linkfactory.class.php';
  48. require_once $maindir.'gradebook/lib/be/attendancelink.class.php';
  49. require_once $maindir.'gradebook/lib/be/category.class.php';
  50. require_once $maindir.'gradebook/lib/be/dropboxlink.class.php';
  51. require_once $maindir.'gradebook/lib/be/evaluation.class.php';
  52. require_once $maindir.'gradebook/lib/be/exerciselink.class.php';
  53. require_once $maindir.'gradebook/lib/be/dropboxlink.class.php';
  54. require_once $maindir.'gradebook/lib/be/result.class.php';
  55. require_once $maindir.'gradebook/lib/be/forumthreadlink.class.php';
  56. */
  57. require_once $maindir.'course_info/download.lib.php';
  58. /**Files inside '/../main/lib/' */
  59. require_once $libdir.'urlmanager.lib.php';
  60. require_once $libdir.'groupmanager.lib.php';
  61. require_once $libdir.'usermanager.lib.php';
  62. require_once $libdir.'social.lib.php';
  63. require_once $libdir.'xht.lib.php';
  64. require_once $libdir.'xmd.lib.php';
  65. require_once $libdir.'formvalidator/FormValidator.class.php';
  66. require_once $libdir.'exercise_show_functions.lib.php';
  67. require_once $libdir.'fileManage.lib.php';
  68. require_once $libdir.'notebook.lib.php';
  69. /**This files need be inside a buffering to clean the objects*/
  70. ob_start();
  71. require_once $libdir.'add_course.lib.inc.php';
  72. require_once $libdir.'geometry.lib.php';
  73. ob_end_clean();
  74. class TestManager
  75. {
  76. /* MAIN CODE */
  77. /**
  78. * This function create in the database a test course and will also load sessions.
  79. * This name will be change each time is used in the tests.
  80. * @param string Course name
  81. * @return void
  82. */
  83. function create_test_course($course_code = 'TESTCOURSE') {
  84. /* Table definitions */
  85. $table_course = Database::get_main_table(TABLE_MAIN_COURSE);
  86. $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
  87. $course_cat_table = Database::get_main_table(TABLE_MAIN_CATEGORY);
  88. global $_configuration, $_user, $_course, $cidReq;
  89. $cidReq = $course_code;
  90. /* Check if the course exists */
  91. $sql = "SELECT code FROM $table_course WHERE code = '$cidReq' ";
  92. $rs = Database::query($sql, __FILE__, __LINE__);
  93. $row = Database::fetch_row($rs);
  94. /* Create the course in the database */
  95. if (empty($row[0])) {
  96. // Create a course
  97. $course_data = array(
  98. 'wanted_code'=> $cidReq,
  99. 'title'=>$cidReq,
  100. 'tutor_name'=>'John Doe',
  101. 'category_code'=>'LANG',
  102. 'course_language'=>'spanish',
  103. 'course_admin_id'=>'001',
  104. 'db_prefix'=> $_configuration['db_prefix'],
  105. 'firstExpirationDelay'=>'999'
  106. );
  107. $res = CourseManager::create_course($course_data['wanted_code'], $course_data['title'],
  108. $course_data['tutor_name'], $course_data['category_code'],
  109. $course_data['course_language'],$course_data['course_admin_id'],
  110. $course_data['db_prefix'], $course_data['firstExpirationDelay']);
  111. }
  112. $sql = "SELECT course.*, course_category.code faCode, course_category.name faName
  113. FROM $course_table
  114. LEFT JOIN $course_cat_table
  115. ON course.category_code = course_category.code
  116. WHERE course.code = '$cidReq'";
  117. $result = Database::query($sql);
  118. /* Create the session */
  119. if (Database::num_rows($result)>0) {
  120. $cData = Database::fetch_array($result);
  121. $_cid = $cData['code' ];
  122. $_course = array();
  123. $_course['id' ] = $cData['code' ]; //auto-assigned integer
  124. $_course['name' ] = $cData['title' ];
  125. $_course['official_code'] = $cData['visual_code' ]; // use in echo
  126. $_course['sysCode' ] = $cData['code' ]; // use as key in db
  127. $_course['path' ] = $cData['directory' ]; // use as key in path
  128. $_course['titular' ] = $cData['tutor_name' ];
  129. $_course['language' ] = $cData['course_language' ];
  130. $_course['extLink' ]['url' ] = $cData['department_url' ];
  131. $_course['extLink' ]['name'] = $cData['department_name' ];
  132. $_course['categoryCode'] = $cData['faCode' ];
  133. $_course['categoryName'] = $cData['faName' ];
  134. $_course['visibility' ] = $cData['visibility' ];
  135. $_course['subscribe_allowed'] = $cData['subscribe' ];
  136. $_course['unsubscribe'] = $cData['unsubscribe' ];
  137. Session::write('_cid',$_cid);
  138. Session::write('_course',$_course);
  139. }
  140. /* Load the session */
  141. $_SESSION['_user']['user_id'] = 1;
  142. $_SESSION['is_courseAdmin'] = 1;
  143. $_SESSION['show'] = showall;
  144. /* Load the user */
  145. $_user['user_id'] = $_SESSION['_user']['user_id'];
  146. }
  147. /**
  148. * This function delete the test course from the database and destroy the sessions.
  149. * @param string the course code than will be delete.
  150. * @return void
  151. */
  152. function delete_test_course($course_code = 'TESTCOURSE') {
  153. $res = CourseManager::delete_course($course_code);
  154. $path = api_get_path(SYS_PATH).'archive';
  155. if ($handle = opendir($path)) {
  156. while (false !== ($file = readdir($handle))) {
  157. if (strpos($file,$course_code)!==false) {
  158. if (is_dir($path.'/'.$file)) {
  159. rmdirr($path.'/'.$file);
  160. }
  161. }
  162. }
  163. closedir($handle);
  164. }
  165. // Check api session destroy
  166. if (!headers_sent() && session_id() != "") {
  167. $res=Session::destroy();
  168. }
  169. }
  170. }