Skip to content

Commit b612956

Browse files
committed
MC-38005: Improve Customer Update through REST API
1 parent 46e2216 commit b612956

File tree

3 files changed

+87
-17
lines changed

3 files changed

+87
-17
lines changed

app/code/Magento/CustomerGraphQl/Model/Context/AddUserInfoToContext.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ public function execute(ContextParametersInterface $contextParameters): ContextP
6060
*/
6161
private function isCustomer(?int $customerId, ?int $customerType): bool
6262
{
63-
return !empty($customerId) && !empty($customerType) && $customerType !== UserContextInterface::USER_TYPE_GUEST;
63+
return !empty($customerId)
64+
&& !empty($customerType)
65+
&& $customerType === UserContextInterface::USER_TYPE_CUSTOMER;
6466
}
6567
}

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

Lines changed: 68 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,21 @@
77

88
namespace Magento\GraphQl\Customer;
99

10+
use Exception;
11+
use Magento\Customer\Api\AccountManagementInterface;
1012
use Magento\Customer\Api\CustomerRepositoryInterface;
1113
use Magento\Customer\Model\CustomerAuthUpdate;
1214
use Magento\Customer\Model\CustomerRegistry;
15+
use Magento\Framework\ObjectManagerInterface;
16+
use Magento\Integration\Api\AdminTokenServiceInterface;
1317
use Magento\Integration\Api\CustomerTokenServiceInterface;
1418
use Magento\TestFramework\Helper\Bootstrap;
19+
use Magento\TestFramework\Bootstrap as TestBootstrap;
1520
use Magento\TestFramework\TestCase\GraphQlAbstract;
1621

22+
/**
23+
* GraphQl tests for @see \Magento\CustomerGraphQl\Model\Customer\GetCustomer.
24+
*/
1725
class GetCustomerTest extends GraphQlAbstract
1826
{
1927
/**
@@ -36,14 +44,23 @@ class GetCustomerTest extends GraphQlAbstract
3644
*/
3745
private $customerRepository;
3846

47+
/**
48+
* @var ObjectManagerInterface
49+
*/
50+
private $objectManager;
51+
52+
/**
53+
* @inheridoc
54+
*/
3955
protected function setUp(): void
4056
{
4157
parent::setUp();
4258

43-
$this->customerTokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class);
44-
$this->customerRegistry = Bootstrap::getObjectManager()->get(CustomerRegistry::class);
45-
$this->customerAuthUpdate = Bootstrap::getObjectManager()->get(CustomerAuthUpdate::class);
46-
$this->customerRepository = Bootstrap::getObjectManager()->get(CustomerRepositoryInterface::class);
59+
$this->objectManager = Bootstrap::getObjectManager();
60+
$this->customerTokenService = $this->objectManager->get(CustomerTokenServiceInterface::class);
61+
$this->customerRegistry = $this->objectManager->get(CustomerRegistry::class);
62+
$this->customerAuthUpdate = $this->objectManager->get(CustomerAuthUpdate::class);
63+
$this->customerRepository = $this->objectManager->get(CustomerRepositoryInterface::class);
4764
}
4865

4966
/**
@@ -71,18 +88,19 @@ public function testGetCustomer()
7188
$this->getCustomerAuthHeaders($currentEmail, $currentPassword)
7289
);
7390

74-
$this->assertEquals(null, $response['customer']['id']);
91+
$this->assertNull($response['customer']['id']);
7592
$this->assertEquals('John', $response['customer']['firstname']);
7693
$this->assertEquals('Smith', $response['customer']['lastname']);
7794
$this->assertEquals($currentEmail, $response['customer']['email']);
7895
}
7996

8097
/**
81-
* @expectedException \Exception
82-
* @expectedExceptionMessage The current customer isn't authorized.
8398
*/
8499
public function testGetCustomerIfUserIsNotAuthorized()
85100
{
101+
$this->expectException(Exception::class);
102+
$this->expectExceptionMessage('The current customer isn\'t authorized.');
103+
86104
$query = <<<QUERY
87105
query {
88106
customer {
@@ -95,17 +113,49 @@ public function testGetCustomerIfUserIsNotAuthorized()
95113
$this->graphQlQuery($query);
96114
}
97115

116+
/**
117+
* @magentoApiDataFixture Magento/User/_files/user_with_role.php
118+
* @return void
119+
*/
120+
public function testGetCustomerIfUserHasWrongType(): void
121+
{
122+
/** @var $adminTokenService AdminTokenServiceInterface */
123+
$adminTokenService = $this->objectManager->get(AdminTokenServiceInterface::class);
124+
$adminToken = $adminTokenService->createAdminAccessToken('adminUser', TestBootstrap::ADMIN_PASSWORD);
125+
126+
$this->expectException(Exception::class);
127+
$this->expectExceptionMessage('The current customer isn\'t authorized.');
128+
129+
$query = <<<QUERY
130+
query {
131+
customer {
132+
firstname
133+
lastname
134+
email
135+
}
136+
}
137+
QUERY;
138+
$this->graphQlQuery(
139+
$query,
140+
[],
141+
'',
142+
['Authorization' => 'Bearer ' . $adminToken]
143+
);
144+
}
145+
98146
/**
99147
* @magentoApiDataFixture Magento/Customer/_files/customer.php
100-
* @expectedException \Exception
101-
* @expectedExceptionMessage The account is locked.
102148
*/
103149
public function testGetCustomerIfAccountIsLocked()
104150
{
105-
$this->lockCustomer(1);
106-
107151
$currentEmail = 'customer@example.com';
108152
$currentPassword = 'password';
153+
$customer = $this->customerRepository->get($currentEmail);
154+
155+
$this->lockCustomer((int)$customer->getId());
156+
157+
$this->expectException(Exception::class);
158+
$this->expectExceptionMessage('The account is locked.');
109159

110160
$query = <<<QUERY
111161
query {
@@ -125,18 +175,19 @@ public function testGetCustomerIfAccountIsLocked()
125175
}
126176

127177
/**
128-
* @magentoApiDataFixture Magento/Customer/_files/customer_confirmation_config_enable.php
178+
* @magentoConfigFixture customer/create_account/confirm 1
129179
* @magentoApiDataFixture Magento/Customer/_files/customer.php
130-
* @expectedExceptionMessage This account isn't confirmed. Verify and try again.
180+
*
131181
*/
132182
public function testAccountIsNotConfirmed()
133183
{
184+
$this->expectExceptionMessage("This account isn't confirmed. Verify and try again.");
134185
$customerEmail = 'customer@example.com';
135186
$currentPassword = 'password';
187+
$customer = $this->customerRepository->get($customerEmail);
136188
$headersMap = $this->getCustomerAuthHeaders($customerEmail, $currentPassword);
137-
$customer = $this->customerRepository->getById(1)->setConfirmation(
138-
\Magento\Customer\Api\AccountManagementInterface::ACCOUNT_CONFIRMATION_REQUIRED
139-
);
189+
$customer = $this->customerRepository->getById((int)$customer->getId())
190+
->setConfirmation(AccountManagementInterface::ACCOUNT_CONFIRMATION_REQUIRED);
140191
$this->customerRepository->save($customer);
141192
$query = <<<QUERY
142193
query {
@@ -158,6 +209,7 @@ public function testAccountIsNotConfirmed()
158209
private function getCustomerAuthHeaders(string $email, string $password): array
159210
{
160211
$customerToken = $this->customerTokenService->createCustomerAccessToken($email, $password);
212+
161213
return ['Authorization' => 'Bearer ' . $customerToken];
162214
}
163215

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\TestFramework\Helper\Bootstrap;
9+
use Magento\User\Model\User;
10+
11+
/** @var $model \Magento\User\Model\User */
12+
$model = Bootstrap::getObjectManager()->create(User::class);
13+
$user = $model->loadByUsername('adminUser');
14+
if ($user->getId()) {
15+
$model->delete();
16+
}

0 commit comments

Comments
 (0)