Skip to content
11 changes: 7 additions & 4 deletions app/code/Magento/Shipping/Model/Config/Source/Allmethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,14 @@ public function __construct(
public function toOptionArray($isActiveOnlyFlag = false)
{
$methods = [['value' => '', 'label' => '']];
$carriers = $this->_shippingConfig->getAllCarriers();

if ($isActiveOnlyFlag) {
$carriers = $this->_shippingConfig->getActiveCarriers();
} else {
$carriers = $this->_shippingConfig->getAllCarriers();
}

foreach ($carriers as $carrierCode => $carrierModel) {
if (!$carrierModel->isActive() && (bool)$isActiveOnlyFlag === true) {
continue;
}
$carrierMethods = $carrierModel->getAllowedMethods();
if (!$carrierMethods) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,45 @@ protected function setUp(): void
}

/**
* Ensure that options converted correctly
* Ensure that options converted correctly when isActiveOnlyFlag=false
*
* @dataProvider getCarriersMethodsProvider
* @param array $expectedArray
* @return void
*/
public function testToOptionArray(array $expectedArray): void
public function testToOptionArrayGetAllCarriers(array $expectedArray): void
{
$expectedArray['getAllCarriers'] = [$this->carriersMock];

$this->shippingConfig->expects($this->once())
->method('getAllCarriers')
->willReturn($expectedArray['getAllCarriers']);
$this->carriersMock->expects($this->once())
->method('isActive')
->willReturn(true);
$this->carriersMock->expects($this->once())
->method('getAllowedMethods')
->willReturn($expectedArray['allowedMethods']);
$this->assertEquals([$expectedArray['expected_result']], $this->allmethods->toOptionArray());
}

/**
* Ensure that options converted correctly when isActiveOnlyFlag=true
*
* @dataProvider getCarriersMethodsProvider
* @param array $expectedArray
* @return void
*/
public function testToOptionArrayGetActiveCarriers(array $expectedArray): void
{
$expectedArray['getActiveCarriers'] = [$this->carriersMock];

$this->shippingConfig->expects($this->once())
->method('getActiveCarriers')
->willReturn($expectedArray['getActiveCarriers']);
$this->carriersMock->expects($this->once())
->method('getAllowedMethods')
->willReturn($expectedArray['allowedMethods']);
$this->assertEquals([$expectedArray['expected_result']], $this->allmethods->toOptionArray(true));
}

/**
* Returns providers data for test
*
Expand All @@ -92,12 +109,14 @@ public static function getCarriersMethodsProvider(): array
[
'allowedMethods' => [null => 'method_title'],
'expected_result' => [ 'value' => [], 'label' => null],
'getAllCarriers' => []
'getAllCarriers' => [],
'getActiveCarriers' => []
],
[
'allowedMethods' => ['method_code' => 'method_title'],
'expected_result' => [ 'value' => [], 'label' => 'method_code'],
'getAllCarriers' => []
'getAllCarriers' => [],
'getActiveCarriers' => []
]

]
Expand Down