test_manager.inc.php 7.8 KB

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