Skip to content

Commit cce616b

Browse files
authored
Merge pull request #7375 from magento-gl/GL_Mainline_PR_03012022
Gl mainline pr 03012022
2 parents 4cc59ff + 44cbad4 commit cce616b

File tree

13 files changed

+608
-25
lines changed

13 files changed

+608
-25
lines changed

app/code/Magento/Checkout/view/frontend/web/template/billing-address/details.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<text args="currentBillingAddress().prefix"></text> <text args="currentBillingAddress().firstname"></text>
99
<text args="currentBillingAddress().middlename"></text>
1010
<text args="currentBillingAddress().lastname"></text> <text args="currentBillingAddress().suffix"></text><br/>
11-
<text args="currentBillingAddress().street.join(', ')"></text><br/>
11+
<text args="_.values(currentBillingAddress().street).join(', ')"></text><br/>
1212
<text args="currentBillingAddress().city "></text>, <span text="currentBillingAddress().region"></span>
1313
<text args="currentBillingAddress().postcode"></text><br/>
1414
<text args="getCountryName(currentBillingAddress().countryId)"></text><br/>

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
namespace Magento\CustomerGraphQl\Model\Context;
99

1010
use Magento\Authorization\Model\UserContextInterface;
11+
use Magento\Customer\Api\Data\CustomerInterface;
1112
use Magento\Customer\Model\ResourceModel\CustomerRepository;
1213
use Magento\Customer\Model\Session;
1314
use Magento\GraphQl\Model\Query\ContextParametersInterface;
1415
use Magento\GraphQl\Model\Query\UserContextParametersProcessorInterface;
1516

1617
/**
17-
* @inheritdoc
18+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
1819
*/
1920
class AddUserInfoToContext implements UserContextParametersProcessorInterface
2021
{
@@ -33,6 +34,11 @@ class AddUserInfoToContext implements UserContextParametersProcessorInterface
3334
*/
3435
private $customerRepository;
3536

37+
/**
38+
* @var CustomerInterface|null
39+
*/
40+
private $loggedInCustomerData = null;
41+
3642
/**
3743
* @param UserContextInterface $userContext
3844
* @param Session $session
@@ -75,6 +81,11 @@ public function execute(ContextParametersInterface $contextParameters): ContextP
7581

7682
$isCustomer = $this->isCustomer($currentUserId, $currentUserType);
7783
$contextParameters->addExtensionAttribute('is_customer', $isCustomer);
84+
85+
if ($this->session->isLoggedIn()) {
86+
$this->loggedInCustomerData = $this->session->getCustomerData();
87+
}
88+
7889
if ($isCustomer) {
7990
$customer = $this->customerRepository->getById($currentUserId);
8091
$this->session->setCustomerData($customer);
@@ -83,6 +94,16 @@ public function execute(ContextParametersInterface $contextParameters): ContextP
8394
return $contextParameters;
8495
}
8596

97+
/**
98+
* Get logged in customer data
99+
*
100+
* @return CustomerInterface
101+
*/
102+
public function getLoggedInCustomerData(): ?CustomerInterface
103+
{
104+
return $this->loggedInCustomerData;
105+
}
106+
86107
/**
87108
* Checking if current user is logged
88109
*

app/code/Magento/CustomerGraphQl/Plugin/ClearCustomerSessionAfterRequest.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@
1010
use Magento\Authorization\Model\UserContextInterface;
1111
use Magento\Customer\Model\ResourceModel\CustomerRepository;
1212
use Magento\Customer\Model\Session as CustomerSession;
13+
use Magento\CustomerGraphQl\Model\Context\AddUserInfoToContext;
14+
use Magento\Framework\App\ObjectManager;
1315
use Magento\Framework\App\ResponseInterface;
1416
use Magento\Framework\Exception\LocalizedException;
1517
use Magento\Framework\Exception\NoSuchEntityException;
1618
use Magento\GraphQl\Controller\GraphQl as GraphQlController;
1719

1820
/**
1921
* Clear the user data out of the session object before returning the GraphQL response
22+
*
23+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
2024
*/
2125
class ClearCustomerSessionAfterRequest
2226
{
@@ -35,19 +39,28 @@ class ClearCustomerSessionAfterRequest
3539
*/
3640
private $customerRepository;
3741

42+
/**
43+
* @var AddUserInfoToContext
44+
*/
45+
private $addUserInfoToContext;
46+
3847
/**
3948
* @param UserContextInterface $userContext
4049
* @param CustomerSession $customerSession
4150
* @param CustomerRepository $customerRepository
51+
* @param AddUserInfoToContext $addUserInfoToContext
4252
*/
4353
public function __construct(
4454
UserContextInterface $userContext,
4555
CustomerSession $customerSession,
46-
CustomerRepository $customerRepository
56+
CustomerRepository $customerRepository,
57+
AddUserInfoToContext $addUserInfoToContext = null
4758
) {
4859
$this->userContext = $userContext;
4960
$this->customerSession = $customerSession;
5061
$this->customerRepository = $customerRepository;
62+
$this->addUserInfoToContext = $addUserInfoToContext ??
63+
ObjectManager::getInstance()->get(AddUserInfoToContext::class);
5164
}
5265

5366
/**
@@ -61,8 +74,10 @@ public function __construct(
6174
*/
6275
public function afterDispatch(GraphQlController $controller, ResponseInterface $response): ResponseInterface
6376
{
64-
$this->customerSession->setCustomerId(null);
65-
$this->customerSession->setCustomerGroupId(null);
77+
$loggedInCustomerData = $this->addUserInfoToContext->getLoggedInCustomerData();
78+
$this->customerSession->setCustomerId($loggedInCustomerData ? $loggedInCustomerData->getId() : null);
79+
$this->customerSession->setCustomerGroupId($loggedInCustomerData ? $loggedInCustomerData->getGroupId() : null);
80+
6681
return $response;
6782
}
6883
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\CustomerGraphQl\Test\Unit\Model\Context;
8+
9+
use Magento\Authorization\Model\UserContextInterface;
10+
use Magento\Customer\Api\Data\CustomerInterface;
11+
use Magento\Customer\Model\ResourceModel\CustomerRepository;
12+
use Magento\Customer\Model\Session;
13+
use Magento\CustomerGraphQl\Model\Context\AddUserInfoToContext;
14+
use Magento\GraphQl\Model\Query\ContextParametersInterface;
15+
use PHPUnit\Framework\MockObject\MockObject;
16+
use PHPUnit\Framework\TestCase;
17+
18+
/**
19+
* @see AddUserInfoToContext
20+
*/
21+
class AddUserInfoToContextTest extends TestCase
22+
{
23+
/**
24+
* @var AddUserInfoToContext
25+
*/
26+
private AddUserInfoToContext $addUserInfoToContext;
27+
28+
/**
29+
* @var UserContextInterface|MockObject
30+
*/
31+
private UserContextInterface $userContextMock;
32+
33+
/**
34+
* @var Session|MockObject
35+
*/
36+
private Session $sessionMock;
37+
38+
/**
39+
* @var CustomerRepository|MockObject
40+
*/
41+
private CustomerRepository $customerRepositoryMock;
42+
43+
/**
44+
* @var ContextParametersInterface|MockObject
45+
*/
46+
private ContextParametersInterface $contextParametersMock;
47+
48+
/**
49+
* @var CustomerInterface|MockObject
50+
*/
51+
private CustomerInterface $customerMock;
52+
53+
protected function setUp(): void
54+
{
55+
$this->userContextMock = $this->createMock(UserContextInterface::class);
56+
$this->sessionMock = $this->createMock(Session::class);
57+
$this->customerRepositoryMock = $this->createMock(CustomerRepository::class);
58+
$this->contextParametersMock = $this->createMock(ContextParametersInterface::class);
59+
$this->customerMock = $this->createMock(CustomerInterface::class);
60+
61+
$this->addUserInfoToContext = new AddUserInfoToContext(
62+
$this->userContextMock,
63+
$this->sessionMock,
64+
$this->customerRepositoryMock
65+
);
66+
}
67+
68+
/**
69+
* Test execute function for user type - customer
70+
*/
71+
public function testExecuteForCustomer(): void
72+
{
73+
$this->userContextMock
74+
->expects($this->once())
75+
->method('getUserId')
76+
->willReturn(10);
77+
$this->contextParametersMock
78+
->expects($this->once())
79+
->method('setUserId');
80+
$this->userContextMock
81+
->expects($this->once())
82+
->method('getUserType')
83+
->willReturn(3);
84+
$this->contextParametersMock
85+
->expects($this->once())
86+
->method('setUserType');
87+
$this->sessionMock
88+
->expects($this->once())
89+
->method('isLoggedIn')
90+
->willReturn(true);
91+
$this->sessionMock
92+
->expects($this->once())
93+
->method('getCustomerData')
94+
->willReturn($this->customerMock);
95+
$this->customerRepositoryMock
96+
->expects($this->once())
97+
->method('getById')
98+
->willReturn($this->customerMock);
99+
$this->sessionMock
100+
->expects($this->once())
101+
->method('setCustomerData');
102+
$this->sessionMock
103+
->expects($this->once())
104+
->method('setCustomerGroupId');
105+
$this->addUserInfoToContext->execute($this->contextParametersMock);
106+
}
107+
108+
protected function tearDown(): void
109+
{
110+
unset(
111+
$this->addCustomerGroupToContext,
112+
$this->userContextMock,
113+
$this->sessionMock,
114+
$this->customerRepositoryMock,
115+
$this->contextParametersMock,
116+
$this->customerMock
117+
);
118+
}
119+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\CustomerGraphQl\Test\Unit\Plugin;
8+
9+
use Magento\Authorization\Model\UserContextInterface;
10+
use Magento\Customer\Api\Data\CustomerInterface;
11+
use Magento\Customer\Model\ResourceModel\CustomerRepository;
12+
use Magento\Customer\Model\Session;
13+
use Magento\CustomerGraphQl\Model\Context\AddUserInfoToContext;
14+
use Magento\CustomerGraphQl\Plugin\ClearCustomerSessionAfterRequest;
15+
use Magento\Framework\App\ResponseInterface;
16+
use Magento\GraphQl\Controller\GraphQl;
17+
use PHPUnit\Framework\MockObject\MockObject;
18+
use PHPUnit\Framework\TestCase;
19+
20+
/**
21+
* @see ClearCustomerSessionAfterRequest
22+
*/
23+
class ClearCustomerSessionAfterRequestTest extends TestCase
24+
{
25+
/**
26+
* @var ClearCustomerSessionAfterRequest
27+
*/
28+
private ClearCustomerSessionAfterRequest $clearCustomerSessionAfterRequest;
29+
30+
/**
31+
* @var UserContextInterface|MockObject
32+
*/
33+
private UserContextInterface $userContextMock;
34+
35+
/**
36+
* @var Session|MockObject
37+
*/
38+
private Session $sessionMock;
39+
40+
/**
41+
* @var CustomerRepository|MockObject
42+
*/
43+
private CustomerRepository $customerRepositoryMock;
44+
45+
/**
46+
* @var AddUserInfoToContext
47+
*/
48+
private AddUserInfoToContext $addUserInfoToContextMock;
49+
50+
/**
51+
* @var GraphQl
52+
*/
53+
private GraphQl $graphQlMock;
54+
55+
/**
56+
* @var ResponseInterface
57+
*/
58+
private ResponseInterface $responseMock;
59+
60+
/**
61+
* @var CustomerInterface|MockObject
62+
*/
63+
private CustomerInterface $customerMock;
64+
65+
protected function setUp(): void
66+
{
67+
$this->userContextMock = $this->createMock(UserContextInterface::class);
68+
$this->sessionMock = $this->createMock(Session::class);
69+
$this->customerRepositoryMock = $this->createMock(CustomerRepository::class);
70+
$this->addUserInfoToContextMock = $this->createMock(AddUserInfoToContext::class);
71+
$this->graphQlMock = $this->createMock(GraphQl::class);
72+
$this->responseMock = $this->createMock(ResponseInterface::class);
73+
$this->customerMock = $this->createMock(CustomerInterface::class);
74+
75+
$this->clearCustomerSessionAfterRequest = new ClearCustomerSessionAfterRequest(
76+
$this->userContextMock,
77+
$this->sessionMock,
78+
$this->customerRepositoryMock,
79+
$this->addUserInfoToContextMock
80+
);
81+
}
82+
83+
/**
84+
* Test after dispatch plugin
85+
*/
86+
public function testAfterDispatch(): void
87+
{
88+
$this->addUserInfoToContextMock
89+
->expects($this->once())
90+
->method('getLoggedInCustomerData');
91+
92+
$this->clearCustomerSessionAfterRequest->afterDispatch($this->graphQlMock, $this->responseMock);
93+
}
94+
95+
/**
96+
* Test after dispatch plugin for logged in customer
97+
*/
98+
public function testAfterDispatchForLoggedInCustomer(): void
99+
{
100+
$this->addUserInfoToContextMock
101+
->expects($this->once())
102+
->method('getLoggedInCustomerData')
103+
->willReturn($this->customerMock);
104+
$this->customerMock
105+
->expects($this->once())
106+
->method('getId');
107+
$this->customerMock
108+
->expects($this->once())
109+
->method('getGroupId');
110+
111+
$this->clearCustomerSessionAfterRequest->afterDispatch($this->graphQlMock, $this->responseMock);
112+
}
113+
114+
protected function tearDown(): void
115+
{
116+
unset(
117+
$this->clearCustomerSessionAfterRequest,
118+
$this->userContextMock,
119+
$this->sessionMock,
120+
$this->customerRepositoryMock,
121+
$this->addUserInfoToContextMock,
122+
$this->graphQlMock,
123+
$this->responseMock,
124+
$this->customerMock
125+
);
126+
}
127+
}

0 commit comments

Comments
 (0)