Skip to content

Commit 1ba4c87

Browse files
committed
MC-38886: [Zilker] Login as Customer: Review and deliver community PRs
1 parent 6242611 commit 1ba4c87

File tree

6 files changed

+401
-1
lines changed

6 files changed

+401
-1
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
namespace Magento\LoginAsCustomerGraphQl\Model\Resolver;
9+
10+
use Magento\Framework\GraphQl\Config\Element\Field;
11+
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
12+
use Magento\Framework\GraphQl\Query\Resolver\Value;
13+
use Magento\Framework\GraphQl\Query\ResolverInterface;
14+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
15+
use Magento\LoginAsCustomerAssistance\Model\IsAssistanceEnabled;
16+
17+
/**
18+
* Determines if the customer allows remote shopping assistance
19+
*/
20+
class isRemoteShoppingAssistanceAllowed implements ResolverInterface
21+
{
22+
/**
23+
* @var IsAssistanceEnabled
24+
*/
25+
private $isAssistanceEnabled;
26+
27+
/**
28+
* @param IsAssistanceEnabled $isAssistanceEnabled
29+
*/
30+
public function __construct(
31+
IsAssistanceEnabled $isAssistanceEnabled
32+
) {
33+
$this->isAssistanceEnabled = $isAssistanceEnabled;
34+
}
35+
36+
/**
37+
* Determines if remote shopping assistance is allowed for the specified customer
38+
*
39+
* @param Field $field
40+
* @param ContextInterface $context
41+
* @param ResolveInfo $info
42+
* @param array|null $value
43+
* @param array|null $args
44+
* @return Value|mixed|void
45+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
46+
*/
47+
public function resolve(
48+
Field $field,
49+
$context,
50+
ResolveInfo $info,
51+
array $value = null,
52+
array $args = null
53+
) {
54+
return $this->isAssistanceEnabled->execute((int)$value['model']->getId());
55+
}
56+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
8+
namespace Magento\LoginAsCustomerGraphQl\Plugin;
9+
10+
11+
use Magento\Customer\Api\Data\CustomerExtensionFactory;
12+
use Magento\Customer\Api\Data\CustomerInterface;
13+
use Magento\Framework\Api\DataObjectHelper;
14+
use Magento\LoginAsCustomerAssistance\Model\IsAssistanceEnabled;
15+
16+
class DataObjectHelperPlugin
17+
{
18+
/**
19+
* @var CustomerExtensionFactory
20+
*/
21+
private $customerExtensionFactory;
22+
23+
/**
24+
* @param CustomerExtensionFactory $customerExtensionFactory
25+
*/
26+
public function __construct(
27+
CustomerExtensionFactory $customerExtensionFactory
28+
) {
29+
$this->customerExtensionFactory = $customerExtensionFactory;
30+
}
31+
32+
/**
33+
* Add assistance_allowed extension attribute value to Customer instance.
34+
*
35+
* @param DataObjectHelper $subject
36+
* @param DataObjectHelper $result
37+
* @param mixed $dataObject
38+
* @param array $data
39+
* @param string $interfaceName
40+
* @return DataObjectHelper
41+
*
42+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
43+
*/
44+
public function afterPopulateWithArray(
45+
DataObjectHelper $subject,
46+
DataObjectHelper $result,
47+
Object $dataObject,
48+
array $data,
49+
string $interfaceName
50+
) {
51+
if ($interfaceName === CustomerInterface::class
52+
&& array_key_exists('allow_remote_shopping_assistance', $data)) {
53+
$isLoginAsCustomerEnabled = $data['allow_remote_shopping_assistance'];
54+
$extensionAttributes = $dataObject->getExtensionAttributes();
55+
if (null === $extensionAttributes) {
56+
$extensionAttributes = $this->customerExtensionFactory->create();
57+
}
58+
$extensionAttributes->setAssistanceAllowed(
59+
$isLoginAsCustomerEnabled ? IsAssistanceEnabled::ALLOWED : IsAssistanceEnabled::DENIED
60+
);
61+
$dataObject->setExtensionAttributes($extensionAttributes);
62+
}
63+
return $result;
64+
}
65+
}
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" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
9+
<type name="Magento\Framework\Api\DataObjectHelper">
10+
<plugin name="add_allow_remote_shopping_assistance_to_customer"
11+
type="Magento\LoginAsCustomerGraphQl\Plugin\DataObjectHelperPlugin"/>
12+
</type>
13+
</config>

app/code/Magento/LoginAsCustomerGraphQl/etc/schema.graphqls

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ type GenerateCustomerTokenAsAdminOutput {
1818
}
1919

2020
type Customer {
21-
allow_remote_shopping_assistance: Boolean! @doc(description: "Indicates whether the customer has enabled remote shopping assistance")
21+
allow_remote_shopping_assistance: Boolean!
22+
@resolver(class: "Magento\\LoginAsCustomerGraphQl\\Model\\Resolver\\isRemoteShoppingAssistanceAllowed")
23+
@doc(description: "Indicates whether the customer has enabled remote shopping assistance")
2224
}
2325

2426
input CustomerCreateInput {
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
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+
namespace Magento\GraphQl\LoginAsCustomerGraphQl;
9+
10+
use Magento\Customer\Api\CustomerRepositoryInterface;
11+
use Magento\Framework\Registry;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
use Magento\TestFramework\TestCase\GraphQlAbstract;
14+
15+
/**
16+
* Tests for create customer (V2) with allow_remote_shopping_assistance input/output
17+
*/
18+
class CreateCustomerV2Test extends GraphQlAbstract
19+
{
20+
/**
21+
* @var Registry
22+
*/
23+
private $registry;
24+
25+
/**
26+
* @var CustomerRepositoryInterface
27+
*/
28+
private $customerRepository;
29+
30+
protected function setUp(): void
31+
{
32+
parent::setUp();
33+
34+
$this->registry = Bootstrap::getObjectManager()->get(Registry::class);
35+
$this->customerRepository = Bootstrap::getObjectManager()->get(CustomerRepositoryInterface::class);
36+
}
37+
38+
/**
39+
* Test setting allow_remote_shopping_assistance to true
40+
*
41+
* @throws \Exception
42+
*/
43+
public function testCreateCustomerAccountWithAllowTrue()
44+
{
45+
$newFirstname = 'Richard';
46+
$newLastname = 'Rowe';
47+
$currentPassword = 'test123#';
48+
$newEmail = '[email protected]';
49+
50+
$query = <<<QUERY
51+
mutation {
52+
createCustomerV2(
53+
input: {
54+
firstname: "{$newFirstname}"
55+
lastname: "{$newLastname}"
56+
email: "{$newEmail}"
57+
password: "{$currentPassword}"
58+
is_subscribed: true
59+
allow_remote_shopping_assistance: true
60+
}
61+
) {
62+
customer {
63+
id
64+
firstname
65+
lastname
66+
email
67+
is_subscribed
68+
allow_remote_shopping_assistance
69+
}
70+
}
71+
}
72+
QUERY;
73+
$response = $this->graphQlMutation($query);
74+
75+
$this->assertNull($response['createCustomerV2']['customer']['id']);
76+
$this->assertEquals($newFirstname, $response['createCustomerV2']['customer']['firstname']);
77+
$this->assertEquals($newLastname, $response['createCustomerV2']['customer']['lastname']);
78+
$this->assertEquals($newEmail, $response['createCustomerV2']['customer']['email']);
79+
$this->assertTrue($response['createCustomerV2']['customer']['is_subscribed']);
80+
$this->assertTrue($response['createCustomerV2']['customer']['allow_remote_shopping_assistance']);
81+
}
82+
83+
/**
84+
* Test setting allow_remote_shopping_assistance to false
85+
*
86+
* @throws \Exception
87+
*/
88+
public function testCreateCustomerAccountWithAllowFalse()
89+
{
90+
$newFirstname = 'Richard';
91+
$newLastname = 'Rowe';
92+
$currentPassword = 'test123#';
93+
$newEmail = '[email protected]';
94+
95+
$query = <<<QUERY
96+
mutation {
97+
createCustomerV2(
98+
input: {
99+
firstname: "{$newFirstname}"
100+
lastname: "{$newLastname}"
101+
email: "{$newEmail}"
102+
password: "{$currentPassword}"
103+
is_subscribed: true
104+
allow_remote_shopping_assistance: false
105+
}
106+
) {
107+
customer {
108+
id
109+
firstname
110+
lastname
111+
email
112+
is_subscribed
113+
allow_remote_shopping_assistance
114+
}
115+
}
116+
}
117+
QUERY;
118+
$response = $this->graphQlMutation($query);
119+
120+
$this->assertNull($response['createCustomerV2']['customer']['id']);
121+
$this->assertEquals($newFirstname, $response['createCustomerV2']['customer']['firstname']);
122+
$this->assertEquals($newLastname, $response['createCustomerV2']['customer']['lastname']);
123+
$this->assertEquals($newEmail, $response['createCustomerV2']['customer']['email']);
124+
$this->assertTrue($response['createCustomerV2']['customer']['is_subscribed']);
125+
$this->assertFalse($response['createCustomerV2']['customer']['allow_remote_shopping_assistance']);
126+
}
127+
128+
/**
129+
* Test omitting allow_remote_shopping_assistance
130+
*
131+
* @throws \Exception
132+
*/
133+
public function testCreateCustomerAccountWithoutAllow()
134+
{
135+
$newFirstname = 'Richard';
136+
$newLastname = 'Rowe';
137+
$currentPassword = 'test123#';
138+
$newEmail = '[email protected]';
139+
140+
$query = <<<QUERY
141+
mutation {
142+
createCustomerV2(
143+
input: {
144+
firstname: "{$newFirstname}"
145+
lastname: "{$newLastname}"
146+
email: "{$newEmail}"
147+
password: "{$currentPassword}"
148+
is_subscribed: true,
149+
}
150+
) {
151+
customer {
152+
id
153+
firstname
154+
lastname
155+
email
156+
is_subscribed
157+
allow_remote_shopping_assistance
158+
}
159+
}
160+
}
161+
QUERY;
162+
$response = $this->graphQlMutation($query);
163+
164+
$this->assertNull($response['createCustomerV2']['customer']['id']);
165+
$this->assertEquals($newFirstname, $response['createCustomerV2']['customer']['firstname']);
166+
$this->assertEquals($newLastname, $response['createCustomerV2']['customer']['lastname']);
167+
$this->assertEquals($newEmail, $response['createCustomerV2']['customer']['email']);
168+
$this->assertTrue($response['createCustomerV2']['customer']['is_subscribed']);
169+
$this->assertFalse($response['createCustomerV2']['customer']['allow_remote_shopping_assistance']);
170+
}
171+
172+
protected function tearDown(): void
173+
{
174+
$newEmail = '[email protected]';
175+
try {
176+
$customer = $this->customerRepository->get($newEmail);
177+
} catch (\Exception $exception) {
178+
return;
179+
}
180+
181+
$this->registry->unregister('isSecureArea');
182+
$this->registry->register('isSecureArea', true);
183+
$this->customerRepository->delete($customer);
184+
$this->registry->unregister('isSecureArea');
185+
$this->registry->register('isSecureArea', false);
186+
parent::tearDown();
187+
}
188+
}

0 commit comments

Comments
 (0)