FilesLibTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. <?php
  2. /* Copyright (C) 2010-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2012 Regis Houssin <regis.houssin@capnetworks.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. * or see http://www.gnu.org/
  18. */
  19. /**
  20. * \file test/phpunit/FilesLibTest.php
  21. * \ingroup test
  22. * \brief PHPUnit test
  23. * \remarks To run this script as CLI: phpunit filename.php
  24. */
  25. global $conf,$user,$langs,$db;
  26. //define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver
  27. //require_once 'PHPUnit/Autoload.php';
  28. require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
  29. require_once dirname(__FILE__).'/../../htdocs/core/lib/files.lib.php';
  30. if (empty($user->id))
  31. {
  32. print "Load permissions for admin user nb 1\n";
  33. $user->fetch(1);
  34. $user->getrights();
  35. }
  36. $conf->global->MAIN_DISABLE_ALL_MAILS=1;
  37. /**
  38. * Class for PHPUnit tests
  39. *
  40. * @backupGlobals disabled
  41. * @backupStaticAttributes enabled
  42. * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
  43. */
  44. class FilesLibTest extends PHPUnit_Framework_TestCase
  45. {
  46. protected $savconf;
  47. protected $savuser;
  48. protected $savlangs;
  49. protected $savdb;
  50. /**
  51. * Constructor
  52. * We save global variables into local variables
  53. *
  54. * @return FilesLibTest
  55. */
  56. function __construct()
  57. {
  58. //$this->sharedFixture
  59. global $conf,$user,$langs,$db;
  60. $this->savconf=$conf;
  61. $this->savuser=$user;
  62. $this->savlangs=$langs;
  63. $this->savdb=$db;
  64. print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  65. //print " - db ".$db->db;
  66. print "\n";
  67. }
  68. // Static methods
  69. public static function setUpBeforeClass()
  70. {
  71. global $conf,$user,$langs,$db;
  72. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  73. print __METHOD__."\n";
  74. }
  75. // tear down after class
  76. public static function tearDownAfterClass()
  77. {
  78. global $conf,$user,$langs,$db;
  79. $db->rollback();
  80. print __METHOD__."\n";
  81. }
  82. /**
  83. * Init phpunit tests
  84. *
  85. * @return void
  86. */
  87. protected function setUp()
  88. {
  89. global $conf,$user,$langs,$db;
  90. $conf=$this->savconf;
  91. $user=$this->savuser;
  92. $langs=$this->savlangs;
  93. $db=$this->savdb;
  94. print __METHOD__."\n";
  95. }
  96. /**
  97. * End phpunit tests
  98. *
  99. * @return void
  100. */
  101. protected function tearDown()
  102. {
  103. print __METHOD__."\n";
  104. }
  105. /**
  106. * testDolBasename
  107. *
  108. * @return int
  109. */
  110. public function testDolBasename()
  111. {
  112. global $conf,$user,$langs,$db;
  113. $conf=$this->savconf;
  114. $user=$this->savuser;
  115. $langs=$this->savlangs;
  116. $db=$this->savdb;
  117. $result=dol_basename('adir/afile');
  118. print __METHOD__." result=".$result."\n";
  119. $this->assertEquals('afile',$result);
  120. $result=dol_basename('adir/afile/');
  121. print __METHOD__." result=".$result."\n";
  122. $this->assertEquals('afile',$result);
  123. $result=dol_basename('adir/νεο'); // With cyrillic data. Here basename fails to return correct value
  124. print __METHOD__." result=".$result."\n";
  125. $this->assertEquals('νεο',$result);
  126. $result=dol_basename('adir/νεο/'); // With cyrillic data. Here basename fails to return correct value
  127. print __METHOD__." result=".$result."\n";
  128. $this->assertEquals('νεο',$result);
  129. }
  130. /**
  131. * testDolCountNbOfLine
  132. *
  133. * @return int
  134. */
  135. public function testDolCountNbOfLine()
  136. {
  137. global $conf,$user,$langs,$db;
  138. $conf=$this->savconf;
  139. $user=$this->savuser;
  140. $langs=$this->savlangs;
  141. $db=$this->savdb;
  142. $file=dirname(__FILE__).'/Example_import_company_1.csv';
  143. $result=dol_count_nb_of_line($file);
  144. print __METHOD__." result=".$result."\n";
  145. $this->assertEquals(3,$result);
  146. return $result;
  147. }
  148. /**
  149. * testDolIsFileDir
  150. *
  151. * @return int
  152. */
  153. public function testDolIsFileDir()
  154. {
  155. global $conf,$user,$langs,$db;
  156. $conf=$this->savconf;
  157. $user=$this->savuser;
  158. $langs=$this->savlangs;
  159. $db=$this->savdb;
  160. $file=dirname(__FILE__).'/Example_import_company_1.csv';
  161. $result=dol_is_file($file);
  162. print __METHOD__." result=".$result."\n";
  163. $this->assertTrue($result);
  164. $result=dol_is_dir($file);
  165. print __METHOD__." result=".$result."\n";
  166. $this->assertFalse($result);
  167. return $result;
  168. }
  169. /**
  170. * testDolOther
  171. *
  172. * @return boolean
  173. */
  174. public function testDolOther()
  175. {
  176. global $conf,$user,$langs,$db;
  177. $conf=$this->savconf;
  178. $user=$this->savuser;
  179. $langs=$this->savlangs;
  180. $db=$this->savdb;
  181. $url='http://www.dolibarr.org';
  182. $result=dol_is_url($url);
  183. print __METHOD__." result=".$result."\n";
  184. $this->assertTrue($result);
  185. $url='https://www.dolibarr.org';
  186. $result=dol_is_url($url);
  187. print __METHOD__." result=".$result."\n";
  188. $this->assertTrue($result);
  189. $url='file://www.dolibarr.org/download/file.zip';
  190. $result=dol_is_url($url);
  191. print __METHOD__." result=".$result."\n";
  192. $this->assertTrue($result);
  193. return $result;
  194. }
  195. /**
  196. * testDolMimeType
  197. *
  198. * @return string
  199. */
  200. public function testDolMimeType()
  201. {
  202. global $conf,$user,$langs,$db;
  203. $conf=$this->savconf;
  204. $user=$this->savuser;
  205. $langs=$this->savlangs;
  206. $db=$this->savdb;
  207. // file.png
  208. $result=dol_mimetype('file.png','',0);
  209. $this->assertEquals('image/png',$result);
  210. $result=dol_mimetype('file.png','',1);
  211. $this->assertEquals('png',$result);
  212. $result=dol_mimetype('file.png','',2);
  213. $this->assertEquals('image.png',$result);
  214. $result=dol_mimetype('file.png','',3);
  215. $this->assertEquals('',$result);
  216. // file.odt
  217. $result=dol_mimetype('file.odt','',0);
  218. $this->assertEquals('application/vnd.oasis.opendocument.text',$result);
  219. $result=dol_mimetype('file.odt','',1);
  220. $this->assertEquals('vnd.oasis.opendocument.text',$result);
  221. $result=dol_mimetype('file.odt','',2);
  222. $this->assertEquals('ooffice.png',$result);
  223. $result=dol_mimetype('file.odt','',3);
  224. $this->assertEquals('',$result);
  225. // file.php
  226. $result=dol_mimetype('file.php','',0);
  227. $this->assertEquals('text/plain',$result);
  228. $result=dol_mimetype('file.php','',1);
  229. $this->assertEquals('plain',$result);
  230. $result=dol_mimetype('file.php','',2);
  231. $this->assertEquals('php.png',$result);
  232. $result=dol_mimetype('file.php','',3);
  233. $this->assertEquals('php',$result);
  234. // file.php.noexe
  235. $result=dol_mimetype('file.php.noexe','',0);
  236. $this->assertEquals('text/plain',$result);
  237. }
  238. /**
  239. * testDolDeleteDir
  240. *
  241. * @return int
  242. */
  243. public function testDolDeleteDir()
  244. {
  245. global $conf,$user,$langs,$db;
  246. $conf=$this->savconf;
  247. $user=$this->savuser;
  248. $langs=$this->savlangs;
  249. $db=$this->savdb;
  250. $dirout=$conf->admin->dir_temp.'/test';
  251. $count=0;
  252. $result=dol_delete_dir_recursive($dirout,$count,1); // If it has no permission to delete, it will fails as if dir does not exists, so we can't test it
  253. print __METHOD__." result=".$result."\n";
  254. $this->assertGreaterThanOrEqual(0,$result);
  255. }
  256. /**
  257. * testDolCopyMoveDelete
  258. *
  259. * @return int
  260. *
  261. * @depends testDolDeleteDir
  262. * The depends says test is run only if previous is ok
  263. */
  264. public function testDolCopyMoveDelete()
  265. {
  266. global $conf,$user,$langs,$db;
  267. $conf=$this->savconf;
  268. $user=$this->savuser;
  269. $langs=$this->savlangs;
  270. $db=$this->savdb;
  271. $file=dirname(__FILE__).'/Example_import_company_1.csv';
  272. $result=dol_copy($file, '/adir/that/does/not/exists/file.csv');
  273. print __METHOD__." result=".$result."\n";
  274. $this->assertLessThan(0,$result,'copy dir that does not exists'); // We should have error
  275. $result=dol_copy($file, $conf->admin->dir_temp.'/file.csv',0,1);
  276. print __METHOD__." result=".$result."\n";
  277. $this->assertGreaterThanOrEqual(1,$result, 'copy file ('.$file.') into a dir that exists ('.$conf->admin->dir_temp.'/file.csv'.')'); // Should be 1
  278. // Again to test with overwriting=0
  279. $result=dol_copy($file, $conf->admin->dir_temp.'/file.csv',0,0);
  280. print __METHOD__." result=".$result."\n";
  281. $this->assertEquals(0,$result, 'copy destination already exists, no overwrite'); // Should be 0
  282. // Again to test with overwriting=1
  283. $result=dol_copy($file, $conf->admin->dir_temp.'/file.csv',0,1);
  284. print __METHOD__." result=".$result."\n";
  285. $this->assertGreaterThanOrEqual(1,$result,'copy destination already exists, overwrite'); // Should be 1
  286. // To test a move that should work
  287. $result=dol_move($conf->admin->dir_temp.'/file.csv',$conf->admin->dir_temp.'/file2.csv',0,1);
  288. print __METHOD__." result=".$result."\n";
  289. $this->assertTrue($result,'move with default mask');
  290. // To test a move that should work with forced mask
  291. $result=dol_move($conf->admin->dir_temp.'/file2.csv',$conf->admin->dir_temp.'/file3.csv','0754',1); // file shoutld be rwxr-wr--
  292. print __METHOD__." result=".$result."\n";
  293. $this->assertTrue($result,'move with forced mask');
  294. // To test a delete that should success
  295. $result=dol_delete_file($conf->admin->dir_temp.'/file3.csv');
  296. print __METHOD__." result=".$result."\n";
  297. $this->assertTrue($result,'delete file');
  298. // Again to test there is error when deleting a non existing file with option disableglob
  299. $result=dol_delete_file($conf->admin->dir_temp.'/file3.csv',1,1);
  300. print __METHOD__." result=".$result."\n";
  301. $this->assertFalse($result,'delete file that does not exists with disableglo must return ko');
  302. // Again to test there is no error when deleting a non existing file without option disableglob
  303. $result=dol_delete_file($conf->admin->dir_temp.'/file3csv',0,1);
  304. print __METHOD__." result=".$result."\n";
  305. $this->assertTrue($result,'delete file that does not exists without disabling glob must return ok');
  306. // Test copy with special char / delete with blob
  307. $result=dol_copy($file, $conf->admin->dir_temp.'/file with [x] and é.csv',0,1);
  308. print __METHOD__." result=".$result."\n";
  309. $this->assertGreaterThanOrEqual(1,$result,'copy file with special chars, overwrite'); // Should be 1
  310. // Try to delete using a glob criteria
  311. $result=dol_delete_file($conf->admin->dir_temp.'/file with [x]*é.csv');
  312. print __METHOD__." result=".$result."\n";
  313. $this->assertTrue($result,'delete file using glob');
  314. }
  315. /**
  316. * testDolCompressUnCompress
  317. *
  318. * @return string
  319. *
  320. * @depends testDolCopyMoveDelete
  321. * The depends says test is run only if previous is ok
  322. */
  323. public function testDolCompressUnCompress()
  324. {
  325. global $conf,$user,$langs,$db;
  326. $conf=$this->savconf;
  327. $user=$this->savuser;
  328. $langs=$this->savlangs;
  329. $db=$this->savdb;
  330. $format='zip';
  331. $filein=dirname(__FILE__).'/Example_import_company_1.csv';
  332. $fileout=$conf->admin->dir_temp.'/test.'.$format;
  333. $dirout=$conf->admin->dir_temp.'/test';
  334. dol_delete_file($fileout);
  335. $count=0;
  336. dol_delete_dir_recursive($dirout,$count,1);
  337. $result=dol_compress_file($filein, $fileout, $format);
  338. print __METHOD__." result=".$result."\n";
  339. $this->assertGreaterThanOrEqual(1,$result);
  340. $result=dol_uncompress($fileout, $dirout);
  341. print __METHOD__." result=".join(',',$result)."\n";
  342. $this->assertEquals(0,count($result));
  343. }
  344. /**
  345. * testDolDirList
  346. *
  347. * @return string
  348. *
  349. * @depends testDolCompressUnCompress
  350. * The depends says test is run only if previous is ok
  351. */
  352. public function testDolDirList()
  353. {
  354. global $conf,$user,$langs,$db;
  355. // Scan dir to guaruante we on't have library jquery twice (we accept exception of duplicte into ckeditor because all dir is removed for debian package, so there is no duplicate).
  356. $founddirs=dol_dir_list(DOL_DOCUMENT_ROOT.'/includes/', 'files', 1, '^jquery\.js', array('ckeditor'));
  357. print __METHOD__." count(founddirs)=".count($founddirs)."\n";
  358. $this->assertEquals(1,count($founddirs));
  359. }
  360. /**
  361. * testDolCheckSecureAccessDocument
  362. *
  363. * @return void
  364. */
  365. public function testDolCheckSecureAccessDocument()
  366. {
  367. global $conf,$user,$langs,$db;
  368. $conf=$this->savconf;
  369. $user=$this->savuser;
  370. $langs=$this->savlangs;
  371. $db=$this->savdb;
  372. //$dummyuser=new User($db);
  373. //$result=restrictedArea($dummyuser,'societe');
  374. $user->rights->facture->lire = 0;
  375. $user->rights->facture->creer = 0;
  376. $filename='SPECIMEN.pdf'; // Filename relative to module part
  377. $result=dol_check_secure_access_document('facture', $filename, 0, '', '', 'read');
  378. $this->assertEquals(1,$result['accessallowed']);
  379. // Check read permission
  380. $user->rights->facture->lire = 1;
  381. $user->rights->facture->creer = 1;
  382. $filename='FA010101/FA010101.pdf'; // Filename relative to module part
  383. $result=dol_check_secure_access_document('facture', $filename, 0, '', '', 'read');
  384. $this->assertEquals(1,$result['accessallowed']);
  385. $user->rights->facture->lire = 0;
  386. $user->rights->facture->creer = 0;
  387. $filename='FA010101/FA010101.pdf'; // Filename relative to module part
  388. $result=dol_check_secure_access_document('facture', $filename, 0, '', '', 'read');
  389. $this->assertEquals(0,$result['accessallowed']);
  390. // Check write permission
  391. $user->rights->facture->lire = 0;
  392. $user->rights->facture->creer = 0;
  393. $filename='FA010101/FA010101.pdf'; // Filename relative to module part
  394. $result=dol_check_secure_access_document('facture', $filename, 0, '', '', 'write');
  395. var_dump($result);
  396. $this->assertEquals(0,$result['accessallowed']);
  397. $user->rights->facture->lire = 1;
  398. $user->rights->facture->creer = 1;
  399. $filename='FA010101/FA010101.pdf'; // Filename relative to module part
  400. $result=dol_check_secure_access_document('facture', $filename, 0, '', '', 'write');
  401. var_dump($result);
  402. $this->assertEquals(1,$result['accessallowed']);
  403. $user->rights->facture->lire = 1;
  404. $user->rights->facture->creer = 0;
  405. $filename='FA010101/FA010101.pdf'; // Filename relative to module part
  406. $result=dol_check_secure_access_document('facture', $filename, 0, '', '', 'write');
  407. var_dump($result);
  408. $this->assertEquals(0,$result['accessallowed']);
  409. }
  410. }