|
8 | 8 | namespace craftcommercetests\unit\elements\product; |
9 | 9 |
|
10 | 10 | use Codeception\Test\Unit; |
| 11 | +use Craft; |
11 | 12 | use craft\commerce\elements\db\VariantQuery; |
12 | 13 | use craft\commerce\elements\Product; |
13 | 14 | use craft\commerce\elements\Variant; |
14 | 15 | use craft\commerce\elements\VariantCollection; |
| 16 | +use craft\controllers\NestedElementsController; |
15 | 17 | use craftcommercetests\fixtures\ProductFixture; |
16 | 18 | use ReflectionClass; |
17 | 19 |
|
@@ -188,4 +190,88 @@ public function testGetVariantsIncludeDisabledParameter(): void |
188 | 190 | } |
189 | 191 | self::assertTrue($hasDisabledVariant); |
190 | 192 | } |
| 193 | + |
| 194 | + /** |
| 195 | + * Tests every combination of the nullable $includeDisabled parameter against the |
| 196 | + * NestedElementsController detection introduced alongside the signature change. |
| 197 | + * Also asserts that the internal $_variants collection is never mutated by the filter. |
| 198 | + * |
| 199 | + * @dataProvider getVariantsNullableIncludeDisabledDataProvider |
| 200 | + */ |
| 201 | + public function testGetVariantsNullableIncludeDisabled(?bool $includeDisabled, bool $useNestedElementsController, int $expectedCount): void |
| 202 | + { |
| 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 | + } |
| 212 | + |
| 213 | + $product = new Product(); |
| 214 | + $product->typeId = 2000; |
| 215 | + |
| 216 | + $enabled = new Variant(); |
| 217 | + $enabled->enabled = true; |
| 218 | + $enabled->sku = 'enabled-sku'; |
| 219 | + |
| 220 | + $disabled = new Variant(); |
| 221 | + $disabled->enabled = false; |
| 222 | + $disabled->sku = 'disabled-sku'; |
| 223 | + |
| 224 | + $product->setVariants([$enabled, $disabled]); |
| 225 | + |
| 226 | + $result = $product->getVariants($includeDisabled); |
| 227 | + self::assertCount($expectedCount, $result); |
| 228 | + |
| 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); |
| 234 | + |
| 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'); |
| 239 | + } finally { |
| 240 | + // Clean up so the controller state does not leak into subsequent tests |
| 241 | + Craft::$app->controller = $originalController; |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + /** |
| 246 | + * @return array<string, array{includeDisabled: bool|null, useNestedElementsController: bool, expectedCount: int}> |
| 247 | + */ |
| 248 | + public function getVariantsNullableIncludeDisabledDataProvider(): array |
| 249 | + { |
| 250 | + return [ |
| 251 | + // null resolves to false when no NestedElementsController is active |
| 252 | + 'null-no-controller-excludes-disabled' => [ |
| 253 | + 'includeDisabled' => null, |
| 254 | + 'useNestedElementsController' => false, |
| 255 | + 'expectedCount' => 1, |
| 256 | + ], |
| 257 | + // null resolves to true when NestedElementsController is the active controller |
| 258 | + 'null-nested-elements-controller-includes-disabled' => [ |
| 259 | + 'includeDisabled' => null, |
| 260 | + 'useNestedElementsController' => true, |
| 261 | + 'expectedCount' => 2, |
| 262 | + ], |
| 263 | + // Explicit false must override the NestedElementsController detection |
| 264 | + 'explicit-false-with-nested-elements-controller' => [ |
| 265 | + 'includeDisabled' => false, |
| 266 | + 'useNestedElementsController' => true, |
| 267 | + 'expectedCount' => 1, |
| 268 | + ], |
| 269 | + // Explicit true must work even without a special controller |
| 270 | + 'explicit-true-without-controller' => [ |
| 271 | + 'includeDisabled' => true, |
| 272 | + 'useNestedElementsController' => false, |
| 273 | + 'expectedCount' => 2, |
| 274 | + ], |
| 275 | + ]; |
| 276 | + } |
191 | 277 | } |
0 commit comments