UserTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. <?php
  2. /* Copyright (C) 2010-2015 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. * or see https://www.gnu.org/
  17. */
  18. /**
  19. * \file test/phpunit/UserTest.php
  20. * \ingroup test
  21. * \brief PHPUnit test
  22. * \remarks To run this script as CLI: phpunit filename.php
  23. */
  24. global $conf,$user,$langs,$db;
  25. //define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver
  26. //require_once 'PHPUnit/Autoload.php';
  27. require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
  28. require_once dirname(__FILE__).'/../../htdocs/user/class/user.class.php';
  29. if (empty($user->id)) {
  30. print "Load permissions for admin user nb 1\n";
  31. $user->fetch(1);
  32. $user->getrights();
  33. }
  34. $conf->global->MAIN_DISABLE_ALL_MAILS=1;
  35. /**
  36. * Class for PHPUnit tests
  37. *
  38. * @backupGlobals disabled
  39. * @backupStaticAttributes enabled
  40. * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
  41. */
  42. class UserTest extends PHPUnit\Framework\TestCase
  43. {
  44. protected $savconf;
  45. protected $savuser;
  46. protected $savlangs;
  47. protected $savdb;
  48. /**
  49. * Constructor
  50. * We save global variables into local variables
  51. *
  52. * @return UserTest
  53. */
  54. public function __construct()
  55. {
  56. parent::__construct();
  57. //$this->sharedFixture
  58. global $conf,$user,$langs,$db;
  59. $this->savconf=$conf;
  60. $this->savuser=$user;
  61. $this->savlangs=$langs;
  62. $this->savdb=$db;
  63. print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  64. //print " - db ".$db->db;
  65. print "\n";
  66. }
  67. /**
  68. * setUpBeforeClass
  69. *
  70. * @return void
  71. */
  72. public static function setUpBeforeClass(): void
  73. {
  74. global $conf,$user,$langs,$db;
  75. if (!empty($conf->global->MAIN_MODULE_LDAP)) {
  76. print "\n".__METHOD__." module LDAP must be disabled.\n"; die(1);
  77. }
  78. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  79. print __METHOD__."\n";
  80. }
  81. /**
  82. * tearDownAfterClass
  83. *
  84. * @return void
  85. */
  86. public static function tearDownAfterClass(): void
  87. {
  88. global $conf,$user,$langs,$db;
  89. $db->rollback();
  90. print __METHOD__."\n";
  91. }
  92. /**
  93. * Init phpunit tests
  94. *
  95. * @return void
  96. */
  97. protected function setUp(): void
  98. {
  99. global $conf,$user,$langs,$db;
  100. $conf=$this->savconf;
  101. $user=$this->savuser;
  102. $langs=$this->savlangs;
  103. $db=$this->savdb;
  104. print __METHOD__."\n";
  105. }
  106. /**
  107. * End phpunit tests
  108. *
  109. * @return void
  110. */
  111. protected function tearDown(): void
  112. {
  113. print __METHOD__."\n";
  114. }
  115. /**
  116. * testUserCreate
  117. *
  118. * @return void
  119. */
  120. public function testUserCreate()
  121. {
  122. global $conf,$user,$langs,$db;
  123. $conf=$this->savconf;
  124. $user=$this->savuser;
  125. $langs=$this->savlangs;
  126. $db=$this->savdb;
  127. print __METHOD__." USER_PASSWORD_GENERATED=".getDolGlobalString('USER_PASSWORD_GENERATED')."\n";
  128. $localobject=new User($this->savdb);
  129. $localobject->initAsSpecimen();
  130. $result=$localobject->create($user);
  131. $this->assertLessThan($result, 0, 'Creation of user has failed: '.$localobject->error);
  132. print __METHOD__." result=".$result."\n";
  133. return $result;
  134. }
  135. /**
  136. * testUserFetch
  137. *
  138. * @param int $id Id of user
  139. * @return void
  140. * @depends testUserCreate
  141. * The depends says test is run only if previous is ok
  142. */
  143. public function testUserFetch($id)
  144. {
  145. global $conf,$user,$langs,$db;
  146. $conf=$this->savconf;
  147. $user=$this->savuser;
  148. $langs=$this->savlangs;
  149. $db=$this->savdb;
  150. $localobject=new User($this->savdb);
  151. $result=$localobject->fetch($id);
  152. $this->assertLessThan($result, 0);
  153. print __METHOD__." id=".$id." result=".$result."\n";
  154. return $localobject;
  155. }
  156. /**
  157. * testUserUpdate
  158. *
  159. * @param User $localobject User
  160. * @return void
  161. * @depends testUserFetch
  162. * The depends says test is run only if previous is ok
  163. */
  164. public function testUserUpdate($localobject)
  165. {
  166. global $conf,$user,$langs,$db;
  167. $conf=$this->savconf;
  168. $user=$this->savuser;
  169. $langs=$this->savlangs;
  170. $db=$this->savdb;
  171. $this->changeProperties($localobject);
  172. $result=$localobject->update($user);
  173. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  174. $this->assertLessThan($result, 0);
  175. // Test everything are still same than specimen
  176. $newlocalobject=new User($this->savdb);
  177. $newlocalobject->initAsSpecimen();
  178. $this->changeProperties($newlocalobject);
  179. $this->assertEquals($this->objCompare($localobject, $newlocalobject, true, array('id','socid','societe_id','specimen','note','ref','pass','pass_indatabase','pass_indatabase_crypted','pass_temp','datec','datem','datelastlogin','datepreviouslogin','flagdelsessionsbefore','iplastlogin','ippreviouslogin','trackid')), array()); // Actual, Expected
  180. return $localobject;
  181. }
  182. /**
  183. * testUserDisable
  184. *
  185. * @param User $localobject User
  186. * @return void
  187. * @depends testUserUpdate
  188. * The depends says test is run only if previous is ok
  189. */
  190. public function testUserDisable($localobject)
  191. {
  192. global $conf,$user,$langs,$db;
  193. $conf=$this->savconf;
  194. $user=$this->savuser;
  195. $langs=$this->savlangs;
  196. $db=$this->savdb;
  197. $result=$localobject->setstatus(0);
  198. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  199. $this->assertLessThan($result, 0);
  200. return $localobject;
  201. }
  202. /**
  203. * testUserOther
  204. *
  205. * @param User $localobject User
  206. * @return void
  207. * @depends testUserDisable
  208. * The depends says test is run only if previous is ok
  209. */
  210. public function testUserOther($localobject)
  211. {
  212. global $conf,$user,$langs,$db;
  213. $conf=$this->savconf;
  214. $user=$this->savuser;
  215. $langs=$this->savlangs;
  216. $db=$this->savdb;
  217. /*$result=$localobject->setstatus(0);
  218. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  219. $this->assertLessThan($result, 0);
  220. */
  221. $localobject->info($localobject->id);
  222. print __METHOD__." localobject->date_creation=".$localobject->date_creation."\n";
  223. $this->assertNotEquals($localobject->date_creation, '');
  224. return $localobject;
  225. }
  226. /**
  227. * testUserHasRight
  228. * @param User $localobject User
  229. * @return User $localobject User
  230. * @depends testUserOther
  231. */
  232. public function testUserHasRight($localobject)
  233. {
  234. global $conf,$user,$langs,$db;
  235. $conf=$this->savconf;
  236. $user=$this->savuser;
  237. $langs=$this->savlangs;
  238. $db=$this->savdb;
  239. /*$result=$localobject->setstatus(0);
  240. print __METHOD__." id=".$localobject->id." result=".$result."\n";
  241. $this->assertLessThan($result, 0);
  242. */
  243. print __METHOD__." id=". $localobject->id ."\n";
  244. //$this->assertNotEquals($user->date_creation, '');
  245. $localobject->addrights(0, 'supplier_proposal');
  246. $this->assertEquals($localobject->hasRight('member', ''), 0);
  247. $this->assertEquals($localobject->hasRight('member', 'member'), 0);$this->assertEquals($localobject->hasRight('product', 'member', 'read'), 0);
  248. $this->assertEquals($localobject->hasRight('member', 'member'), 0);$this->assertEquals($localobject->hasRight('produit', 'member', 'read'), 0);
  249. return $localobject;
  250. }
  251. /**
  252. * testUserSetPassword
  253. *
  254. * @param User $localobject User
  255. * @return void
  256. * @depends testUserHasRight
  257. * The depends says test is run only if previous is ok
  258. */
  259. public function testUserSetPassword($localobject)
  260. {
  261. global $conf,$user,$langs,$db;
  262. $conf=$this->savconf;
  263. $user=$this->savuser;
  264. $langs=$this->savlangs;
  265. $db=$this->savdb;
  266. // Test the 'none' password generator
  267. $conf->global->USER_PASSWORD_GENERATED = 'none';
  268. $localobject->error = '';
  269. $result = $localobject->setPassword($user, 'abcdef');
  270. print __METHOD__." set a small password with USER_PASSWORD_GENERATED = none\n";
  271. print __METHOD__." localobject->error=".$localobject->error."\n";
  272. $this->assertEquals('abcdef', $result);
  273. // Test the 'standard' password generator
  274. $conf->global->USER_PASSWORD_GENERATED = 'standard';
  275. $localobject->error = '';
  276. $result = $localobject->setPassword($user, '123456789AA');
  277. print __METHOD__." set a too small password with USER_PASSWORD_GENERATED = standard\n";
  278. print __METHOD__." localobject->error=".$localobject->error."\n";
  279. $this->assertEquals(-1, $result, 'We must receive a negative error code (pass too small) and we did not here');
  280. // Test the 'perso' password generator
  281. $conf->global->USER_PASSWORD_GENERATED = 'perso';
  282. $conf->global->USER_PASSWORD_PATTERN = '12;2;2;2;3;1';
  283. $localobject->error = '';
  284. $result = $localobject->setPassword($user, '1234567892BB');
  285. print __METHOD__." set a too small password with USER_PASSWORD_GENERATED = perso\n";
  286. print __METHOD__." localobject->error=".$localobject->error."\n";
  287. $this->assertEquals(-1, $result, 'We must receive a negative error code (pass too small) and we did not here');
  288. $localobject->error = '';
  289. $result = $localobject->setPassword($user, '$*34567890AB');
  290. print __METHOD__." set a good password\n";
  291. print __METHOD__." localobject->error=".$localobject->error."\n";
  292. $this->assertEquals('$*34567890AB', $result, 'We must get the password as it is valid (pass enough long) and we did not here');
  293. // Test uppercase : $chartofound = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  294. $localobject->error = '';
  295. $result = $localobject->setPassword($user, '$*123456789A');
  296. print __METHOD__." set a password without uppercase\n";
  297. print __METHOD__." localobject->error=".$localobject->error."\n";
  298. $this->assertEquals(-1, $result, 'We must receive a negative error code (pass without enough uppercase) and we did not here');
  299. $localobject->error = '';
  300. $result = $localobject->setPassword($user, '$*34567890CD');
  301. print __METHOD__." set a password with enough uppercase\n";
  302. print __METHOD__." localobject->error=".$localobject->error."\n";
  303. $this->assertEquals('$*34567890CD', $result, 'We must get the password as it is valid (pass with enough uppercase) and we did not here');
  304. // Test digits : $chartofound = "!@#$%&*()_-+={}[]\\|:;'/";
  305. $localobject->error = '';
  306. $result = $localobject->setPassword($user, '$*ABCDEFGHIJ');
  307. print __METHOD__." set a password without digits\n";
  308. print __METHOD__." localobject->error=".$localobject->error."\n";
  309. $this->assertEquals(-1, $result, 'We must receive a negative error code (pass without enough digits) and we did not here');
  310. $localobject->error = '';
  311. $result = $localobject->setPassword($user, '$*12ABCDEFGH');
  312. print __METHOD__." set a password with enough digits\n";
  313. print __METHOD__." localobject->error=".$localobject->error."\n";
  314. $this->assertEquals('$*12ABCDEFGH', $result, 'We must get the password as it is valid (pass with enough digits) and we did not here');
  315. // Test special chars : $chartofound = "!@#$%&*()_-+={}[]\\|:;'/";
  316. $localobject->error = '';
  317. $result = $localobject->setPassword($user, '1234567890AA');
  318. print __METHOD__." set a password without enough special chars\n";
  319. print __METHOD__." localobject->error=".$localobject->error."\n";
  320. $this->assertEquals(-1, $result, 'We must receive a negative error code (pass without enough special chars) and we did not here');
  321. $localobject->error = '';
  322. $result = $localobject->setPassword($user, '$*12345678AA');
  323. print __METHOD__." set a password with enough special chars\n";
  324. print __METHOD__." localobject->error=".$localobject->error."\n";
  325. $this->assertEquals('$*12345678AA', $result, 'We must get the password as it is valid (pass with enough special chars) and we did not here');
  326. // Test consecutive chars
  327. $localobject->error = '';
  328. $result = $localobject->setPassword($user, '$*1111567890AA');
  329. print __METHOD__." set a password with too many consecutive chars\n";
  330. print __METHOD__." localobject->error=".$localobject->error."\n";
  331. $this->assertEquals(-1, $result, 'We must receive a negative error code (pass has n consecutive similar chars) and we did not here');
  332. $localobject->error = '';
  333. $result = $localobject->setPassword($user, '$*11145678AA');
  334. print __METHOD__." set a password with noo too much consecutive chars\n";
  335. print __METHOD__." localobject->error=".$localobject->error."\n";
  336. $this->assertEquals('$*11145678AA', $result, 'We must get the password as it is valid (pass has not too much similar consecutive chars) and we did not here');
  337. return $localobject->id;
  338. }
  339. /**
  340. * testUserDelete
  341. *
  342. * @param int $id User id
  343. * @return void
  344. * @depends testUserSetPassword
  345. * The depends says test is run only if previous is ok
  346. */
  347. public function testUserDelete($id)
  348. {
  349. global $conf,$user,$langs,$db;
  350. $conf=$this->savconf;
  351. $user=$this->savuser;
  352. $langs=$this->savlangs;
  353. $db=$this->savdb;
  354. $localobject=new User($this->savdb);
  355. $result=$localobject->fetch($id);
  356. $result=$localobject->delete($user);
  357. print __METHOD__." id=".$id." result=".$result."\n";
  358. $this->assertLessThan($result, 0);
  359. return $result;
  360. }
  361. /**
  362. * testUserAddPermission
  363. *
  364. * @param int $id User id
  365. * @return void
  366. * @depends testUserDelete
  367. * The depends says test is run only if previous is ok
  368. */
  369. public function testUserAddPermission($id)
  370. {
  371. global $conf,$user,$langs,$db;
  372. $conf=$this->savconf;
  373. $user=$this->savuser;
  374. $langs=$this->savlangs;
  375. $db=$this->savdb;
  376. $localobject=new User($this->savdb);
  377. $result=$localobject->fetch(1); // Other tests use the user id 1
  378. $result=$localobject->addrights(0, 'supplier_proposal');
  379. print __METHOD__." id=".$id." result=".$result."\n";
  380. $this->assertLessThan($result, 0);
  381. return $result;
  382. }
  383. /**
  384. * Edit an object to test updates
  385. *
  386. * @param Object $localobject Object User
  387. * @return void
  388. */
  389. public function changeProperties(&$localobject)
  390. {
  391. $localobject->note_private='New note after update';
  392. }
  393. /**
  394. * Compare all public properties values of 2 objects
  395. *
  396. * @param Object $oA Object operand 1
  397. * @param Object $oB Object operand 2
  398. * @param boolean $ignoretype False will not report diff if type of value differs
  399. * @param array $fieldstoignorearray Array of fields to ignore in diff
  400. * @return array Array with differences
  401. */
  402. public function objCompare($oA, $oB, $ignoretype = true, $fieldstoignorearray = array('id'))
  403. {
  404. $retAr=array();
  405. if (get_class($oA) !== get_class($oB)) {
  406. $retAr[]="Supplied objects are not of same class.";
  407. } else {
  408. $oVarsA=get_object_vars($oA);
  409. $oVarsB=get_object_vars($oB);
  410. $aKeys=array_keys($oVarsA);
  411. foreach ($aKeys as $sKey) {
  412. if (in_array($sKey, $fieldstoignorearray)) {
  413. continue;
  414. }
  415. if (! $ignoretype && ($oVarsA[$sKey] !== $oVarsB[$sKey])) {
  416. $retAr[]=$sKey.' : '.(is_object($oVarsA[$sKey])?get_class($oVarsA[$sKey]):$oVarsA[$sKey]).' <> '.(is_object($oVarsB[$sKey])?get_class($oVarsB[$sKey]):$oVarsB[$sKey]);
  417. }
  418. if ($ignoretype && ($oVarsA[$sKey] != $oVarsB[$sKey])) {
  419. $retAr[]=$sKey.' : '.(is_object($oVarsA[$sKey])?get_class($oVarsA[$sKey]):$oVarsA[$sKey]).' <> '.(is_object($oVarsB[$sKey])?get_class($oVarsB[$sKey]):$oVarsB[$sKey]);
  420. }
  421. }
  422. }
  423. return $retAr;
  424. }
  425. }