|
1 | 1 | <?php |
2 | 2 | /** |
3 | | - * Copyright © Magento, Inc. All rights reserved. |
4 | | - * See COPYING.txt for license details. |
| 3 | + * Copyright 2017 Adobe |
| 4 | + * All Rights Reserved. |
5 | 5 | */ |
6 | 6 |
|
7 | 7 | namespace Magento\CatalogWidget\Block\Product; |
8 | 8 |
|
9 | 9 | use Magento\Bundle\Test\Fixture\Option as BundleOptionFixture; |
10 | 10 | use Magento\Bundle\Test\Fixture\Product as BundleProductFixture; |
| 11 | +use Magento\Catalog\Test\Fixture\MultiselectAttribute as MultiselectAttributeFixture; |
| 12 | +use Magento\Catalog\Test\Fixture\Category as CategoryFixture; |
11 | 13 | use Magento\Catalog\Test\Fixture\Product as ProductFixture; |
12 | 14 | use Magento\Catalog\Model\Indexer\Product\Eav\Processor; |
13 | 15 | use Magento\Catalog\Model\ResourceModel\Category\Collection as CategoryCollection; |
|
18 | 20 | use Magento\TestFramework\Fixture\DataFixtureStorageManager; |
19 | 21 | use Magento\TestFramework\Fixture\DbIsolation; |
20 | 22 | use Magento\TestFramework\Helper\Bootstrap; |
| 23 | +use PHPUnit\Framework\Attributes\DataProvider; |
21 | 24 | use PHPUnit\Framework\TestCase; |
22 | 25 |
|
23 | 26 | /** |
24 | 27 | * Tests for @see \Magento\CatalogWidget\Block\Product\ProductsList |
| 28 | + * |
| 29 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
25 | 30 | */ |
26 | 31 | class ProductListTest extends TestCase |
27 | 32 | { |
@@ -368,4 +373,165 @@ public static function priceFilterDataProvider(): array |
368 | 373 | ], |
369 | 374 | ]; |
370 | 375 | } |
| 376 | + |
| 377 | + #[ |
| 378 | + DbIsolation(false), |
| 379 | + DataProvider('collectionResultWithMultiselectAttributeDataProvider'), |
| 380 | + DataFixture( |
| 381 | + MultiselectAttributeFixture::class, |
| 382 | + [ |
| 383 | + 'scope' => 'global', |
| 384 | + 'options' => ['option_1', 'option_2'] |
| 385 | + ], |
| 386 | + 'gl_multiselect' |
| 387 | + ), |
| 388 | + DataFixture(CategoryFixture::class, as: 'category'), |
| 389 | + DataFixture( |
| 390 | + ProductFixture::class, |
| 391 | + [ |
| 392 | + 'category_ids' => ['$category.id$'] |
| 393 | + ], |
| 394 | + as: 'product1' |
| 395 | + ), |
| 396 | + DataFixture( |
| 397 | + ProductFixture::class, |
| 398 | + [ |
| 399 | + 'custom_attributes' => [ |
| 400 | + ['attribute_code' => '$gl_multiselect.attribute_code$', 'value' => '$gl_multiselect.option_1$'] |
| 401 | + ] |
| 402 | + ], |
| 403 | + as: 'product2' |
| 404 | + ), |
| 405 | + DataFixture( |
| 406 | + ProductFixture::class, |
| 407 | + [ |
| 408 | + 'custom_attributes' => [ |
| 409 | + ['attribute_code' => '$gl_multiselect.attribute_code$', 'value' => '$gl_multiselect.option_2$'] |
| 410 | + ] |
| 411 | + ], |
| 412 | + as: 'product3' |
| 413 | + ), |
| 414 | + DataFixture( |
| 415 | + ProductFixture::class, |
| 416 | + [ |
| 417 | + 'category_ids' => ['$category.id$'], |
| 418 | + 'custom_attributes' => [ |
| 419 | + ['attribute_code' => '$gl_multiselect.attribute_code$', 'value' => '$gl_multiselect.option_1$'] |
| 420 | + ] |
| 421 | + ], |
| 422 | + as: 'product4' |
| 423 | + ), |
| 424 | + DataFixture( |
| 425 | + ProductFixture::class, |
| 426 | + [ |
| 427 | + 'category_ids' => ['$category.id$'], |
| 428 | + 'custom_attributes' => [ |
| 429 | + ['attribute_code' => '$gl_multiselect.attribute_code$', 'value' => '$gl_multiselect.option_2$'] |
| 430 | + ] |
| 431 | + ], |
| 432 | + as: 'product5' |
| 433 | + ) |
| 434 | + ] |
| 435 | + public function testCollectionResultWithMultiselectAttribute( |
| 436 | + array $conditions, |
| 437 | + array $products |
| 438 | + ): void { |
| 439 | + $fixtures = DataFixtureStorageManager::getStorage(); |
| 440 | + $conditions = array_map( |
| 441 | + function ($condition) use ($fixtures) { |
| 442 | + if (isset($condition['value']) && is_callable($condition['value'])) { |
| 443 | + $condition['value'] = $condition['value']($fixtures); |
| 444 | + } |
| 445 | + if (isset($condition['attribute']) && $fixtures->get($condition['attribute'])) { |
| 446 | + $condition['attribute'] = $fixtures->get($condition['attribute'])->getAttributeCode(); |
| 447 | + } |
| 448 | + return $condition; |
| 449 | + }, |
| 450 | + $conditions |
| 451 | + ); |
| 452 | + $products = array_map( |
| 453 | + function ($product) use ($fixtures) { |
| 454 | + return $fixtures->get($product)->getSku(); |
| 455 | + }, |
| 456 | + $products |
| 457 | + ); |
| 458 | + |
| 459 | + $this->block->setConditions($conditions); |
| 460 | + $collection = $this->block->createCollection(); |
| 461 | + $collection->load(); |
| 462 | + |
| 463 | + $this->assertEqualsCanonicalizing( |
| 464 | + $products, |
| 465 | + $collection->getColumnValues('sku') |
| 466 | + ); |
| 467 | + } |
| 468 | + |
| 469 | + public static function collectionResultWithMultiselectAttributeDataProvider(): array |
| 470 | + { |
| 471 | + return [ |
| 472 | + 'global multiselect with match ANY' => [ |
| 473 | + [ |
| 474 | + '1' => [ |
| 475 | + 'type' => \Magento\CatalogWidget\Model\Rule\Condition\Combine::class, |
| 476 | + 'aggregator' => 'any', |
| 477 | + 'value' => '1', |
| 478 | + 'new_child' => '', |
| 479 | + ], |
| 480 | + '1--1' => [ |
| 481 | + 'type' => \Magento\CatalogWidget\Model\Rule\Condition\Product::class, |
| 482 | + 'attribute' => 'category_ids', |
| 483 | + 'operator' => '==', |
| 484 | + 'value' => fn ($fixtures) => $fixtures->get('category')->getId(), |
| 485 | + ], |
| 486 | + '1--2' => [ |
| 487 | + 'type' => \Magento\CatalogWidget\Model\Rule\Condition\Product::class, |
| 488 | + 'attribute' => 'gl_multiselect', |
| 489 | + 'operator' => '()', |
| 490 | + 'value' => fn ($fixtures) => $fixtures->get('gl_multiselect')->getData('option_1'), |
| 491 | + ], |
| 492 | + ], |
| 493 | + ['product1', 'product2', 'product4', 'product5'] |
| 494 | + ], |
| 495 | + 'global multiselect with match AND' => [ |
| 496 | + [ |
| 497 | + '1' => [ |
| 498 | + 'type' => \Magento\CatalogWidget\Model\Rule\Condition\Combine::class, |
| 499 | + 'aggregator' => 'all', |
| 500 | + 'value' => '1', |
| 501 | + 'new_child' => '', |
| 502 | + ], |
| 503 | + '1--1' => [ |
| 504 | + 'type' => \Magento\CatalogWidget\Model\Rule\Condition\Product::class, |
| 505 | + 'attribute' => 'category_ids', |
| 506 | + 'operator' => '==', |
| 507 | + 'value' => fn ($fixtures) => $fixtures->get('category')->getId(), |
| 508 | + ], |
| 509 | + '1--2' => [ |
| 510 | + 'type' => \Magento\CatalogWidget\Model\Rule\Condition\Product::class, |
| 511 | + 'attribute' => 'gl_multiselect', |
| 512 | + 'operator' => '()', |
| 513 | + 'value' => fn ($fixtures) => $fixtures->get('gl_multiselect')->getData('option_1'), |
| 514 | + ], |
| 515 | + ], |
| 516 | + ['product4'] |
| 517 | + ], |
| 518 | + 'global multiselect with single value' => [ |
| 519 | + [ |
| 520 | + '1' => [ |
| 521 | + 'type' => \Magento\CatalogWidget\Model\Rule\Condition\Combine::class, |
| 522 | + 'aggregator' => 'all', |
| 523 | + 'value' => '1', |
| 524 | + 'new_child' => '', |
| 525 | + ], |
| 526 | + '1--1' => [ |
| 527 | + 'type' => \Magento\CatalogWidget\Model\Rule\Condition\Product::class, |
| 528 | + 'attribute' => 'gl_multiselect', |
| 529 | + 'operator' => '()', |
| 530 | + 'value' => fn ($fixtures) => $fixtures->get('gl_multiselect')->getData('option_2'), |
| 531 | + ], |
| 532 | + ], |
| 533 | + ['product3', 'product5'] |
| 534 | + ] |
| 535 | + ]; |
| 536 | + } |
371 | 537 | } |
0 commit comments