Skip to content

Commit a6e663e

Browse files
authored
Merge pull request #4657 from magento-tsg/2.3.3-develop-pr69
[TSG] Fixes for 2.3 (pr69) (2.3.3-develop)
2 parents 88b6bcd + bc581c8 commit a6e663e

File tree

8 files changed

+276
-24
lines changed

8 files changed

+276
-24
lines changed

app/code/Magento/Braintree/view/frontend/web/js/view/payment/method-renderer/cc-form.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,10 @@ define(
246246
return;
247247
}
248248

249-
self.setPaymentPayload(payload);
250-
self.placeOrder();
249+
if (self.validateCardType()) {
250+
self.setPaymentPayload(payload);
251+
self.placeOrder();
252+
}
251253
});
252254
}
253255
},

app/code/Magento/Shipping/Model/Shipping.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Shipping\Model;
78

89
use Magento\Framework\App\ObjectManager;
@@ -272,7 +273,9 @@ public function collectRates(\Magento\Quote\Model\Quote\Address\RateRequest $req
272273
*/
273274
private function prepareCarrier(string $carrierCode, RateRequest $request): AbstractCarrier
274275
{
275-
$carrier = $this->_carrierFactory->create($carrierCode, $request->getStoreId());
276+
$carrier = $this->isShippingCarrierAvailable($carrierCode)
277+
? $this->_carrierFactory->create($carrierCode, $request->getStoreId())
278+
: null;
276279
if (!$carrier) {
277280
throw new \RuntimeException('Failed to initialize carrier');
278281
}
@@ -360,6 +363,7 @@ public function composePackagesForCarrier($carrier, $request)
360363
{
361364
$allItems = $request->getAllItems();
362365
$fullItems = [];
366+
$weightItems = [];
363367

364368
$maxWeight = (double)$carrier->getConfigData('max_package_weight');
365369

@@ -425,15 +429,13 @@ public function composePackagesForCarrier($carrier, $request)
425429

426430
if (!empty($decimalItems)) {
427431
foreach ($decimalItems as $decimalItem) {
428-
$fullItems = array_merge(
429-
$fullItems,
430-
array_fill(0, $decimalItem['qty'] * $qty, $decimalItem['weight'])
431-
);
432+
$weightItems[] = array_fill(0, $decimalItem['qty'] * $qty, $decimalItem['weight']);
432433
}
433434
} else {
434-
$fullItems = array_merge($fullItems, array_fill(0, $qty, $itemWeight));
435+
$weightItems[] = array_fill(0, $qty, $itemWeight);
435436
}
436437
}
438+
$fullItems = array_merge($fullItems, ...$weightItems);
437439
sort($fullItems);
438440

439441
return $this->_makePieces($fullItems, $maxWeight);
@@ -532,4 +534,18 @@ public function setCarrierAvailabilityConfigField($code = 'active')
532534
$this->_availabilityConfigField = $code;
533535
return $this;
534536
}
537+
538+
/**
539+
* Checks availability of carrier.
540+
*
541+
* @param string $carrierCode
542+
* @return bool
543+
*/
544+
private function isShippingCarrierAvailable(string $carrierCode): bool
545+
{
546+
return $this->_scopeConfig->isSetFlag(
547+
'carriers/' . $carrierCode . '/' . $this->_availabilityConfigField,
548+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
549+
);
550+
}
535551
}

app/code/Magento/Shipping/Test/Unit/Model/ShippingTest.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Shipping\Test\Unit\Model;
78

89
use Magento\Catalog\Model\Product;
910
use Magento\Catalog\Model\Product\Type as ProductType;
1011
use Magento\CatalogInventory\Model\Stock\Item as StockItem;
1112
use Magento\CatalogInventory\Model\StockRegistry;
13+
use Magento\Framework\App\Config\ScopeConfigInterface;
1214
use Magento\Quote\Model\Quote\Item as QuoteItem;
1315
use Magento\Shipping\Model\Carrier\AbstractCarrierInterface;
1416
use Magento\Shipping\Model\CarrierFactory;
@@ -19,12 +21,14 @@
1921
use PHPUnit_Framework_MockObject_MockObject as MockObject;
2022

2123
/**
22-
* @see Shipping
24+
* Unit tests for \Magento\Shipping\Model\Shipping class.
25+
*
26+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2327
*/
2428
class ShippingTest extends \PHPUnit\Framework\TestCase
2529
{
2630
/**
27-
* Test identification number of product
31+
* Test identification number of product.
2832
*
2933
* @var int
3034
*/
@@ -50,22 +54,34 @@ class ShippingTest extends \PHPUnit\Framework\TestCase
5054
*/
5155
private $carrier;
5256

57+
/**
58+
* @var ScopeConfigInterface|MockObject
59+
*/
60+
private $scopeConfig;
61+
62+
/**
63+
* @inheritdoc
64+
*/
5365
protected function setUp()
5466
{
5567
$this->stockRegistry = $this->createMock(StockRegistry::class);
5668
$this->stockItemData = $this->createMock(StockItem::class);
69+
$this->scopeConfig = $this->createMock(ScopeConfigInterface::class);
5770

5871
$this->shipping = (new ObjectManagerHelper($this))->getObject(
5972
Shipping::class,
6073
[
6174
'stockRegistry' => $this->stockRegistry,
6275
'carrierFactory' => $this->getCarrierFactory(),
76+
'scopeConfig' => $this->scopeConfig,
6377
]
6478
);
6579
}
6680

6781
/**
82+
* Compose Packages For Carrier.
6883
*
84+
* @return void
6985
*/
7086
public function testComposePackages()
7187
{
@@ -125,14 +141,25 @@ function ($key) {
125141

126142
/**
127143
* Active flag should be set before collecting carrier rates.
144+
*
145+
* @return void
128146
*/
129147
public function testCollectCarrierRatesSetActiveFlag()
130148
{
149+
$carrierCode = 'carrier';
150+
$scopeStore = 'store';
151+
$this->scopeConfig->expects($this->once())
152+
->method('isSetFlag')
153+
->with(
154+
'carriers/' . $carrierCode . '/active',
155+
$scopeStore
156+
)
157+
->willReturn(true);
131158
$this->carrier->expects($this->atLeastOnce())
132159
->method('setActiveFlag')
133160
->with('active');
134161

135-
$this->shipping->collectCarrierRates('carrier', new RateRequest());
162+
$this->shipping->collectCarrierRates($carrierCode, new RateRequest());
136163
}
137164

138165
/**
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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\OfflineShipping\Model;
9+
10+
/**
11+
* Integration tests for offline shipping carriers.
12+
* @magentoAppIsolation enabled
13+
*/
14+
class CollectRatesTest extends \Magento\Shipping\Model\CollectRatesAbstract
15+
{
16+
/**
17+
* @var string
18+
*/
19+
protected $carrier = 'flatrate';
20+
21+
/**
22+
* @var string
23+
*/
24+
protected $errorMessage = 'This shipping method is not available. To use this shipping method, please contact us.';
25+
26+
/**
27+
* @magentoConfigFixture default_store carriers/flatrate/active 1
28+
* @magentoConfigFixture default_store carriers/flatrate/sallowspecific 1
29+
* @magentoConfigFixture default_store carriers/flatrate/specificcountry UK
30+
* @magentoConfigFixture default_store carriers/flatrate/showmethod 1
31+
*/
32+
// phpcs:ignore Generic.CodeAnalysis.UselessOverridingMethod
33+
public function testCollectRatesWhenShippingCarrierIsAvailableAndNotApplicable()
34+
{
35+
parent::testCollectRatesWhenShippingCarrierIsAvailableAndNotApplicable();
36+
}
37+
38+
/**
39+
* @magentoConfigFixture default_store carriers/flatrate/active 0
40+
* @magentoConfigFixture default_store carriers/flatrate/sallowspecific 1
41+
* @magentoConfigFixture default_store carriers/flatrate/specificcountry UK
42+
* @magentoConfigFixture default_store carriers/flatrate/showmethod 1
43+
*/
44+
// phpcs:ignore Generic.CodeAnalysis.UselessOverridingMethod
45+
public function testCollectRatesWhenShippingCarrierIsNotAvailableAndNotApplicable()
46+
{
47+
parent::testCollectRatesWhenShippingCarrierIsNotAvailableAndNotApplicable();
48+
}
49+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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\Shipping\Model;
9+
10+
use Magento\Framework\DataObject;
11+
use Magento\Framework\ObjectManagerInterface;
12+
use Magento\Quote\Model\Quote\Address\RateResult\Error;
13+
use Magento\Quote\Model\Quote\Address\RateResult\Method;
14+
use Magento\Shipping\Model\Rate\Result;
15+
use Magento\TestFramework\Helper\Bootstrap;
16+
17+
/**
18+
* Abstract class for testing shipping carriers.
19+
*/
20+
abstract class CollectRatesAbstract extends \PHPUnit\Framework\TestCase
21+
{
22+
/**
23+
* @var ObjectManagerInterface
24+
*/
25+
private $objectManager;
26+
27+
/**
28+
* @var Shipping
29+
*/
30+
protected $shipping;
31+
32+
/**
33+
* @var string
34+
*/
35+
protected $carrier = '';
36+
37+
/**
38+
* @var string
39+
*/
40+
protected $errorMessage = '';
41+
42+
/**
43+
* @inheritdoc
44+
*/
45+
protected function setUp()
46+
{
47+
$this->objectManager = Bootstrap::getObjectManager();
48+
$this->shipping = $this->objectManager->get(Shipping::class);
49+
}
50+
51+
/**
52+
* Tests that an error message is displayed when the shipping method is enabled and not applicable.
53+
*
54+
* @return void
55+
*/
56+
public function testCollectRatesWhenShippingCarrierIsAvailableAndNotApplicable()
57+
{
58+
$result = $this->shipping->collectRatesByAddress($this->getAddress(), $this->carrier);
59+
$rate = $this->getRate($result->getResult());
60+
61+
static::assertEquals($this->carrier, $rate->getData('carrier'));
62+
static::assertEquals($this->errorMessage, $rate->getData('error_message'));
63+
}
64+
65+
/**
66+
* Tests that shipping rates don't return when the shipping method is disabled and not applicable.
67+
*
68+
* @return void
69+
*/
70+
public function testCollectRatesWhenShippingCarrierIsNotAvailableAndNotApplicable()
71+
{
72+
$result = $this->shipping->collectRatesByAddress($this->getAddress(), $this->carrier);
73+
$rate = $this->getRate($result->getResult());
74+
75+
static::assertNull($rate);
76+
}
77+
78+
/**
79+
* Returns customer address.
80+
*
81+
* @return DataObject
82+
*/
83+
private function getAddress(): DataObject
84+
{
85+
$address = $this->objectManager->create(
86+
DataObject::class,
87+
[
88+
'data' => [
89+
'region_id' => 'CA',
90+
'postcode' => '11111',
91+
'lastname' => 'John',
92+
'firstname' => 'Doe',
93+
'street' => 'Some street',
94+
'city' => 'Los Angeles',
95+
'email' => 'john.doe@example.com',
96+
'telephone' => '11111111',
97+
'country_id' => 'US',
98+
'item_qty' => 1,
99+
],
100+
]
101+
);
102+
103+
return $address;
104+
}
105+
106+
/**
107+
* Returns shipping rate by the result.
108+
*
109+
* @param Result $result
110+
* @return Method|Error
111+
*/
112+
private function getRate(Result $result)
113+
{
114+
$rates = $result->getAllRates();
115+
116+
return array_pop($rates);
117+
}
118+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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\Ups\Model;
9+
10+
/**
11+
* Integration tests for online shipping carriers.
12+
* @magentoAppIsolation enabled
13+
*/
14+
class CollectRatesTest extends \Magento\Shipping\Model\CollectRatesAbstract
15+
{
16+
/**
17+
* @var string
18+
*/
19+
protected $carrier = 'ups';
20+
21+
/**
22+
* @var string
23+
*/
24+
protected $errorMessage = 'This shipping method is currently unavailable. ' .
25+
'If you would like to ship using this shipping method, please contact us.';
26+
27+
/**
28+
* @magentoConfigFixture default_store carriers/ups/active 1
29+
* @magentoConfigFixture default_store carriers/ups/type UPS
30+
* @magentoConfigFixture default_store carriers/ups/sallowspecific 1
31+
* @magentoConfigFixture default_store carriers/ups/specificcountry UK
32+
* @magentoConfigFixture default_store carriers/ups/showmethod 1
33+
*/
34+
// phpcs:ignore Generic.CodeAnalysis.UselessOverridingMethod
35+
public function testCollectRatesWhenShippingCarrierIsAvailableAndNotApplicable()
36+
{
37+
parent::testCollectRatesWhenShippingCarrierIsAvailableAndNotApplicable();
38+
}
39+
40+
/**
41+
* @magentoConfigFixture default_store carriers/ups/active 0
42+
* @magentoConfigFixture default_store carriers/ups/type UPS
43+
* @magentoConfigFixture default_store carriers/ups/sallowspecific 1
44+
* @magentoConfigFixture default_store carriers/ups/specificcountry UK
45+
* @magentoConfigFixture default_store carriers/ups/showmethod 1
46+
*/
47+
// phpcs:ignore Generic.CodeAnalysis.UselessOverridingMethod
48+
public function testCollectRatesWhenShippingCarrierIsNotAvailableAndNotApplicable()
49+
{
50+
parent::testCollectRatesWhenShippingCarrierIsNotAvailableAndNotApplicable();
51+
}
52+
}

0 commit comments

Comments
 (0)