Skip to content

Commit c020ed2

Browse files
committed
Merge branch '33378_graphQL_customer_log' of github.com:khrystynastolbova/magento2 into 2.4-develop-prs
2 parents 05788bb + 9fdbc70 commit c020ed2

File tree

2 files changed

+78
-36
lines changed

2 files changed

+78
-36
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
10+
<event name="customer_login">
11+
<observer name="customer_log_login" instance="Magento\Customer\Observer\LogLastLoginAtObserver" />
12+
</event>
13+
</config>

dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/GenerateCustomerTokenTest.php

Lines changed: 65 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,40 @@
77

88
namespace Magento\GraphQl\Customer;
99

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;
1015
use Magento\TestFramework\TestCase\GraphQlAbstract;
1116

1217
/**
1318
* API-functional tests cases for generateCustomerToken mutation
1419
*/
1520
class GenerateCustomerTokenTest extends GraphQlAbstract
1621
{
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+
1736
/**
1837
* Verify customer token with valid credentials
1938
*
2039
* @magentoApiDataFixture Magento/Customer/_files/customer.php
2140
*/
22-
public function testGenerateCustomerValidToken()
41+
public function testGenerateCustomerValidToken(): void
2342
{
24-
$email = 'customer@example.com';
25-
$password = 'password';
26-
27-
$mutation = $this->getQuery($email, $password);
43+
$mutation = $this->getQuery();
2844

2945
$response = $this->graphQlMutation($mutation);
3046
$this->assertArrayHasKey('generateCustomerToken', $response);
@@ -41,7 +57,7 @@ public function testGenerateCustomerValidToken()
4157
* @param string $password
4258
* @param string $message
4359
*/
44-
public function testGenerateCustomerTokenInvalidData(string $email, string $password, string $message)
60+
public function testGenerateCustomerTokenInvalidData(string $email, string $password, string $message): void
4561
{
4662
$this->expectException(\Exception::class);
4763

@@ -55,12 +71,9 @@ public function testGenerateCustomerTokenInvalidData(string $email, string $pass
5571
*
5672
* @magentoApiDataFixture Magento/Customer/_files/customer.php
5773
*/
58-
public function testRegenerateCustomerToken()
74+
public function testRegenerateCustomerToken(): void
5975
{
60-
$email = 'customer@example.com';
61-
$password = 'password';
62-
63-
$mutation = $this->getQuery($email, $password);
76+
$mutation = $this->getQuery();
6477

6578
$response1 = $this->graphQlMutation($mutation);
6679
$token1 = $response1['generateCustomerToken']['token'];
@@ -110,7 +123,7 @@ public function dataProviderInvalidCustomerInfo(): array
110123
* @param string $password
111124
* @return string
112125
*/
113-
private function getQuery(string $email, string $password) : string
126+
private function getQuery(string $email = 'customer@example.com', string $password = 'password'): string
114127
{
115128
return <<<MUTATION
116129
mutation {
@@ -127,22 +140,12 @@ private function getQuery(string $email, string $password) : string
127140
/**
128141
* Verify customer with empty email
129142
*/
130-
public function testGenerateCustomerTokenWithEmptyEmail()
143+
public function testGenerateCustomerTokenWithEmptyEmail(): void
131144
{
132145
$email = '';
133146
$password = 'bad-password';
134147

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);
146149

147150
$this->expectException(\Exception::class);
148151
$this->expectExceptionMessage('GraphQL response contains errors: Specify the "email" value.');
@@ -152,25 +155,51 @@ public function testGenerateCustomerTokenWithEmptyEmail()
152155
/**
153156
* Verify customer with empty password
154157
*/
155-
public function testGenerateCustomerTokenWithEmptyPassword()
158+
public function testGenerateCustomerTokenWithEmptyPassword(): void
156159
{
157160
$email = 'customer@example.com';
158161
$password = '';
159162

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);
171164

172165
$this->expectException(\Exception::class);
173166
$this->expectExceptionMessage('GraphQL response contains errors: Specify the "password" value.');
174167
$this->graphQlMutation($mutation);
175168
}
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+
}
176205
}

0 commit comments

Comments
 (0)