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