UserTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\User\Model;
  7. use Magento\Framework\Serialize\Serializer\Json;
  8. /**
  9. * @magentoAppArea adminhtml
  10. */
  11. class UserTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\User\Model\User
  15. */
  16. protected $_model;
  17. /**
  18. * @var \Magento\Framework\Stdlib\DateTime
  19. */
  20. protected $_dateTime;
  21. /**
  22. * @var \Magento\Authorization\Model\Role
  23. */
  24. protected static $_newRole;
  25. /**
  26. * @var Json
  27. */
  28. private $serializer;
  29. protected function setUp()
  30. {
  31. $this->_model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  32. \Magento\User\Model\User::class
  33. );
  34. $this->_dateTime = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  35. \Magento\Framework\Stdlib\DateTime::class
  36. );
  37. $this->serializer = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  38. Json::class
  39. );
  40. }
  41. /**
  42. * @magentoDbIsolation enabled
  43. */
  44. public function testCRUD()
  45. {
  46. $this->_model->setFirstname(
  47. "John"
  48. )->setLastname(
  49. "Doe"
  50. )->setUsername(
  51. 'user2'
  52. )->setPassword(
  53. \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD
  54. )->setEmail(
  55. 'user@magento.com'
  56. );
  57. $crud = new \Magento\TestFramework\Entity($this->_model, ['firstname' => '_New_name_']);
  58. $crud->testCrud();
  59. }
  60. /**
  61. * @magentoDataFixture Magento/User/_files/dummy_user.php
  62. */
  63. public function testCreatedOnUpdate()
  64. {
  65. $this->_model->loadByUsername('user_created_date');
  66. $this->assertEquals('2010-01-06 00:00:00', $this->_model->getCreated());
  67. //reload to update lognum record
  68. $this->_model->getResource()->recordLogin($this->_model);
  69. $this->_model->reload();
  70. $this->assertEquals('2010-01-06 00:00:00', $this->_model->getCreated());
  71. }
  72. /**
  73. * Ensure that an exception is not thrown, if the user does not exist
  74. */
  75. public function testLoadByUsername()
  76. {
  77. $this->_model->loadByUsername('non_existing_user');
  78. $this->assertNull($this->_model->getId(), 'The admin user has an unexpected ID');
  79. $this->_model->loadByUsername(\Magento\TestFramework\Bootstrap::ADMIN_NAME);
  80. $this->assertNotEmpty($this->_model->getId(), 'The admin user should have been loaded');
  81. }
  82. /**
  83. * Test that user role is updated after save
  84. *
  85. * @magentoDataFixture roleDataFixture
  86. */
  87. public function testUpdateRoleOnSave()
  88. {
  89. $this->_model->loadByUsername(\Magento\TestFramework\Bootstrap::ADMIN_NAME);
  90. $this->assertEquals(\Magento\TestFramework\Bootstrap::ADMIN_ROLE_NAME, $this->_model->getRole()->getRoleName());
  91. $this->_model->setRoleId(self::$_newRole->getId())->save();
  92. $this->assertEquals('admin_role', $this->_model->getRole()->getRoleName());
  93. }
  94. public static function roleDataFixture()
  95. {
  96. self::$_newRole = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  97. \Magento\Authorization\Model\Role::class
  98. );
  99. self::$_newRole->setName('admin_role')->setRoleType('G')->setPid('1');
  100. self::$_newRole->save();
  101. }
  102. /**
  103. * @magentoDbIsolation enabled
  104. */
  105. public function testSaveExtra()
  106. {
  107. $this->_model->loadByUsername(\Magento\TestFramework\Bootstrap::ADMIN_NAME);
  108. $this->_model->saveExtra(['test' => 'val']);
  109. $this->_model->loadByUsername(\Magento\TestFramework\Bootstrap::ADMIN_NAME);
  110. $extra = $this->serializer->unserialize($this->_model->getExtra());
  111. $this->assertEquals($extra['test'], 'val');
  112. }
  113. /**
  114. * @magentoDataFixture roleDataFixture
  115. */
  116. public function testGetRoles()
  117. {
  118. $this->_model->loadByUsername(\Magento\TestFramework\Bootstrap::ADMIN_NAME);
  119. $roles = $this->_model->getRoles();
  120. $this->assertEquals(1, count($roles));
  121. $this->assertEquals(\Magento\TestFramework\Bootstrap::ADMIN_ROLE_NAME, $this->_model->getRole()->getRoleName());
  122. $this->_model->setRoleId(self::$_newRole->getId())->save();
  123. $roles = $this->_model->getRoles();
  124. $this->assertEquals(1, count($roles));
  125. $this->assertEquals(self::$_newRole->getId(), $roles[0]);
  126. }
  127. /**
  128. * @magentoDataFixture roleDataFixture
  129. */
  130. public function testGetRole()
  131. {
  132. $this->_model->loadByUsername(\Magento\TestFramework\Bootstrap::ADMIN_NAME);
  133. $role = $this->_model->getRole();
  134. $this->assertInstanceOf(\Magento\Authorization\Model\Role::class, $role);
  135. $this->assertEquals(\Magento\TestFramework\Bootstrap::ADMIN_ROLE_NAME, $this->_model->getRole()->getRoleName());
  136. $this->_model->setRoleId(self::$_newRole->getId())->save();
  137. $role = $this->_model->getRole();
  138. $this->assertEquals(self::$_newRole->getId(), $role->getId());
  139. }
  140. /**
  141. * @magentoDbIsolation enabled
  142. */
  143. public function testDeleteFromRole()
  144. {
  145. $this->_model->loadByUsername(\Magento\TestFramework\Bootstrap::ADMIN_NAME);
  146. $roles = $this->_model->getRoles();
  147. $this->_model->setRoleId(reset($roles))->deleteFromRole();
  148. $role = $this->_model->getRole();
  149. $this->assertNull($role->getId());
  150. }
  151. public function testRoleUserExists()
  152. {
  153. $this->_model->loadByUsername(\Magento\TestFramework\Bootstrap::ADMIN_NAME);
  154. $role = $this->_model->getRole();
  155. $this->_model->setRoleId($role->getId());
  156. $this->assertTrue($this->_model->roleUserExists());
  157. $this->_model->setRoleId(100);
  158. $this->assertFalse($this->_model->roleUserExists());
  159. }
  160. public function testGetCollection()
  161. {
  162. $this->assertInstanceOf(
  163. \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection::class,
  164. $this->_model->getCollection()
  165. );
  166. }
  167. public function testGetName()
  168. {
  169. $firstname = \Magento\TestFramework\Bootstrap::ADMIN_FIRSTNAME;
  170. $lastname = \Magento\TestFramework\Bootstrap::ADMIN_LASTNAME;
  171. $this->_model->loadByUsername(\Magento\TestFramework\Bootstrap::ADMIN_NAME);
  172. $this->assertEquals("$firstname $lastname", $this->_model->getName());
  173. $this->assertEquals("$firstname///$lastname", $this->_model->getName('///'));
  174. }
  175. public function testGetUninitializedAclRole()
  176. {
  177. $newuser = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\User\Model\User::class);
  178. $newuser->setUserId(10);
  179. $this->assertNull($newuser->getAclRole(), "User role was not initialized and is expected to be empty.");
  180. }
  181. /**
  182. * @magentoAppIsolation enabled
  183. * @magentoAdminConfigFixture admin/captcha/enable 0
  184. * @magentoAdminConfigFixture admin/security/use_case_sensitive_login 1
  185. */
  186. public function testAuthenticate()
  187. {
  188. $this->assertFalse($this->_model->authenticate('User', \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD));
  189. $this->assertTrue(
  190. $this->_model->authenticate(
  191. \Magento\TestFramework\Bootstrap::ADMIN_NAME,
  192. \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD
  193. )
  194. );
  195. }
  196. /**
  197. * @magentoAppIsolation enabled
  198. * @magentoAdminConfigFixture admin/captcha/enable 0
  199. * @magentoConfigFixture current_store admin/security/use_case_sensitive_login 0
  200. */
  201. public function testAuthenticateCaseInsensitive()
  202. {
  203. $this->assertTrue($this->_model->authenticate('user', \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD));
  204. $this->assertTrue(
  205. $this->_model->authenticate(
  206. \Magento\TestFramework\Bootstrap::ADMIN_NAME,
  207. \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD
  208. )
  209. );
  210. }
  211. /**
  212. * @expectedException \Magento\Framework\Exception\LocalizedException
  213. * @expectedException \Magento\Framework\Exception\AuthenticationException
  214. * @magentoDbIsolation enabled
  215. */
  216. public function testAuthenticateInactiveUser()
  217. {
  218. $this->_model->load(1);
  219. $this->_model->setIsActive(0)->save();
  220. $this->_model->authenticate(
  221. \Magento\TestFramework\Bootstrap::ADMIN_NAME,
  222. \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD
  223. );
  224. }
  225. /**
  226. * @expectedException \Magento\Framework\Exception\AuthenticationException
  227. * @magentoDbIsolation enabled
  228. */
  229. public function testAuthenticateUserWithoutRole()
  230. {
  231. $this->_model->loadByUsername(\Magento\TestFramework\Bootstrap::ADMIN_NAME);
  232. $roles = $this->_model->getRoles();
  233. $this->_model->setRoleId(reset($roles))->deleteFromRole();
  234. $this->_model->authenticate(
  235. \Magento\TestFramework\Bootstrap::ADMIN_NAME,
  236. \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD
  237. );
  238. }
  239. /**
  240. * @magentoDbIsolation enabled
  241. * @magentoAdminConfigFixture admin/captcha/enable 0
  242. */
  243. public function testLoginsAreLogged()
  244. {
  245. $this->_model->loadByUsername(\Magento\TestFramework\Bootstrap::ADMIN_NAME);
  246. $lognum = $this->_model->getLognum();
  247. $beforeLogin = time();
  248. $this->_model->login(
  249. \Magento\TestFramework\Bootstrap::ADMIN_NAME,
  250. \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD
  251. )->reload();
  252. $loginTime = strtotime($this->_model->getLogdate());
  253. $this->assertTrue($beforeLogin <= $loginTime && $loginTime <= time());
  254. $this->assertEquals(++$lognum, $this->_model->getLognum());
  255. $beforeLogin = time();
  256. $this->_model->login(
  257. \Magento\TestFramework\Bootstrap::ADMIN_NAME,
  258. \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD
  259. )->reload();
  260. $loginTime = strtotime($this->_model->getLogdate());
  261. $this->assertTrue($beforeLogin <= $loginTime && $loginTime <= time());
  262. $this->assertEquals(++$lognum, $this->_model->getLognum());
  263. }
  264. public function testReload()
  265. {
  266. $this->_model->loadByUsername(\Magento\TestFramework\Bootstrap::ADMIN_NAME);
  267. $this->_model->setFirstname('NewFirstName');
  268. $this->assertEquals('NewFirstName', $this->_model->getFirstname());
  269. $this->_model->reload();
  270. $this->assertEquals(\Magento\TestFramework\Bootstrap::ADMIN_FIRSTNAME, $this->_model->getFirstname());
  271. }
  272. /**
  273. * @magentoDbIsolation enabled
  274. */
  275. public function testHasAssigned2Role()
  276. {
  277. $this->_model->loadByUsername(\Magento\TestFramework\Bootstrap::ADMIN_NAME);
  278. $role = $this->_model->hasAssigned2Role($this->_model);
  279. $this->assertEquals(1, count($role));
  280. $this->assertArrayHasKey('role_id', $role[0]);
  281. $roles = $this->_model->getRoles();
  282. $this->_model->setRoleId(reset($roles))->deleteFromRole();
  283. $this->assertEmpty($this->_model->hasAssigned2Role($this->_model));
  284. }
  285. /**
  286. * @expectedException \Magento\Framework\Exception\LocalizedException
  287. * @expectedExceptionMessage "User Name" is required. Enter and try again.
  288. * @expectedExceptionMessage "First Name" is required. Enter and try again.
  289. * @expectedExceptionMessage "Last Name" is required. Enter and try again.
  290. * @expectedExceptionMessage Please enter a valid email.
  291. * @expectedExceptionMessage "Password" is required. Enter and try again.
  292. * @magentoDbIsolation enabled
  293. */
  294. public function testBeforeSaveRequiredFieldsValidation()
  295. {
  296. $this->_model->setSomething('some_value');
  297. // force model change
  298. $this->_model->save();
  299. }
  300. /**
  301. * @magentoDbIsolation enabled
  302. */
  303. public function testBeforeSavePasswordHash()
  304. {
  305. $this->_model->setUsername(
  306. 'john.doe'
  307. )->setFirstname(
  308. 'John'
  309. )->setLastname(
  310. 'Doe'
  311. )->setEmail(
  312. 'jdoe@example.com'
  313. )->setPassword(
  314. '123123q'
  315. );
  316. $this->_model->save();
  317. $this->assertNotContains('123123q', $this->_model->getPassword(), 'Password is expected to be hashed');
  318. $this->assertRegExp(
  319. '/^[0-9a-f]+:[0-9a-zA-Z]{32}:[0-9]+$/',
  320. $this->_model->getPassword(),
  321. 'Salt is expected to be saved along with the password'
  322. );
  323. /** @var \Magento\User\Model\User $model */
  324. $model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\User\Model\User::class);
  325. $model->load($this->_model->getId());
  326. $this->assertEquals(
  327. $this->_model->getPassword(),
  328. $model->getPassword(),
  329. 'Password data has been corrupted during saving'
  330. );
  331. }
  332. /**
  333. * @expectedException \Magento\Framework\Exception\LocalizedException
  334. * @expectedExceptionMessage Your password confirmation must match your password.
  335. * @magentoDbIsolation enabled
  336. */
  337. public function testBeforeSavePasswordsDoNotMatch()
  338. {
  339. $this->_model->setPassword('password2');
  340. $this->_model->setPasswordConfirmation('password1');
  341. $this->_model->save();
  342. }
  343. /**
  344. * @expectedException \Magento\Framework\Exception\LocalizedException
  345. * @expectedExceptionMessage Your password must include both numeric and alphabetic characters.
  346. * @magentoDbIsolation enabled
  347. */
  348. public function testBeforeSavePasswordTooShort()
  349. {
  350. $this->_model->setPassword('123456');
  351. $this->_model->save();
  352. }
  353. /**
  354. * @dataProvider beforeSavePasswordInsecureDataProvider
  355. * @expectedException \Magento\Framework\Exception\LocalizedException
  356. * @expectedExceptionMessage Your password must include both numeric and alphabetic characters.
  357. * @magentoDbIsolation enabled
  358. * @param string $password
  359. */
  360. public function testBeforeSavePasswordInsecure($password)
  361. {
  362. $this->_model->setPassword($password);
  363. $this->_model->save();
  364. }
  365. public function beforeSavePasswordInsecureDataProvider()
  366. {
  367. return ['alpha chars only' => ['aaaaaaaa'], 'digits only' => ['1234567']];
  368. }
  369. /**
  370. * @expectedException \Magento\Framework\Exception\LocalizedException
  371. * @expectedExceptionMessage A user with the same user name or email already exists.
  372. * @magentoDbIsolation enabled
  373. */
  374. public function testBeforeSaveUserIdentityViolation()
  375. {
  376. $this->_model->setUsername('user');
  377. $this->_model->save();
  378. }
  379. /**
  380. * @magentoDbIsolation enabled
  381. */
  382. public function testBeforeSaveValidationSuccess()
  383. {
  384. $this->_model->setUsername(
  385. 'user1'
  386. )->setFirstname(
  387. 'John'
  388. )->setLastname(
  389. 'Doe'
  390. )->setEmail(
  391. 'jdoe@example.com'
  392. )->setPassword(
  393. '1234abc'
  394. )->setPasswordConfirmation(
  395. '1234abc'
  396. );
  397. $this->_model->save();
  398. }
  399. /**
  400. * @magentoDbIsolation enabled
  401. */
  402. public function testChangeResetPasswordLinkToken()
  403. {
  404. $this->_model->loadByUsername(\Magento\TestFramework\Bootstrap::ADMIN_NAME);
  405. $this->_model->changeResetPasswordLinkToken('test');
  406. $date = $this->_model->getRpTokenCreatedAt();
  407. $this->assertNotNull($date);
  408. $this->_model->save();
  409. $this->_model->loadByUsername(\Magento\TestFramework\Bootstrap::ADMIN_NAME);
  410. $this->assertEquals('test', $this->_model->getRpToken());
  411. $this->assertEquals(strtotime($date), strtotime($this->_model->getRpTokenCreatedAt()));
  412. }
  413. /**
  414. * @magentoDbIsolation enabled
  415. * @magentoAppIsolation enabled
  416. * @magentoConfigFixture default/admin/emails/password_reset_link_expiration_period 2
  417. */
  418. public function testIsResetPasswordLinkTokenExpired()
  419. {
  420. $this->_model->loadByUsername(\Magento\TestFramework\Bootstrap::ADMIN_NAME);
  421. $this->assertTrue($this->_model->isResetPasswordLinkTokenExpired());
  422. $this->_model->changeResetPasswordLinkToken('test');
  423. $this->_model->save();
  424. $this->_model->loadByUsername(\Magento\TestFramework\Bootstrap::ADMIN_NAME);
  425. $this->assertFalse($this->_model->isResetPasswordLinkTokenExpired());
  426. $this->_model->setRpTokenCreatedAt($this->_dateTime->formatDate(time() - 60 * 60 * 2 + 2));
  427. $this->assertFalse($this->_model->isResetPasswordLinkTokenExpired());
  428. $this->_model->setRpTokenCreatedAt($this->_dateTime->formatDate(time() - 60 * 60 * 2 - 1));
  429. $this->assertTrue($this->_model->isResetPasswordLinkTokenExpired());
  430. }
  431. public function testGetSetHasAvailableResources()
  432. {
  433. $this->_model->setHasAvailableResources(true);
  434. $this->assertTrue($this->_model->hasAvailableResources());
  435. $this->_model->setHasAvailableResources(false);
  436. $this->assertFalse($this->_model->hasAvailableResources());
  437. }
  438. /**
  439. * Here we test if admin identity check executed successfully
  440. *
  441. * @magentoDataFixture Magento/User/_files/user_with_role.php
  442. */
  443. public function testPerformIdentityCheck()
  444. {
  445. $this->_model->loadByUsername('adminUser');
  446. $passwordString = \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD;
  447. $this->_model->performIdentityCheck($passwordString);
  448. }
  449. /**
  450. * Here we check for a wrong password
  451. *
  452. * @magentoDataFixture Magento/User/_files/user_with_role.php
  453. * @expectedException \Magento\Framework\Exception\AuthenticationException
  454. */
  455. public function testPerformIdentityCheckWrongPassword()
  456. {
  457. $this->_model->loadByUsername('adminUser');
  458. $passwordString = 'wrongPassword';
  459. $this->_model->performIdentityCheck($passwordString);
  460. $this->expectExceptionMessage(
  461. 'The password entered for the current user is invalid. Verify the password and try again.'
  462. );
  463. }
  464. /**
  465. * Here we check for a locked user
  466. *
  467. * @magentoDataFixture Magento/User/_files/locked_users.php
  468. * @expectedException \Magento\Framework\Exception\State\UserLockedException
  469. */
  470. public function testPerformIdentityCheckLockExpires()
  471. {
  472. $this->_model->loadByUsername('adminUser2');
  473. $this->_model->performIdentityCheck(\Magento\TestFramework\Bootstrap::ADMIN_PASSWORD);
  474. $this->expectExceptionMessage(
  475. 'The account sign-in was incorrect or your account is disabled temporarily. '
  476. . 'Please wait and try again later.'
  477. );
  478. }
  479. }