fileUpload.lib.test.php 10 KB

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