fileUpload.lib.test.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 testhandle_uploaded_document() {
  178. global $_course;
  179. $uploaded_file='';
  180. $base_work_dir='';
  181. $upload_path='';
  182. $user_id=1;
  183. $to_group_id=0;
  184. $to_user_id=NULL;
  185. $unzip=0;
  186. $what_if_file_exists='';
  187. $output=true;
  188. $clean_name = disable_dangerous_file($clean_name);
  189. $where_to_save = $base_work_dir.$upload_path;
  190. $new_name = unique_name($where_to_save, $clean_name);
  191. $new_file_path = $upload_path.$new_name;
  192. $clean_name = disable_dangerous_file($clean_name);
  193. $file_path = $upload_path.$clean_name;
  194. ob_start();
  195. $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);
  196. $this->assertTrue(is_null($res));
  197. ob_end_clean();
  198. //var_dump($res);
  199. }
  200. //updating
  201. function testupdate_existing_document() {
  202. $_course='';
  203. $document_id='';
  204. $filesize='';
  205. $res= update_existing_document($_course,$document_id,$filesize,$readonly=0);
  206. $this->assertTrue(is_bool($res));
  207. //var_dump($res);
  208. }
  209. function testitem_property_update_on_folder() {
  210. global $_course, $user_id;
  211. $path=api_get_path(SYS_COURSE_PATH).'document/license.txt';
  212. $res= item_property_update_on_folder($_course,$path,$user_id);
  213. $this->assertTrue(is_null($res));
  214. //var_dump($res);
  215. }
  216. //unique name
  217. function testunique_name() {
  218. $path='';
  219. $name='';
  220. $res= unique_name($path,$name);
  221. $this->assertTrue(is_string($res));
  222. //var_dump($res);
  223. }
  224. //unzipping
  225. function testunzip_uploaded_document() {
  226. //require_once(api_get_path(LIBRARY_PATH).'pclzip/pclzip.lib.php');
  227. global $_course;
  228. global $_user;
  229. global $to_user_id;
  230. global $to_group_id;
  231. $uploaded_file='';
  232. $upload_path='';
  233. $base_work_dir='';
  234. $max_filled_space='';
  235. ob_start();
  236. $res= unzip_uploaded_document($uploaded_file, $upload_path, $base_work_dir, $max_filled_space, $output = true, $to_group_id=0);
  237. ob_end_clean();
  238. $this->assertTrue(is_bool($res));
  239. //var_dump($res);
  240. }
  241. function testunzip_uploaded_file() {
  242. global $_course;
  243. $uploadedFile = 'README.txt';
  244. $uploadPath = api_get_path(SYS_PATH).$_course.'/document';
  245. $baseWorkDir = api_get_path(SYS_PATH);
  246. $maxFilledSpace = 1000;
  247. $res= unzip_uploaded_file($uploadedFile, $uploadPath, $baseWorkDir, $maxFilledSpace);
  248. $this->assertTrue(is_bool($res));
  249. //var_dump($res);
  250. }
  251. //disable
  252. function testDisableDangerousFile() {
  253. $filename = 'index.php';
  254. $res= disable_dangerous_file($filename);
  255. $this->assertTrue(is_string($res));
  256. //var_dump($res);
  257. }
  258. //replace
  259. function testreplace_img_path_in_html_file() {
  260. global $_course;
  261. $originalImgPath='';
  262. $newImgPath='';
  263. $htmlFile='file:///var/www/chamilotest/documentation/credits.html';
  264. $res= replace_img_path_in_html_file($originalImgPath, $newImgPath, $htmlFile);
  265. $this->assertTrue(is_null($res));
  266. //var_dump($res);
  267. }
  268. function testApiReplaceLinksInHtml() {
  269. $base_work_dir=api_get_path(SYS_COURSE_PATH);
  270. $upload_path=$base_work_dir.'upload/blog';
  271. $full_file_name = 'doc.php';
  272. $res=api_replace_links_in_html($upload_path,$full_file_name);
  273. $this->assertTrue(is_null($res));
  274. //var_dump($res);
  275. }
  276. function testApiReplaceLinksInString() {
  277. $base_work_dir=api_get_path(SYS_COURSE_PATH);
  278. $upload_path=$base_work_dir.'upload/blog';
  279. $buffer=ob_get_contents();
  280. $res=api_replace_links_in_string($upload_path,$buffer);
  281. $this->assertTrue(is_string($res));
  282. //var_dump($res);
  283. }
  284. function testApiReplaceParameter() {
  285. $count = 0;
  286. $matches = array();
  287. $href_list=array();
  288. $file_path_list[] = $matches[1];
  289. $base_work_dir=api_get_path(SYS_COURSE_PATH);
  290. $upload_path=$base_work_dir.'upload/blog';
  291. $replaceWhat[$count] = $href_list[$count];
  292. /** To can test this function you need to comment "die ('can not create file')"
  293. * $res return void
  294. */
  295. $replaceBy[$count] = " $param_name=\"" . $file_path_list[$count] . "\" target =\"_top\"";
  296. $replaceBy[$count] = $replaceWhat[$count];
  297. $buffer = str_replace($replaceWhat, $replaceBy, $buffer);
  298. $param_name="src";
  299. $res=api_replace_parameter($upload_path, $buffer, $param_name="src");
  300. $this->assertTrue(is_string($res));
  301. //var_dump($res);
  302. }
  303. //clenaning
  304. function testCleanUpFilesInZip() {
  305. $p_event='';
  306. $p_header['filename']='';
  307. $res=clean_up_files_in_zip($p_event, &$p_header);
  308. $this->assertTrue(is_numeric($res));
  309. //var_dump($res);
  310. }
  311. function testCleanUpPath() {
  312. $base_work_dir=api_get_path(SYS_COURSE_PATH);
  313. $path = $base_work_dir.'upload/blog';
  314. $res = clean_up_path($path);
  315. $this->assertTrue(is_numeric($res));
  316. }
  317. }
  318. ?>