work.lib.test.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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_work() {
  192. global $_otherusers;
  193. $user_id=1;
  194. $res=display_user_link_work($user_id, $name='');
  195. $this->assertTrue(is_string($res));
  196. //var_dump($res);
  197. }
  198. /**
  199. * create a group of select from a date
  200. */
  201. function testget_date_from_group() {
  202. $group='';
  203. $res=get_date_from_group($group);
  204. $this->assertTrue(is_string($res));
  205. //var_dump($res);
  206. }
  207. /**
  208. * @param string Path of the directory
  209. * @return array The list of ids of all the directories in the path
  210. */
  211. function testget_parent_directories() {
  212. $my_cur_dir_path='';
  213. $res=get_parent_directories($my_cur_dir_path);
  214. $this->assertTrue(is_array($res));
  215. //var_dump($res);
  216. }
  217. /**
  218. * Returns a list of subdirectories found in the given directory.
  219. *
  220. * The list return starts from the given base directory.
  221. * If you require the subdirs of /var/www/ (or /var/www), you will get 'abc/', 'def/', but not '/var/www/abc/'...
  222. * @param string Base dir
  223. * @param integer 0 if we only want dirs from this level, 1 if we want to recurse into subdirs
  224. * @return strings_array The list of subdirs in 'abc/' form, -1 on error, and 0 if none found
  225. * @todo Add a session check to see if subdirs_list doesn't exist yet (cached copy)
  226. */
  227. function testget_subdirs_list() {
  228. $path_name = api_get_path(SYS_PATH);
  229. $basedir = $path_name.'/';
  230. $dh = opendir($basedir);
  231. $entry = readdir($dh);
  232. $dirs_list[] = $entry;
  233. $res=get_subdirs_list($basedir='',$recurse=0);
  234. $this->assertTrue(is_numeric($res));
  235. $this->assertTrue(is_array($dirs_list));
  236. //var_dump($res);
  237. //var_dump($dirs_list);
  238. }
  239. /**
  240. * Gets the id of a student publication with a given path
  241. * @param string $path
  242. * @return true if is found / false if not found
  243. */
  244. function testget_work_id() {
  245. global $cidReq;
  246. $path_name = api_get_path(SYS_PATH);
  247. $path=$path_name.$cidReq;
  248. $res=get_work_id($path);
  249. $this->assertTrue(is_bool($res));
  250. //var_dump($res);
  251. }
  252. /**
  253. * Get the path of a document in the student_publication table (path relative to the course directory)
  254. * @param integer Element ID
  255. * @return string Path (or -1 on error)
  256. */
  257. function testget_work_path() {
  258. $id=1;
  259. $res=get_work_path($id);
  260. $this->assertTrue(is_numeric($res));
  261. //var_dump($res);
  262. }
  263. /**
  264. * Insert into the DB of the course all the directories
  265. * @param string path of the /work directory of the course
  266. * @return -1 on error, sql query result on success
  267. * @version April 2008
  268. */
  269. function testinsert_all_directory_in_course_table() {
  270. $path_name = api_get_path(SYS_COURSE_PATH);
  271. $base_work_dir=$path_name.'work/testing';
  272. $dir_to_array =directory_to_array($base_work_dir,true);
  273. $res=insert_all_directory_in_course_table($base_work_dir);
  274. $this->assertTrue(is_null($res));
  275. //var_dump($res);
  276. }
  277. /**
  278. * Checks if the first given directory exists as a subdir of the second given directory
  279. * This function should now be deprecated by Security::check_abs_path()
  280. * @param string Subdir
  281. * @param string Base dir
  282. * @return integer -1 on error, 0 if not subdir, 1 if subdir
  283. */
  284. function testis_subdir_of() {
  285. $path_name = api_get_path(SYS_COURSE_PATH);
  286. $subdir=$path_name.'work/testing';
  287. $basedir=$path_name;
  288. $res=is_subdir_of($subdir,$basedir);
  289. $this->assertTrue(is_numeric($res));
  290. //var_dump($res);
  291. }
  292. /**
  293. * returns all the javascript that is required for easily
  294. * validation when you create a work
  295. * this goes into the $htmlHeadXtra[] array
  296. */
  297. function testto_javascript_work() {
  298. $res=to_javascript_work();
  299. $this->assertTrue(is_string($res));
  300. //var_dump($res);
  301. }
  302. /**
  303. * converts 1-9 to 01-09
  304. */
  305. function testtwo_digits() {
  306. $number=1;
  307. $res=two_digits($number);
  308. $this->assertTrue(is_numeric($res));
  309. //var_dump($res);
  310. }
  311. /**
  312. * Update the url of a dir in the student_publication table
  313. * @param string old path
  314. * @param string new path
  315. */
  316. function testupdate_dir_name() {
  317. global $base_work_dir;
  318. $path='';
  319. $new_name='';
  320. $res=update_dir_name($path,$new_name);
  321. $this->assertTrue(is_null($res));
  322. //var_dump($res);
  323. }
  324. /**
  325. * Update the url of a work in the student_publication table
  326. * @param integer ID of the work to update
  327. * @param string Destination directory where the work has been moved (must end with a '/')
  328. * @return -1 on error, sql query result on success
  329. */
  330. function testupdate_work_url() {
  331. $id=1;
  332. $path_name = api_get_path(SYS_COURSE_PATH);
  333. $new_path=$path_name.'work/testing';
  334. $res=update_work_url($id,$new_path);
  335. $this->assertTrue(is_numeric($res));
  336. //var_dump($res);
  337. }
  338. /**
  339. * Delete a work-tool directory
  340. * @param string Base "work" directory for this course as /var/www/dokeos/courses/ABCD/work/
  341. * @param string The directory name as the bit after "work/", without trailing slash
  342. * @return integer -1 on error
  343. */
  344. function testdel_dir() {
  345. global $cidReq;
  346. $path_name = api_get_path(SYS_PATH);
  347. $base_work_dir=$path_name.$cidReq.'work/testing';
  348. $dir= $path_name.'testing/';
  349. $id=-1;
  350. $res=del_dir($base_work_dir,$dir,$id);
  351. $this->assertTrue(is_numeric($res));
  352. //var_dump($res);
  353. }
  354. }
  355. ?>