Skip to content

Commit c2a3fd4

Browse files
committed
Merge remote-tracking branch 'origin/ACP2E-4312' into PR_2025_11_07_muntianu
2 parents f7762fc + c87526d commit c2a3fd4

File tree

4 files changed

+190
-12
lines changed

4 files changed

+190
-12
lines changed

app/code/Magento/CustomerGraphQl/Model/GetGuestOrdersByEmail.php

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,51 @@
77

88
namespace Magento\CustomerGraphQl\Model;
99

10+
use Magento\Customer\Api\Data\CustomerInterface;
11+
use Magento\Customer\Model\Config\Share;
1012
use Magento\Framework\Api\SearchCriteriaBuilder;
13+
use Magento\Framework\App\Config\ScopeConfigInterface;
1114
use Magento\Sales\Api\OrderRepositoryInterface;
1215
use Magento\Sales\Api\Data\OrderSearchResultInterface;
1316

1417
class GetGuestOrdersByEmail
1518
{
1619
/**
1720
* @param OrderRepositoryInterface $orderRepository
21+
* @param ScopeConfigInterface $scopeConfig
1822
* @param SearchCriteriaBuilder $searchCriteriaBuilder
1923
*/
2024
public function __construct(
2125
private readonly OrderRepositoryInterface $orderRepository,
22-
private SearchCriteriaBuilder $searchCriteriaBuilder
26+
private readonly ScopeConfigInterface $scopeConfig,
27+
private readonly SearchCriteriaBuilder $searchCriteriaBuilder
2328
) {
2429
}
2530

2631
/**
2732
* Retrieve customer orders collection
2833
*
29-
* @param string $email
34+
* @param CustomerInterface $customer
3035
* @return OrderSearchResultInterface
3136
*/
32-
public function execute(string $email): OrderSearchResultInterface
37+
public function execute(CustomerInterface $customer): OrderSearchResultInterface
3338
{
34-
$this->searchCriteriaBuilder->addFilter(
35-
'customer_email',
36-
$email,
37-
'eq'
38-
)->addFilter(
39-
'customer_is_guest',
40-
1,
41-
'eq'
39+
$this->searchCriteriaBuilder
40+
->addFilter('customer_email', $customer->getEmail())
41+
->addFilter('customer_is_guest', 1);
42+
43+
$customerAccountShareScope = (int)$this->scopeConfig->getValue(
44+
Share::XML_PATH_CUSTOMER_ACCOUNT_SHARE,
45+
ScopeConfigInterface::SCOPE_TYPE_DEFAULT
4246
);
47+
48+
if ($customerAccountShareScope === Share::SHARE_WEBSITE) {
49+
$this->searchCriteriaBuilder->addFilter(
50+
'store_id',
51+
$customer->getStoreId()
52+
);
53+
}
54+
4355
return $this->orderRepository->getList($this->searchCriteriaBuilder->create());
4456
}
4557
}

app/code/Magento/CustomerGraphQl/Plugin/Model/MergeGuestOrder.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ public function __construct(
3030
* @param AccountManagement $subject
3131
* @param CustomerInterface $customer
3232
* @return CustomerInterface
33+
*
34+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
3335
*/
3436
public function afterCreateAccount(AccountManagement $subject, CustomerInterface $customer)
3537
{
36-
$searchResult = $this->getGuestOrdersByEmail->execute($customer->getEmail());
38+
$searchResult = $this->getGuestOrdersByEmail->execute($customer);
3739
foreach ($searchResult->getItems() as $order) {
3840
$this->customerAssignment->execute($order, $customer);
3941
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CustomerGraphQl\Test\Unit\Model;
9+
10+
use Magento\Customer\Api\Data\CustomerInterface;
11+
use Magento\Customer\Model\Config\Share;
12+
use Magento\CustomerGraphQl\Model\GetGuestOrdersByEmail;
13+
use Magento\Framework\Api\SearchCriteria;
14+
use Magento\Framework\Api\SearchCriteriaBuilder;
15+
use Magento\Framework\App\Config\ScopeConfigInterface;
16+
use Magento\Sales\Api\Data\OrderSearchResultInterface;
17+
use Magento\Sales\Api\OrderRepositoryInterface;
18+
use PHPUnit\Framework\MockObject\Exception;
19+
use PHPUnit\Framework\MockObject\MockObject;
20+
use PHPUnit\Framework\TestCase;
21+
22+
class GetGuestOrdersByEmailTest extends TestCase
23+
{
24+
/**
25+
* @var OrderRepositoryInterface|MockObject
26+
*/
27+
private OrderRepositoryInterface $orderRepository;
28+
29+
/**
30+
* @var ScopeConfigInterface|MockObject
31+
*/
32+
private ScopeConfigInterface $scopeConfig;
33+
34+
/**
35+
* @var SearchCriteriaBuilder|MockObject
36+
*/
37+
private SearchCriteriaBuilder $searchCriteriaBuilder;
38+
39+
/**
40+
* @var GetGuestOrdersByEmail
41+
*/
42+
private GetGuestOrdersByEmail $getGuestOrdersByEmail;
43+
44+
/**
45+
* @return void
46+
*/
47+
protected function setUp(): void
48+
{
49+
$this->orderRepository = $this->createMock(OrderRepositoryInterface::class);
50+
$this->scopeConfig = $this->createMock(ScopeConfigInterface::class);
51+
$this->searchCriteriaBuilder = $this->createMock(SearchCriteriaBuilder::class);
52+
$this->getGuestOrdersByEmail = new GetGuestOrdersByEmail(
53+
$this->orderRepository,
54+
$this->scopeConfig,
55+
$this->searchCriteriaBuilder
56+
);
57+
58+
parent::setUp();
59+
}
60+
61+
/**
62+
* @return void
63+
* @throws Exception
64+
*/
65+
public function testExecute(): void
66+
{
67+
$email = '[email protected]';
68+
$storeId = 1;
69+
$customer = $this->createMock(CustomerInterface::class);
70+
$customer->expects($this->once())->method('getEmail')->willReturn($email);
71+
$customer->expects($this->once())->method('getStoreId')->willReturn($storeId);
72+
$this->searchCriteriaBuilder->expects($this->exactly(3))
73+
->method('addFilter')
74+
->willReturnSelf();
75+
$searchCriteria = $this->createMock(SearchCriteria::class);
76+
$this->searchCriteriaBuilder->expects($this->once())
77+
->method('create')
78+
->willReturn($searchCriteria);
79+
$this->scopeConfig->expects($this->once())
80+
->method('getValue')
81+
->with(
82+
Share::XML_PATH_CUSTOMER_ACCOUNT_SHARE,
83+
ScopeConfigInterface::SCOPE_TYPE_DEFAULT
84+
)
85+
->willReturn(1);
86+
$this->orderRepository->expects($this->once())
87+
->method('getList')
88+
->with($searchCriteria)
89+
->willReturn($this->createMock(OrderSearchResultInterface::class));
90+
$this->getGuestOrdersByEmail->execute($customer);
91+
}
92+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CustomerGraphQl\Test\Unit\Plugin\Model;
9+
10+
use Magento\Customer\Api\Data\CustomerInterface;
11+
use Magento\Customer\Model\AccountManagement;
12+
use Magento\CustomerGraphQl\Model\GetGuestOrdersByEmail;
13+
use Magento\CustomerGraphQl\Plugin\Model\MergeGuestOrder;
14+
use Magento\Sales\Api\Data\OrderInterface;
15+
use Magento\Sales\Api\Data\OrderSearchResultInterface;
16+
use Magento\Sales\Model\Order\CustomerAssignment;
17+
use PHPUnit\Framework\MockObject\Exception;
18+
use PHPUnit\Framework\MockObject\MockObject;
19+
use PHPUnit\Framework\TestCase;
20+
21+
class MergeGuestOrderTest extends TestCase
22+
{
23+
/**
24+
* @var GetGuestOrdersByEmail|MockObject
25+
*/
26+
private GetGuestOrdersByEmail $getGuestOrdersByEmail;
27+
28+
/**
29+
* @var CustomerAssignment|MockObject
30+
*/
31+
private CustomerAssignment $customerAssignment;
32+
33+
/**
34+
* @var MergeGuestOrder
35+
*/
36+
private MergeGuestOrder $mergeGuestOrder;
37+
38+
/**
39+
* @return void
40+
*/
41+
protected function setUp(): void
42+
{
43+
$this->customerAssignment = $this->createMock(CustomerAssignment::class);
44+
$this->getGuestOrdersByEmail = $this->createMock(GetGuestOrdersByEmail::class);
45+
$this->mergeGuestOrder = new MergeGuestOrder(
46+
$this->getGuestOrdersByEmail,
47+
$this->customerAssignment
48+
);
49+
parent::setUp();
50+
}
51+
52+
/**
53+
* @return void
54+
* @throws Exception
55+
*/
56+
public function testAfterCreateAccount(): void
57+
{
58+
$subject = $this->createMock(AccountManagement::class);
59+
$customer = $this->createMock(CustomerInterface::class);
60+
$result = $this->createMock(OrderSearchResultInterface::class);
61+
$order = $this->createMock(OrderInterface::class);
62+
$result->expects($this->once())->method('getItems')->willReturn([$order]);
63+
64+
$this->getGuestOrdersByEmail->expects($this->once())
65+
->method('execute')
66+
->with($customer)
67+
->willReturn($result);
68+
$this->customerAssignment->expects($this->once())->method('execute')->with($order, $customer);
69+
70+
$this->mergeGuestOrder->afterCreateAccount($subject, $customer);
71+
}
72+
}

0 commit comments

Comments
 (0)