UserTest.php 15 KB

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