UserTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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()
  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()
  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()
  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()
  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','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. * testUserSetPassword
  228. *
  229. * @param User $localobject User
  230. * @return void
  231. * @depends testUserOther
  232. * The depends says test is run only if previous is ok
  233. */
  234. public function testUserSetPassword($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. // Test the 'none' password generator
  242. $conf->global->USER_PASSWORD_GENERATED = 'none';
  243. $localobject->error = '';
  244. $result = $localobject->setPassword($user, 'abcdef');
  245. print __METHOD__." set a small password with USER_PASSWORD_GENERATED = none\n";
  246. print __METHOD__." localobject->error=".$localobject->error."\n";
  247. $this->assertEquals('abcdef', $result);
  248. // Test the 'standard' password generator
  249. $conf->global->USER_PASSWORD_GENERATED = 'standard';
  250. $localobject->error = '';
  251. $result = $localobject->setPassword($user, '123456789AA');
  252. print __METHOD__." set a too small password with USER_PASSWORD_GENERATED = standard\n";
  253. print __METHOD__." localobject->error=".$localobject->error."\n";
  254. $this->assertEquals(-1, $result, 'We must receive a negative error code (pass too small) and we did not here');
  255. // Test the 'perso' password generator
  256. $conf->global->USER_PASSWORD_GENERATED = 'perso';
  257. $conf->global->USER_PASSWORD_PATTERN = '12;2;2;2;3;1';
  258. $localobject->error = '';
  259. $result = $localobject->setPassword($user, '1234567892BB');
  260. print __METHOD__." set a too small password with USER_PASSWORD_GENERATED = perso\n";
  261. print __METHOD__." localobject->error=".$localobject->error."\n";
  262. $this->assertEquals(-1, $result, 'We must receive a negative error code (pass too small) and we did not here');
  263. $localobject->error = '';
  264. $result = $localobject->setPassword($user, '$*34567890AB');
  265. print __METHOD__." set a good password\n";
  266. print __METHOD__." localobject->error=".$localobject->error."\n";
  267. $this->assertEquals('$*34567890AB', $result, 'We must get the password as it is valid (pass enough long) and we did not here');
  268. // Test uppercase : $chartofound = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  269. $localobject->error = '';
  270. $result = $localobject->setPassword($user, '$*123456789A');
  271. print __METHOD__." set a password without uppercase\n";
  272. print __METHOD__." localobject->error=".$localobject->error."\n";
  273. $this->assertEquals(-1, $result, 'We must receive a negative error code (pass without enough uppercase) and we did not here');
  274. $localobject->error = '';
  275. $result = $localobject->setPassword($user, '$*34567890CD');
  276. print __METHOD__." set a password with enough uppercase\n";
  277. print __METHOD__." localobject->error=".$localobject->error."\n";
  278. $this->assertEquals('$*34567890CD', $result, 'We must get the password as it is valid (pass with enough uppercase) and we did not here');
  279. // Test digits : $chartofound = "!@#$%&*()_-+={}[]\\|:;'/";
  280. $localobject->error = '';
  281. $result = $localobject->setPassword($user, '$*ABCDEFGHIJ');
  282. print __METHOD__." set a password without digits\n";
  283. print __METHOD__." localobject->error=".$localobject->error."\n";
  284. $this->assertEquals(-1, $result, 'We must receive a negative error code (pass without enough digits) and we did not here');
  285. $localobject->error = '';
  286. $result = $localobject->setPassword($user, '$*12ABCDEFGH');
  287. print __METHOD__." set a password with enough digits\n";
  288. print __METHOD__." localobject->error=".$localobject->error."\n";
  289. $this->assertEquals('$*12ABCDEFGH', $result, 'We must get the password as it is valid (pass with enough digits) and we did not here');
  290. // Test special chars : $chartofound = "!@#$%&*()_-+={}[]\\|:;'/";
  291. $localobject->error = '';
  292. $result = $localobject->setPassword($user, '1234567890AA');
  293. print __METHOD__." set a password without enough special chars\n";
  294. print __METHOD__." localobject->error=".$localobject->error."\n";
  295. $this->assertEquals(-1, $result, 'We must receive a negative error code (pass without enough special chars) and we did not here');
  296. $localobject->error = '';
  297. $result = $localobject->setPassword($user, '$*12345678AA');
  298. print __METHOD__." set a password with enough special chars\n";
  299. print __METHOD__." localobject->error=".$localobject->error."\n";
  300. $this->assertEquals('$*12345678AA', $result, 'We must get the password as it is valid (pass with enough special chars) and we did not here');
  301. // Test consecutive chars
  302. $localobject->error = '';
  303. $result = $localobject->setPassword($user, '$*1111567890AA');
  304. print __METHOD__." set a password with too many consecutive chars\n";
  305. print __METHOD__." localobject->error=".$localobject->error."\n";
  306. $this->assertEquals(-1, $result, 'We must receive a negative error code (pass has n consecutive similar chars) and we did not here');
  307. $localobject->error = '';
  308. $result = $localobject->setPassword($user, '$*11145678AA');
  309. print __METHOD__." set a password with noo too much consecutive chars\n";
  310. print __METHOD__." localobject->error=".$localobject->error."\n";
  311. $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');
  312. return $localobject->id;
  313. }
  314. /**
  315. * testUserDelete
  316. *
  317. * @param int $id User id
  318. * @return void
  319. * @depends testUserSetPassword
  320. * The depends says test is run only if previous is ok
  321. */
  322. public function testUserDelete($id)
  323. {
  324. global $conf,$user,$langs,$db;
  325. $conf=$this->savconf;
  326. $user=$this->savuser;
  327. $langs=$this->savlangs;
  328. $db=$this->savdb;
  329. $localobject=new User($this->savdb);
  330. $result=$localobject->fetch($id);
  331. $result=$localobject->delete($user);
  332. print __METHOD__." id=".$id." result=".$result."\n";
  333. $this->assertLessThan($result, 0);
  334. return $result;
  335. }
  336. /**
  337. * testUserAddPermission
  338. *
  339. * @param int $id User id
  340. * @return void
  341. * @depends testUserDelete
  342. * The depends says test is run only if previous is ok
  343. */
  344. public function testUserAddPermission($id)
  345. {
  346. global $conf,$user,$langs,$db;
  347. $conf=$this->savconf;
  348. $user=$this->savuser;
  349. $langs=$this->savlangs;
  350. $db=$this->savdb;
  351. $localobject=new User($this->savdb);
  352. $result=$localobject->fetch(1); // Other tests use the user id 1
  353. $result=$localobject->addrights(0, 'supplier_proposal');
  354. print __METHOD__." id=".$id." result=".$result."\n";
  355. $this->assertLessThan($result, 0);
  356. return $result;
  357. }
  358. /**
  359. * Edit an object to test updates
  360. *
  361. * @param Object $localobject Object User
  362. * @return void
  363. */
  364. public function changeProperties(&$localobject)
  365. {
  366. $localobject->note_private='New note after update';
  367. }
  368. /**
  369. * Compare all public properties values of 2 objects
  370. *
  371. * @param Object $oA Object operand 1
  372. * @param Object $oB Object operand 2
  373. * @param boolean $ignoretype False will not report diff if type of value differs
  374. * @param array $fieldstoignorearray Array of fields to ignore in diff
  375. * @return array Array with differences
  376. */
  377. public function objCompare($oA, $oB, $ignoretype = true, $fieldstoignorearray = array('id'))
  378. {
  379. $retAr=array();
  380. if (get_class($oA) !== get_class($oB)) {
  381. $retAr[]="Supplied objects are not of same class.";
  382. } else {
  383. $oVarsA=get_object_vars($oA);
  384. $oVarsB=get_object_vars($oB);
  385. $aKeys=array_keys($oVarsA);
  386. foreach ($aKeys as $sKey) {
  387. if (in_array($sKey, $fieldstoignorearray)) {
  388. continue;
  389. }
  390. if (! $ignoretype && ($oVarsA[$sKey] !== $oVarsB[$sKey])) {
  391. $retAr[]=$sKey.' : '.(is_object($oVarsA[$sKey])?get_class($oVarsA[$sKey]):$oVarsA[$sKey]).' <> '.(is_object($oVarsB[$sKey])?get_class($oVarsB[$sKey]):$oVarsB[$sKey]);
  392. }
  393. if ($ignoretype && ($oVarsA[$sKey] != $oVarsB[$sKey])) {
  394. $retAr[]=$sKey.' : '.(is_object($oVarsA[$sKey])?get_class($oVarsA[$sKey]):$oVarsA[$sKey]).' <> '.(is_object($oVarsB[$sKey])?get_class($oVarsB[$sKey]):$oVarsB[$sKey]);
  395. }
  396. }
  397. }
  398. return $retAr;
  399. }
  400. }