fileUpload.lib.test.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <?php
  2. require_once(api_get_path(LIBRARY_PATH).'fileUpload.lib.php');
  3. require_once(api_get_path(LIBRARY_PATH).'document.lib.php');
  4. require_once(api_get_path(LIBRARY_PATH).'pclzip/pclzip.lib.php');
  5. class TestFileUpload extends UnitTestCase
  6. {
  7. public function __construct()
  8. {
  9. $this->UnitTestCase('File upload library - main/inc/lib/fileUpload.lib.test.php');
  10. }
  11. //Creating
  12. function testCreateUnexistingDirectory()
  13. {
  14. global $_course, $user_id;
  15. $to_group_id = 1;
  16. $to_user_id = 1;
  17. $base_work_dir = api_get_path(SYS_COURSE_PATH).'document/';
  18. $desired_dir_name = 'images';
  19. $res = create_unexisting_directory(
  20. $_course,
  21. $user_id,
  22. $to_group_id,
  23. $to_user_id,
  24. $base_work_dir,
  25. $desired_dir_name
  26. );
  27. if (!is_null($res)) {
  28. $this->assertTrue(is_bool($res));
  29. $this->assertFalse($res);
  30. }
  31. }
  32. /**
  33. * Creates a file containing an html redirection to a given url
  34. * @param string $filePath
  35. * @param string $url
  36. * @return void
  37. */
  38. function testCreateLinkFile()
  39. {
  40. $base_work_dir = api_get_path(SYS_COURSE_PATH);
  41. $filePath = $base_work_dir.'upload/blog';
  42. $dirurl = api_get_path(WEB_COURSE_PATH);
  43. $url = $dirurl.'doc.php';
  44. $res = create_link_file($filePath, $url);
  45. if (!is_numeric($res)) {
  46. $this->assertFalse($res);
  47. } else {
  48. $this->assertTrue($res);
  49. }
  50. }
  51. /**
  52. * This recursive function can be used during the upgrade process form older versions of Dokeos
  53. * It crawls the given directory, checks if the file is in the DB and adds it if it's not
  54. *
  55. * @param string $base_work_dir
  56. * @param string $current_path , needed for recursivity
  57. */
  58. function testAddAllDocumentsInFolderToDatabase()
  59. {
  60. global $_course, $user_id;
  61. $base_work_dir = api_get_path(SYS_PATH).'';
  62. $current_path = 'courses/';
  63. $to_group_id = 0;
  64. $res = add_all_documents_in_folder_to_database($_course, $user_id, $base_work_dir, $current_path, $to_group_id);
  65. $this->assertTrue(is_null($res));
  66. //var_dump($res);
  67. }
  68. function testAddDocument()
  69. {
  70. global $_course;
  71. $path = '';
  72. $filetype = '';
  73. $filesize = '';
  74. $title = '';
  75. $res = add_document($_course, $path, $filetype, $filesize, $title);
  76. if (!is_numeric($res)) :
  77. $this->assertTrue(is_bool($res));
  78. endif;
  79. //var_dump($res);
  80. }
  81. function testAddExtOnMime()
  82. {
  83. $fileName = '';
  84. $fileType = '';
  85. $res = add_ext_on_mime($fileName, $fileType);
  86. $this->assertTrue(is_string($res));
  87. //var_dump($res);
  88. }
  89. //build
  90. function testBuildMissingFilesForm()
  91. {
  92. global $_course;
  93. $missing_files = array();
  94. $upload_path = api_get_path(SYS_CODE_PATH).'default_course_document/images';
  95. $file_name = 'board.jpg';
  96. $res = build_missing_files_form($missing_files, $upload_path, $file_name);
  97. $this->assertTrue(is_string($res));
  98. //var_dump($res);
  99. }
  100. //check
  101. function testCheckForMissingFiles()
  102. {
  103. $file = '';
  104. $res = check_for_missing_files($file);
  105. $this->assertTrue(is_bool($res));
  106. }
  107. //space
  108. function testDirTotalSpace()
  109. {
  110. $dirPath = api_get_path(SYS_CODE_PATH).'default_course_document/images';
  111. $res = dir_total_space($dirPath);
  112. $this->assertTrue($res > 0, 'The default_course_document/images dir should be larger than 0 bytes');
  113. }
  114. //filter
  115. function testfilter_extension()
  116. {
  117. $filename = 'index.php';
  118. $res = filter_extension($filename);
  119. $this->assertTrue(is_numeric($res));
  120. }
  121. //get
  122. function testget_document_title()
  123. {
  124. $name = '';
  125. $res = get_document_title($name);
  126. $this->assertTrue(is_string($res));
  127. //var_dump($res);
  128. }
  129. function testget_levels()
  130. {
  131. $filename = 'readme.txt';
  132. $res = get_levels($filename);
  133. $this->assertTrue(is_numeric($res));
  134. //var_dump($res);
  135. }
  136. //access
  137. function testhtaccess2txt()
  138. {
  139. $filename = 'readme.txt';
  140. $res = htaccess2txt($filename);
  141. $this->assertTrue(is_string($res));
  142. //var_dump($res);
  143. }
  144. //moving
  145. function testmove_uploaded_file_collection_into_directory()
  146. {
  147. global $_course;
  148. $uploaded_file_collection = array();
  149. $base_work_dir = api_get_path(SYS_COURSE_PATH).'upload/';
  150. $missing_files_dir = '';
  151. $user_id = 1;
  152. $to_group_id = '';
  153. $to_user_id = '';
  154. $max_filled_space = '';
  155. $res = move_uploaded_file_collection_into_directory(
  156. $_course,
  157. $uploaded_file_collection,
  158. $base_work_dir,
  159. $missing_files_dir,
  160. $user_id,
  161. $to_group_id,
  162. $to_user_id,
  163. $max_filled_space
  164. );
  165. $this->assertTrue(is_null($res));
  166. //var_dump($res);
  167. }
  168. //php?
  169. function testphp2phps()
  170. {
  171. $fileName = 'index.php';
  172. $res = php2phps($fileName);
  173. $this->assertTrue(is_string($res));
  174. //var_dump($res);
  175. }
  176. //searching
  177. function testsearch_img_from_html()
  178. {
  179. $imgFilePath = array();
  180. $htmlFile = 'file:///var/www/chamilotest/documentation/credits.html';
  181. $res = search_img_from_html($htmlFile);
  182. if (is_array($res)) {
  183. $this->assertTrue(is_array($res));
  184. } else {
  185. $this->assertTrue(is_null($res));
  186. }
  187. //var_dump($res);
  188. }
  189. //uploading
  190. function testprocess_uploaded_file()
  191. {
  192. $uploaded_file = '';
  193. ob_start();
  194. $res = process_uploaded_file($uploaded_file);
  195. ob_end_clean();
  196. $this->assertTrue(is_bool($res));
  197. }
  198. function testhandle_uploaded_document()
  199. {
  200. global $_course;
  201. $uploaded_file = '';
  202. $base_work_dir = '';
  203. $upload_path = '';
  204. $user_id = 1;
  205. $to_group_id = 0;
  206. $to_user_id = null;
  207. $unzip = 0;
  208. $what_if_file_exists = '';
  209. $output = true;
  210. $clean_name = disable_dangerous_file($clean_name);
  211. $where_to_save = $base_work_dir.$upload_path;
  212. $new_file_path = $upload_path.$clean_name;
  213. $clean_name = disable_dangerous_file($clean_name);
  214. $file_path = $upload_path.$clean_name;
  215. ob_start();
  216. $res = handle_uploaded_document(
  217. $_course,
  218. $uploaded_file,
  219. $base_work_dir,
  220. $upload_path,
  221. $user_id,
  222. $to_group_id = 0,
  223. $to_user_id = null,
  224. $unzip = 0,
  225. $what_if_file_exists = '',
  226. $output = true
  227. );
  228. $this->assertTrue(is_null($res));
  229. ob_end_clean();
  230. //var_dump($res);
  231. }
  232. //updating
  233. function testupdate_existing_document()
  234. {
  235. $_course = '';
  236. $document_id = '';
  237. $filesize = '';
  238. $res = update_existing_document($_course, $document_id, $filesize, $readonly = 0);
  239. $this->assertTrue(is_bool($res));
  240. //var_dump($res);
  241. }
  242. function testitem_property_update_on_folder()
  243. {
  244. global $_course, $user_id;
  245. $path = api_get_path(SYS_COURSE_PATH).'document/license.txt';
  246. $res = item_property_update_on_folder($_course, $path, $user_id);
  247. $this->assertTrue(is_null($res));
  248. //var_dump($res);
  249. }
  250. //unzipping
  251. function testunzip_uploaded_document()
  252. {
  253. //require_once(api_get_path(LIBRARY_PATH).'pclzip/pclzip.lib.php');
  254. global $_course;
  255. global $_user;
  256. global $to_user_id;
  257. global $to_group_id;
  258. $uploaded_file = '';
  259. $upload_path = '';
  260. $base_work_dir = '';
  261. $max_filled_space = '';
  262. ob_start();
  263. $res = unzip_uploaded_document(
  264. $uploaded_file,
  265. $upload_path,
  266. $base_work_dir,
  267. $max_filled_space,
  268. $output = true,
  269. $to_group_id = 0
  270. );
  271. ob_end_clean();
  272. $this->assertTrue(is_bool($res));
  273. //var_dump($res);
  274. }
  275. function testunzip_uploaded_file()
  276. {
  277. global $_course;
  278. $uploadedFile = 'README.txt';
  279. $uploadPath = api_get_path(SYS_PATH).$_course.'/document';
  280. $baseWorkDir = api_get_path(SYS_PATH);
  281. $maxFilledSpace = 1000;
  282. $res = unzip_uploaded_file($uploadedFile, $uploadPath, $baseWorkDir, $maxFilledSpace);
  283. $this->assertTrue(is_bool($res));
  284. //var_dump($res);
  285. }
  286. //disable
  287. function testDisableDangerousFile()
  288. {
  289. $filename = 'index.php';
  290. $res = disable_dangerous_file($filename);
  291. $this->assertTrue(is_string($res));
  292. //var_dump($res);
  293. }
  294. //replace
  295. function testreplace_img_path_in_html_file()
  296. {
  297. global $_course;
  298. $originalImgPath = '';
  299. $newImgPath = '';
  300. $htmlFile = 'file:///var/www/chamilotest/documentation/credits.html';
  301. $res = replace_img_path_in_html_file($originalImgPath, $newImgPath, $htmlFile);
  302. $this->assertTrue(is_null($res));
  303. //var_dump($res);
  304. }
  305. function testCleanUpFilesInZip()
  306. {
  307. $p_event = '';
  308. $p_header['filename'] = '';
  309. $res = clean_up_files_in_zip($p_event, &$p_header);
  310. $this->assertTrue(is_numeric($res));
  311. //var_dump($res);
  312. }
  313. function testCleanUpPath()
  314. {
  315. $base_work_dir = api_get_path(SYS_COURSE_PATH);
  316. $path = $base_work_dir.'upload/blog';
  317. $res = clean_up_path($path);
  318. $this->assertTrue(is_numeric($res));
  319. }
  320. }