test_manager.inc.php 7.7 KB

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