UserTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\User\Controller\Adminhtml;
  7. use Magento\Framework\App\Request\Http as HttpRequest;
  8. use Magento\TestFramework\Bootstrap;
  9. /**
  10. * @magentoAppArea adminhtml
  11. */
  12. class UserTest extends \Magento\TestFramework\TestCase\AbstractBackendController
  13. {
  14. /**
  15. * Verify that the main user page contains the user grid
  16. */
  17. public function testIndexAction()
  18. {
  19. $this->dispatch('backend/admin/user/index');
  20. $response = $this->getResponse()->getBody();
  21. $this->assertContains('Users', $response);
  22. $this->assertEquals(
  23. 1,
  24. \Magento\TestFramework\Helper\Xpath::getElementsCountForXpath(
  25. '//*[@id="permissionsUserGrid_table"]',
  26. $response
  27. )
  28. );
  29. }
  30. /**
  31. * Verify that attempting to save a user when no data is present redirects back to the main user page
  32. */
  33. public function testSaveActionNoData()
  34. {
  35. $this->getRequest()->setMethod(HttpRequest::METHOD_POST);
  36. $this->dispatch('backend/admin/user/save');
  37. $this->assertRedirect($this->stringContains('backend/admin/user/index/'));
  38. }
  39. /**
  40. * Verify that a user cannot be saved if it no longer exists
  41. *
  42. * @magentoDataFixture Magento/User/_files/dummy_user.php
  43. */
  44. public function testSaveActionWrongId()
  45. {
  46. /** @var $user \Magento\User\Model\User */
  47. $user = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  48. \Magento\User\Model\User::class
  49. )->loadByUsername(
  50. 'dummy_username'
  51. );
  52. $userId = $user->getId();
  53. $this->assertNotEmpty($userId, 'Broken fixture');
  54. $user->delete();
  55. $this->getRequest()->setMethod(HttpRequest::METHOD_POST);
  56. $this->getRequest()->setPostValue('user_id', $userId);
  57. $this->dispatch('backend/admin/user/save');
  58. $this->assertSessionMessages(
  59. $this->equalTo(['This user no longer exists.']),
  60. \Magento\Framework\Message\MessageInterface::TYPE_ERROR
  61. );
  62. $this->assertRedirect($this->stringContains('backend/admin/user/index/'));
  63. }
  64. /**
  65. * Verify that users cannot be saved if the admin password is not correct
  66. *
  67. * @magentoDbIsolation enabled
  68. */
  69. public function testSaveActionMissingCurrentAdminPassword()
  70. {
  71. $fixture = uniqid();
  72. $this->getRequest()->setMethod(HttpRequest::METHOD_POST);
  73. $this->getRequest()->setPostValue(
  74. [
  75. 'username' => $fixture,
  76. 'email' => "{$fixture}@example.com",
  77. 'firstname' => 'First',
  78. 'lastname' => 'Last',
  79. 'password' => 'password_with_1_number',
  80. 'password_confirmation' => 'password_with_1_number',
  81. ]
  82. );
  83. $this->dispatch('backend/admin/user/save');
  84. $this->assertSessionMessages(
  85. $this->equalTo(
  86. ['The password entered for the current user is invalid. Verify the password and try again.']
  87. )
  88. );
  89. $this->assertRedirect($this->stringContains('backend/admin/user/edit'));
  90. }
  91. /**
  92. * Verify that users can be successfully saved when data is correct
  93. *
  94. * @magentoDbIsolation enabled
  95. */
  96. public function testSaveAction()
  97. {
  98. $fixture = uniqid();
  99. $this->getRequest()->setMethod(HttpRequest::METHOD_POST);
  100. $this->getRequest()->setPostValue(
  101. [
  102. 'username' => $fixture,
  103. 'email' => "{$fixture}@example.com",
  104. 'firstname' => 'First',
  105. 'lastname' => 'Last',
  106. 'password' => 'password_with_1_number',
  107. 'password_confirmation' => 'password_with_1_number',
  108. \Magento\User\Block\User\Edit\Tab\Main::CURRENT_USER_PASSWORD_FIELD => Bootstrap::ADMIN_PASSWORD,
  109. ]
  110. );
  111. $this->dispatch('backend/admin/user/save');
  112. $this->assertSessionMessages(
  113. $this->equalTo(['You saved the user.']),
  114. \Magento\Framework\Message\MessageInterface::TYPE_SUCCESS
  115. );
  116. $this->assertRedirect($this->stringContains('backend/admin/user/index/'));
  117. }
  118. /**
  119. * Verify that users with the same username or email as an existing user cannot be created
  120. *
  121. * @magentoDbIsolation enabled
  122. * @magentoDataFixture Magento/User/_files/user_with_role.php
  123. */
  124. public function testSaveActionDuplicateUser()
  125. {
  126. $this->getRequest()->setMethod(HttpRequest::METHOD_POST);
  127. $this->getRequest()->setPostValue(
  128. [
  129. 'username' => 'adminUser',
  130. 'email' => 'adminUser@example.com',
  131. 'firstname' => 'John',
  132. 'lastname' => 'Doe',
  133. 'password' => \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD,
  134. 'password_confirmation' => \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD,
  135. \Magento\User\Block\User\Edit\Tab\Main::CURRENT_USER_PASSWORD_FIELD => Bootstrap::ADMIN_PASSWORD,
  136. ]
  137. );
  138. $this->dispatch('backend/admin/user/save/active_tab/main_section');
  139. $this->assertSessionMessages(
  140. $this->equalTo(['A user with the same user name or email already exists.']),
  141. \Magento\Framework\Message\MessageInterface::TYPE_ERROR
  142. );
  143. $this->assertRedirect($this->stringContains('backend/admin/user/edit/'));
  144. $this->assertRedirect($this->matchesRegularExpression('/^((?!active_tab).)*$/'));
  145. }
  146. /**
  147. * Verify password change properly updates fields when the request is valid.
  148. *
  149. * @param array $postData
  150. * @param bool $isPasswordCorrect
  151. *
  152. * @magentoDbIsolation enabled
  153. * @dataProvider saveActionPasswordChangeDataProvider
  154. */
  155. public function testSaveActionPasswordChange($postData, $isPasswordCorrect)
  156. {
  157. $this->getRequest()->setMethod(HttpRequest::METHOD_POST);
  158. $this->getRequest()->setPostValue($postData);
  159. $this->dispatch('backend/admin/user/save');
  160. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  161. /** @var $user \Magento\User\Model\User */
  162. $user = $objectManager->create(\Magento\User\Model\User::class);
  163. $user->loadByUsername($postData['username']);
  164. if ($isPasswordCorrect) {
  165. $this->assertRedirect($this->stringContains('backend/admin/user/index'));
  166. $this->assertEquals($postData['username'], $user->getUsername());
  167. $this->assertEquals($postData['email'], $user->getEmail());
  168. $this->assertEquals($postData['firstname'], $user->getFirstname());
  169. $this->assertEquals($postData['lastname'], $user->getLastname());
  170. $encryptor = $objectManager->get(\Magento\Framework\Encryption\EncryptorInterface::class);
  171. $this->assertTrue($encryptor->validateHash($postData['password'], $user->getPassword()));
  172. } else {
  173. $this->assertRedirect($this->stringContains('backend/admin/user/edit'));
  174. $this->assertEmpty($user->getData());
  175. }
  176. }
  177. /**
  178. * Dataprovider for testSaveActionPasswordChange
  179. *
  180. * @return array
  181. */
  182. public function saveActionPasswordChangeDataProvider()
  183. {
  184. $password = uniqid('123q');
  185. $passwordPairs = [
  186. ['password' => $password, 'password_confirmation' => $password, 'is_correct' => true],
  187. ['password' => $password, 'password_confirmation' => '', 'is_correct' => false],
  188. ['password' => $password, 'password_confirmation' => $password . '123', 'is_correct' => false],
  189. ['password' => '', 'password_confirmation' => '', 'is_correct' => false],
  190. ['password' => '', 'password_confirmation' => $password, 'is_correct' => false],
  191. ];
  192. $data = [];
  193. foreach ($passwordPairs as $passwordPair) {
  194. $fixture = uniqid();
  195. $postData = [
  196. 'username' => $fixture,
  197. 'email' => "{$fixture}@example.com",
  198. 'firstname' => 'First',
  199. 'lastname' => 'Last',
  200. 'password' => $passwordPair['password'],
  201. 'password_confirmation' => $passwordPair['password_confirmation'],
  202. \Magento\User\Block\User\Edit\Tab\Main::CURRENT_USER_PASSWORD_FIELD => Bootstrap::ADMIN_PASSWORD,
  203. ];
  204. $data[] = [$postData, $passwordPair['is_correct']];
  205. }
  206. return $data;
  207. }
  208. /**
  209. * Verify that the role grid is present when requested
  210. */
  211. public function testRoleGridAction()
  212. {
  213. $this->getRequest()->setParam('ajax', true)->setParam('isAjax', true);
  214. $this->dispatch('backend/admin/user/roleGrid');
  215. $expected = '%a<table %a id="permissionsUserGrid_table">%a';
  216. $this->assertStringMatchesFormat($expected, $this->getResponse()->getBody());
  217. }
  218. /**
  219. * Verify that the roles grid is present when requested
  220. *
  221. * @depends testSaveAction
  222. */
  223. public function testRolesGridAction()
  224. {
  225. $this->getRequest()->setParam('ajax', true)->setParam('isAjax', true)->setParam('user_id', 1);
  226. $this->dispatch('backend/admin/user/rolesGrid');
  227. $expected = '%a<table %a id="permissionsUserRolesGrid_table">%a';
  228. $this->assertStringMatchesFormat($expected, $this->getResponse()->getBody());
  229. }
  230. /**
  231. * Verify that expected header and fieldsets are present for edit
  232. *
  233. * @depends testSaveAction
  234. */
  235. public function testEditAction()
  236. {
  237. $this->getRequest()->setParam('user_id', 1);
  238. $this->dispatch('backend/admin/user/edit');
  239. $response = $this->getResponse()->getBody();
  240. //check "User Information" header and fieldset
  241. $this->assertContains('data-ui-id="adminhtml-user-edit-tabs-title"', $response);
  242. $this->assertContains('User Information', $response);
  243. $this->assertEquals(
  244. 1,
  245. \Magento\TestFramework\Helper\Xpath::getElementsCountForXpath(
  246. '//*[@id="user_base_fieldset"]',
  247. $response
  248. )
  249. );
  250. }
  251. /**
  252. * Verify that validation passes on correct data
  253. */
  254. public function testValidateActionSuccess()
  255. {
  256. $data = [
  257. 'username' => 'admin2',
  258. 'firstname' => 'new firstname',
  259. 'lastname' => 'new lastname',
  260. 'email' => 'example@domain.com',
  261. 'password' => 'password123',
  262. 'password_confirmation' => 'password123',
  263. ];
  264. $this->getRequest()->setPostValue($data);
  265. $this->dispatch('backend/admin/user/validate');
  266. $body = $this->getResponse()->getBody();
  267. $this->assertEquals('{"error":0}', $body);
  268. }
  269. /**
  270. * Verify that an unknown top level domain on an email address does not fail validation
  271. */
  272. public function testValidateActionUnknownTldSuccess()
  273. {
  274. $data = [
  275. 'username' => 'admin2',
  276. 'firstname' => 'new firstname',
  277. 'lastname' => 'new lastname',
  278. 'email' => 'example@domain.unknown',
  279. 'password' => 'password123',
  280. 'password_confirmation' => 'password123',
  281. ];
  282. $this->getRequest()->setPostValue($data);
  283. $this->dispatch('backend/admin/user/validate');
  284. $body = $this->getResponse()->getBody();
  285. $this->assertEquals('{"error":0}', $body);
  286. }
  287. /**
  288. * Verify that an invalid email address format fails the validation
  289. */
  290. public function testValidateActionError()
  291. {
  292. $data = [
  293. 'username' => 'admin2',
  294. 'firstname' => 'new firstname',
  295. 'lastname' => 'new lastname',
  296. 'email' => 'example@-domain.cim',
  297. 'password' => 'password123',
  298. 'password_confirmation' => 'password123',
  299. ];
  300. /**
  301. * set customer data
  302. */
  303. $this->getRequest()->setPostValue($data);
  304. $this->dispatch('backend/admin/user/validate');
  305. $body = $this->getResponse()->getBody();
  306. $this->assertContains('{"error":1,"html_message":', $body);
  307. $this->assertContains("'-domain.cim' is not a valid hostname for email address 'example@-domain.cim", $body);
  308. }
  309. }