work.lib.test.php 11 KB

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