Skip to content

Commit fab00b6

Browse files
MC-29999: A site with optional telephone will sometimes require one
1 parent 90a479f commit fab00b6

File tree

3 files changed

+94
-12
lines changed

3 files changed

+94
-12
lines changed

app/code/Magento/Customer/Model/Address/Validator/General.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\Customer\Model\Address\Validator;
77

8+
use Magento\Customer\Api\AddressMetadataInterface;
89
use Magento\Customer\Model\Address\AbstractAddress;
910
use Magento\Customer\Model\Address\ValidatorInterface;
1011

@@ -87,6 +88,7 @@ private function checkRequiredFields(AbstractAddress $address)
8788
*/
8889
private function checkOptionalFields(AbstractAddress $address)
8990
{
91+
$this->reloadAddressAttributes($address);
9092
$errors = [];
9193
if ($this->isTelephoneRequired()
9294
&& !\Zend_Validate::is($address->getTelephone(), 'NotEmpty')
@@ -148,4 +150,17 @@ private function isFaxRequired()
148150
{
149151
return $this->eavConfig->getAttribute('customer_address', 'fax')->getIsRequired();
150152
}
153+
154+
/**
155+
* Reload address attributes for the certain store
156+
*
157+
* @param AbstractAddress $address
158+
* @return void
159+
*/
160+
private function reloadAddressAttributes(AbstractAddress $address): void
161+
{
162+
$attributeSetId = $address->getAttributeSetId() ?: AddressMetadataInterface::ATTRIBUTE_SET_ID_ADDRESS;
163+
$address->setData('attribute_set_id', $attributeSetId);
164+
$this->eavConfig->getEntityAttributes(AddressMetadataInterface::ENTITY_TYPE_ADDRESS, $address);
165+
}
151166
}

app/code/Magento/Eav/Model/Config.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,8 @@ private function loadAttributes($entityTypeCode)
245245
/**
246246
* Associate object with identifier
247247
*
248-
* @param mixed $obj
249-
* @param mixed $id
248+
* @param mixed $obj
249+
* @param mixed $id
250250
* @return void
251251
* @codeCoverageIgnore
252252
*/
@@ -271,8 +271,8 @@ private function saveAttribute(AbstractAttribute $attribute, $entityTypeCode, $a
271271
/**
272272
* Specify reference for entity type id
273273
*
274-
* @param int $id
275-
* @param string $code
274+
* @param int $id
275+
* @param string $code
276276
* @return $this
277277
* @codeCoverageIgnore
278278
*/
@@ -296,9 +296,9 @@ protected function _getEntityTypeReference($id)
296296
/**
297297
* Specify reference between entity attribute id and attribute code
298298
*
299-
* @param int $id
300-
* @param string $code
301-
* @param string $entityTypeCode
299+
* @param int $id
300+
* @param string $code
301+
* @param string $entityTypeCode
302302
* @return $this
303303
*/
304304
protected function _addAttributeReference($id, $code, $entityTypeCode)
@@ -522,9 +522,9 @@ public function getAttributes($entityType)
522522
/**
523523
* Get attribute by code for entity type
524524
*
525-
* @param mixed $entityType
526-
* @param mixed $code
527-
* @return AbstractAttribute
525+
* @param mixed $entityType
526+
* @param mixed $code
527+
* @return AbstractAttribute
528528
* @throws LocalizedException
529529
*/
530530
public function getAttribute($entityType, $code)
@@ -737,8 +737,8 @@ public function getEntityAttributeCodes($entityType, $object = null)
737737
/**
738738
* Get all entity type attributes
739739
*
740-
* @param int|string|Type $entityType
741-
* @param \Magento\Framework\DataObject|null $object
740+
* @param int|string|Type $entityType
741+
* @param \Magento\Framework\DataObject|null $object
742742
* @return AbstractAttribute[]
743743
*
744744
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
@@ -822,6 +822,10 @@ protected function _createAttribute($entityType, $attributeData)
822822
$fullAttributeData = array_key_exists('is_required', $attributeData);
823823

824824
if ($existsFullAttribute || (!$existsFullAttribute && !$fullAttributeData)) {
825+
$scopeIsRequired = $attributeData['scope_is_required'] ?? null;
826+
if ($scopeIsRequired !== null) {
827+
$attribute->setData('scope_is_required', $scopeIsRequired);
828+
}
825829
return $attribute;
826830
}
827831
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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\Customer\Model;
9+
10+
use Magento\Customer\Model\Metadata\AddressMetadata;
11+
use Magento\Eav\Model\Config;
12+
use Magento\Store\Model\StoreManagerInterface;
13+
use Magento\TestFramework\Helper\Bootstrap;
14+
15+
class CustomerAddressAttributeTest extends \PHPUnit\Framework\TestCase
16+
{
17+
/**
18+
* @var Config
19+
*/
20+
private $config;
21+
22+
/**
23+
* @var StoreManagerInterface
24+
*/
25+
private $storeManager;
26+
27+
/**
28+
* @inheritdoc
29+
*/
30+
protected function setUp()
31+
{
32+
$objectManager = Bootstrap::getObjectManager();
33+
$this->config = $objectManager->get(Config::class);
34+
$this->storeManager = $objectManager->get(StoreManagerInterface::class);
35+
}
36+
37+
/**
38+
* Tests cached scope_is_required attribute value for a certain website
39+
*
40+
* @return void
41+
* @magentoDataFixture Magento/Store/_files/second_website_with_two_stores.php
42+
*/
43+
public function testGetScopeIsRequiredAttributeValueFromCache(): void
44+
{
45+
$attributeCode = 'telephone';
46+
$entityType = AddressMetadata::ENTITY_TYPE_ADDRESS;
47+
$attribute = $this->config->getAttribute($entityType, $attributeCode);
48+
$currentStore = $this->storeManager->getStore();
49+
$secondWebsite = $this->storeManager->getWebsite('test');
50+
$attribute->setWebsite($secondWebsite->getId());
51+
$attribute->setData('scope_is_required', '0');
52+
$attribute->save();
53+
$this->config->getAttribute($entityType, $attributeCode);
54+
$this->storeManager->setCurrentStore('fixture_second_store');
55+
try {
56+
$this->config->getEntityAttributes($attribute->getEntityTypeId(), $attribute);
57+
$scopeAttribute = $this->config->getAttribute($entityType, $attributeCode);
58+
$this->assertEquals(0, $scopeAttribute->getIsRequired());
59+
} finally {
60+
$this->storeManager->setCurrentStore($currentStore);
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)