3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
7
+
6
8
namespace Magento \User \Model \ResourceModel ;
7
9
10
+ use Magento \Authorization \Model \ResourceModel \Role \Collection as UserRoleCollection ;
11
+ use Magento \Authorization \Model \ResourceModel \Role \CollectionFactory as UserRoleCollectionFactory ;
12
+ use Magento \Authorization \Model \UserContextInterface ;
8
13
use Magento \TestFramework \Helper \Bootstrap ;
9
- use Magento \User \Model \User ;
10
14
use Magento \User \Model \ResourceModel \User as UserResourceModel ;
15
+ use Magento \User \Model \User ;
16
+ use Magento \User \Model \UserFactory ;
11
17
12
18
/**
13
19
* @magentoAppArea adminhtml
14
20
*/
15
21
class UserTest extends \PHPUnit \Framework \TestCase
16
22
{
17
- /** @var UserResourceModel */
23
+ /**
24
+ * @var UserResourceModel
25
+ */
18
26
private $ model ;
19
27
28
+ /**
29
+ * @var UserRoleCollectionFactory
30
+ */
31
+ private $ userRoleCollectionFactory ;
32
+
33
+ /**
34
+ * @var UserFactory
35
+ */
36
+ private $ userFactory ;
37
+
38
+ /**
39
+ * @inheritdoc
40
+ */
20
41
protected function setUp ()
21
42
{
22
- $ this ->model = Bootstrap::getObjectManager ()->get (
23
- UserResourceModel:: class
24
- );
43
+ $ this ->model = Bootstrap::getObjectManager ()->get (UserResourceModel::class);
44
+ $ this -> userRoleCollectionFactory = Bootstrap:: getObjectManager ()-> get (UserRoleCollectionFactory:: class);
45
+ $ this -> userFactory = Bootstrap:: getObjectManager ()-> get (UserFactory::class );
25
46
}
26
47
27
48
/**
28
49
* Tests if latest password is stored after user creating
29
50
* when password lifetime config value is zero (disabled as fact)
30
51
*
52
+ * @return void
31
53
* @magentoConfigFixture current_store admin/security/password_lifetime 0
32
54
* @magentoDataFixture Magento/User/_files/dummy_user.php
33
55
*/
34
- public function testGetLatestPasswordWhenZeroPasswordLifetime ()
56
+ public function testGetLatestPasswordWhenZeroPasswordLifetime (): void
35
57
{
36
58
/** @var User $user */
37
- $ user = Bootstrap::getObjectManager ()->create (
38
- User::class
39
- );
59
+ $ user = $ this ->userFactory ->create ();
40
60
$ user ->loadByUsername ('dummy_username ' );
41
61
$ latestPassword = $ this ->model ->getLatestPassword ($ user ->getId ());
42
62
@@ -46,12 +66,49 @@ public function testGetLatestPasswordWhenZeroPasswordLifetime()
46
66
);
47
67
}
48
68
49
- public function testCountAll ()
69
+ /**
70
+ * Test that user role is not deleted after deleting empty user.
71
+ *
72
+ * @return void
73
+ */
74
+ public function testDelete (): void
75
+ {
76
+ $ this ->checkRoleCollectionSize ();
77
+ /** @var User $user */
78
+ $ user = $ this ->userFactory ->create ();
79
+ $ this ->model ->delete ($ user );
80
+ $ this ->checkRoleCollectionSize ();
81
+ }
82
+
83
+ /**
84
+ * Ensure that role collection size is correct.
85
+ *
86
+ * @return void
87
+ */
88
+ private function checkRoleCollectionSize (): void
89
+ {
90
+ /** @var UserRoleCollection $roleCollection */
91
+ $ roleCollection = $ this ->userRoleCollectionFactory ->create ();
92
+ $ roleCollection ->setUserFilter (0 , UserContextInterface::USER_TYPE_ADMIN );
93
+ $ this ->assertEquals (1 , $ roleCollection ->getSize ());
94
+ }
95
+
96
+ /**
97
+ * Check total user count.
98
+ *
99
+ * @return void
100
+ */
101
+ public function testCountAll (): void
50
102
{
51
103
$ this ->assertSame (1 , $ this ->model ->countAll ());
52
104
}
53
105
54
- public function testGetValidationRulesBeforeSave ()
106
+ /**
107
+ * Check validation rules has correct type.
108
+ *
109
+ * @return void
110
+ */
111
+ public function testGetValidationRulesBeforeSave (): void
55
112
{
56
113
$ rules = $ this ->model ->getValidationRulesBeforeSave ();
57
114
$ this ->assertInstanceOf ('Zend_Validate_Interface ' , $ rules );
0 commit comments