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. * Fetches all document data for the given user/group
  104. *
  105. * @param array $_course
  106. * @param string $path
  107. * @param int $to_group_id
  108. * @param int $to_user_id
  109. * @param boolean $can_see_invisible
  110. * @return array with all document data
  111. */
  112. function testget_all_document_data() {
  113. global $_course;
  114. $path = '/';
  115. $to_group_id = 0;
  116. $to_user_id = NULL;
  117. $can_see_invisible = false;
  118. $res=DocumentManager::get_all_document_data($_course, $path,
  119. $to_group_id, $to_user_id, $can_see_invisible);
  120. if (empty($_course['dbName'])) {
  121. $this->assertFalse($res);
  122. } else {
  123. $this->assertTrue(is_array($res));
  124. }
  125. }
  126. /**
  127. * Gets the paths of all folders in a course
  128. * can show all folders (exept for the deleted ones) or only visible ones
  129. * @param array $_course
  130. * @param boolean $can_see_invisible
  131. * @param int $to_group_id
  132. * @return array with paths
  133. */
  134. function testget_all_document_folders() {
  135. global $_course;
  136. $to_group_id = '0';
  137. $can_see_invisible = false;
  138. $res = DocumentManager::get_all_document_folders($_course, $to_group_id, $can_see_invisible);
  139. if (empty($_course['dbName'])) {
  140. $this->assertFalse($res);
  141. } else {
  142. $this->assertTrue(is_array($res));
  143. }
  144. }
  145. /**
  146. * @return the document folder quota of the current course, in bytes
  147. */
  148. /* Removed temporarily, causing problems for some reason on automated tests
  149. * server
  150. function testget_course_quota() {
  151. global $_course, $maxFilledSpace;
  152. $res = DocumentManager::get_course_quota();
  153. $this->assertTrue(is_string($res));
  154. }
  155. */
  156. /** Gets the id of a document with a given path
  157. *
  158. * @param array $_course
  159. * @param string $path
  160. * @return int id of document / false if no doc found
  161. */
  162. function testget_document_id() {
  163. global $_course;
  164. $path = Database::escape_string($path);
  165. $res=DocumentManager::get_document_id($_course, $path);
  166. $this->assertTrue(is_bool($res));
  167. }
  168. /** This check if a document is a folder or not
  169. * @param array $_course
  170. * @param int $document_id of the item
  171. * @return boolean true/false
  172. **/
  173. function testis_folder() {
  174. global $_course;
  175. $document_id = 1;
  176. $res=DocumentManager::is_folder($_course, $document_id);
  177. $this->assertTrue(is_bool($res));
  178. }
  179. /**
  180. * return true if the documentpath have visibility=1 as item_property
  181. *
  182. * @param string $document_path the relative complete path of the document
  183. * @param array $course the _course array info of the document's course
  184. */
  185. function testis_visible() {
  186. global $_course;
  187. $doc_path = Database::escape_string($doc_path);
  188. $res=DocumentManager::is_visible($doc_path, $_course);
  189. $this->assertTrue(is_bool($res));
  190. }
  191. /**
  192. * Allow to set a specific document as a new template for FCKEditor for a
  193. * particular user in a particular course
  194. *
  195. * @param string $title
  196. * @param string $description
  197. * @param int $document_id_for_template the document id
  198. * @param string $couse_code
  199. * @param int $user_id
  200. */
  201. function testset_document_as_template() {
  202. global $_course,$_user;
  203. $title='test';
  204. $description='test';
  205. $document_id_for_template='';
  206. $couse_code=$_course;
  207. $user_id=$_user;
  208. $image='';
  209. $res=DocumentManager::set_document_as_template($title, $description,
  210. $document_id_for_template,
  211. $couse_code, $user_id,
  212. $image
  213. );
  214. $this->assertTrue(is_bool($res));
  215. }
  216. function testdocuments_total_space() {
  217. $to_group_id='0';
  218. $res= DocumentManager::documents_total_space($to_group_id);
  219. if(!is_null($res)):
  220. $this->assertTrue(is_numeric($res));
  221. endif;
  222. }
  223. function testenough_space() {
  224. $file_size='';
  225. $max_dir_space='';
  226. $res= DocumentManager::enough_space($file_size, $max_dir_space);
  227. $this->assertTrue(is_bool($res));
  228. }
  229. //The following function throws exceptions because of SimpleTest, because
  230. // it declares uncapturable headers
  231. /* function teststring_send_for_download() {
  232. $full_string='';
  233. $forced = false;
  234. $name = '';
  235. $filename = $name;
  236. $len = strlen($full_string);
  237. if (!headers_sent()) {
  238. $res=DocumentManager::string_send_for_download($full_string, $forced, $name);
  239. }
  240. $this->assertTrue(empty($res));
  241. }
  242. */
  243. /**
  244. * Unset a document as template
  245. *
  246. * @param int $document_id
  247. * @param string $couse_code
  248. * @param int $user_id
  249. * @return void null
  250. */
  251. function testunset_document_as_template() {
  252. $document_id=Database::escape_string($document_id);
  253. $course_code=Database::escape_string($course_code);
  254. $user_id=Database::escape_string($user_id);
  255. $res=DocumentManager::unset_document_as_template($document_id,
  256. $course_code,
  257. $user_id
  258. );
  259. $this->assertNull($res);
  260. //var_dump($res);
  261. }
  262. /*
  263. public function testdeleteCourseInDocument(){
  264. $this->dmanager = null;
  265. global $_course;
  266. $code = $_course;
  267. $res = CourseManager::delete_course($code);
  268. $path = api_get_path(SYS_PATH).'archive';
  269. if ($handle = opendir($path)) {
  270. while (false !== ($file = readdir($handle))) {
  271. if (strpos($file,$code)!==false) {
  272. if (is_dir($path.'/'.$file)) {
  273. rmdirr($path.'/'.$file);
  274. }
  275. }
  276. }
  277. closedir($handle);
  278. }
  279. }*/
  280. }
  281. ?>