test_manager.inc.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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.'admin/calendar.lib.php';
  32. require_once $maindir.'dropbox/dropbox_class.inc.php';
  33. require_once $maindir.'dropbox/dropbox_functions.inc.php';
  34. require_once $maindir.'survey/survey.lib.php';
  35. require_once $maindir.'exercice/export/scorm/scorm_classes.php';
  36. require_once $maindir.'exercice/export/qti2/qti2_classes.php';
  37. require_once $maindir.'exercice/export/exercise_import.inc.php';
  38. require_once $maindir.'exercice/exercise_result.class.php';
  39. require_once $maindir.'exercice/answer.class.php';
  40. require_once $maindir.'exercice/exercise.class.php';
  41. require_once $maindir.'exercice/fill_blanks.class.php';
  42. require_once $maindir.'exercice/freeanswer.class.php';
  43. require_once $maindir.'forum/forumfunction.inc.php';
  44. /*
  45. require_once $maindir.'gradebook/lib/be/gradebookitem.class.php';
  46. require_once $maindir.'gradebook/lib/be/abstractlink.class.php';
  47. require_once $maindir.'gradebook/lib/be/evallink.class.php';
  48. require_once $maindir.'gradebook/lib/be/linkfactory.class.php';
  49. require_once $maindir.'gradebook/lib/be/attendancelink.class.php';
  50. require_once $maindir.'gradebook/lib/be/category.class.php';
  51. require_once $maindir.'gradebook/lib/be/dropboxlink.class.php';
  52. require_once $maindir.'gradebook/lib/be/evaluation.class.php';
  53. require_once $maindir.'gradebook/lib/be/exerciselink.class.php';
  54. require_once $maindir.'gradebook/lib/be/dropboxlink.class.php';
  55. require_once $maindir.'gradebook/lib/be/result.class.php';
  56. require_once $maindir.'gradebook/lib/be/forumthreadlink.class.php';
  57. */
  58. require_once $maindir.'course_info/download.lib.php';
  59. /**Files inside '/../main/lib/' */
  60. require_once $libdir.'urlmanager.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.'fileManager.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.'main_api.lib.php';
  73. require_once $libdir.'add_course.lib.inc.php';
  74. require_once $libdir.'geometry.lib.php';
  75. ob_end_clean();
  76. /**Problem with this file to test objects*/
  77. //require_once $maindir.'exercice/exercise.lib.php';
  78. class TestManager
  79. {
  80. /* MAIN CODE */
  81. /**
  82. * This function create in the database a test course and will also load sessions.
  83. * This name will be change each time is used in the tests.
  84. * @param string Course name
  85. * @return void
  86. */
  87. function create_test_course($course_code = 'TESTCOURSE')
  88. {
  89. /* Table definitions */
  90. $table_course = Database::get_main_table(TABLE_MAIN_COURSE);
  91. $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
  92. $course_cat_table = Database::get_main_table(TABLE_MAIN_CATEGORY);
  93. global $_configuration, $_user, $_course, $cidReq;
  94. $cidReq = $course_code;
  95. /* Check if the course exists */
  96. $sql = "SELECT code FROM $table_course WHERE code = '$cidReq' ";
  97. $rs = Database::query($sql, __FILE__, __LINE__);
  98. $row = Database::fetch_row($rs);
  99. /* Create the course in the database */
  100. if (empty($row[0])) {
  101. // Create a course
  102. $course_data = array(
  103. 'wanted_code' => $cidReq,
  104. 'title' => $cidReq,
  105. 'tutor_name' => 'John Doe',
  106. 'category_code' => 'LANG',
  107. 'course_language' => 'spanish',
  108. 'course_admin_id' => '001',
  109. 'db_prefix' => $_configuration['db_prefix'],
  110. 'firstExpirationDelay' => '999'
  111. );
  112. $res = CourseManager::create_course(
  113. $course_data['wanted_code'],
  114. $course_data['title'],
  115. $course_data['tutor_name'],
  116. $course_data['category_code'],
  117. $course_data['course_language'],
  118. $course_data['course_admin_id'],
  119. $course_data['db_prefix'],
  120. $course_data['firstExpirationDelay']
  121. );
  122. }
  123. $sql = "SELECT course.*, course_category.code faCode, course_category.name faName
  124. FROM $course_table
  125. LEFT JOIN $course_cat_table
  126. ON course.category_code = course_category.code
  127. WHERE course.code = '$cidReq'";
  128. $result = Database::query($sql);
  129. /* Create the session */
  130. if (Database::num_rows($result) > 0) {
  131. $cData = Database::fetch_array($result);
  132. $_cid = $cData['code'];
  133. $_course = array();
  134. $_course['id'] = $cData['code']; //auto-assigned integer
  135. $_course['name'] = $cData['title'];
  136. $_course['official_code'] = $cData['visual_code']; // use in echo
  137. $_course['sysCode'] = $cData['code']; // use as key in db
  138. $_course['path'] = $cData['directory']; // use as key in path
  139. $_course['dbName'] = $cData['db_name']; // use as key in db list
  140. $_course['dbNameGlu'] = $_configuration['table_prefix'].$cData['db_name'].$_configuration['db_glue']; // use in all queries
  141. $_course['titular'] = $cData['tutor_name'];
  142. $_course['language'] = $cData['course_language'];
  143. $_course['extLink']['url'] = $cData['department_url'];
  144. $_course['extLink']['name'] = $cData['department_name'];
  145. $_course['categoryCode'] = $cData['faCode'];
  146. $_course['categoryName'] = $cData['faName'];
  147. $_course['visibility'] = $cData['visibility'];
  148. $_course['subscribe_allowed'] = $cData['subscribe'];
  149. $_course['unsubscribe'] = $cData['unsubscribe'];
  150. Session::write('_cid', $_cid);
  151. Session::write('_course', $_course);
  152. }
  153. /* Load the session */
  154. $_SESSION['_user']['user_id'] = 1;
  155. $_SESSION['is_courseAdmin'] = 1;
  156. $_SESSION['show'] = showall;
  157. /* Load the user */
  158. $_user['user_id'] = $_SESSION['_user']['user_id'];
  159. }
  160. /**
  161. * This function delete the test course from the database and destroy the sessions.
  162. * @param string the course code than will be delete.
  163. * @return void
  164. */
  165. function delete_test_course($course_code = 'TESTCOURSE')
  166. {
  167. $res = CourseManager::delete_course($course_code);
  168. $path = api_get_path(SYS_PATH).'archive';
  169. if ($handle = opendir($path)) {
  170. while (false !== ($file = readdir($handle))) {
  171. if (strpos($file, $course_code) !== false) {
  172. if (is_dir($path.'/'.$file)) {
  173. rmdirr($path.'/'.$file);
  174. }
  175. }
  176. }
  177. closedir($handle);
  178. }
  179. // Check api session destroy
  180. if (!headers_sent() && session_id() != "") {
  181. $res = Session::destroy();
  182. }
  183. }
  184. }