Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes
UserTest Class Reference
Inheritance diagram for UserTest:

Public Member Functions

 testCRUD ()
 
 testCreatedOnUpdate ()
 
 testLoadByUsername ()
 
 testUpdateRoleOnSave ()
 
 testSaveExtra ()
 
 testGetRoles ()
 
 testGetRole ()
 
 testDeleteFromRole ()
 
 testRoleUserExists ()
 
 testGetCollection ()
 
 testGetName ()
 
 testGetUninitializedAclRole ()
 
 testAuthenticate ()
 
 testAuthenticateCaseInsensitive ()
 
 testAuthenticateInactiveUser ()
 
 testAuthenticateUserWithoutRole ()
 
 testLoginsAreLogged ()
 
 testReload ()
 
 testHasAssigned2Role ()
 
 testBeforeSaveRequiredFieldsValidation ()
 
 testBeforeSavePasswordsDoNotMatch ()
 
 testBeforeSavePasswordTooShort ()
 
 testBeforeSavePasswordInsecure ($password)
 
 beforeSavePasswordInsecureDataProvider ()
 
 testBeforeSaveUserIdentityViolation ()
 
 testBeforeSaveValidationSuccess ()
 
 testChangeResetPasswordLinkToken ()
 
 testIsResetPasswordLinkTokenExpired ()
 
 testGetSetHasAvailableResources ()
 
 testPerformIdentityCheck ()
 
 testPerformIdentityCheckWrongPassword ()
 
 testPerformIdentityCheckLockExpires ()
 

Static Public Member Functions

static roleDataFixture ()
 

Protected Member Functions

 setUp ()
 

Protected Attributes

 $_model
 
 $_dateTime
 

Static Protected Attributes

static $_newRole
 

Detailed Description

@magentoAppArea adminhtml

Definition at line 14 of file UserTest.php.

Member Function Documentation

◆ beforeSavePasswordInsecureDataProvider()

beforeSavePasswordInsecureDataProvider ( )

Definition at line 403 of file UserTest.php.

404  {
405  return ['alpha chars only' => ['aaaaaaaa'], 'digits only' => ['1234567']];
406  }

◆ roleDataFixture()

static roleDataFixture ( )
static

Definition at line 107 of file UserTest.php.

108  {
110  \Magento\Authorization\Model\Role::class
111  );
112  self::$_newRole->setName('admin_role')->setRoleType('G')->setPid('1');
113  self::$_newRole->save();
114  }

◆ setUp()

setUp ( )
protected

Definition at line 36 of file UserTest.php.

37  {
39  \Magento\User\Model\User::class
40  );
42  \Magento\Framework\Stdlib\DateTime::class
43  );
45  Json::class
46  );
47  }

◆ testAuthenticate()

testAuthenticate ( )

@magentoAppIsolation enabled @magentoAdminConfigFixture admin/captcha/enable 0 @magentoAdminConfigFixture admin/security/use_case_sensitive_login 1

Definition at line 208 of file UserTest.php.

209  {
210  $this->assertFalse($this->_model->authenticate('User', \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD));
211  $this->assertTrue(
212  $this->_model->authenticate(
213  \Magento\TestFramework\Bootstrap::ADMIN_NAME,
214  \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD
215  )
216  );
217  }

◆ testAuthenticateCaseInsensitive()

testAuthenticateCaseInsensitive ( )

@magentoAppIsolation enabled @magentoAdminConfigFixture admin/captcha/enable 0 @magentoConfigFixture current_store admin/security/use_case_sensitive_login 0

Definition at line 224 of file UserTest.php.

225  {
226  $this->assertTrue($this->_model->authenticate('user', \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD));
227  $this->assertTrue(
228  $this->_model->authenticate(
229  \Magento\TestFramework\Bootstrap::ADMIN_NAME,
230  \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD
231  )
232  );
233  }

◆ testAuthenticateInactiveUser()

testAuthenticateInactiveUser ( )

@expectedException \Magento\Framework\Exception\LocalizedException @expectedException \Magento\Framework\Exception\AuthenticationException @magentoDbIsolation enabled

Definition at line 240 of file UserTest.php.

241  {
242  $this->_model->load(1);
243  $this->_model->setIsActive(0)->save();
244  $this->_model->authenticate(
245  \Magento\TestFramework\Bootstrap::ADMIN_NAME,
246  \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD
247  );
248  }

◆ testAuthenticateUserWithoutRole()

testAuthenticateUserWithoutRole ( )

@expectedException \Magento\Framework\Exception\AuthenticationException @magentoDbIsolation enabled

Definition at line 254 of file UserTest.php.

255  {
256  $this->_model->loadByUsername(\Magento\TestFramework\Bootstrap::ADMIN_NAME);
257  $roles = $this->_model->getRoles();
258  $this->_model->setRoleId(reset($roles))->deleteFromRole();
259  $this->_model->authenticate(
260  \Magento\TestFramework\Bootstrap::ADMIN_NAME,
261  \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD
262  );
263  }

◆ testBeforeSavePasswordInsecure()

testBeforeSavePasswordInsecure (   $password)

@dataProvider beforeSavePasswordInsecureDataProvider @expectedException \Magento\Framework\Exception\LocalizedException @expectedExceptionMessage Your password must include both numeric and alphabetic characters. @magentoDbIsolation enabled

Parameters
string$password

Definition at line 397 of file UserTest.php.

398  {
399  $this->_model->setPassword($password);
400  $this->_model->save();
401  }

◆ testBeforeSavePasswordsDoNotMatch()

testBeforeSavePasswordsDoNotMatch ( )

@expectedException \Magento\Framework\Exception\LocalizedException @expectedExceptionMessage Your password confirmation must match your password. @magentoDbIsolation enabled

Definition at line 372 of file UserTest.php.

373  {
374  $this->_model->setPassword('password2');
375  $this->_model->setPasswordConfirmation('password1');
376  $this->_model->save();
377  }

◆ testBeforeSavePasswordTooShort()

testBeforeSavePasswordTooShort ( )

@expectedException \Magento\Framework\Exception\LocalizedException @expectedExceptionMessage Your password must include both numeric and alphabetic characters. @magentoDbIsolation enabled

Definition at line 384 of file UserTest.php.

385  {
386  $this->_model->setPassword('123456');
387  $this->_model->save();
388  }

◆ testBeforeSaveRequiredFieldsValidation()

testBeforeSaveRequiredFieldsValidation ( )

@expectedException \Magento\Framework\Exception\LocalizedException @expectedExceptionMessage "User Name" is required. Enter and try again. @expectedExceptionMessage "First Name" is required. Enter and try again. @expectedExceptionMessage "Last Name" is required. Enter and try again. @expectedExceptionMessage Please enter a valid email. @expectedExceptionMessage "Password" is required. Enter and try again. @magentoDbIsolation enabled

Definition at line 326 of file UserTest.php.

327  {
328  $this->_model->setSomething('some_value');
329  // force model change
330  $this->_model->save();
331  }

◆ testBeforeSaveUserIdentityViolation()

testBeforeSaveUserIdentityViolation ( )

@expectedException \Magento\Framework\Exception\LocalizedException @expectedExceptionMessage A user with the same user name or email already exists. @magentoDbIsolation enabled

Definition at line 413 of file UserTest.php.

414  {
415  $this->_model->setUsername('user');
416  $this->_model->save();
417  }

◆ testBeforeSaveValidationSuccess()

testBeforeSaveValidationSuccess ( )

@magentoDbIsolation enabled

Definition at line 422 of file UserTest.php.

423  {
424  $this->_model->setUsername(
425  'user1'
426  )->setFirstname(
427  'John'
428  )->setLastname(
429  'Doe'
430  )->setEmail(
432  )->setPassword(
433  '1234abc'
434  )->setPasswordConfirmation(
435  '1234abc'
436  );
437  $this->_model->save();
438  }

◆ testChangeResetPasswordLinkToken()

testChangeResetPasswordLinkToken ( )

@magentoDbIsolation enabled

Definition at line 443 of file UserTest.php.

444  {
445  $this->_model->loadByUsername(\Magento\TestFramework\Bootstrap::ADMIN_NAME);
446  $this->_model->changeResetPasswordLinkToken('test');
447  $date = $this->_model->getRpTokenCreatedAt();
448  $this->assertNotNull($date);
449  $this->_model->save();
450  $this->_model->loadByUsername(\Magento\TestFramework\Bootstrap::ADMIN_NAME);
451  $this->assertEquals('test', $this->_model->getRpToken());
452  $this->assertEquals(strtotime($date), strtotime($this->_model->getRpTokenCreatedAt()));
453  }

◆ testCreatedOnUpdate()

testCreatedOnUpdate ( )

@magentoDataFixture Magento/User/_files/dummy_user.php

Definition at line 73 of file UserTest.php.

74  {
75  $this->_model->loadByUsername('user_created_date');
76  $this->assertEquals('2010-01-06 00:00:00', $this->_model->getCreated());
77  //reload to update lognum record
78  $this->_model->getResource()->recordLogin($this->_model);
79  $this->_model->reload();
80  $this->assertEquals('2010-01-06 00:00:00', $this->_model->getCreated());
81  }

◆ testCRUD()

testCRUD ( )

@magentoDbIsolation enabled

Definition at line 52 of file UserTest.php.

53  {
54  $this->_model->setFirstname(
55  "John"
56  )->setLastname(
57  "Doe"
58  )->setUsername(
59  'user2'
60  )->setPassword(
61  \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD
62  )->setEmail(
64  );
65 
66  $crud = new \Magento\TestFramework\Entity($this->_model, ['firstname' => '_New_name_']);
67  $crud->testCrud();
68  }

◆ testDeleteFromRole()

testDeleteFromRole ( )

@magentoDbIsolation enabled

Definition at line 160 of file UserTest.php.

161  {
162  $this->_model->loadByUsername(\Magento\TestFramework\Bootstrap::ADMIN_NAME);
163  $roles = $this->_model->getRoles();
164  $this->_model->setRoleId(reset($roles))->deleteFromRole();
165  $role = $this->_model->getRole();
166  $this->assertNull($role->getId());
167  }

◆ testGetCollection()

testGetCollection ( )

Definition at line 179 of file UserTest.php.

180  {
181  $this->assertInstanceOf(
182  \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection::class,
183  $this->_model->getCollection()
184  );
185  }

◆ testGetName()

testGetName ( )

Definition at line 187 of file UserTest.php.

188  {
191  $this->_model->loadByUsername(\Magento\TestFramework\Bootstrap::ADMIN_NAME);
192  $this->assertEquals("$firstname $lastname", $this->_model->getName());
193  $this->assertEquals("$firstname///$lastname", $this->_model->getName('
194  }

◆ testGetRole()

testGetRole ( )

@magentoDataFixture roleDataFixture

Definition at line 146 of file UserTest.php.

147  {
148  $this->_model->loadByUsername(\Magento\TestFramework\Bootstrap::ADMIN_NAME);
149  $role = $this->_model->getRole();
150  $this->assertInstanceOf(\Magento\Authorization\Model\Role::class, $role);
151  $this->assertEquals(\Magento\TestFramework\Bootstrap::ADMIN_ROLE_NAME, $this->_model->getRole()->getRoleName());
152  $this->_model->setRoleId(self::$_newRole->getId())->save();
153  $role = $this->_model->getRole();
154  $this->assertEquals(self::$_newRole->getId(), $role->getId());
155  }

◆ testGetRoles()

testGetRoles ( )

@magentoDataFixture roleDataFixture

Definition at line 131 of file UserTest.php.

132  {
133  $this->_model->loadByUsername(\Magento\TestFramework\Bootstrap::ADMIN_NAME);
134  $roles = $this->_model->getRoles();
135  $this->assertEquals(1, count($roles));
136  $this->assertEquals(\Magento\TestFramework\Bootstrap::ADMIN_ROLE_NAME, $this->_model->getRole()->getRoleName());
137  $this->_model->setRoleId(self::$_newRole->getId())->save();
138  $roles = $this->_model->getRoles();
139  $this->assertEquals(1, count($roles));
140  $this->assertEquals(self::$_newRole->getId(), $roles[0]);
141  }

◆ testGetSetHasAvailableResources()

testGetSetHasAvailableResources ( )

Definition at line 475 of file UserTest.php.

476  {
477  $this->_model->setHasAvailableResources(true);
478  $this->assertTrue($this->_model->hasAvailableResources());
479 
480  $this->_model->setHasAvailableResources(false);
481  $this->assertFalse($this->_model->hasAvailableResources());
482  }

◆ testGetUninitializedAclRole()

testGetUninitializedAclRole ( )

Definition at line 196 of file UserTest.php.

197  {
198  $newuser = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\User\Model\User::class);
199  $newuser->setUserId(10);
200  $this->assertNull($newuser->getAclRole(), "User role was not initialized and is expected to be empty.");
201  }

◆ testHasAssigned2Role()

testHasAssigned2Role ( )

@magentoDbIsolation enabled

Definition at line 306 of file UserTest.php.

307  {
308  $this->_model->loadByUsername(\Magento\TestFramework\Bootstrap::ADMIN_NAME);
309  $role = $this->_model->hasAssigned2Role($this->_model);
310  $this->assertEquals(1, count($role));
311  $this->assertArrayHasKey('role_id', $role[0]);
312  $roles = $this->_model->getRoles();
313  $this->_model->setRoleId(reset($roles))->deleteFromRole();
314  $this->assertEmpty($this->_model->hasAssigned2Role($this->_model));
315  }

◆ testIsResetPasswordLinkTokenExpired()

testIsResetPasswordLinkTokenExpired ( )

@magentoDbIsolation enabled @magentoAppIsolation enabled @magentoConfigFixture default/admin/emails/password_reset_link_expiration_period 2

Definition at line 460 of file UserTest.php.

461  {
462  $this->_model->loadByUsername(\Magento\TestFramework\Bootstrap::ADMIN_NAME);
463  $this->assertTrue($this->_model->isResetPasswordLinkTokenExpired());
464  $this->_model->changeResetPasswordLinkToken('test');
465  $this->_model->save();
466  $this->_model->loadByUsername(\Magento\TestFramework\Bootstrap::ADMIN_NAME);
467  $this->assertFalse($this->_model->isResetPasswordLinkTokenExpired());
468  $this->_model->setRpTokenCreatedAt($this->_dateTime->formatDate(time() - 60 * 60 * 2 + 2));
469  $this->assertFalse($this->_model->isResetPasswordLinkTokenExpired());
470 
471  $this->_model->setRpTokenCreatedAt($this->_dateTime->formatDate(time() - 60 * 60 * 2 - 1));
472  $this->assertTrue($this->_model->isResetPasswordLinkTokenExpired());
473  }

◆ testLoadByUsername()

testLoadByUsername ( )

Ensure that an exception is not thrown, if the user does not exist

Definition at line 86 of file UserTest.php.

87  {
88  $this->_model->loadByUsername('non_existing_user');
89  $this->assertNull($this->_model->getId(), 'The admin user has an unexpected ID');
90  $this->_model->loadByUsername(\Magento\TestFramework\Bootstrap::ADMIN_NAME);
91  $this->assertNotEmpty($this->_model->getId(), 'The admin user should have been loaded');
92  }

◆ testLoginsAreLogged()

testLoginsAreLogged ( )

@magentoDbIsolation enabled @magentoAdminConfigFixture admin/captcha/enable 0

Definition at line 269 of file UserTest.php.

270  {
271  $this->_model->loadByUsername(\Magento\TestFramework\Bootstrap::ADMIN_NAME);
272  $lognum = $this->_model->getLognum();
273 
274  $beforeLogin = time();
275  $this->_model->login(
276  \Magento\TestFramework\Bootstrap::ADMIN_NAME,
277  \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD
278  )->reload();
279  $loginTime = strtotime($this->_model->getLogdate());
280 
281  $this->assertTrue($beforeLogin <= $loginTime && $loginTime <= time());
282  $this->assertEquals(++$lognum, $this->_model->getLognum());
283 
284  $beforeLogin = time();
285  $this->_model->login(
286  \Magento\TestFramework\Bootstrap::ADMIN_NAME,
287  \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD
288  )->reload();
289  $loginTime = strtotime($this->_model->getLogdate());
290  $this->assertTrue($beforeLogin <= $loginTime && $loginTime <= time());
291  $this->assertEquals(++$lognum, $this->_model->getLognum());
292  }

◆ testPerformIdentityCheck()

testPerformIdentityCheck ( )

Here we test if admin identity check executed successfully

@magentoDataFixture Magento/User/_files/user_with_role.php

Definition at line 489 of file UserTest.php.

490  {
491  $this->_model->loadByUsername('adminUser');
493  $this->_model->performIdentityCheck($passwordString);
494  }

◆ testPerformIdentityCheckLockExpires()

testPerformIdentityCheckLockExpires ( )

Here we check for a locked user

@magentoDataFixture Magento/User/_files/locked_users.php @expectedException \Magento\Framework\Exception\State\UserLockedException

Definition at line 519 of file UserTest.php.

520  {
521  $this->_model->loadByUsername('adminUser2');
522  $this->_model->performIdentityCheck(\Magento\TestFramework\Bootstrap::ADMIN_PASSWORD);
523 
524  $this->expectExceptionMessage(
525  'The account sign-in was incorrect or your account is disabled temporarily. '
526  . 'Please wait and try again later.'
527  );
528  }

◆ testPerformIdentityCheckWrongPassword()

testPerformIdentityCheckWrongPassword ( )

Here we check for a wrong password

@magentoDataFixture Magento/User/_files/user_with_role.php @expectedException \Magento\Framework\Exception\AuthenticationException

Definition at line 502 of file UserTest.php.

503  {
504  $this->_model->loadByUsername('adminUser');
505  $passwordString = 'wrongPassword';
506  $this->_model->performIdentityCheck($passwordString);
507 
508  $this->expectExceptionMessage(
509  'The password entered for the current user is invalid. Verify the password and try again.'
510  );
511  }

◆ testReload()

testReload ( )

Definition at line 294 of file UserTest.php.

295  {
296  $this->_model->loadByUsername(\Magento\TestFramework\Bootstrap::ADMIN_NAME);
297  $this->_model->setFirstname('NewFirstName');
298  $this->assertEquals('NewFirstName', $this->_model->getFirstname());
299  $this->_model->reload();
300  $this->assertEquals(\Magento\TestFramework\Bootstrap::ADMIN_FIRSTNAME, $this->_model->getFirstname());
301  }

◆ testRoleUserExists()

testRoleUserExists ( )

Definition at line 169 of file UserTest.php.

170  {
171  $this->_model->loadByUsername(\Magento\TestFramework\Bootstrap::ADMIN_NAME);
172  $role = $this->_model->getRole();
173  $this->_model->setRoleId($role->getId());
174  $this->assertTrue($this->_model->roleUserExists());
175  $this->_model->setRoleId(100);
176  $this->assertFalse($this->_model->roleUserExists());
177  }

◆ testSaveExtra()

testSaveExtra ( )

@magentoDbIsolation enabled

Definition at line 119 of file UserTest.php.

120  {
121  $this->_model->loadByUsername(\Magento\TestFramework\Bootstrap::ADMIN_NAME);
122  $this->_model->saveExtra(['test' => 'val']);
123  $this->_model->loadByUsername(\Magento\TestFramework\Bootstrap::ADMIN_NAME);
124  $extra = $this->serializer->unserialize($this->_model->getExtra());
125  $this->assertEquals($extra['test'], 'val');
126  }
if( $_orders &&count( $_orders))( 'Orders') ?></caption >< thead >< tr >< th scopeforeach( $_orders as $_order)(__( 'Order #')) ?>" class $extra
Definition: history.phtml:32

◆ testUpdateRoleOnSave()

testUpdateRoleOnSave ( )

Test that user role is updated after save

@magentoDataFixture roleDataFixture

Definition at line 99 of file UserTest.php.

100  {
101  $this->_model->loadByUsername(\Magento\TestFramework\Bootstrap::ADMIN_NAME);
102  $this->assertEquals(\Magento\TestFramework\Bootstrap::ADMIN_ROLE_NAME, $this->_model->getRole()->getRoleName());
103  $this->_model->setRoleId(self::$_newRole->getId())->save();
104  $this->assertEquals('admin_role', $this->_model->getRole()->getRoleName());
105  }

Field Documentation

◆ $_dateTime

$_dateTime
protected

Definition at line 24 of file UserTest.php.

◆ $_model

$_model
protected

Definition at line 19 of file UserTest.php.

◆ $_newRole

$_newRole
staticprotected

Definition at line 29 of file UserTest.php.


The documentation for this class was generated from the following file: