Skip to content

Commit 44dcacf

Browse files
committed
Merge branch 'MC-42903' of https://github.com/magento-l3/magento2ce into PR-2021-08-18
2 parents 7d58f1e + aa9f5f1 commit 44dcacf

File tree

7 files changed

+329
-18
lines changed

7 files changed

+329
-18
lines changed

app/code/Magento/Customer/Model/Config/Backend/Show/Customer.php

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\Customer\Model\Config\Backend\Show;
77

88
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
9+
use Magento\Framework\App\Config\ScopeConfigInterface;
910

1011
/**
1112
* Customer Show Customer Model
@@ -24,6 +25,21 @@ class Customer extends \Magento\Framework\App\Config\Value
2425
*/
2526
protected $storeManager;
2627

28+
/**
29+
* @var string
30+
*/
31+
private $telephoneShowDefaultValue = 'req';
32+
33+
/**
34+
* @var array
35+
*/
36+
private $valueConfig = [
37+
'' => ['is_required' => 0, 'is_visible' => 0],
38+
'opt' => ['is_required' => 0, 'is_visible' => 1],
39+
'1' => ['is_required' => 0, 'is_visible' => 1],
40+
'req' => ['is_required' => 1, 'is_visible' => 1],
41+
];
42+
2743
/**
2844
* @param \Magento\Framework\Model\Context $context
2945
* @param \Magento\Framework\Registry $registry
@@ -80,20 +96,8 @@ public function afterSave()
8096
{
8197
$result = parent::afterSave();
8298

83-
$valueConfig = [
84-
'' => ['is_required' => 0, 'is_visible' => 0],
85-
'opt' => ['is_required' => 0, 'is_visible' => 1],
86-
'1' => ['is_required' => 0, 'is_visible' => 1],
87-
'req' => ['is_required' => 1, 'is_visible' => 1],
88-
];
89-
9099
$value = $this->getValue();
91-
if (isset($valueConfig[$value])) {
92-
$data = $valueConfig[$value];
93-
} else {
94-
$data = $valueConfig[''];
95-
}
96-
100+
$data = $this->getValueConfig($value);
97101
if ($this->getScope() == 'websites') {
98102
$website = $this->storeManager->getWebsite($this->getScopeCode());
99103
$dataFieldPrefix = 'scope_';
@@ -133,8 +137,31 @@ public function afterDelete()
133137
$attributeObject->setData('scope_is_visible', null);
134138
$attributeObject->save();
135139
}
140+
} else if ($this->getScope() == ScopeConfigInterface::SCOPE_TYPE_DEFAULT) {
141+
$valueConfig = $this->getValueConfig($this->telephoneShowDefaultValue);
142+
foreach ($this->_getAttributeObjects() as $attributeObject) {
143+
$attributeObject->setData('is_required', $valueConfig['is_required']);
144+
$attributeObject->setData('is_visible', $valueConfig['is_visible']);
145+
$attributeObject->save();
146+
}
136147
}
137148

138149
return $result;
139150
}
151+
152+
/**
153+
* Get value config
154+
*
155+
* @param string|int $value
156+
* @return array
157+
*/
158+
private function getValueConfig($value): array
159+
{
160+
if (isset($this->valueConfig[$value])) {
161+
$config = $this->valueConfig[$value];
162+
} else {
163+
$config = $this->valueConfig[''];
164+
}
165+
return $config;
166+
}
140167
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ input CartAddressInput {
118118
region_id: Int
119119
postcode: String
120120
country_code: String!
121-
telephone: String!
121+
telephone: String
122122
save_in_address_book: Boolean @doc(description: "Determines whether to save the address in the customer's address book. The default value is true")
123123
}
124124

@@ -224,7 +224,7 @@ interface CartAddressInterface @typeResolver(class: "\\Magento\\QuoteGraphQl\\Mo
224224
region: CartAddressRegion
225225
postcode: String
226226
country: CartAddressCountry!
227-
telephone: String!
227+
telephone: String
228228
}
229229

230230
type ShippingCartAddress implements CartAddressInterface {

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

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,13 @@ private function getQuery(string $maskedQuoteId): string
164164
company
165165
street
166166
city
167-
region
167+
region
168168
{
169169
code
170170
label
171171
}
172172
postcode
173-
country
173+
country
174174
{
175175
code
176176
label
@@ -195,4 +195,44 @@ private function getHeaderMap(string $username = 'customer@example.com', string
195195
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
196196
return $headerMap;
197197
}
198+
199+
/**
200+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
201+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
202+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
203+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
204+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address_without_telephone.php
205+
*/
206+
public function testGetSpecifiedBillingAddressWithoutTelephone()
207+
{
208+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
209+
$query = $this->getQuery($maskedQuoteId);
210+
211+
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
212+
self::assertArrayHasKey('cart', $response);
213+
self::assertArrayHasKey('billing_address', $response['cart']);
214+
215+
$expectedBillingAddressData = [
216+
'firstname' => 'John',
217+
'lastname' => 'Smith',
218+
'company' => 'CompanyName',
219+
'street' => [
220+
'Green str, 67'
221+
],
222+
'city' => 'CityM',
223+
'region' => [
224+
'code' => 'AL',
225+
'label' => 'Alabama',
226+
],
227+
'postcode' => '75477',
228+
'country' => [
229+
'code' => 'US',
230+
'label' => 'US',
231+
],
232+
'telephone' => '',
233+
'__typename' => 'BillingCartAddress',
234+
'customer_notes' => null,
235+
];
236+
self::assertEquals($expectedBillingAddressData, $response['cart']['billing_address']);
237+
}
198238
}

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

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ public function testSetNewBillingAddressWithoutCustomerAddressIdAndAddress()
418418
QUERY;
419419

420420
self::expectExceptionMessage(
421-
'The billing address must contain either "customer_address_id", "address", or "same_as_shipping".'
421+
'The billing address must contain either "customer_address_id", "address", or "same_as_shipping".'
422422
);
423423
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
424424
}
@@ -1919,4 +1919,106 @@ public function testSetBillingAddressAndPlaceOrderWithGuestCheckoutDisabled()
19191919
{
19201920
$this->testSetBillingAddressAndPlaceOrder();
19211921
}
1922+
1923+
/**
1924+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
1925+
* @magentoApiDataFixture Magento/Customer/_files/attribute_telephone_not_required_address.php
1926+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
1927+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
1928+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
1929+
*/
1930+
public function testSetNewBillingAddressWithoutTelephone()
1931+
{
1932+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
1933+
1934+
$query = <<<QUERY
1935+
mutation {
1936+
setBillingAddressOnCart(
1937+
input: {
1938+
cart_id: "$maskedQuoteId"
1939+
billing_address: {
1940+
address: {
1941+
firstname: "test firstname"
1942+
lastname: "test lastname"
1943+
company: "test company"
1944+
street: ["test street 1", "test street 2"]
1945+
city: "test city"
1946+
region: "AZ"
1947+
postcode: "887766"
1948+
country_code: "US"
1949+
telephone: ""
1950+
}
1951+
use_for_shipping: true
1952+
}
1953+
}
1954+
) {
1955+
cart {
1956+
billing_address {
1957+
firstname
1958+
lastname
1959+
company
1960+
street
1961+
city
1962+
postcode
1963+
telephone
1964+
country {
1965+
code
1966+
label
1967+
}
1968+
__typename
1969+
}
1970+
shipping_addresses {
1971+
firstname
1972+
lastname
1973+
company
1974+
street
1975+
city
1976+
postcode
1977+
telephone
1978+
country {
1979+
code
1980+
label
1981+
}
1982+
__typename
1983+
}
1984+
}
1985+
}
1986+
}
1987+
QUERY;
1988+
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
1989+
1990+
self::assertArrayHasKey('cart', $response['setBillingAddressOnCart']);
1991+
$cartResponse = $response['setBillingAddressOnCart']['cart'];
1992+
self::assertArrayHasKey('billing_address', $cartResponse);
1993+
$billingAddressResponse = $cartResponse['billing_address'];
1994+
self::assertArrayHasKey('shipping_addresses', $cartResponse);
1995+
$shippingAddressResponse = current($cartResponse['shipping_addresses']);
1996+
$this->assertNewAddressWithoutTelephone($billingAddressResponse);
1997+
$this->assertNewAddressWithoutTelephone($shippingAddressResponse, 'ShippingCartAddress');
1998+
}
1999+
2000+
/**
2001+
* Verify the all the whitelisted fields for a New Address Object without telephone
2002+
*
2003+
* @param array $addressResponse
2004+
* @param string $addressType
2005+
*/
2006+
private function assertNewAddressWithoutTelephone(
2007+
array $addressResponse,
2008+
string $addressType = 'BillingCartAddress'
2009+
): void {
2010+
$assertionMap = [
2011+
['response_field' => 'firstname', 'expected_value' => 'test firstname'],
2012+
['response_field' => 'lastname', 'expected_value' => 'test lastname'],
2013+
['response_field' => 'company', 'expected_value' => 'test company'],
2014+
['response_field' => 'street', 'expected_value' => [0 => 'test street 1', 1 => 'test street 2']],
2015+
['response_field' => 'city', 'expected_value' => 'test city'],
2016+
['response_field' => 'postcode', 'expected_value' => '887766'],
2017+
['response_field' => 'telephone', 'expected_value' => ''],
2018+
['response_field' => 'country', 'expected_value' => ['code' => 'US', 'label' => 'US']],
2019+
['response_field' => '__typename', 'expected_value' => $addressType]
2020+
];
2021+
2022+
$this->assertResponseFields($addressResponse, $assertionMap);
2023+
}
19222024
}

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

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1963,4 +1963,85 @@ public function testSetNewShippingAddressAndPlaceOrderWithGuestCheckoutDisabled(
19631963
{
19641964
$this->testSetNewShippingAddressAndPlaceOrder();
19651965
}
1966+
1967+
/**
1968+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
1969+
* @magentoApiDataFixture Magento/Customer/_files/attribute_telephone_not_required_address.php
1970+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
1971+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
1972+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
1973+
*/
1974+
public function testSetNewShippingAddressWithoutTelephone()
1975+
{
1976+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
1977+
1978+
$query = <<<QUERY
1979+
mutation {
1980+
setShippingAddressesOnCart(
1981+
input: {
1982+
cart_id: "$maskedQuoteId"
1983+
shipping_addresses: {
1984+
address: {
1985+
firstname: "test firstname"
1986+
lastname: "test lastname"
1987+
company: "test company"
1988+
street: ["test street 1", "test street 2"]
1989+
city: "test city"
1990+
region: "AZ"
1991+
postcode: "887766"
1992+
country_code: "US"
1993+
telephone: ""
1994+
}
1995+
}
1996+
}
1997+
) {
1998+
cart {
1999+
shipping_addresses {
2000+
firstname
2001+
lastname
2002+
company
2003+
street
2004+
city
2005+
postcode
2006+
telephone
2007+
country {
2008+
code
2009+
label
2010+
}
2011+
__typename
2012+
}
2013+
}
2014+
}
2015+
}
2016+
QUERY;
2017+
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
2018+
self::assertArrayHasKey('cart', $response['setShippingAddressesOnCart']);
2019+
$cartResponse = $response['setShippingAddressesOnCart']['cart'];
2020+
self::assertArrayHasKey('shipping_addresses', $cartResponse);
2021+
$shippingAddressResponse = current($cartResponse['shipping_addresses']);
2022+
$this->assertNewAddressWithoutTelephone($shippingAddressResponse);
2023+
}
2024+
2025+
/**
2026+
* Verify the all the whitelisted fields for a New Address Object without telephone
2027+
*
2028+
* @param array $addressResponse
2029+
*/
2030+
private function assertNewAddressWithoutTelephone(
2031+
array $addressResponse
2032+
): void {
2033+
$assertionMap = [
2034+
['response_field' => 'firstname', 'expected_value' => 'test firstname'],
2035+
['response_field' => 'lastname', 'expected_value' => 'test lastname'],
2036+
['response_field' => 'company', 'expected_value' => 'test company'],
2037+
['response_field' => 'street', 'expected_value' => [0 => 'test street 1', 1 => 'test street 2']],
2038+
['response_field' => 'city', 'expected_value' => 'test city'],
2039+
['response_field' => 'postcode', 'expected_value' => '887766'],
2040+
['response_field' => 'telephone', 'expected_value' => ''],
2041+
['response_field' => 'country', 'expected_value' => ['code' => 'US', 'label' => 'US']],
2042+
['response_field' => '__typename', 'expected_value' => 'ShippingCartAddress']
2043+
];
2044+
2045+
$this->assertResponseFields($addressResponse, $assertionMap);
2046+
}
19662047
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
use Magento\TestFramework\Helper\Bootstrap;
8+
use Magento\Customer\Model\Attribute;
9+
use Magento\Eav\Model\AttributeRepository;
10+
11+
/** @var Attribute $model */
12+
$attribute = Bootstrap::getObjectManager()->create(Attribute::class);
13+
/** @var AttributeRepository $attributeRepository */
14+
$attributeRepository = Bootstrap::getObjectManager()->create(AttributeRepository::class);
15+
$attribute->loadByCode('customer_address', 'telephone');
16+
$attribute->setIsRequired(false);
17+
$attributeRepository->save($attribute);

0 commit comments

Comments
 (0)