Skip to content

Commit f21146e

Browse files
author
Stanislav Idolov
authored
ENGCOM-2083: Removed duplicate line and added comment on variable #16294
2 parents 382465a + f6b5b01 commit f21146e

File tree

3 files changed

+77
-33
lines changed

3 files changed

+77
-33
lines changed

app/code/Magento/Customer/Model/ResourceModel/AddressRepository.php

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
use Magento\Framework\Api\SearchCriteriaInterface;
1414
use Magento\Framework\Api\SortOrder;
1515
use Magento\Framework\Exception\InputException;
16+
use Magento\Framework\App\ObjectManager;
17+
use Magento\Store\Model\ScopeInterface;
1618

1719
/**
1820
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -61,6 +63,16 @@ class AddressRepository implements \Magento\Customer\Api\AddressRepositoryInterf
6163
*/
6264
protected $extensionAttributesJoinProcessor;
6365

66+
/**
67+
* @var \Magento\Directory\Model\AllowedCountries
68+
*/
69+
private $allowedCountriesReader;
70+
71+
/**
72+
* @var \Magento\Customer\Model\Config\Share
73+
*/
74+
private $shareConfig;
75+
6476
/**
6577
* @param \Magento\Customer\Model\AddressFactory $addressFactory
6678
* @param \Magento\Customer\Model\AddressRegistry $addressRegistry
@@ -70,6 +82,10 @@ class AddressRepository implements \Magento\Customer\Api\AddressRepositoryInterf
7082
* @param \Magento\Customer\Api\Data\AddressSearchResultsInterfaceFactory $addressSearchResultsFactory
7183
* @param \Magento\Customer\Model\ResourceModel\Address\CollectionFactory $addressCollectionFactory
7284
* @param \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor
85+
* @param \Magento\Directory\Model\AllowedCountries|null $allowedCountriesReader
86+
* @param \Magento\Customer\Model\Config\Share|null $shareConfig
87+
*
88+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
7389
*/
7490
public function __construct(
7591
\Magento\Customer\Model\AddressFactory $addressFactory,
@@ -79,7 +95,9 @@ public function __construct(
7995
\Magento\Directory\Helper\Data $directoryData,
8096
\Magento\Customer\Api\Data\AddressSearchResultsInterfaceFactory $addressSearchResultsFactory,
8197
\Magento\Customer\Model\ResourceModel\Address\CollectionFactory $addressCollectionFactory,
82-
\Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor
98+
\Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor,
99+
\Magento\Directory\Model\AllowedCountries $allowedCountriesReader = null,
100+
\Magento\Customer\Model\Config\Share $shareConfig = null
83101
) {
84102
$this->addressFactory = $addressFactory;
85103
$this->addressRegistry = $addressRegistry;
@@ -89,6 +107,10 @@ public function __construct(
89107
$this->addressSearchResultsFactory = $addressSearchResultsFactory;
90108
$this->addressCollectionFactory = $addressCollectionFactory;
91109
$this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor;
110+
$this->allowedCountriesReader = $allowedCountriesReader
111+
?: ObjectManager::getInstance()->get(\Magento\Directory\Model\AllowedCountries::class);
112+
$this->shareConfig = $shareConfig
113+
?: ObjectManager::getInstance()->get(\Magento\Customer\Model\Config\Share::class);
92114
}
93115

94116
/**
@@ -290,7 +312,7 @@ private function _validate(CustomerAddressModel $customerAddressModel)
290312
$exception->addError(__('%fieldName is a required field.', ['fieldName' => 'countryId']));
291313
} else {
292314
//Checking if such country exists.
293-
if (!in_array($countryId, $this->directoryData->getCountryCollection()->getAllIds(), true)) {
315+
if (!in_array($countryId, $this->getWebsiteAllowedCountries($customerAddressModel), true)) {
294316
$exception->addError(
295317
__(
296318
'Invalid value of "%value" provided for the %fieldName field.',
@@ -334,4 +356,23 @@ private function _validate(CustomerAddressModel $customerAddressModel)
334356

335357
return $exception;
336358
}
359+
360+
/**
361+
* Return allowed counties per website.
362+
*
363+
* @param \Magento\Customer\Model\Address $customerAddressModel
364+
* @return array
365+
*/
366+
private function getWebsiteAllowedCountries(\Magento\Customer\Model\Address $customerAddressModel)
367+
{
368+
$websiteId = null;
369+
370+
if (!$this->shareConfig->isGlobalScope()) {
371+
$websiteId = $customerAddressModel->getCustomer()
372+
? $customerAddressModel->getCustomer()->getWebsiteId()
373+
: null;
374+
}
375+
376+
return $this->allowedCountriesReader->getAllowedCountries(ScopeInterface::SCOPE_WEBSITE, $websiteId);
377+
}
337378
}

app/code/Magento/Customer/Test/Unit/Model/ResourceModel/AddressRepositoryTest.php

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
namespace Magento\Customer\Test\Unit\Model\ResourceModel;
77

88
use Magento\Customer\Api\Data\AddressInterface as AddressData;
9-
use Magento\Directory\Model\ResourceModel\Country\Collection as Countries;
109
use Magento\Framework\Exception\InputException;
10+
use Magento\Store\Model\ScopeInterface;
1111

1212
/**
1313
* Unit test for Magento\Customer\Model\ResourceModel\AddressRepository
@@ -71,6 +71,16 @@ class AddressRepositoryTest extends \PHPUnit_Framework_TestCase
7171
*/
7272
protected $repository;
7373

74+
/**
75+
* @var \Magento\Directory\Model\AllowedCountries|\PHPUnit_Framework_MockObject_MockObject
76+
*/
77+
private $allowedCountriesReaderMock;
78+
79+
/**
80+
* @var \Magento\Customer\Model\Config\Share|\PHPUnit_Framework_MockObject_MockObject
81+
*/
82+
private $shareConfigMock;
83+
7484
protected function setUp()
7585
{
7686
$this->addressFactory = $this->getMock(
@@ -137,6 +147,26 @@ protected function setUp()
137147
false
138148
);
139149

150+
$this->allowedCountriesReaderMock = $this->getMock(
151+
\Magento\Directory\Model\AllowedCountries::class,
152+
['getAllowedCountries'],
153+
[],
154+
'',
155+
false
156+
);
157+
$this->shareConfigMock = $this->getMock(
158+
\Magento\Customer\Model\Config\Share::class,
159+
['isGlobalScope'],
160+
[],
161+
'',
162+
false
163+
);
164+
$this->shareConfigMock->method('isGlobalScope')->willReturn(false);
165+
$this->allowedCountriesReaderMock
166+
->method('getAllowedCountries')
167+
->with(ScopeInterface::SCOPE_WEBSITE, null)
168+
->willReturn(['1', '2']);
169+
140170
$this->repository = new \Magento\Customer\Model\ResourceModel\AddressRepository(
141171
$this->addressFactory,
142172
$this->addressRegistry,
@@ -145,7 +175,9 @@ protected function setUp()
145175
$this->directoryData,
146176
$this->addressSearchResultsFactory,
147177
$this->addressCollectionFactory,
148-
$this->extensionAttributesJoinProcessor
178+
$this->extensionAttributesJoinProcessor,
179+
$this->allowedCountriesReaderMock,
180+
$this->shareConfigMock
149181
);
150182
}
151183

@@ -315,15 +347,6 @@ public function testSaveWithInvalidRegion()
315347
->method('getRegion')
316348
->willReturn('');
317349

318-
/** @var \PHPUnit_Framework_MockObject_MockObject $countryCollection */
319-
$countryCollection = $this->getMockBuilder(Countries::class)
320-
->disableOriginalConstructor()
321-
->getMock();
322-
$countryCollection->expects($this->once())->method('getAllIds')->willReturn(['1', '2']);
323-
$this->directoryData->expects($this->once())
324-
->method('getCountryCollection')
325-
->willReturn($countryCollection);
326-
327350
$this->repository->save($customerAddress);
328351
}
329352

@@ -408,15 +431,6 @@ public function testSaveWithInvalidRegionId()
408431
->method('getRegion')
409432
->willReturn('');
410433

411-
/** @var \PHPUnit_Framework_MockObject_MockObject $countryCollection */
412-
$countryCollection = $this->getMockBuilder(Countries::class)
413-
->disableOriginalConstructor()
414-
->getMock();
415-
$countryCollection->expects($this->once())->method('getAllIds')->willReturn(['1', '2']);
416-
$this->directoryData->expects($this->once())
417-
->method('getCountryCollection')
418-
->willReturn($countryCollection);
419-
420434
$this->repository->save($customerAddress);
421435
}
422436

@@ -714,15 +728,6 @@ private function prepareAddressData($countryId, $regionId)
714728
$countryModel->expects($this->any())->method('getRegionCollection')->willReturn($regionCollection);
715729
$regionCollection->expects($this->any())->method('getAllIds')->willReturn(['3', '4']);
716730

717-
/** @var \PHPUnit_Framework_MockObject_MockObject $countryCollection */
718-
$countryCollection = $this->getMockBuilder(Countries::class)
719-
->disableOriginalConstructor()
720-
->getMock();
721-
$countryCollection->expects($this->once())->method('getAllIds')->willReturn(['1', '2']);
722-
$this->directoryData->expects($this->once())
723-
->method('getCountryCollection')
724-
->willReturn($countryCollection);
725-
726731
return $customerAddress;
727732
}
728733
}

app/code/Magento/Sales/Block/Adminhtml/Order/Create/Totals/Discount.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
class Discount extends \Magento\Sales\Block\Adminhtml\Order\Create\Totals\DefaultTotals
1717
{
18-
//protected $_template = 'tax/checkout/subtotal.phtml';
19-
2018
/**
2119
* Tax config
2220
*

0 commit comments

Comments
 (0)