8
8
namespace Magento \GraphQl \Customer ;
9
9
10
10
use Magento \Customer \Api \AccountManagementInterface ;
11
+ use Magento \Customer \Api \CustomerRepositoryInterface ;
12
+ use Magento \Customer \Model \CustomerAuthUpdate ;
11
13
use Magento \Customer \Model \CustomerRegistry ;
14
+ use Magento \Framework \Exception \AuthenticationException ;
12
15
use Magento \Framework \Exception \LocalizedException ;
16
+ use Magento \Framework \Exception \NoSuchEntityException ;
13
17
use Magento \Integration \Api \CustomerTokenServiceInterface ;
14
18
use Magento \TestFramework \Helper \Bootstrap ;
15
19
use Magento \TestFramework \TestCase \GraphQlAbstract ;
@@ -34,11 +38,23 @@ class ChangeCustomerPasswordTest extends GraphQlAbstract
34
38
*/
35
39
private $ customerRegistry ;
36
40
41
+ /**
42
+ * @var CustomerAuthUpdate
43
+ */
44
+ private $ customerAuthUpdate ;
45
+
46
+ /**
47
+ * @var CustomerRepositoryInterface
48
+ */
49
+ private $ customerRepository ;
50
+
37
51
protected function setUp ()
38
52
{
39
53
$ this ->customerTokenService = Bootstrap::getObjectManager ()->get (CustomerTokenServiceInterface::class);
40
54
$ this ->accountManagement = Bootstrap::getObjectManager ()->get (AccountManagementInterface::class);
41
55
$ this ->customerRegistry = Bootstrap::getObjectManager ()->get (CustomerRegistry::class);
56
+ $ this ->customerAuthUpdate = Bootstrap::getObjectManager ()->get (CustomerAuthUpdate::class);
57
+ $ this ->customerRepository = Bootstrap::getObjectManager ()->get (CustomerRepositoryInterface::class);
42
58
}
43
59
44
60
/**
@@ -47,19 +63,19 @@ protected function setUp()
47
63
public function testChangePassword ()
48
64
{
49
65
$ customerEmail = 'customer@example.com ' ;
50
- $ oldCustomerPassword = 'password ' ;
51
- $ newCustomerPassword = 'anotherPassword1 ' ;
66
+ $ currentPassword = 'password ' ;
67
+ $ newPassword = 'anotherPassword1 ' ;
52
68
53
- $ query = $ this ->getChangePassQuery ( $ oldCustomerPassword , $ newCustomerPassword );
54
- $ headerMap = $ this ->getCustomerAuthHeaders ($ customerEmail , $ oldCustomerPassword );
69
+ $ query = $ this ->getQuery ( $ currentPassword , $ newPassword );
70
+ $ headerMap = $ this ->getCustomerAuthHeaders ($ customerEmail , $ currentPassword );
55
71
56
72
$ response = $ this ->graphQlMutation ($ query , [], '' , $ headerMap );
57
73
$ this ->assertEquals ($ customerEmail , $ response ['changeCustomerPassword ' ]['email ' ]);
58
74
59
75
try {
60
76
// registry contains the old password hash so needs to be reset
61
77
$ this ->customerRegistry ->removeByEmail ($ customerEmail );
62
- $ this ->accountManagement ->authenticate ($ customerEmail , $ newCustomerPassword );
78
+ $ this ->accountManagement ->authenticate ($ customerEmail , $ newPassword );
63
79
} catch (LocalizedException $ e ) {
64
80
$ this ->fail ('Password was not changed: ' . $ e ->getMessage ());
65
81
}
@@ -71,7 +87,7 @@ public function testChangePassword()
71
87
*/
72
88
public function testChangePasswordIfUserIsNotAuthorizedTest ()
73
89
{
74
- $ query = $ this ->getChangePassQuery ('currentpassword ' , 'newpassword ' );
90
+ $ query = $ this ->getQuery ('currentpassword ' , 'newpassword ' );
75
91
$ this ->graphQlMutation ($ query );
76
92
}
77
93
@@ -81,11 +97,11 @@ public function testChangePasswordIfUserIsNotAuthorizedTest()
81
97
public function testChangeWeakPassword ()
82
98
{
83
99
$ customerEmail = 'customer@example.com ' ;
84
- $ oldCustomerPassword = 'password ' ;
85
- $ newCustomerPassword = 'weakpass ' ;
100
+ $ currentPassword = 'password ' ;
101
+ $ newPassword = 'weakpass ' ;
86
102
87
- $ query = $ this ->getChangePassQuery ( $ oldCustomerPassword , $ newCustomerPassword );
88
- $ headerMap = $ this ->getCustomerAuthHeaders ($ customerEmail , $ oldCustomerPassword );
103
+ $ query = $ this ->getQuery ( $ currentPassword , $ newPassword );
104
+ $ headerMap = $ this ->getCustomerAuthHeaders ($ customerEmail , $ currentPassword );
89
105
90
106
$ this ->expectException (\Exception::class);
91
107
$ this ->expectExceptionMessageRegExp ('/Minimum of different classes of characters in password is.*/ ' );
@@ -101,17 +117,123 @@ public function testChangeWeakPassword()
101
117
public function testChangePasswordIfPasswordIsInvalid ()
102
118
{
103
119
$ customerEmail = 'customer@example.com ' ;
104
- $ oldCustomerPassword = 'password ' ;
105
- $ newCustomerPassword = 'anotherPassword1 ' ;
106
- $ incorrectPassword = 'password-incorrect ' ;
120
+ $ currentPassword = 'password ' ;
121
+ $ newPassword = 'anotherPassword1 ' ;
122
+ $ incorrectCurrentPassword = 'password-incorrect ' ;
123
+
124
+ $ query = $ this ->getQuery ($ incorrectCurrentPassword , $ newPassword );
125
+
126
+ $ headerMap = $ this ->getCustomerAuthHeaders ($ customerEmail , $ currentPassword );
127
+ $ this ->graphQlMutation ($ query , [], '' , $ headerMap );
128
+ }
129
+
130
+ /**
131
+ * @magentoApiDataFixture Magento/Customer/_files/customer.php
132
+ * @expectedException \Exception
133
+ * @expectedExceptionMessage Specify the "currentPassword" value.
134
+ */
135
+ public function testChangePasswordIfCurrentPasswordIsEmpty ()
136
+ {
137
+ $ customerEmail = 'customer@example.com ' ;
138
+ $ currentPassword = 'password ' ;
139
+ $ newPassword = 'anotherPassword1 ' ;
140
+ $ incorrectCurrentPassword = '' ;
141
+
142
+ $ query = $ this ->getQuery ($ incorrectCurrentPassword , $ newPassword );
143
+
144
+ $ headerMap = $ this ->getCustomerAuthHeaders ($ customerEmail , $ currentPassword );
145
+ $ this ->graphQlMutation ($ query , [], '' , $ headerMap );
146
+ }
147
+
148
+ /**
149
+ * @magentoApiDataFixture Magento/Customer/_files/customer.php
150
+ * @expectedException \Exception
151
+ * @expectedExceptionMessage Specify the "newPassword" value.
152
+ */
153
+ public function testChangePasswordIfNewPasswordIsEmpty ()
154
+ {
155
+ $ customerEmail = 'customer@example.com ' ;
156
+ $ currentPassword = 'password ' ;
157
+ $ incorrectNewPassword = '' ;
107
158
108
- $ query = $ this ->getChangePassQuery ( $ incorrectPassword , $ newCustomerPassword );
159
+ $ query = $ this ->getQuery ( $ currentPassword , $ incorrectNewPassword );
109
160
110
- $ headerMap = $ this ->getCustomerAuthHeaders ($ customerEmail , $ oldCustomerPassword );
161
+ $ headerMap = $ this ->getCustomerAuthHeaders ($ customerEmail , $ currentPassword );
111
162
$ this ->graphQlMutation ($ query , [], '' , $ headerMap );
112
163
}
113
164
114
- private function getChangePassQuery ($ currentPassword , $ newPassword )
165
+ /**
166
+ * @magentoApiDataFixture Magento/GraphQl/Customer/_files/enable_customer_account_confirmation.php
167
+ * @magentoApiDataFixture Magento/Customer/_files/customer.php
168
+ * @expectedException \Exception
169
+ * @expectedExceptionMessage This account isn't confirmed. Verify and try again.
170
+ */
171
+ public function testChangePasswordIfAccountIsNotConfirmed ()
172
+ {
173
+ $ customerEmail = 'customer@example.com ' ;
174
+ $ currentPassword = 'password ' ;
175
+ $ newPassword = 'anotherPassword1 ' ;
176
+
177
+ /* get header map before setting the customer unconfirmed */
178
+ $ headerMap = $ this ->getCustomerAuthHeaders ($ customerEmail , $ currentPassword );
179
+
180
+ $ this ->setCustomerConfirmation (1 );
181
+ $ query = $ this ->getQuery ($ currentPassword , $ newPassword );
182
+
183
+ $ this ->graphQlMutation ($ query , [], '' , $ headerMap );
184
+ }
185
+
186
+ /**
187
+ * @magentoApiDataFixture Magento/Customer/_files/customer.php
188
+ * @expectedException \Exception
189
+ * @expectedExceptionMessage The account is locked.
190
+ */
191
+ public function testChangePasswordIfCustomerIsLocked ()
192
+ {
193
+ $ customerEmail = 'customer@example.com ' ;
194
+ $ currentPassword = 'password ' ;
195
+ $ newPassword = 'anotherPassword1 ' ;
196
+
197
+ $ this ->lockCustomer (1 );
198
+ $ query = $ this ->getQuery ($ currentPassword , $ newPassword );
199
+
200
+ $ headerMap = $ this ->getCustomerAuthHeaders ($ customerEmail , $ currentPassword );
201
+ $ this ->graphQlMutation ($ query , [], '' , $ headerMap );
202
+ }
203
+
204
+ /**
205
+ * @param int $customerId
206
+ *
207
+ * @return void
208
+ * @throws NoSuchEntityException
209
+ */
210
+ private function lockCustomer (int $ customerId ): void
211
+ {
212
+ $ customerSecure = $ this ->customerRegistry ->retrieveSecureData ($ customerId );
213
+ $ customerSecure ->setLockExpires ('2030-12-31 00:00:00 ' );
214
+ $ this ->customerAuthUpdate ->saveAuth ($ customerId );
215
+ }
216
+
217
+ /**
218
+ * @param int $customerId
219
+ *
220
+ * @return void
221
+ * @throws LocalizedException
222
+ */
223
+ private function setCustomerConfirmation (int $ customerId ): void
224
+ {
225
+ $ customer = $ this ->customerRepository ->getById ($ customerId );
226
+ $ customer ->setConfirmation ('d5a21f15bd4cc21bd1b21ef6d9989a38 ' );
227
+ $ this ->customerRepository ->save ($ customer );
228
+ }
229
+
230
+ /**
231
+ * @param $currentPassword
232
+ * @param $newPassword
233
+ *
234
+ * @return string
235
+ */
236
+ private function getQuery ($ currentPassword , $ newPassword )
115
237
{
116
238
$ query = <<<QUERY
117
239
mutation {
@@ -133,7 +255,9 @@ private function getChangePassQuery($currentPassword, $newPassword)
133
255
/**
134
256
* @param string $email
135
257
* @param string $password
258
+ *
136
259
* @return array
260
+ * @throws AuthenticationException
137
261
*/
138
262
private function getCustomerAuthHeaders (string $ email , string $ password ): array
139
263
{
0 commit comments