|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Catalog\Pricing\Price; |
| 9 | + |
| 10 | +use Magento\Catalog\Model\Product; |
| 11 | +use Magento\Catalog\Model\ResourceModel\Product\Collection; |
| 12 | +use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; |
| 13 | +use Magento\Catalog\Test\Fixture\Product as ProductFixture; |
| 14 | +use Magento\Framework\Pricing\PriceInfo\Base as PriceInfo; |
| 15 | +use Magento\TestFramework\Fixture\DataFixture; |
| 16 | +use Magento\TestFramework\Fixture\DbIsolation; |
| 17 | +use Magento\TestFramework\Helper\Bootstrap; |
| 18 | +use PHPUnit\Framework\TestCase; |
| 19 | + |
| 20 | +/** |
| 21 | + * Integration test for SpecialPrice pricing class |
| 22 | + */ |
| 23 | +#[ |
| 24 | + DbIsolation(true) |
| 25 | +] |
| 26 | +class SpecialPriceTest extends TestCase |
| 27 | +{ |
| 28 | + /** |
| 29 | + * @var \Magento\Framework\ObjectManagerInterface |
| 30 | + */ |
| 31 | + private $objectManager; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var CollectionFactory |
| 35 | + */ |
| 36 | + private $productCollectionFactory; |
| 37 | + |
| 38 | + /** |
| 39 | + * Set up |
| 40 | + */ |
| 41 | + protected function setUp(): void |
| 42 | + { |
| 43 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 44 | + $this->productCollectionFactory = $this->objectManager->get(CollectionFactory::class); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Test that special price is loaded from database when attribute is not in product collection |
| 49 | + * |
| 50 | + * This tests the fix for the issue where special_price doesn't display when |
| 51 | + * the attribute has "Used in Product Listing = No" setting |
| 52 | + * |
| 53 | + * @return void |
| 54 | + */ |
| 55 | + #[ |
| 56 | + DataFixture( |
| 57 | + ProductFixture::class, |
| 58 | + ['sku' => 'simple-special-price', 'price' => 100, 'special_price' => 90] |
| 59 | + ), |
| 60 | + ] |
| 61 | + public function testGetSpecialPriceLoadsFromDatabaseWhenNotInCollection(): void |
| 62 | + { |
| 63 | + |
| 64 | + // Load product in a collection WITHOUT special_price attribute |
| 65 | + /** @var Collection $collection */ |
| 66 | + $collection = $this->productCollectionFactory->create(); |
| 67 | + $collection->addFieldToFilter('sku', 'simple-special-price') |
| 68 | + ->addAttributeToSelect(['name', 'price']) // Intentionally exclude special_price |
| 69 | + ->load(); |
| 70 | + |
| 71 | + /** @var Product $product */ |
| 72 | + $product = $collection->getFirstItem(); |
| 73 | + |
| 74 | + // Verify special_price is not loaded in the product data |
| 75 | + $this->assertFalse( |
| 76 | + $product->hasData('special_price'), |
| 77 | + 'Special price should not be loaded in product collection' |
| 78 | + ); |
| 79 | + |
| 80 | + // Get the SpecialPrice pricing object |
| 81 | + /** @var PriceInfo $priceInfo */ |
| 82 | + $priceInfo = $product->getPriceInfo(); |
| 83 | + /** @var SpecialPrice $specialPriceObject */ |
| 84 | + $specialPriceObject = $priceInfo->getPrice(SpecialPrice::PRICE_CODE); |
| 85 | + |
| 86 | + // Call getSpecialPrice() - it should load from database |
| 87 | + $specialPrice = $specialPriceObject->getSpecialPrice(); |
| 88 | + |
| 89 | + // Assert special price was loaded correctly |
| 90 | + $this->assertNotNull($specialPrice, 'Special price should be loaded from database'); |
| 91 | + $this->assertEquals(90, $specialPrice, 'Special price should match the saved value'); |
| 92 | + |
| 93 | + // Verify the attribute is now cached in product data |
| 94 | + $this->assertTrue( |
| 95 | + $product->hasData('special_price'), |
| 96 | + 'Special price should be cached in product after loading' |
| 97 | + ); |
| 98 | + } |
| 99 | +} |
0 commit comments