FilesLibTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. parent::__construct();
  59. //$this->sharedFixture
  60. global $conf,$user,$langs,$db;
  61. $this->savconf=$conf;
  62. $this->savuser=$user;
  63. $this->savlangs=$langs;
  64. $this->savdb=$db;
  65. print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  66. //print " - db ".$db->db;
  67. print "\n";
  68. }
  69. // Static methods
  70. public static function setUpBeforeClass()
  71. {
  72. global $conf,$user,$langs,$db;
  73. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  74. print __METHOD__."\n";
  75. }
  76. // tear down after class
  77. public static function tearDownAfterClass()
  78. {
  79. global $conf,$user,$langs,$db;
  80. $db->rollback();
  81. print __METHOD__."\n";
  82. }
  83. /**
  84. * Init phpunit tests
  85. *
  86. * @return void
  87. */
  88. protected function setUp()
  89. {
  90. global $conf,$user,$langs,$db;
  91. $conf=$this->savconf;
  92. $user=$this->savuser;
  93. $langs=$this->savlangs;
  94. $db=$this->savdb;
  95. print __METHOD__."\n";
  96. }
  97. /**
  98. * End phpunit tests
  99. *
  100. * @return void
  101. */
  102. protected function tearDown()
  103. {
  104. print __METHOD__."\n";
  105. }
  106. /**
  107. * testDolBasename
  108. *
  109. * @return int
  110. */
  111. public function testDolBasename()
  112. {
  113. global $conf,$user,$langs,$db;
  114. $conf=$this->savconf;
  115. $user=$this->savuser;
  116. $langs=$this->savlangs;
  117. $db=$this->savdb;
  118. $result=dol_basename('adir/afile');
  119. print __METHOD__." result=".$result."\n";
  120. $this->assertEquals('afile',$result);
  121. $result=dol_basename('adir/afile/');
  122. print __METHOD__." result=".$result."\n";
  123. $this->assertEquals('afile',$result);
  124. $result=dol_basename('adir/νεο'); // With cyrillic data. Here basename fails to return correct value
  125. print __METHOD__." result=".$result."\n";
  126. $this->assertEquals('νεο',$result);
  127. $result=dol_basename('adir/νεο/'); // With cyrillic data. Here basename fails to return correct value
  128. print __METHOD__." result=".$result."\n";
  129. $this->assertEquals('νεο',$result);
  130. }
  131. /**
  132. * testDolCountNbOfLine
  133. *
  134. * @return int
  135. */
  136. public function testDolCountNbOfLine()
  137. {
  138. global $conf,$user,$langs,$db;
  139. $conf=$this->savconf;
  140. $user=$this->savuser;
  141. $langs=$this->savlangs;
  142. $db=$this->savdb;
  143. $file=dirname(__FILE__).'/Example_import_company_1.csv';
  144. $result=dol_count_nb_of_line($file);
  145. print __METHOD__." result=".$result."\n";
  146. $this->assertEquals(3,$result);
  147. return $result;
  148. }
  149. /**
  150. * testDolIsFileDir
  151. *
  152. * @return int
  153. */
  154. public function testDolIsFileDir()
  155. {
  156. global $conf,$user,$langs,$db;
  157. $conf=$this->savconf;
  158. $user=$this->savuser;
  159. $langs=$this->savlangs;
  160. $db=$this->savdb;
  161. $file=dirname(__FILE__).'/Example_import_company_1.csv';
  162. $result=dol_is_file($file);
  163. print __METHOD__." result=".$result."\n";
  164. $this->assertTrue($result);
  165. $result=dol_is_dir($file);
  166. print __METHOD__." result=".$result."\n";
  167. $this->assertFalse($result);
  168. return $result;
  169. }
  170. /**
  171. * testDolOther
  172. *
  173. * @return boolean
  174. */
  175. public function testDolOther()
  176. {
  177. global $conf,$user,$langs,$db;
  178. $conf=$this->savconf;
  179. $user=$this->savuser;
  180. $langs=$this->savlangs;
  181. $db=$this->savdb;
  182. $url='http://www.dolibarr.org';
  183. $result=dol_is_url($url);
  184. print __METHOD__." result=".$result."\n";
  185. $this->assertTrue($result);
  186. $url='https://www.dolibarr.org';
  187. $result=dol_is_url($url);
  188. print __METHOD__." result=".$result."\n";
  189. $this->assertTrue($result);
  190. $url='file://www.dolibarr.org/download/file.zip';
  191. $result=dol_is_url($url);
  192. print __METHOD__." result=".$result."\n";
  193. $this->assertTrue($result);
  194. return $result;
  195. }
  196. /**
  197. * testDolMimeType
  198. *
  199. * @return string
  200. */
  201. public function testDolMimeType()
  202. {
  203. global $conf,$user,$langs,$db;
  204. $conf=$this->savconf;
  205. $user=$this->savuser;
  206. $langs=$this->savlangs;
  207. $db=$this->savdb;
  208. // file.png
  209. $result=dol_mimetype('file.png','',0);
  210. $this->assertEquals('image/png',$result);
  211. $result=dol_mimetype('file.png','',1);
  212. $this->assertEquals('png',$result);
  213. $result=dol_mimetype('file.png','',2);
  214. $this->assertEquals('image.png',$result);
  215. $result=dol_mimetype('file.png','',3);
  216. $this->assertEquals('',$result);
  217. // file.odt
  218. $result=dol_mimetype('file.odt','',0);
  219. $this->assertEquals('application/vnd.oasis.opendocument.text',$result);
  220. $result=dol_mimetype('file.odt','',1);
  221. $this->assertEquals('vnd.oasis.opendocument.text',$result);
  222. $result=dol_mimetype('file.odt','',2);
  223. $this->assertEquals('ooffice.png',$result);
  224. $result=dol_mimetype('file.odt','',3);
  225. $this->assertEquals('',$result);
  226. // file.php
  227. $result=dol_mimetype('file.php','',0);
  228. $this->assertEquals('text/plain',$result);
  229. $result=dol_mimetype('file.php','',1);
  230. $this->assertEquals('plain',$result);
  231. $result=dol_mimetype('file.php','',2);
  232. $this->assertEquals('php.png',$result);
  233. $result=dol_mimetype('file.php','',3);
  234. $this->assertEquals('php',$result);
  235. // file.php.noexe
  236. $result=dol_mimetype('file.php.noexe','',0);
  237. $this->assertEquals('text/plain',$result);
  238. }
  239. /**
  240. * testDolDeleteDir
  241. *
  242. * @return int
  243. */
  244. public function testDolDeleteDir()
  245. {
  246. global $conf,$user,$langs,$db;
  247. $conf=$this->savconf;
  248. $user=$this->savuser;
  249. $langs=$this->savlangs;
  250. $db=$this->savdb;
  251. $dirout=$conf->admin->dir_temp.'/test';
  252. $dirout2=$conf->admin->dir_temp.'/test2';
  253. $count=0;
  254. $result=dol_delete_dir_recursive($dirout,$count); // If it has no permission to delete, it will fails as if dir does not exists, so we can't test it
  255. print __METHOD__." result=".$result."\n";
  256. $this->assertGreaterThanOrEqual(0,$result);
  257. $count=0;
  258. $countdeleted=0;
  259. $result=dol_delete_dir_recursive($dirout,$count,1,0,$countdeleted); // If it has no permission to delete, it will fails as if dir does not exists, so we can't test it
  260. print __METHOD__." result=".$result."\n";
  261. $this->assertGreaterThanOrEqual(0,$result);
  262. $this->assertGreaterThanOrEqual(0,$countdeleted);
  263. dol_mkdir($dirout2);
  264. $count=0;
  265. $countdeleted=0;
  266. $result=dol_delete_dir_recursive($dirout2,$count,1,0,$countdeleted); // If it has no permission to delete, it will fails as if dir does not exists, so we can't test it
  267. print __METHOD__." result=".$result."\n";
  268. $this->assertGreaterThanOrEqual(1,$result);
  269. $this->assertGreaterThanOrEqual(1,$countdeleted);
  270. }
  271. /**
  272. * testDolCopyMoveDelete
  273. *
  274. * @return int
  275. *
  276. * @depends testDolDeleteDir
  277. * The depends says test is run only if previous is ok
  278. */
  279. public function testDolCopyMoveDelete()
  280. {
  281. global $conf,$user,$langs,$db;
  282. $conf=$this->savconf;
  283. $user=$this->savuser;
  284. $langs=$this->savlangs;
  285. $db=$this->savdb;
  286. $file=dirname(__FILE__).'/Example_import_company_1.csv';
  287. $result=dol_copy($file, '/adir/that/does/not/exists/file.csv');
  288. print __METHOD__." result=".$result."\n";
  289. $this->assertLessThan(0,$result,'copy dir that does not exists'); // We should have error
  290. $result=dol_copy($file, $conf->admin->dir_temp.'/file.csv',0,1);
  291. print __METHOD__." result=".$result."\n";
  292. $this->assertGreaterThanOrEqual(1,$result, 'copy file ('.$file.') into a dir that exists ('.$conf->admin->dir_temp.'/file.csv'.')'); // Should be 1
  293. // Again to test with overwriting=0
  294. $result=dol_copy($file, $conf->admin->dir_temp.'/file.csv',0,0);
  295. print __METHOD__." result=".$result."\n";
  296. $this->assertEquals(0,$result, 'copy destination already exists, no overwrite'); // Should be 0
  297. // Again to test with overwriting=1
  298. $result=dol_copy($file, $conf->admin->dir_temp.'/file.csv',0,1);
  299. print __METHOD__." result=".$result."\n";
  300. $this->assertGreaterThanOrEqual(1,$result,'copy destination already exists, overwrite'); // Should be 1
  301. // To test a move that should work
  302. $result=dol_move($conf->admin->dir_temp.'/file.csv',$conf->admin->dir_temp.'/file2.csv',0,1);
  303. print __METHOD__." result=".$result."\n";
  304. $this->assertTrue($result,'move with default mask');
  305. // To test a move that should work with forced mask
  306. $result=dol_move($conf->admin->dir_temp.'/file2.csv',$conf->admin->dir_temp.'/file3.csv','0754',1); // file shoutld be rwxr-wr--
  307. print __METHOD__." result=".$result."\n";
  308. $this->assertTrue($result,'move with forced mask');
  309. // To test a delete that should success
  310. $result=dol_delete_file($conf->admin->dir_temp.'/file3.csv');
  311. print __METHOD__." result=".$result."\n";
  312. $this->assertTrue($result,'delete file');
  313. // Again to test there is error when deleting a non existing file with option disableglob
  314. $result=dol_delete_file($conf->admin->dir_temp.'/file3.csv',1,1);
  315. print __METHOD__." result=".$result."\n";
  316. $this->assertFalse($result,'delete file that does not exists with disableglo must return ko');
  317. // Again to test there is no error when deleting a non existing file without option disableglob
  318. $result=dol_delete_file($conf->admin->dir_temp.'/file3csv',0,1);
  319. print __METHOD__." result=".$result."\n";
  320. $this->assertTrue($result,'delete file that does not exists without disabling glob must return ok');
  321. // Test copy with special char / delete with blob
  322. $result=dol_copy($file, $conf->admin->dir_temp.'/file with [x] and é.csv',0,1);
  323. print __METHOD__." result=".$result."\n";
  324. $this->assertGreaterThanOrEqual(1,$result,'copy file with special chars, overwrite'); // Should be 1
  325. // Try to delete using a glob criteria
  326. $result=dol_delete_file($conf->admin->dir_temp.'/file with [x]*é.csv');
  327. print __METHOD__." result=".$result."\n";
  328. $this->assertTrue($result,'delete file using glob');
  329. }
  330. /**
  331. * testDolCompressUnCompress
  332. *
  333. * @return string
  334. *
  335. * @depends testDolCopyMoveDelete
  336. * The depends says test is run only if previous is ok
  337. */
  338. public function testDolCompressUnCompress()
  339. {
  340. global $conf,$user,$langs,$db;
  341. $conf=$this->savconf;
  342. $user=$this->savuser;
  343. $langs=$this->savlangs;
  344. $db=$this->savdb;
  345. $format='zip';
  346. $filein=dirname(__FILE__).'/Example_import_company_1.csv';
  347. $fileout=$conf->admin->dir_temp.'/test.'.$format;
  348. $dirout=$conf->admin->dir_temp.'/test';
  349. dol_delete_file($fileout);
  350. $count=0;
  351. dol_delete_dir_recursive($dirout,$count,1);
  352. $result=dol_compress_file($filein, $fileout, $format);
  353. print __METHOD__." result=".$result."\n";
  354. $this->assertGreaterThanOrEqual(1,$result);
  355. $result=dol_uncompress($fileout, $dirout);
  356. print __METHOD__." result=".join(',',$result)."\n";
  357. $this->assertEquals(0,count($result));
  358. }
  359. /**
  360. * testDolDirList
  361. *
  362. * @return string
  363. *
  364. * @depends testDolCompressUnCompress
  365. * The depends says test is run only if previous is ok
  366. */
  367. public function testDolDirList()
  368. {
  369. global $conf,$user,$langs,$db;
  370. // 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).
  371. $founddirs=dol_dir_list(DOL_DOCUMENT_ROOT.'/includes/', 'files', 1, '^jquery\.js', array('ckeditor'));
  372. print __METHOD__." count(founddirs)=".count($founddirs)."\n";
  373. $this->assertEquals(1,count($founddirs));
  374. }
  375. /**
  376. * testDolCheckSecureAccessDocument
  377. *
  378. * @return void
  379. */
  380. public function testDolCheckSecureAccessDocument()
  381. {
  382. global $conf,$user,$langs,$db;
  383. $conf=$this->savconf;
  384. $user=$this->savuser;
  385. $langs=$this->savlangs;
  386. $db=$this->savdb;
  387. //$dummyuser=new User($db);
  388. //$result=restrictedArea($dummyuser,'societe');
  389. // We save user properties
  390. $savpermlire = $user->rights->facture->lire;
  391. $savpermcreer = $user->rights->facture->creer;
  392. // Check access to SPECIMEN
  393. $user->rights->facture->lire = 0;
  394. $user->rights->facture->creer = 0;
  395. $filename='SPECIMEN.pdf'; // Filename relative to module part
  396. $result=dol_check_secure_access_document('facture', $filename, 0, '', '', 'read');
  397. $this->assertEquals(1,$result['accessallowed']);
  398. // Check read permission
  399. $user->rights->facture->lire = 1;
  400. $user->rights->facture->creer = 1;
  401. $filename='FA010101/FA010101.pdf'; // Filename relative to module part
  402. $result=dol_check_secure_access_document('facture', $filename, 0, '', '', 'read');
  403. $this->assertEquals(1,$result['accessallowed']);
  404. $user->rights->facture->lire = 0;
  405. $user->rights->facture->creer = 0;
  406. $filename='FA010101/FA010101.pdf'; // Filename relative to module part
  407. $result=dol_check_secure_access_document('facture', $filename, 0, '', '', 'read');
  408. $this->assertEquals(0,$result['accessallowed']);
  409. // Check write permission
  410. $user->rights->facture->lire = 0;
  411. $user->rights->facture->creer = 0;
  412. $filename='FA010101/FA010101.pdf'; // Filename relative to module part
  413. $result=dol_check_secure_access_document('facture', $filename, 0, '', '', 'write');
  414. $this->assertEquals(0,$result['accessallowed']);
  415. $user->rights->facture->lire = 1;
  416. $user->rights->facture->creer = 1;
  417. $filename='FA010101/FA010101.pdf'; // Filename relative to module part
  418. $result=dol_check_secure_access_document('facture', $filename, 0, '', '', 'write');
  419. $this->assertEquals(1,$result['accessallowed']);
  420. $user->rights->facture->lire = 1;
  421. $user->rights->facture->creer = 0;
  422. $filename='FA010101/FA010101.pdf'; // Filename relative to module part
  423. $result=dol_check_secure_access_document('facture', $filename, 0, '', '', 'write');
  424. $this->assertEquals(0,$result['accessallowed']);
  425. // We restore user properties
  426. $user->rights->facture->lire = $savpermlire;
  427. $user->rights->facture->creer = $savpermcreer;
  428. }
  429. }