work.lib.test.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. <?php
  2. require_once(api_get_path(SYS_CODE_PATH).'work/work.lib.php');
  3. require_once(api_get_path(LIBRARY_PATH).'course.lib.php');
  4. class TestWork extends UnitTestCase {
  5. /**
  6. * @param string Base work dir (.../work)
  7. * @param string $desiredDirName complete path of the desired name
  8. * @return string actual directory name if it succeeds, boolean false
  9. * otherwise
  10. */
  11. function testcreate_unexisting_work_directory() {
  12. $path_name = api_get_path(SYS_COURSE_PATH);
  13. $base_work_dir=$path_name.'testing/';
  14. $desired_dir_name= $path_name.'testing';
  15. $res=create_unexisting_work_directory($base_work_dir,$desired_dir_name);
  16. $this->assertTrue(is_bool($res));
  17. }
  18. /**
  19. * Builds the form thats enables the user to
  20. * select a directory to browse/upload in
  21. * This function has been copied from the document/document.inc.php library
  22. *
  23. * @param array $folders
  24. * @param string $curdirpath
  25. * @param string $group_dir
  26. * @return string html form
  27. */
  28. function testbuild_work_directory_selector() {
  29. $folders=array();
  30. $curdirpath='';
  31. $group_dir='';
  32. $res=build_work_directory_selector($folders,$curdirpath,$group_dir='');
  33. $this->assertTrue(is_string($res));
  34. //var_dump($res);
  35. }
  36. /**
  37. * Builds the form thats enables the user to
  38. * move a document from one directory to another
  39. * This function has been copied from the document/document.inc.php library
  40. *
  41. * @param array $folders
  42. * @param string $curdirpath
  43. * @param string $move_file
  44. * @return string html form
  45. */
  46. function testbuild_work_move_to_selector() {
  47. $folders=array();
  48. $curdirpath='';
  49. $move_file='';
  50. $group_dir='';
  51. $res=build_work_move_to_selector($folders,$curdirpath,$move_file,$group_dir='');
  52. $this->assertTrue(is_string($res));
  53. //var_dump($res);
  54. }
  55. function testconvert_date_to_array() {
  56. $date=date('d/m/Y',time());
  57. $group=array();
  58. $res=convert_date_to_array($date,$group);
  59. $this->assertTrue(is_array($res));
  60. //var_dump($res);
  61. }
  62. /**
  63. * @param string e.g converts "2008-10-06 12:45:00" to timestamp
  64. */
  65. function testconvert_date_to_number() {
  66. $default='2008-10-06 12:45:00';
  67. $res=convert_date_to_number($default);
  68. $this->assertTrue(is_numeric($res));
  69. //var_dump($res);
  70. }
  71. /**
  72. * This function displays the number of files contained in a directory
  73. * @param string the path of the directory complete e.g. /var/www/dokeos
  74. * @param boolean true if we want the total quantity of files include in others child directorys , false only files in the directory
  75. * @return array the first element is an integer with the number of files in the folder, the second element is the number of directories
  76. */
  77. function testcount_dir() {
  78. $path_name = api_get_path(SYS_COURSE_PATH);
  79. $path_dir=$path_name;
  80. $recurse=0;
  81. ob_start();
  82. $res=count_dir($path_dir, $recurse);
  83. $this->assertTrue(is_array($res));
  84. ob_end_clean();
  85. //var_dump($res);
  86. }
  87. /**
  88. * create a group of select from a date
  89. * @return array list
  90. */
  91. function testcreate_group_date_select() {
  92. $res=create_group_date_select($prefix='');
  93. $this->assertTrue(is_array($res));
  94. //var_dump($res);
  95. }
  96. /**
  97. * Transform an all directory structure (only directories) in an array
  98. * @param string path of the directory
  99. * @return array the directory structure into an array
  100. */
  101. function testdirectory_to_array() {
  102. $path_name = api_get_path(SYS_PATH);
  103. $directory= $path_name;
  104. $res=directory_to_array($directory);
  105. $this->assertTrue(is_array($res));
  106. //var_dump($res);
  107. }
  108. /**
  109. * Displays action links (for admins, authorized groups members and authorized students)
  110. * @param string Current dir
  111. * @param integer Whether to show tool options
  112. * @param integer Whether to show upload form option
  113. * @return void
  114. */
  115. function testdisplay_action_links() {
  116. $cur_dir_path='';
  117. $always_show_tool_options=1;
  118. $always_show_upload_form=1;
  119. ob_start();
  120. $res=display_action_links($cur_dir_path, $always_show_tool_options, $always_show_upload_form);
  121. $this->assertTrue(is_null($res));
  122. ob_end_clean();
  123. //var_dump($res);
  124. }
  125. /**
  126. * Displays the form where course admins can specify wether uploaded documents
  127. * are visible or invisible by default.
  128. *
  129. * @param $uploadvisibledisabled
  130. * @return html with two list radio button
  131. */
  132. function testdisplay_default_visibility_form() {
  133. $uploadvisibledisabled='';
  134. ob_start();
  135. $res=display_default_visibility_form($uploadvisibledisabled);
  136. $this->assertTrue(is_null($res));
  137. ob_end_clean();
  138. //var_dump($res);
  139. }
  140. /**
  141. * Display the list of student publications, taking into account the user status
  142. *
  143. * @param $currentCourseRepositoryWeb, the web location of the course folder
  144. * @param $link_target_parameter - should there be a target parameter for the links
  145. * @param $dateFormatLong - date format
  146. * @param $origin - typically empty or 'learnpath'
  147. */
  148. function testdisplay_student_publications_list() {
  149. global $charset,$timeNoSecFormat,$dateFormatShort,$gradebook,$dateFormatLong;
  150. $work_dir='';
  151. $sub_course_dir='';
  152. $currentCourseRepositoryWeb='';
  153. $link_target_parameter='';
  154. $origin='learnpath';
  155. $add_in_where_query='';
  156. ob_start();
  157. $res=display_student_publications_list($work_dir,$sub_course_dir,$currentCourseRepositoryWeb, $link_target_parameter, $dateFormatLong, $origin,$add_in_where_query='');
  158. $this->assertTrue(is_null($res));
  159. ob_end_clean();
  160. //var_dump($res);
  161. }
  162. /**
  163. * Displays all options for this tool.
  164. * These are
  165. * - make all files visible / invisible
  166. * - set the default visibility of uploaded files
  167. *
  168. * @param $uploadvisibledisabled
  169. * @param $origin
  170. * @param $base_work_dir Base working directory (up to '/work')
  171. * @param $cur_dir_path Current subdirectory of 'work/'
  172. * @param $cur_dir_path_url Current subdirectory of 'work/', url-encoded
  173. */
  174. function testdisplay_tool_options() {
  175. global $charset, $group_properties,$gradebook,$base_work_dir;
  176. $uploadvisibledisabled='';
  177. $origin='';
  178. $cur_dir_path='';
  179. $cur_dir_path_url='';
  180. ob_start();
  181. $res=display_tool_options($uploadvisibledisabled, $origin,$base_work_dir,$cur_dir_path,$cur_dir_path_url);
  182. $this->assertTrue(is_null($res));
  183. ob_end_clean();
  184. //var_dump($res);
  185. }
  186. /**
  187. * This function displays the firstname and lastname of the user as a link to the user tool.
  188. * @see this is the same function as in the new forum, so this probably has to move to a user library.
  189. * @todo move this function to the user library
  190. */
  191. function testdisplay_user_link() {
  192. global $_otherusers;
  193. $user_id=1;
  194. $name='';
  195. $res=display_user_link($user_id, $name='');
  196. $this->assertTrue(is_string($res));
  197. //var_dump($res);
  198. }
  199. /**
  200. * create a group of select from a date
  201. */
  202. function testget_date_from_group() {
  203. $group='';
  204. $res=get_date_from_group($group);
  205. $this->assertTrue(is_string($res));
  206. //var_dump($res);
  207. }
  208. /**
  209. * @param string Path of the directory
  210. * @return array The list of ids of all the directories in the path
  211. */
  212. function testget_parent_directories() {
  213. $my_cur_dir_path='';
  214. $res=get_parent_directories($my_cur_dir_path);
  215. $this->assertTrue(is_array($res));
  216. //var_dump($res);
  217. }
  218. /**
  219. * Returns a list of subdirectories found in the given directory.
  220. *
  221. * The list return starts from the given base directory.
  222. * If you require the subdirs of /var/www/ (or /var/www), you will get 'abc/', 'def/', but not '/var/www/abc/'...
  223. * @param string Base dir
  224. * @param integer 0 if we only want dirs from this level, 1 if we want to recurse into subdirs
  225. * @return strings_array The list of subdirs in 'abc/' form, -1 on error, and 0 if none found
  226. * @todo Add a session check to see if subdirs_list doesn't exist yet (cached copy)
  227. */
  228. function testget_subdirs_list() {
  229. $path_name = api_get_path(SYS_PATH);
  230. $basedir = $path_name.'/';
  231. $dh = opendir($basedir);
  232. $entry = readdir($dh);
  233. $dirs_list[] = $entry;
  234. $res=get_subdirs_list($basedir='',$recurse=0);
  235. $this->assertTrue(is_numeric($res));
  236. $this->assertTrue(is_array($dirs_list));
  237. //var_dump($res);
  238. //var_dump($dirs_list);
  239. }
  240. /**
  241. * Gets the id of a student publication with a given path
  242. * @param string $path
  243. * @return true if is found / false if not found
  244. */
  245. function testget_work_id() {
  246. global $cidReq;
  247. $path_name = api_get_path(SYS_PATH);
  248. $path=$path_name.$cidReq;
  249. $res=get_work_id($path);
  250. $this->assertTrue(is_bool($res));
  251. //var_dump($res);
  252. }
  253. /**
  254. * Get the path of a document in the student_publication table (path relative to the course directory)
  255. * @param integer Element ID
  256. * @return string Path (or -1 on error)
  257. */
  258. function testget_work_path() {
  259. $id=1;
  260. $res=get_work_path($id);
  261. $this->assertTrue(is_numeric($res));
  262. //var_dump($res);
  263. }
  264. /**
  265. * Insert into the DB of the course all the directories
  266. * @param string path of the /work directory of the course
  267. * @return -1 on error, sql query result on success
  268. * @version April 2008
  269. */
  270. function testinsert_all_directory_in_course_table() {
  271. $path_name = api_get_path(SYS_COURSE_PATH);
  272. $base_work_dir=$path_name.'work/testing';
  273. $dir_to_array =directory_to_array($base_work_dir,true);
  274. $res=insert_all_directory_in_course_table($base_work_dir);
  275. $this->assertTrue(is_null($res));
  276. //var_dump($res);
  277. }
  278. /**
  279. * Checks if the first given directory exists as a subdir of the second given directory
  280. * This function should now be deprecated by Security::check_abs_path()
  281. * @param string Subdir
  282. * @param string Base dir
  283. * @return integer -1 on error, 0 if not subdir, 1 if subdir
  284. */
  285. function testis_subdir_of() {
  286. $path_name = api_get_path(SYS_COURSE_PATH);
  287. $subdir=$path_name.'work/testing';
  288. $basedir=$path_name;
  289. $res=is_subdir_of($subdir,$basedir);
  290. $this->assertTrue(is_numeric($res));
  291. //var_dump($res);
  292. }
  293. /**
  294. * returns all the javascript that is required for easily
  295. * validation when you create a work
  296. * this goes into the $htmlHeadXtra[] array
  297. */
  298. function testto_javascript_work() {
  299. $res=to_javascript_work();
  300. $this->assertTrue(is_string($res));
  301. //var_dump($res);
  302. }
  303. /**
  304. * converts 1-9 to 01-09
  305. */
  306. function testtwo_digits() {
  307. $number=1;
  308. $res=two_digits($number);
  309. $this->assertTrue(is_numeric($res));
  310. //var_dump($res);
  311. }
  312. /**
  313. * Update the url of a dir in the student_publication table
  314. * @param string old path
  315. * @param string new path
  316. */
  317. function testupdate_dir_name() {
  318. global $base_work_dir;
  319. $path='';
  320. $new_name='';
  321. $res=update_dir_name($path,$new_name);
  322. $this->assertTrue(is_null($res));
  323. //var_dump($res);
  324. }
  325. /**
  326. * Update the url of a work in the student_publication table
  327. * @param integer ID of the work to update
  328. * @param string Destination directory where the work has been moved (must end with a '/')
  329. * @return -1 on error, sql query result on success
  330. */
  331. function testupdate_work_url() {
  332. $id=1;
  333. $path_name = api_get_path(SYS_COURSE_PATH);
  334. $new_path=$path_name.'work/testing';
  335. $res=update_work_url($id,$new_path);
  336. $this->assertTrue(is_numeric($res));
  337. //var_dump($res);
  338. }
  339. /**
  340. * Delete a work-tool directory
  341. * @param string Base "work" directory for this course as /var/www/dokeos/courses/ABCD/work/
  342. * @param string The directory name as the bit after "work/", without trailing slash
  343. * @return integer -1 on error
  344. */
  345. function testdel_dir() {
  346. global $cidReq;
  347. $path_name = api_get_path(SYS_PATH);
  348. $base_work_dir=$path_name.$cidReq.'work/testing';
  349. $dir= $path_name.'testing/';
  350. $id=-1;
  351. $res=del_dir($base_work_dir,$dir,$id);
  352. $this->assertTrue(is_numeric($res));
  353. //var_dump($res);
  354. }
  355. /**
  356. * This functon only is added to the end of the test and the end of the files in the all test.
  357. */
  358. /*
  359. public function testDeleteCourse() {
  360. global $cidReq;
  361. $resu = CourseManager::delete_course($cidReq);
  362. session_destroy();
  363. }
  364. */
  365. public function testDeleteCourse() {
  366. $code = 'COURSEX';
  367. $res = CourseManager::delete_course($code);
  368. $path = api_get_path(SYS_PATH).'archive';
  369. if ($handle = opendir($path)) {
  370. while (false !== ($file = readdir($handle))) {
  371. if (strpos($file,$code)!==false) {
  372. if (is_dir($path.'/'.$file)) {
  373. rmdirr($path.'/'.$file);
  374. }
  375. }
  376. }
  377. closedir($handle);
  378. }
  379. session_destroy();
  380. }
  381. }
  382. ?>