test_manager.inc.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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.'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.'fileDisplay.lib.php';
  61. require_once $libdir.'groupmanager.lib.php';
  62. require_once $libdir.'usermanager.lib.php';
  63. require_once $libdir.'social.lib.php';
  64. require_once $libdir.'xht.lib.php';
  65. require_once $libdir.'xmd.lib.php';
  66. require_once $libdir.'formvalidator/FormValidator.class.php';
  67. require_once $libdir.'exercise_show_functions.lib.php';
  68. require_once $libdir.'fileManage.lib.php';
  69. require_once $libdir.'notebook.lib.php';
  70. /**This files need be inside a buffering to clean the objects*/
  71. ob_start();
  72. require_once $libdir.'add_course.lib.inc.php';
  73. require_once $libdir.'geometry.lib.php';
  74. ob_end_clean();
  75. class TestManager
  76. {
  77. /* MAIN CODE */
  78. /**
  79. * This function create in the database a test course and will also load sessions.
  80. * This name will be change each time is used in the tests.
  81. * @param string Course name
  82. * @return void
  83. */
  84. function create_test_course($course_code = 'TESTCOURSE') {
  85. /* Table definitions */
  86. $table_course = Database::get_main_table(TABLE_MAIN_COURSE);
  87. $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
  88. $course_cat_table = Database::get_main_table(TABLE_MAIN_CATEGORY);
  89. global $_configuration, $_user, $_course, $cidReq;
  90. $cidReq = $course_code;
  91. /* Check if the course exists */
  92. $sql = "SELECT code FROM $table_course WHERE code = '$cidReq' ";
  93. $rs = Database::query($sql, __FILE__, __LINE__);
  94. $row = Database::fetch_row($rs);
  95. /* Create the course in the database */
  96. if (empty($row[0])) {
  97. // Create a course
  98. $course_data = array(
  99. 'wanted_code'=> $cidReq,
  100. 'title'=>$cidReq,
  101. 'tutor_name'=>'John Doe',
  102. 'category_code'=>'LANG',
  103. 'course_language'=>'spanish',
  104. 'course_admin_id'=>'001',
  105. 'db_prefix'=> $_configuration['db_prefix'],
  106. 'firstExpirationDelay'=>'999'
  107. );
  108. $res = CourseManager::create_course($course_data['wanted_code'], $course_data['title'],
  109. $course_data['tutor_name'], $course_data['category_code'],
  110. $course_data['course_language'],$course_data['course_admin_id'],
  111. $course_data['db_prefix'], $course_data['firstExpirationDelay']);
  112. }
  113. $sql = "SELECT course.*, course_category.code faCode, course_category.name faName
  114. FROM $course_table
  115. LEFT JOIN $course_cat_table
  116. ON course.category_code = course_category.code
  117. WHERE course.code = '$cidReq'";
  118. $result = Database::query($sql);
  119. /* Create the session */
  120. if (Database::num_rows($result)>0) {
  121. $cData = Database::fetch_array($result);
  122. $_cid = $cData['code' ];
  123. $_course = array();
  124. $_course['id' ] = $cData['code' ]; //auto-assigned integer
  125. $_course['name' ] = $cData['title' ];
  126. $_course['official_code'] = $cData['visual_code' ]; // use in echo
  127. $_course['sysCode' ] = $cData['code' ]; // use as key in db
  128. $_course['path' ] = $cData['directory' ]; // use as key in path
  129. $_course['titular' ] = $cData['tutor_name' ];
  130. $_course['language' ] = $cData['course_language' ];
  131. $_course['extLink' ]['url' ] = $cData['department_url' ];
  132. $_course['extLink' ]['name'] = $cData['department_name' ];
  133. $_course['categoryCode'] = $cData['faCode' ];
  134. $_course['categoryName'] = $cData['faName' ];
  135. $_course['visibility' ] = $cData['visibility' ];
  136. $_course['subscribe_allowed'] = $cData['subscribe' ];
  137. $_course['unsubscribe'] = $cData['unsubscribe' ];
  138. Session::write('_cid',$_cid);
  139. Session::write('_course',$_course);
  140. }
  141. /* Load the session */
  142. $_SESSION['_user']['user_id'] = 1;
  143. $_SESSION['is_courseAdmin'] = 1;
  144. $_SESSION['show'] = showall;
  145. /* Load the user */
  146. $_user['user_id'] = $_SESSION['_user']['user_id'];
  147. }
  148. /**
  149. * This function delete the test course from the database and destroy the sessions.
  150. * @param string the course code than will be delete.
  151. * @return void
  152. */
  153. function delete_test_course($course_code = 'TESTCOURSE') {
  154. $res = CourseManager::delete_course($course_code);
  155. $path = api_get_path(SYS_PATH).'archive';
  156. if ($handle = opendir($path)) {
  157. while (false !== ($file = readdir($handle))) {
  158. if (strpos($file,$course_code)!==false) {
  159. if (is_dir($path.'/'.$file)) {
  160. rmdirr($path.'/'.$file);
  161. }
  162. }
  163. }
  164. closedir($handle);
  165. }
  166. // Check api session destroy
  167. if (!headers_sent() && session_id() != "") {
  168. $res=Session::destroy();
  169. }
  170. }
  171. }