work.lib.test.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. * This function displays the number of files contained in a directory
  64. * @param string the path of the directory complete e.g. /var/www/dokeos
  65. * @param boolean true if we want the total quantity of files include in others child directorys , false only files in the directory
  66. * @return array the first element is an integer with the number of files in the folder, the second element is the number of directories
  67. */
  68. function testcount_dir() {
  69. $path_name = api_get_path(SYS_COURSE_PATH);
  70. $path_dir=$path_name;
  71. $recurse=0;
  72. ob_start();
  73. $res=count_dir($path_dir, $recurse);
  74. $this->assertTrue(is_array($res));
  75. ob_end_clean();
  76. //var_dump($res);
  77. }
  78. /**
  79. * create a group of select from a date
  80. * @return array list
  81. */
  82. function testcreate_group_date_select() {
  83. $res=create_group_date_select($prefix='');
  84. $this->assertTrue(is_array($res));
  85. //var_dump($res);
  86. }
  87. /**
  88. * Transform an all directory structure (only directories) in an array
  89. * @param string path of the directory
  90. * @return array the directory structure into an array
  91. */
  92. function testdirectory_to_array() {
  93. $path_name = api_get_path(SYS_PATH);
  94. $directory= $path_name;
  95. $res=directory_to_array($directory);
  96. $this->assertTrue(is_array($res));
  97. //var_dump($res);
  98. }
  99. /**
  100. * Displays action links (for admins, authorized groups members and authorized students)
  101. * @param string Current dir
  102. * @param integer Whether to show tool options
  103. * @param integer Whether to show upload form option
  104. * @return void
  105. */
  106. function testdisplay_action_links() {
  107. $cur_dir_path='';
  108. $always_show_tool_options=1;
  109. $always_show_upload_form=1;
  110. ob_start();
  111. $res=display_action_links($cur_dir_path, $always_show_tool_options, $always_show_upload_form);
  112. $this->assertTrue(is_null($res));
  113. ob_end_clean();
  114. //var_dump($res);
  115. }
  116. /**
  117. * Displays the form where course admins can specify wether uploaded documents
  118. * are visible or invisible by default.
  119. *
  120. * @param $uploadvisibledisabled
  121. * @return html with two list radio button
  122. */
  123. function testdisplay_default_visibility_form() {
  124. $uploadvisibledisabled='';
  125. ob_start();
  126. $res=display_default_visibility_form($uploadvisibledisabled);
  127. $this->assertTrue(is_null($res));
  128. ob_end_clean();
  129. //var_dump($res);
  130. }
  131. /**
  132. * Displays all options for this tool.
  133. * These are
  134. * - make all files visible / invisible
  135. * - set the default visibility of uploaded files
  136. *
  137. * @param $uploadvisibledisabled
  138. * @param $origin
  139. * @param $base_work_dir Base working directory (up to '/work')
  140. * @param $cur_dir_path Current subdirectory of 'work/'
  141. * @param $cur_dir_path_url Current subdirectory of 'work/', url-encoded
  142. */
  143. function testdisplay_tool_options() {
  144. global $charset, $group_properties,$gradebook,$base_work_dir;
  145. $uploadvisibledisabled='';
  146. $origin='';
  147. $cur_dir_path='';
  148. $cur_dir_path_url='';
  149. ob_start();
  150. $res=display_tool_options($uploadvisibledisabled, $origin,$base_work_dir,$cur_dir_path,$cur_dir_path_url);
  151. $this->assertTrue(is_null($res));
  152. ob_end_clean();
  153. //var_dump($res);
  154. }
  155. /**
  156. * This function displays the firstname and lastname of the user as a link to the user tool.
  157. * @see this is the same function as in the new forum, so this probably has to move to a user library.
  158. * @todo move this function to the user library
  159. */
  160. function testdisplay_user_link_work() {
  161. global $_otherusers;
  162. $user_id=1;
  163. $res=display_user_link_work($user_id, $name='');
  164. $this->assertTrue(is_string($res));
  165. //var_dump($res);
  166. }
  167. /**
  168. * create a group of select from a date
  169. */
  170. function testget_date_from_group() {
  171. $group='';
  172. $res=get_date_from_group($group);
  173. $this->assertTrue(is_string($res));
  174. //var_dump($res);
  175. }
  176. /**
  177. * @param string Path of the directory
  178. * @return array The list of ids of all the directories in the path
  179. */
  180. function testget_parent_directories() {
  181. $my_cur_dir_path='';
  182. $res=get_parent_directories($my_cur_dir_path);
  183. $this->assertTrue(is_array($res));
  184. //var_dump($res);
  185. }
  186. /**
  187. * Gets the id of a student publication with a given path
  188. * @param string $path
  189. * @return true if is found / false if not found
  190. */
  191. function testget_work_id() {
  192. global $cidReq;
  193. $path_name = api_get_path(SYS_PATH);
  194. $path=$path_name.$cidReq;
  195. $res=get_work_id($path);
  196. $this->assertTrue(is_bool($res));
  197. //var_dump($res);
  198. }
  199. /**
  200. * Get the path of a document in the student_publication table (path relative to the course directory)
  201. * @param integer Element ID
  202. * @return string Path (or -1 on error)
  203. */
  204. function testget_work_path() {
  205. $id=1;
  206. $res=get_work_path($id);
  207. $this->assertTrue(is_numeric($res));
  208. //var_dump($res);
  209. }
  210. /**
  211. * Insert into the DB of the course all the directories
  212. * @param string path of the /work directory of the course
  213. * @return -1 on error, sql query result on success
  214. * @version April 2008
  215. */
  216. function testinsert_all_directory_in_course_table() {
  217. $path_name = api_get_path(SYS_COURSE_PATH);
  218. $base_work_dir=$path_name.'work/testing';
  219. $dir_to_array =directory_to_array($base_work_dir,true);
  220. $res=insert_all_directory_in_course_table($base_work_dir);
  221. $this->assertTrue(is_null($res));
  222. //var_dump($res);
  223. }
  224. /**
  225. * Checks if the first given directory exists as a subdir of the second given directory
  226. * This function should now be deprecated by Security::check_abs_path()
  227. * @param string Subdir
  228. * @param string Base dir
  229. * @return integer -1 on error, 0 if not subdir, 1 if subdir
  230. */
  231. function testis_subdir_of() {
  232. $path_name = api_get_path(SYS_COURSE_PATH);
  233. $subdir=$path_name.'work/testing';
  234. $basedir=$path_name;
  235. $res=is_subdir_of($subdir,$basedir);
  236. $this->assertTrue(is_numeric($res));
  237. //var_dump($res);
  238. }
  239. /**
  240. * returns all the javascript that is required for easily
  241. * validation when you create a work
  242. * this goes into the $htmlHeadXtra[] array
  243. */
  244. function testto_javascript_work() {
  245. $res=to_javascript_work();
  246. $this->assertTrue(is_string($res));
  247. //var_dump($res);
  248. }
  249. /**
  250. * converts 1-9 to 01-09
  251. */
  252. function testtwo_digits() {
  253. $number=1;
  254. $res=two_digits($number);
  255. $this->assertTrue(is_numeric($res));
  256. //var_dump($res);
  257. }
  258. /**
  259. * Update the url of a dir in the student_publication table
  260. * @param string old path
  261. * @param string new path
  262. */
  263. function testupdate_dir_name() {
  264. global $base_work_dir;
  265. $path='';
  266. $new_name='';
  267. $res=update_dir_name($path,$new_name);
  268. $this->assertTrue(is_null($res));
  269. //var_dump($res);
  270. }
  271. /**
  272. * Update the url of a work in the student_publication table
  273. * @param integer ID of the work to update
  274. * @param string Destination directory where the work has been moved (must end with a '/')
  275. * @return -1 on error, sql query result on success
  276. */
  277. function testupdate_work_url() {
  278. $id=1;
  279. $path_name = api_get_path(SYS_COURSE_PATH);
  280. $new_path=$path_name.'work/testing';
  281. $res=update_work_url($id,$new_path);
  282. $this->assertTrue(is_numeric($res));
  283. //var_dump($res);
  284. }
  285. /**
  286. * Delete a work-tool directory
  287. * @param string Base "work" directory for this course as /var/www/dokeos/courses/ABCD/work/
  288. * @param string The directory name as the bit after "work/", without trailing slash
  289. * @return integer -1 on error
  290. */
  291. function testdel_dir() {
  292. global $cidReq;
  293. $path_name = api_get_path(SYS_PATH);
  294. $base_work_dir=$path_name.$cidReq.'work/testing';
  295. $dir= $path_name.'testing/';
  296. $id=-1;
  297. $res=del_dir($base_work_dir,$dir,$id);
  298. $this->assertTrue(is_numeric($res));
  299. //var_dump($res);
  300. }
  301. }
  302. ?>