Skip to content

Commit 199d69d

Browse files
committed
Merge remote-tracking branch 'origin/AC-15415-V1' into spartans_pr_11112025
2 parents bb0e6b0 + 1ddd7a5 commit 199d69d

File tree

2 files changed

+114
-11
lines changed

2 files changed

+114
-11
lines changed

app/code/Magento/Shipping/Model/Config/Source/Allmethods.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,14 @@ public function __construct(
4545
public function toOptionArray($isActiveOnlyFlag = false)
4646
{
4747
$methods = [['value' => '', 'label' => '']];
48-
$carriers = $this->_shippingConfig->getAllCarriers();
48+
49+
if ($isActiveOnlyFlag) {
50+
$carriers = $this->_shippingConfig->getActiveCarriers();
51+
} else {
52+
$carriers = $this->_shippingConfig->getAllCarriers();
53+
}
54+
4955
foreach ($carriers as $carrierCode => $carrierModel) {
50-
if (!$carrierModel->isActive() && (bool)$isActiveOnlyFlag === true) {
51-
continue;
52-
}
5356
$carrierMethods = $carrierModel->getAllowedMethods();
5457
if (!$carrierMethods) {
5558
continue;

app/code/Magento/Shipping/Test/Unit/Model/Config/Source/AllmethodsTest.php

Lines changed: 107 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,28 +58,45 @@ protected function setUp(): void
5858
}
5959

6060
/**
61-
* Ensure that options converted correctly
61+
* Ensure that options converted correctly when isActiveOnlyFlag=false
6262
*
6363
* @dataProvider getCarriersMethodsProvider
6464
* @param array $expectedArray
6565
* @return void
6666
*/
67-
public function testToOptionArray(array $expectedArray): void
67+
public function testToOptionArrayGetAllCarriers(array $expectedArray): void
6868
{
6969
$expectedArray['getAllCarriers'] = [$this->carriersMock];
7070

7171
$this->shippingConfig->expects($this->once())
7272
->method('getAllCarriers')
7373
->willReturn($expectedArray['getAllCarriers']);
74-
$this->carriersMock->expects($this->once())
75-
->method('isActive')
76-
->willReturn(true);
7774
$this->carriersMock->expects($this->once())
7875
->method('getAllowedMethods')
7976
->willReturn($expectedArray['allowedMethods']);
8077
$this->assertEquals([$expectedArray['expected_result']], $this->allmethods->toOptionArray());
8178
}
8279

80+
/**
81+
* Ensure that options converted correctly when isActiveOnlyFlag=true
82+
*
83+
* @dataProvider getCarriersMethodsProvider
84+
* @param array $expectedArray
85+
* @return void
86+
*/
87+
public function testToOptionArrayGetActiveCarriers(array $expectedArray): void
88+
{
89+
$expectedArray['getActiveCarriers'] = [$this->carriersMock];
90+
91+
$this->shippingConfig->expects($this->once())
92+
->method('getActiveCarriers')
93+
->willReturn($expectedArray['getActiveCarriers']);
94+
$this->carriersMock->expects($this->once())
95+
->method('getAllowedMethods')
96+
->willReturn($expectedArray['allowedMethods']);
97+
$this->assertEquals([$expectedArray['expected_result']], $this->allmethods->toOptionArray(true));
98+
}
99+
83100
/**
84101
* Returns providers data for test
85102
*
@@ -92,15 +109,98 @@ public static function getCarriersMethodsProvider(): array
92109
[
93110
'allowedMethods' => [null => 'method_title'],
94111
'expected_result' => [ 'value' => [], 'label' => null],
95-
'getAllCarriers' => []
112+
'getAllCarriers' => [],
113+
'getActiveCarriers' => []
96114
],
97115
[
98116
'allowedMethods' => ['method_code' => 'method_title'],
99117
'expected_result' => [ 'value' => [], 'label' => 'method_code'],
100-
'getAllCarriers' => []
118+
'getAllCarriers' => [],
119+
'getActiveCarriers' => []
101120
]
102121

103122
]
104123
];
105124
}
125+
126+
/**
127+
* Ensures carriers with no allowed methods are skipped entirely
128+
*
129+
* @return void
130+
*/
131+
public function testSkipsCarrierWhenNoAllowedMethods(): void
132+
{
133+
$this->shippingConfig->expects($this->once())
134+
->method('getAllCarriers')
135+
->willReturn(['flatrate' => $this->carriersMock]);
136+
137+
$this->carriersMock->expects($this->once())
138+
->method('getAllowedMethods')
139+
->willReturn(null);
140+
141+
$result = $this->allmethods->toOptionArray(false);
142+
143+
$this->assertSame([
144+
['value' => '', 'label' => ''],
145+
], $result);
146+
}
147+
148+
/**
149+
* Ensures a proper entry is added for a valid method
150+
*
151+
* @return void
152+
*/
153+
public function testAddsMethodEntryForValidMethod(): void
154+
{
155+
$this->shippingConfig->expects($this->once())
156+
->method('getActiveCarriers')
157+
->willReturn(['flatrate' => $this->carriersMock]);
158+
159+
$this->carriersMock->expects($this->once())
160+
->method('getAllowedMethods')
161+
->willReturn(['fixed' => 'Fixed Rate']);
162+
163+
$this->scopeConfig->expects($this->once())
164+
->method('getValue')
165+
->with('carriers/flatrate/title', \Magento\Store\Model\ScopeInterface::SCOPE_STORE)
166+
->willReturn('Flat Rate');
167+
168+
$result = $this->allmethods->toOptionArray(true);
169+
170+
$this->assertArrayHasKey('flatrate', $result);
171+
$this->assertSame('Flat Rate', $result['flatrate']['label']);
172+
$this->assertSame([
173+
[
174+
'value' => 'flatrate_fixed',
175+
'label' => '[flatrate] Fixed Rate',
176+
]
177+
], $result['flatrate']['value']);
178+
}
179+
180+
/**
181+
* Ensures null/empty method codes are not added
182+
*
183+
* @return void
184+
*/
185+
public function testSkipsNullAndEmptyMethodCodes(): void
186+
{
187+
$this->shippingConfig->expects($this->once())
188+
->method('getAllCarriers')
189+
->willReturn(['flatrate' => $this->carriersMock]);
190+
191+
$this->carriersMock->expects($this->once())
192+
->method('getAllowedMethods')
193+
->willReturn([0 => 'Zero Title', '' => 'Empty Title']);
194+
195+
$this->scopeConfig->expects($this->once())
196+
->method('getValue')
197+
->with('carriers/flatrate/title', \Magento\Store\Model\ScopeInterface::SCOPE_STORE)
198+
->willReturn('Flat Rate');
199+
200+
$result = $this->allmethods->toOptionArray(false);
201+
202+
$this->assertArrayHasKey('flatrate', $result);
203+
$this->assertSame('Flat Rate', $result['flatrate']['label']);
204+
$this->assertSame([], $result['flatrate']['value']);
205+
}
106206
}

0 commit comments

Comments
 (0)