7
7
8
8
namespace Magento \GraphQl \Customer ;
9
9
10
+ use Magento \Customer \Model \Log ;
11
+ use Magento \Customer \Model \Logger ;
12
+ use Magento \Framework \App \ResourceConnection ;
13
+ use Magento \Framework \DB \Adapter \AdapterInterface ;
14
+ use Magento \TestFramework \Helper \Bootstrap ;
10
15
use Magento \TestFramework \TestCase \GraphQlAbstract ;
11
16
12
17
/**
13
18
* API-functional tests cases for generateCustomerToken mutation
14
19
*/
15
20
class GenerateCustomerTokenTest extends GraphQlAbstract
16
21
{
22
+
23
+ /**
24
+ * @var Logger
25
+ */
26
+ private $ logger ;
27
+
28
+ /**
29
+ * @inheritdoc
30
+ */
31
+ protected function setUp (): void
32
+ {
33
+ parent ::setUp ();
34
+ $ this ->logger = Bootstrap::getObjectManager ()->get (Logger::class);
35
+ }
36
+
17
37
/**
18
38
* Verify customer token with valid credentials
19
39
*
20
40
* @magentoApiDataFixture Magento/Customer/_files/customer.php
21
41
*/
22
- public function testGenerateCustomerValidToken ()
42
+ public function testGenerateCustomerValidToken (): void
23
43
{
24
- $ email = 'customer@example.com ' ;
25
- $ password = 'password ' ;
26
-
27
- $ mutation = $ this ->getQuery ($ email , $ password );
44
+ $ mutation = $ this ->getQuery ();
28
45
29
46
$ response = $ this ->graphQlMutation ($ mutation );
30
47
$ this ->assertArrayHasKey ('generateCustomerToken ' , $ response );
@@ -41,7 +58,7 @@ public function testGenerateCustomerValidToken()
41
58
* @param string $password
42
59
* @param string $message
43
60
*/
44
- public function testGenerateCustomerTokenInvalidData (string $ email , string $ password , string $ message )
61
+ public function testGenerateCustomerTokenInvalidData (string $ email , string $ password , string $ message ): void
45
62
{
46
63
$ this ->expectException (\Exception::class);
47
64
@@ -55,12 +72,9 @@ public function testGenerateCustomerTokenInvalidData(string $email, string $pass
55
72
*
56
73
* @magentoApiDataFixture Magento/Customer/_files/customer.php
57
74
*/
58
- public function testRegenerateCustomerToken ()
75
+ public function testRegenerateCustomerToken (): void
59
76
{
60
- $ email = 'customer@example.com ' ;
61
- $ password = 'password ' ;
62
-
63
- $ mutation = $ this ->getQuery ($ email , $ password );
77
+ $ mutation = $ this ->getQuery ();
64
78
65
79
$ response1 = $ this ->graphQlMutation ($ mutation );
66
80
$ token1 = $ response1 ['generateCustomerToken ' ]['token ' ];
@@ -110,7 +124,7 @@ public function dataProviderInvalidCustomerInfo(): array
110
124
* @param string $password
111
125
* @return string
112
126
*/
113
- private function getQuery (string $ email , string $ password) : string
127
+ private function getQuery (string $ email = ' customer@example.com ' , string $ password = ' password ' ) : string
114
128
{
115
129
return <<<MUTATION
116
130
mutation {
@@ -127,22 +141,12 @@ private function getQuery(string $email, string $password) : string
127
141
/**
128
142
* Verify customer with empty email
129
143
*/
130
- public function testGenerateCustomerTokenWithEmptyEmail ()
144
+ public function testGenerateCustomerTokenWithEmptyEmail (): void
131
145
{
132
146
$ email = '' ;
133
147
$ password = 'bad-password ' ;
134
148
135
- $ mutation
136
- = <<<MUTATION
137
- mutation {
138
- generateCustomerToken(
139
- email: " {$ email }"
140
- password: " {$ password }"
141
- ) {
142
- token
143
- }
144
- }
145
- MUTATION ;
149
+ $ mutation = $ this ->getQuery ($ email , $ password );
146
150
147
151
$ this ->expectException (\Exception::class);
148
152
$ this ->expectExceptionMessage ('GraphQL response contains errors: Specify the "email" value. ' );
@@ -152,25 +156,51 @@ public function testGenerateCustomerTokenWithEmptyEmail()
152
156
/**
153
157
* Verify customer with empty password
154
158
*/
155
- public function testGenerateCustomerTokenWithEmptyPassword ()
159
+ public function testGenerateCustomerTokenWithEmptyPassword (): void
156
160
{
157
161
$ email = 'customer@example.com ' ;
158
162
$ password = '' ;
159
163
160
- $ mutation
161
- = <<<MUTATION
162
- mutation {
163
- generateCustomerToken(
164
- email: " {$ email }"
165
- password: " {$ password }"
166
- ) {
167
- token
168
- }
169
- }
170
- MUTATION ;
164
+ $ mutation = $ this ->getQuery ($ email , $ password );
171
165
172
166
$ this ->expectException (\Exception::class);
173
167
$ this ->expectExceptionMessage ('GraphQL response contains errors: Specify the "password" value. ' );
174
168
$ this ->graphQlMutation ($ mutation );
175
169
}
170
+
171
+ /**
172
+ * Verify customer log after generate customer token
173
+ *
174
+ * @magentoApiDataFixture Magento/Customer/_files/customer.php
175
+ */
176
+ public function testCustomerLogAfterGenerateCustomerToken (): void
177
+ {
178
+ $ response = $ this ->graphQlMutation ($ this ->getQuery ());
179
+ $ this ->assertArrayHasKey ('generateCustomerToken ' , $ response );
180
+ $ this ->assertIsArray ($ response ['generateCustomerToken ' ]);
181
+
182
+ /** @var Log $log */
183
+ $ log = $ this ->logger ->get (1 );
184
+ $ this ->assertNotEmpty ($ log ->getLastLoginAt ());
185
+ }
186
+
187
+ /**
188
+ * Ensure that customer log record is deleted.
189
+ *
190
+ * @return void
191
+ */
192
+ protected function tearDown (): void
193
+ {
194
+ if ($ this ->logger ->get (1 )->getLastLoginAt ()) {
195
+ /** @var AdapterInterface $connection */
196
+ /** @var ResourceConnection $resource */
197
+ $ resource = Bootstrap::getObjectManager ()->get (ResourceConnection::class);
198
+ $ connection = $ resource ->getConnection (ResourceConnection::DEFAULT_CONNECTION );
199
+ $ connection ->delete (
200
+ $ resource ->getTableName ('customer_log ' ),
201
+ ['customer_id ' => 1 ]
202
+ );
203
+ }
204
+ parent ::tearDown ();
205
+ }
176
206
}
0 commit comments