Skip to content

Commit 41cf471

Browse files
committed
Merge branch 'ACP2E-3215' of https://github.com/adobe-commerce-tier-4/magento2ce into PR-10-01-2024
2 parents 5e11df0 + e1efdbc commit 41cf471

File tree

4 files changed

+177
-14
lines changed

4 files changed

+177
-14
lines changed

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99

1010
use Magento\Authorization\Model\UserContextInterface;
1111
use Magento\Customer\Api\Data\CustomerInterface;
12+
use Magento\Customer\Model\Config\Share;
1213
use Magento\Customer\Model\ResourceModel\CustomerRepository;
1314
use Magento\Customer\Model\Session;
1415
use Magento\Framework\ObjectManager\ResetAfterRequestInterface;
1516
use Magento\GraphQl\Model\Query\ContextParametersInterface;
1617
use Magento\GraphQl\Model\Query\UserContextParametersProcessorInterface;
18+
use Magento\Store\Model\StoreManagerInterface;
1719

1820
/**
1921
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
@@ -40,20 +42,35 @@ class AddUserInfoToContext implements UserContextParametersProcessorInterface, R
4042
*/
4143
private $customerRepository;
4244

45+
/**
46+
* @var Share
47+
*/
48+
private $configShare;
49+
50+
/**
51+
* @var StoreManagerInterface
52+
*/
53+
private $storeManager;
4354
/**
4455
* @param UserContextInterface $userContext
4556
* @param Session $session
4657
* @param CustomerRepository $customerRepository
58+
* @param Share $configShare
59+
* @param StoreManagerInterface $storeManager
4760
*/
4861
public function __construct(
4962
UserContextInterface $userContext,
5063
Session $session,
51-
CustomerRepository $customerRepository
64+
CustomerRepository $customerRepository,
65+
Share $configShare,
66+
StoreManagerInterface $storeManager
5267
) {
5368
$this->userContext = $userContext;
5469
$this->userContextFromConstructor = $userContext;
5570
$this->session = $session;
5671
$this->customerRepository = $customerRepository;
72+
$this->configShare = $configShare;
73+
$this->storeManager = $storeManager;
5774
}
5875

5976
/**
@@ -119,8 +136,14 @@ public function getLoggedInCustomerData(): ?CustomerInterface
119136
*/
120137
private function isCustomer(?int $customerId, ?int $customerType): bool
121138
{
122-
return !empty($customerId)
139+
$result = !empty($customerId)
123140
&& !empty($customerType)
124141
&& $customerType === UserContextInterface::USER_TYPE_CUSTOMER;
142+
143+
if ($result && $this->configShare->isWebsiteScope()) {
144+
$customer = $this->customerRepository->getById($customerId);
145+
return (int)$customer->getWebsiteId() === (int)$this->storeManager->getStore()->getWebsiteId();
146+
}
147+
return $result;
125148
}
126149
}

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

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,18 @@
77

88
namespace Magento\GraphQl\Quote\Customer;
99

10-
use Exception;
10+
use Magento\Catalog\Helper\Data;
11+
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
12+
use Magento\Customer\Test\Fixture\Customer;
1113
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
1214
use Magento\Integration\Api\CustomerTokenServiceInterface;
15+
use Magento\Store\Test\Fixture\Group as StoreGroupFixture;
16+
use Magento\Store\Test\Fixture\Store as StoreFixture;
17+
use Magento\Store\Test\Fixture\Website as WebsiteFixture;
18+
use Magento\TestFramework\Fixture\Config;
19+
use Magento\TestFramework\Fixture\DataFixture;
20+
use Magento\TestFramework\Fixture\DataFixtureStorage;
21+
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
1322
use Magento\TestFramework\Helper\Bootstrap;
1423
use Magento\Quote\Model\ResourceModel\Quote\Collection;
1524
use Magento\Framework\ObjectManagerInterface;
@@ -35,11 +44,17 @@ class GetCustomerCartTest extends GraphQlAbstract
3544
*/
3645
private $objectManager;
3746

47+
/**
48+
* @var DataFixtureStorage;
49+
*/
50+
private $fixtures;
51+
3852
protected function setUp(): void
3953
{
4054
$this->objectManager = Bootstrap::getObjectManager();
4155
$this->getMaskedQuoteIdByReservedOrderId = $this->objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
4256
$this->customerTokenService = $this->objectManager->get(CustomerTokenServiceInterface::class);
57+
$this->fixtures = $this->objectManager->get(DataFixtureStorageManager::class)->getStorage();
4358
}
4459

4560
/**
@@ -133,6 +148,41 @@ public function testGetCustomerCartWithNoCustomerToken()
133148
$this->graphQlQuery($customerCartQuery);
134149
}
135150

151+
/**
152+
* Test graphql customer cart should expect an exception when customer doesn't belong to given website
153+
*/
154+
#[
155+
DataFixture(WebsiteFixture::class, as: 'website2'),
156+
DataFixture(StoreGroupFixture::class, ['website_id' => '$website2.id$'], 'store_group2'),
157+
DataFixture(StoreFixture::class, ['store_group_id' => '$store_group2.id$'], 'store2'),
158+
DataFixture(ProductFixture::class, ['website_ids' => [1, '$website2.id$' ]], as: 'product'),
159+
DataFixture(
160+
Customer::class,
161+
[
162+
'store_id' => '$store2.id$',
163+
'website_id' => '$website2.id$',
164+
'addresses' => [[]]
165+
],
166+
as: 'customer'
167+
)
168+
]
169+
public function testGetCustomerCartCustomerNotBelongingToWebsite()
170+
{
171+
$this->expectException(\Magento\TestFramework\TestCase\GraphQl\ResponseContainsErrorsException::class);
172+
$this->expectExceptionMessage('The request is allowed for logged in customer');
173+
174+
$customer = $this->fixtures->get('customer');
175+
$customStore = $this->fixtures->get('store2');
176+
177+
$generateTokenQuery = $this->generateCustomerToken($customer->getEmail(), 'password');
178+
179+
$tokenResponse = $this->graphQlMutation($generateTokenQuery, [], '', ['Store' => $customStore->getCode()]);
180+
$token = $tokenResponse['generateCustomerToken']['token'];
181+
182+
$customerCartQuery = $this->getCustomerCartQuery();
183+
$this->graphQlMutation($customerCartQuery, [], '', ['Authorization' => 'Bearer ' . $token]);
184+
}
185+
136186
/**
137187
* Query for customer cart after customer token is revoked
138188
*
@@ -262,4 +312,25 @@ private function getHeaderMap(string $username = 'customer@example.com', string
262312
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
263313
return $headerMap;
264314
}
315+
316+
/**
317+
* Get customer login token query
318+
*
319+
* @param string $email
320+
* @param string $password
321+
* @return string
322+
*/
323+
private function generateCustomerToken(string $email, string $password) : string
324+
{
325+
return <<<MUTATION
326+
mutation {
327+
generateCustomerToken(
328+
email: "{$email}"
329+
password: "{$password}"
330+
) {
331+
token
332+
}
333+
}
334+
MUTATION;
335+
}
265336
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Sales/CustomerOrdersTest.php

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public function testGetCustomerOrders()
143143
$query,
144144
[],
145145
'',
146-
$this->getCustomerHeaders($customerToken, null)
146+
$this->getCustomerHeaders($customerToken, $store2->getCode())
147147
);
148148

149149
$this->assertEquals(2, count($response['customer']['orders']['items']));
@@ -153,10 +153,59 @@ public function testGetCustomerOrders()
153153
$query,
154154
[],
155155
'',
156-
$this->getCustomerHeaders($customerToken, null)
156+
$this->getCustomerHeaders($customerToken, $store2->getCode())
157+
);
158+
159+
$this->assertEquals(1, count($response['customer']['orders']['items']));
160+
}
161+
162+
/**
163+
* Test graphql customer orders when customer doesn't have access to custom website in Multi-Store setup.
164+
165+
* @dataProvider dataProviderScope
166+
*/
167+
#[
168+
DataFixture(WebsiteFixture::class, as: 'website2'),
169+
DataFixture(StoreGroupFixture::class, ['website_id' => '$website2.id$'], 'store_group2'),
170+
DataFixture(StoreFixture::class, ['store_group_id' => '$store_group2.id$'], 'store2'),
171+
DataFixture(StoreFixture::class, ['store_group_id' => '$store_group2.id$'], 'store3'),
172+
DataFixture(ProductFixture::class, ['website_ids' => [1, '$website2.id$' ]], as: 'product'),
173+
DataFixture(
174+
Customer::class,
175+
[
176+
'store_id' => '$store2.id$',
177+
'website_id' => '$website2.id$',
178+
'addresses' => [[]]
179+
],
180+
as: 'customer'
181+
)
182+
]
183+
public function testGetCustomerOrdersCustomerHasNoAccess($scope)
184+
{
185+
$store2 = $this->fixtures->get('store2');
186+
$customer = $this->fixtures->get('customer');
187+
$currentEmail = $customer->getEmail();
188+
$currentPassword = 'password';
189+
190+
$generateToken = $this->generateCustomerToken($currentEmail, $currentPassword);
191+
$tokenResponse = $this->graphQlMutationWithResponseHeaders(
192+
$generateToken,
193+
[],
194+
'',
195+
['Store' => $store2->getCode()]
157196
);
197+
$customerToken = $tokenResponse['body']['generateCustomerToken']['token'];
158198

159-
$this->assertEquals(0, count($response['customer']['orders']['items']));
199+
$query = $this->getCustomerOrdersQuery($scope);
200+
201+
$this->expectException(\Magento\TestFramework\TestCase\GraphQl\ResponseContainsErrorsException::class);
202+
$this->expectExceptionMessage('The current customer isn\'t authorized.');
203+
$this->graphQlQuery(
204+
$query,
205+
[],
206+
'',
207+
$this->getCustomerHeaders($customerToken, null)
208+
);
160209
}
161210

162211
/**
@@ -225,4 +274,19 @@ private function generateCustomerToken(string $email, string $password) : string
225274
}
226275
MUTATION;
227276
}
277+
278+
/**
279+
* Scopes Data provider
280+
*
281+
* @return array
282+
*/
283+
public function dataProviderScope()
284+
{
285+
return [
286+
'store scope' => ['STORE'],
287+
'website scope' => ['WEBSITE'],
288+
'global scope' => ['GLOBAL'],
289+
'no scope' => [null],
290+
];
291+
}
228292
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Sales/DateOfFirstOrderResolverTest.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,18 @@ public function testGetCustomerOrdersGlobalScope()
121121
self::assertEquals(1, count($response['customer']['orders']['items']));
122122
self::assertArrayHasKey('date_of_first_order', $response['customer']['orders']);
123123

124-
$customerAuthHeaders = $this->getCustomerHeaders($token, null);
124+
$customerAuthHeaders = $this->getCustomerHeaders($token, $store2->getCode());
125125
$query = $this->getCustomerOrdersQueryWithFilters('GLOBAL');
126126
$response = $this->graphQlQuery($query, [], '', $customerAuthHeaders);
127127
self::assertNotEmpty($response['customer']['orders']['items']);
128128
self::assertEquals(2, count($response['customer']['orders']['items']));
129129
self::assertArrayHasKey('date_of_first_order', $response['customer']['orders']);
130+
131+
$customerAuthHeaders = $this->getCustomerHeaders($token, null);
132+
$query = $this->getCustomerOrdersQueryWithFilters('GLOBAL');
133+
$this->expectException(\Magento\TestFramework\TestCase\GraphQl\ResponseContainsErrorsException::class);
134+
$this->expectExceptionMessage('The current customer isn\'t authorized.');
135+
$this->graphQlQuery($query, [], '', $customerAuthHeaders);
130136
}
131137

132138
/**
@@ -175,13 +181,6 @@ public function testGetCustomerOrdersStoreScope()
175181
);
176182
$token = $tokenResponse['body']['generateCustomerToken']['token'];
177183

178-
$customerAuthHeaders = $this->getCustomerHeaders($token, null);
179-
$query = $this->getCustomerOrdersQueryWithFilters('STORE', '+1 years');
180-
$response = $this->graphQlQuery($query, [], '', $customerAuthHeaders);
181-
self::assertEmpty($response['customer']['orders']['items']);
182-
self::assertEquals(0, count($response['customer']['orders']['items']));
183-
self::assertNull($response['customer']['orders']['date_of_first_order']);
184-
185184
$customerAuthHeaders = $this->getCustomerHeaders($token, $store2->getCode());
186185
$query = $this->getCustomerOrdersQueryWithFilters('STORE', null);
187186
$response = $this->graphQlQuery($query, [], '', $customerAuthHeaders);
@@ -192,6 +191,12 @@ public function testGetCustomerOrdersStoreScope()
192191
$response = $this->graphQlQuery($query, [], '', $customerAuthHeaders);
193192
self::assertEquals(2, count($response['customer']['orders']['items']));
194193
self::assertArrayHasKey('date_of_first_order', $response['customer']['orders']);
194+
195+
$customerAuthHeaders = $this->getCustomerHeaders($token, null);
196+
$query = $this->getCustomerOrdersQueryWithFilters('STORE', '+1 years');
197+
$this->expectException(\Magento\TestFramework\TestCase\GraphQl\ResponseContainsErrorsException::class);
198+
$this->expectExceptionMessage('The current customer isn\'t authorized.');
199+
$this->graphQlQuery($query, [], '', $customerAuthHeaders);
195200
}
196201

197202
/**

0 commit comments

Comments
 (0)