document.lib.test.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. require_once(api_get_path(LIBRARY_PATH).'document.lib.php');
  3. class TestDocumentManager extends UnitTestCase {
  4. public function __construct() {
  5. $this->UnitTestCase('Document Manager library - main/inc/lib/document.lib.test.php');
  6. }
  7. /**
  8. * This checks if a document has the readonly property checked, then see if
  9. * the user is the owner of this file, if all this is true then return true.
  10. *
  11. * @param array $_course
  12. * @param int $user_id id of the current user
  13. * @param string $file path stored in the database
  14. * @param int $document_id in case you dont have the file path ,insert the
  15. * id of the file here and leave $file in blank ''
  16. * @return boolean true/false
  17. **/
  18. public function testcheck_readonly() {
  19. global $user_id, $_course;
  20. $file='';
  21. $res=DocumentManager::check_readonly($_course,$user_id,$file);
  22. $this->assertTrue(is_bool($res));
  23. }
  24. /**
  25. * This deletes a document by changing visibility to 2, renaming it to
  26. * filename_DELETED_#id
  27. * Files/folders that are inside a deleted folder get visibility 2
  28. *
  29. * @param array $_course
  30. * @param string $path, path stored in the database
  31. * @param string ,$base_work_dir, path to the documents folder
  32. * @return boolean true/false
  33. * @todo now only files/folders in a folder get visibility 2, we should
  34. * rename them too.
  35. */
  36. function testdelete_document() {
  37. global $_course;
  38. $path='';
  39. $base_work_dir='';
  40. ob_start();
  41. $res=DocumentManager::delete_document($_course, $path, $base_work_dir);
  42. ob_end_clean();
  43. $this->assertTrue(is_bool($res));
  44. }
  45. /**
  46. * Removes documents from search engine database
  47. *
  48. * @param string $course_id Course code
  49. * @param int $document_id Document id to delete
  50. * @return void
  51. */
  52. function testdelete_document_from_search_engine() {
  53. global $cidReq;
  54. $course_id = $cidReq;
  55. $document_id='';
  56. $res=DocumentManager::delete_document_from_search_engine($course_id, $document_id);
  57. $this->assertNull($res);
  58. }
  59. /**
  60. * Get the content type of a file by checking the extension
  61. * We could use mime_content_type() with php-versions > 4.3,
  62. * but this doesn't work as it should on Windows installations
  63. *
  64. * @param string $filename or boolean TRUE to return complete array
  65. *
  66. */
  67. function testfile_get_mime_type() {
  68. $filename='';
  69. $res=DocumentManager::file_get_mime_type($filename);
  70. $this->assertTrue(is_string($res));
  71. }
  72. /**
  73. * This function streams a file to the client
  74. *
  75. * @param string $full_file_name
  76. * @param boolean $forced
  77. * @param string $name
  78. * @return false if file doesn't exist, true if stream succeeded
  79. */
  80. function testfile_send_for_download() {
  81. $full_file_name='';
  82. $forced = false;
  83. $name = '';
  84. $res=DocumentManager::file_send_for_download($full_file_name, $forced, $name);
  85. $this->assertTrue(is_bool($res));
  86. }
  87. /**
  88. * @todo ??not only check if a file is visible, but also check if the user
  89. * is allowed to see the file??
  90. * @return true if the user is allowed to see the document, false otherwise
  91. * (bool)
  92. */
  93. /* function testfile_visible_to_user() {
  94. global $_course,$tbl_document;
  95. $tbl_document = Database::get_course_table(TABLE_DOCUMENT);
  96. $this_course= $_course['dbName'].'.';
  97. $dirurl = api_get_path(WEB_COURSE_PATH);
  98. $doc_url= $dirurl.'COURSETEST/document/video/painting.mpg?cidReq=COURSETEST';
  99. $res=DocumentManager::file_visible_to_user($this_course, $doc_url);
  100. $this->assertTrue(is_bool($res));
  101. }
  102. */
  103. /**
  104. * Fetches all document data for the given user/group
  105. *
  106. * @param array $_course
  107. * @param string $path
  108. * @param int $to_group_id
  109. * @param int $to_user_id
  110. * @param boolean $can_see_invisible
  111. * @return array with all document data
  112. */
  113. function testget_all_document_data() {
  114. global $_course;
  115. $path = '/';
  116. $to_group_id = 0;
  117. $to_user_id = NULL;
  118. $can_see_invisible = false;
  119. $res=DocumentManager::get_all_document_data($_course, $path,
  120. $to_group_id, $to_user_id, $can_see_invisible);
  121. if (empty($_course['dbName'])) {
  122. $this->assertFalse($res);
  123. } else {
  124. $this->assertTrue(is_array($res));
  125. }
  126. }
  127. /**
  128. * Gets the paths of all folders in a course
  129. * can show all folders (exept for the deleted ones) or only visible ones
  130. * @param array $_course
  131. * @param boolean $can_see_invisible
  132. * @param int $to_group_id
  133. * @return array with paths
  134. */
  135. function testget_all_document_folders() {
  136. global $_course;
  137. $to_group_id = '0';
  138. $can_see_invisible = false;
  139. $res = DocumentManager::get_all_document_folders($_course, $to_group_id, $can_see_invisible);
  140. if (empty($_course['dbName'])) {
  141. $this->assertFalse($res);
  142. } else {
  143. $this->assertTrue(is_array($res));
  144. }
  145. }
  146. /**
  147. * @return the document folder quota of the current course, in bytes
  148. */
  149. /* Removed temporarily, causing problems for some reason on automated tests
  150. * server
  151. function testget_course_quota() {
  152. global $_course, $maxFilledSpace;
  153. $res = DocumentManager::get_course_quota();
  154. $this->assertTrue(is_string($res));
  155. }
  156. */
  157. /** Gets the id of a document with a given path
  158. *
  159. * @param array $_course
  160. * @param string $path
  161. * @return int id of document / false if no doc found
  162. */
  163. function testget_document_id() {
  164. global $_course;
  165. $path = Database::escape_string($path);
  166. $res=DocumentManager::get_document_id($_course, $path);
  167. $this->assertTrue(is_bool($res));
  168. }
  169. /** This check if a document is a folder or not
  170. * @param array $_course
  171. * @param int $document_id of the item
  172. * @return boolean true/false
  173. **/
  174. function testis_folder() {
  175. global $_course;
  176. $document_id = 1;
  177. $res=DocumentManager::is_folder($_course, $document_id);
  178. $this->assertTrue(is_bool($res));
  179. }
  180. /**
  181. * return true if the documentpath have visibility=1 as item_property
  182. *
  183. * @param string $document_path the relative complete path of the document
  184. * @param array $course the _course array info of the document's course
  185. */
  186. function testis_visible() {
  187. global $_course;
  188. $doc_path = Database::escape_string($doc_path);
  189. $res=DocumentManager::is_visible($doc_path, $_course);
  190. $this->assertTrue(is_bool($res));
  191. }
  192. /**
  193. * Allow to set a specific document as a new template for FCKEditor for a
  194. * particular user in a particular course
  195. *
  196. * @param string $title
  197. * @param string $description
  198. * @param int $document_id_for_template the document id
  199. * @param string $couse_code
  200. * @param int $user_id
  201. */
  202. function testset_document_as_template() {
  203. global $_course,$_user;
  204. $title='test';
  205. $description='test';
  206. $document_id_for_template='';
  207. $couse_code=$_course;
  208. $user_id=$_user;
  209. $image='';
  210. $res=DocumentManager::set_document_as_template($title, $description,
  211. $document_id_for_template,
  212. $couse_code, $user_id,
  213. $image
  214. );
  215. $this->assertTrue(is_bool($res));
  216. }
  217. function testdocuments_total_space() {
  218. $to_group_id='0';
  219. $res= DocumentManager::documents_total_space($to_group_id);
  220. if(!is_null($res)):
  221. $this->assertTrue(is_numeric($res));
  222. endif;
  223. }
  224. function testenough_space() {
  225. $file_size='';
  226. $max_dir_space='';
  227. $res= DocumentManager::enough_space($file_size, $max_dir_space);
  228. $this->assertTrue(is_bool($res));
  229. }
  230. //The following function throws exceptions because of SimpleTest, because
  231. // it declares uncapturable headers
  232. /* function teststring_send_for_download() {
  233. $full_string='';
  234. $forced = false;
  235. $name = '';
  236. $filename = $name;
  237. $len = strlen($full_string);
  238. if (!headers_sent()) {
  239. $res=DocumentManager::string_send_for_download($full_string, $forced, $name);
  240. }
  241. $this->assertTrue(empty($res));
  242. }
  243. */
  244. /**
  245. * Unset a document as template
  246. *
  247. * @param int $document_id
  248. * @param string $couse_code
  249. * @param int $user_id
  250. * @return void null
  251. */
  252. function testunset_document_as_template() {
  253. $document_id=Database::escape_string($document_id);
  254. $course_code=Database::escape_string($course_code);
  255. $user_id=Database::escape_string($user_id);
  256. $res=DocumentManager::unset_document_as_template($document_id,
  257. $course_code,
  258. $user_id
  259. );
  260. $this->assertNull($res);
  261. //var_dump($res);
  262. }
  263. /*
  264. public function testdeleteCourseInDocument(){
  265. $this->dmanager = null;
  266. global $_course;
  267. $code = $_course;
  268. $res = CourseManager::delete_course($code);
  269. $path = api_get_path(SYS_PATH).'archive';
  270. if ($handle = opendir($path)) {
  271. while (false !== ($file = readdir($handle))) {
  272. if (strpos($file,$code)!==false) {
  273. if (is_dir($path.'/'.$file)) {
  274. rmdirr($path.'/'.$file);
  275. }
  276. }
  277. }
  278. closedir($handle);
  279. }
  280. }*/
  281. }
  282. ?>