Skip to content

Commit abd8347

Browse files
committed
MTA-3787: Add variations to CheckoutWithBraintreePaypalCartTest
- merge commit
2 parents 67ac3ad + ab69a79 commit abd8347

File tree

36 files changed

+1062
-59
lines changed

36 files changed

+1062
-59
lines changed

app/code/Magento/CatalogWidget/Model/Rule/Condition/Combine.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function getNewChildSelectOptions()
8282
public function collectValidatedAttributes($productCollection)
8383
{
8484
foreach ($this->getConditions() as $condition) {
85-
$condition->addToCollection($productCollection);
85+
$condition->collectValidatedAttributes($productCollection);
8686
}
8787
return $this;
8888
}

app/code/Magento/CatalogWidget/Model/Rule/Condition/Product.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,12 @@ public function getMappedSqlField()
223223

224224
return $result;
225225
}
226+
227+
/**
228+
* {@inheritdoc}
229+
*/
230+
public function collectValidatedAttributes($productCollection)
231+
{
232+
return $this->addToCollection($productCollection);
233+
}
226234
}

app/code/Magento/CatalogWidget/Test/Unit/Model/Rule/Condition/CombineTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ public function testCollectValidatedAttributes()
8080
->disableOriginalConstructor()
8181
->getMock();
8282
$condition = $this->getMockBuilder(\Magento\CatalogWidget\Model\Rule\Condition\Combine::class)
83-
->disableOriginalConstructor()->setMethods(['addToCollection'])
83+
->disableOriginalConstructor()->setMethods(['collectValidatedAttributes'])
8484
->getMock();
85-
$condition->expects($this->any())->method('addToCollection')->with($collection)
85+
$condition->expects($this->any())->method('collectValidatedAttributes')->with($collection)
8686
->will($this->returnSelf());
8787

8888
$this->condition->setConditions([$condition]);

app/code/Magento/Checkout/Block/Cart/LayoutProcessor.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,14 @@ public function process($jsLayout)
8585
'visible' => true,
8686
'formElement' => 'select',
8787
'label' => __('Country'),
88-
'options' => $this->countryCollection->loadByStore()->toOptionArray(),
88+
'options' => [],
8989
'value' => null
9090
],
9191
'region_id' => [
9292
'visible' => true,
9393
'formElement' => 'select',
9494
'label' => __('State/Province'),
95-
'options' => $this->regionCollection->load()->toOptionArray(),
95+
'options' => [],
9696
'value' => null
9797
],
9898
'postcode' => [
@@ -103,6 +103,13 @@ public function process($jsLayout)
103103
]
104104
];
105105

106+
if (!isset($jsLayout['components']['checkoutProvider']['dictionaries'])) {
107+
$jsLayout['components']['checkoutProvider']['dictionaries'] = [
108+
'country_id' => $this->countryCollection->loadByStore()->toOptionArray(),
109+
'region_id' => $this->regionCollection->addAllowedCountriesFilter()->toOptionArray(),
110+
];
111+
}
112+
106113
if (isset($jsLayout['components']['block-summary']['children']['block-shipping']['children']
107114
['address-fieldsets']['children'])
108115
) {

app/code/Magento/Checkout/Block/Checkout/AttributeMerger.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,15 @@ protected function getFieldConfig(
192192
'visible' => isset($additionalConfig['visible']) ? $additionalConfig['visible'] : true,
193193
];
194194

195+
if ($attributeCode === 'region_id' || $attributeCode === 'country_id') {
196+
unset($element['options']);
197+
$element['deps'] = [$providerName];
198+
$element['imports'] = [
199+
'initialOptions' => 'index = ' . $providerName . ':dictionaries.' . $attributeCode,
200+
'setOptions' => 'index = ' . $providerName . ':dictionaries.' . $attributeCode
201+
];
202+
}
203+
195204
if (isset($attributeConfig['value']) && $attributeConfig['value'] != null) {
196205
$element['value'] = $attributeConfig['value'];
197206
} elseif (isset($attributeConfig['default']) && $attributeConfig['default'] != null) {
@@ -341,18 +350,19 @@ protected function getCustomer()
341350
* @param string $attributeCode
342351
* @param array $attributeConfig
343352
* @return array
353+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
344354
*/
345355
protected function getFieldOptions($attributeCode, array $attributeConfig)
346356
{
347-
$options = isset($attributeConfig['options']) ? $attributeConfig['options'] : [];
348-
return ($attributeCode == 'country_id') ? $this->orderCountryOptions($options) : $options;
357+
return isset($attributeConfig['options']) ? $attributeConfig['options'] : [];
349358
}
350359

351360
/**
352361
* Order country options. Move top countries to the beginning of the list.
353362
*
354363
* @param array $countryOptions
355364
* @return array
365+
* @deprecated
356366
*/
357367
protected function orderCountryOptions(array $countryOptions)
358368
{
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Checkout\Block\Checkout;
7+
8+
use Magento\Directory\Helper\Data as DirectoryHelper;
9+
use Magento\Store\Api\StoreResolverInterface;
10+
11+
/**
12+
* Directory data processor.
13+
*
14+
* This class adds various country and region dictionaries to checkout page.
15+
* This data can be used by other UI components during checkout flow.
16+
*/
17+
class DirectoryDataProcessor implements \Magento\Checkout\Block\Checkout\LayoutProcessorInterface
18+
{
19+
/**
20+
* @var array
21+
*/
22+
private $countryOptions;
23+
24+
/**
25+
* @var array
26+
*/
27+
private $regionOptions;
28+
29+
/**
30+
* @var \Magento\Directory\Model\ResourceModel\Region\CollectionFactory
31+
*/
32+
private $regionCollectionFactory;
33+
34+
/**
35+
* @var \Magento\Directory\Model\ResourceModel\Region\CollectionFactory
36+
*/
37+
private $countryCollectionFactory;
38+
39+
/**
40+
* @var StoreResolverInterface
41+
*/
42+
private $storeResolver;
43+
44+
/**
45+
* @var DirectoryHelper
46+
*/
47+
private $directoryHelper;
48+
49+
/**
50+
* @param \Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countryCollection
51+
* @param \Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regionCollection
52+
* @param StoreResolverInterface $storeResolver
53+
* @param DirectoryHelper $directoryHelper
54+
*/
55+
public function __construct(
56+
\Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countryCollection,
57+
\Magento\Directory\Model\ResourceModel\Region\CollectionFactory $regionCollection,
58+
StoreResolverInterface $storeResolver,
59+
DirectoryHelper $directoryHelper
60+
) {
61+
$this->countryCollectionFactory = $countryCollection;
62+
$this->regionCollectionFactory = $regionCollection;
63+
$this->storeResolver = $storeResolver;
64+
$this->directoryHelper = $directoryHelper;
65+
}
66+
67+
/**
68+
* Process js Layout of block
69+
*
70+
* @param array $jsLayout
71+
* @return array
72+
*/
73+
public function process($jsLayout)
74+
{
75+
if (!isset($jsLayout['components']['checkoutProvider']['dictionaries'])) {
76+
$jsLayout['components']['checkoutProvider']['dictionaries'] = [
77+
'country_id' => $this->getCountryOptions(),
78+
'region_id' => $this->getRegionOptions(),
79+
];
80+
}
81+
82+
return $jsLayout;
83+
}
84+
85+
/**
86+
* Get country options list.
87+
*
88+
* @return array
89+
*/
90+
private function getCountryOptions()
91+
{
92+
if (!isset($this->countryOptions)) {
93+
$this->countryOptions = $this->countryCollectionFactory->create()->loadByStore(
94+
$this->storeResolver->getCurrentStoreId()
95+
)->toOptionArray();
96+
$this->countryOptions = $this->orderCountryOptions($this->countryOptions);
97+
}
98+
99+
return $this->countryOptions;
100+
}
101+
102+
/**
103+
* Get region options list.
104+
*
105+
* @return array
106+
*/
107+
private function getRegionOptions()
108+
{
109+
if (!isset($this->regionOptions)) {
110+
$this->regionOptions = $this->regionCollectionFactory->create()->addAllowedCountriesFilter(
111+
$this->storeResolver->getCurrentStoreId()
112+
)->toOptionArray();
113+
}
114+
115+
return $this->regionOptions;
116+
}
117+
118+
/**
119+
* Sort country options by top country codes.
120+
*
121+
* @param array $countryOptions
122+
* @return array
123+
*/
124+
private function orderCountryOptions(array $countryOptions)
125+
{
126+
$topCountryCodes = $this->directoryHelper->getTopCountryCodes();
127+
if (empty($topCountryCodes)) {
128+
return $countryOptions;
129+
}
130+
131+
$headOptions = [];
132+
$tailOptions = [[
133+
'value' => 'delimiter',
134+
'label' => '──────────',
135+
'disabled' => true,
136+
]];
137+
foreach ($countryOptions as $countryOption) {
138+
if (empty($countryOption['value']) || in_array($countryOption['value'], $topCountryCodes)) {
139+
array_push($headOptions, $countryOption);
140+
} else {
141+
array_push($tailOptions, $countryOption);
142+
}
143+
}
144+
return array_merge($headOptions, $tailOptions);
145+
}
146+
}

app/code/Magento/Checkout/Block/Checkout/LayoutProcessor.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77

88
use Magento\Checkout\Helper\Data;
99
use Magento\Framework\App\ObjectManager;
10+
use Magento\Store\Api\StoreResolverInterface;
1011

12+
/**
13+
* Class LayoutProcessor
14+
*/
1115
class LayoutProcessor implements \Magento\Checkout\Block\Checkout\LayoutProcessorInterface
1216
{
1317
/**
@@ -35,6 +39,16 @@ class LayoutProcessor implements \Magento\Checkout\Block\Checkout\LayoutProcesso
3539
*/
3640
private $checkoutDataHelper;
3741

42+
/**
43+
* @var StoreResolverInterface
44+
*/
45+
private $storeResolver;
46+
47+
/**
48+
* @var \Magento\Shipping\Model\Config
49+
*/
50+
private $shippingConfig;
51+
3852
/**
3953
* @param \Magento\Customer\Model\AttributeMetadataDataProvider $attributeMetadataDataProvider
4054
* @param \Magento\Ui\Component\Form\AttributeMapper $attributeMapper
@@ -146,6 +160,16 @@ public function process($jsLayout)
146160
$elements
147161
);
148162
}
163+
if (isset($jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
164+
['step-config']['children']['shipping-rates-validation']['children']
165+
)) {
166+
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
167+
['step-config']['children']['shipping-rates-validation']['children'] =
168+
$this->processShippingChildrenComponents(
169+
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
170+
['step-config']['children']['shipping-rates-validation']['children']
171+
);
172+
}
149173

150174
if (isset($jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']
151175
['children']['shippingAddress']['children']['shipping-address-fieldset']['children']
@@ -163,6 +187,26 @@ public function process($jsLayout)
163187
return $jsLayout;
164188
}
165189

190+
/**
191+
* Process shipping configuration to exclude inactive carriers.
192+
*
193+
* @param array $shippingRatesLayout
194+
* @return array
195+
*/
196+
private function processShippingChildrenComponents($shippingRatesLayout)
197+
{
198+
$activeCarriers = $this->getShippingConfig()->getActiveCarriers(
199+
$this->getStoreResolver()->getCurrentStoreId()
200+
);
201+
foreach (array_keys($shippingRatesLayout) as $carrierName) {
202+
$carrierKey = str_replace('-rates-validation', '', $carrierName);
203+
if (!array_key_exists($carrierKey, $activeCarriers)) {
204+
unset($shippingRatesLayout[$carrierName]);
205+
}
206+
}
207+
return $shippingRatesLayout;
208+
}
209+
166210
/**
167211
* Appends billing address form component to payment layout
168212
* @param array $paymentLayout
@@ -314,4 +358,34 @@ private function getCheckoutDataHelper()
314358

315359
return $this->checkoutDataHelper;
316360
}
361+
362+
/**
363+
* Retrieve Shipping Configuration.
364+
*
365+
* @return \Magento\Shipping\Model\Config
366+
* @deprecated
367+
*/
368+
private function getShippingConfig()
369+
{
370+
if (!$this->shippingConfig) {
371+
$this->shippingConfig = ObjectManager::getInstance()->get(\Magento\Shipping\Model\Config::class);
372+
}
373+
374+
return $this->shippingConfig;
375+
}
376+
377+
/**
378+
* Get store resolver.
379+
*
380+
* @return StoreResolverInterface
381+
* @deprecated
382+
*/
383+
private function getStoreResolver()
384+
{
385+
if (!$this->storeResolver) {
386+
$this->storeResolver = ObjectManager::getInstance()->get(StoreResolverInterface::class);
387+
}
388+
389+
return $this->storeResolver;
390+
}
317391
}

app/code/Magento/Checkout/Test/Unit/Block/Cart/LayoutProcessorTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,20 @@ public function testProcess()
6969
$this->countryCollection->expects($this->once())->method('loadByStore')->willReturnSelf();
7070
$this->countryCollection->expects($this->once())->method('toOptionArray')->willReturn($countries);
7171

72-
$this->regionCollection->expects($this->once())->method('load')->willReturnSelf();
72+
$this->regionCollection->expects($this->once())->method('addAllowedCountriesFilter')->willReturnSelf();
7373
$this->regionCollection->expects($this->once())->method('toOptionArray')->willReturn($regions);
7474

7575
$layoutMerged = $layout;
7676
$layoutMerged['components']['block-summary']['children']['block-shipping']['children']
7777
['address-fieldsets']['children']['fieldThree'] = ['param' => 'value'];
7878
$layoutMergedPointer = &$layoutMerged['components']['block-summary']['children']['block-shipping']
7979
['children']['address-fieldsets']['children'];
80-
80+
$layoutMerged['components']['checkoutProvider'] = [
81+
'dictionaries' => [
82+
'country_id' => [],
83+
'region_id' => [],
84+
]
85+
];
8186
$elements = [
8287
'city' => [
8388
'visible' => false,

0 commit comments

Comments
 (0)