Skip to content

Commit 8dca44d

Browse files
committed
More protective getVariants() include disbaled test code
1 parent 0f61d72 commit 8dca44d

2 files changed

Lines changed: 34 additions & 31 deletions

File tree

src/elements/Product.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1214,7 +1214,7 @@ public function getVariants(?bool $includeDisabled = null): VariantCollection
12141214
}
12151215

12161216
// When reordering variants we need to make sure disabled variants are included when calculating sort order
1217-
// @TODO: Remove in 6.0 when variants return and element query
1217+
// @TODO: Remove in 6.0 when updating `getVariants()` to start returning an element query instance.
12181218
$includeDisabled ??= Craft::$app->controller instanceof NestedElementsController;
12191219

12201220
return $this->_variants->filter(fn(Variant $variant) => $includeDisabled || ($variant->getStatus() === self::STATUS_ENABLED));

tests/unit/elements/product/ProductGetVariantsTest.php

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -200,44 +200,47 @@ public function testGetVariantsIncludeDisabledParameter(): void
200200
*/
201201
public function testGetVariantsNullableIncludeDisabled(?bool $includeDisabled, bool $useNestedElementsController, int $expectedCount): void
202202
{
203-
if ($useNestedElementsController) {
204-
$mockController = $this->getMockBuilder(NestedElementsController::class)
205-
->disableOriginalConstructor()
206-
->getMock();
207-
Craft::$app->controller = $mockController;
208-
} else {
209-
Craft::$app->controller = null;
210-
}
203+
$originalController = Craft::$app->controller;
204+
205+
try {
206+
if ($useNestedElementsController) {
207+
$mockController = $this->getMockBuilder(NestedElementsController::class)
208+
->disableOriginalConstructor()
209+
->getMock();
210+
Craft::$app->controller = $mockController;
211+
}
211212

212-
$product = new Product();
213-
$product->typeId = 2000;
213+
$product = new Product();
214+
$product->typeId = 2000;
214215

215-
$enabled = new Variant();
216-
$enabled->enabled = true;
217-
$enabled->sku = 'enabled-sku';
216+
$enabled = new Variant();
217+
$enabled->enabled = true;
218+
$enabled->sku = 'enabled-sku';
218219

219-
$disabled = new Variant();
220-
$disabled->enabled = false;
221-
$disabled->sku = 'disabled-sku';
220+
$disabled = new Variant();
221+
$disabled->enabled = false;
222+
$disabled->sku = 'disabled-sku';
222223

223-
$product->setVariants([$enabled, $disabled]);
224+
$product->setVariants([$enabled, $disabled]);
224225

225-
$result = $product->getVariants($includeDisabled);
226-
self::assertCount($expectedCount, $result);
226+
$result = $product->getVariants($includeDisabled);
227+
self::assertCount($expectedCount, $result);
227228

228-
// The internal collection must never be mutated by the filter —
229-
// regardless of which parameter was passed, all set variants must be retained.
230-
$reflection = new ReflectionClass($product);
231-
$variantsProperty = $reflection->getProperty('_variants');
232-
$variantsProperty->setAccessible(true);
229+
// The internal collection must never be mutated by the filter —
230+
// regardless of which parameter was passed, all set variants must be retained.
231+
$reflection = new ReflectionClass($product);
232+
$variantsProperty = $reflection->getProperty('_variants');
233+
$variantsProperty->setAccessible(true);
233234

234-
/** @var VariantCollection $internalVariants */
235-
$internalVariants = $variantsProperty->getValue($product);
236-
self::assertInstanceOf(VariantCollection::class, $internalVariants);
237-
self::assertCount(2, $internalVariants, '_variants must retain all variants regardless of the filter applied');
235+
/** @var VariantCollection $internalVariants */
236+
$internalVariants = $variantsProperty->getValue($product);
237+
self::assertInstanceOf(VariantCollection::class, $internalVariants);
238+
self::assertCount(2, $internalVariants, '_variants must retain all variants regardless of the filter applied');
238239

239-
// Clean up so the controller state does not leak into subsequent tests
240-
Craft::$app->controller = null;
240+
} finally {
241+
// Clean up so the controller state does not leak into subsequent tests
242+
Craft::$app->controller = $originalController;
243+
}
241244
}
242245

243246
/**

0 commit comments

Comments
 (0)