|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Catalog\Model\Plugin\SpecialPricePluginForREST; |
| 9 | + |
| 10 | +use Magento\Authorization\Test\Fixture\Role as RoleFixture; |
| 11 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 12 | +use Magento\Catalog\Api\SpecialPriceStorageInterface; |
| 13 | +use Magento\Framework\Webapi\Rest\Request; |
| 14 | +use Magento\Integration\Api\AdminTokenServiceInterface; |
| 15 | +use Magento\Store\Model\StoreManagerInterface; |
| 16 | +use Magento\TestFramework\Fixture\DataFixture; |
| 17 | +use Magento\TestFramework\Fixture\DataFixtureStorage; |
| 18 | +use Magento\TestFramework\Fixture\DataFixtureStorageManager; |
| 19 | +use Magento\TestFramework\Helper\Bootstrap; |
| 20 | +use Magento\Catalog\Test\Fixture\Product as ProductFixture; |
| 21 | +use Magento\TestFramework\TestCase\WebapiAbstract; |
| 22 | +use Magento\User\Test\Fixture\User as UserFixture; |
| 23 | + |
| 24 | +/** |
| 25 | + * WebAPI test to validate SpecialPriceStoragePlugin behavior |
| 26 | + */ |
| 27 | +class SpecialPriceStoragePluginTest extends WebapiAbstract |
| 28 | +{ |
| 29 | + /** |
| 30 | + * @var DataFixtureStorage |
| 31 | + */ |
| 32 | + private $fixtures; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var SpecialPriceStorageInterface |
| 36 | + */ |
| 37 | + private SpecialPriceStorageInterface $specialPriceStorage; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var ProductRepositoryInterface |
| 41 | + */ |
| 42 | + private ProductRepositoryInterface $productRepository; |
| 43 | + |
| 44 | + /** |
| 45 | + * @var StoreManagerInterface |
| 46 | + */ |
| 47 | + private StoreManagerInterface $storeManager; |
| 48 | + |
| 49 | + protected function setUp(): void |
| 50 | + { |
| 51 | + $this->fixtures = Bootstrap::getObjectManager()->get(DataFixtureStorageManager::class)->getStorage(); |
| 52 | + $objectManager = Bootstrap::getObjectManager(); |
| 53 | + $this->specialPriceStorage = $objectManager->get(SpecialPriceStorageInterface::class); |
| 54 | + $this->productRepository = $objectManager->get(ProductRepositoryInterface::class); |
| 55 | + $this->storeManager = $objectManager->get(StoreManagerInterface::class); |
| 56 | + |
| 57 | + } |
| 58 | + |
| 59 | + #[ |
| 60 | + DataFixture(ProductFixture::class, as: 'product'), |
| 61 | + DataFixture(RoleFixture::class, as: 'restrictedRole'), |
| 62 | + DataFixture(UserFixture::class, ['role_id' => '$restrictedRole.id$'], 'restrictedUser'), |
| 63 | + DataFixture( |
| 64 | + \Magento\Customer\Test\Fixture\Customer::class, |
| 65 | + [ |
| 66 | + 'email' => 'john@doe.com', |
| 67 | + 'password' => 'test@123', |
| 68 | + 'addresses' => [ |
| 69 | + [ |
| 70 | + 'country_id' => 'US', |
| 71 | + 'region_id' => 32, |
| 72 | + 'city' => 'Boston', |
| 73 | + 'street' => ['10 Milk Street'], |
| 74 | + 'postcode' => '02108', |
| 75 | + 'telephone' => '1234567890', |
| 76 | + 'default_billing' => true, |
| 77 | + 'default_shipping' => true, |
| 78 | + ], |
| 79 | + ], |
| 80 | + ], |
| 81 | + 'customer' |
| 82 | + ), |
| 83 | + ] |
| 84 | + public function testSpecialPriceIsAppliedToAllStoresInWebsite(): void |
| 85 | + { |
| 86 | + $objectManager = Bootstrap::getObjectManager(); |
| 87 | + $product = $this->fixtures->get('product'); |
| 88 | + $sku = $product->getSku(); |
| 89 | + $storeId= $product->getStoreId(); |
| 90 | + $product->setSpecialPrice(123.45); |
| 91 | + |
| 92 | + $Store = $this->storeManager->getStore($storeId); |
| 93 | + $website = $Store->getWebsite(); |
| 94 | + $storeIds = $website->getStoreIds(); |
| 95 | + |
| 96 | + $customer = $this->fixtures->get('customer'); |
| 97 | + $restrictedUser = $this->fixtures->get('restrictedUser'); |
| 98 | + |
| 99 | + $adminTokens = Bootstrap::getObjectManager()->get(AdminTokenServiceInterface::class); |
| 100 | + $accessToken = $adminTokens->createAdminAccessToken( |
| 101 | + $restrictedUser->getData('username'), |
| 102 | + \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD |
| 103 | + ); |
| 104 | + |
| 105 | + |
| 106 | + $data = [ |
| 107 | + 'sku' => $sku, |
| 108 | + 'price' =>123.45, |
| 109 | + 'store_id' => $storeId |
| 110 | + ]; |
| 111 | + |
| 112 | + $serviceInfo = [ |
| 113 | + 'rest' => [ |
| 114 | + 'resourcePath' => '/V1/products/special-price', |
| 115 | + 'httpMethod' => Request::HTTP_METHOD_POST, |
| 116 | + 'token' => $accessToken, |
| 117 | + ], |
| 118 | + ]; |
| 119 | + $response = $this->_webApiCall( |
| 120 | + $serviceInfo, |
| 121 | + [ |
| 122 | + 'prices' => [ |
| 123 | + $data |
| 124 | + ] |
| 125 | + ] |
| 126 | + ); |
| 127 | + foreach ($storeIds as $storeId) { |
| 128 | + $product = $this->productRepository->get($sku, false, $storeId); |
| 129 | + $this->assertNotNull( |
| 130 | + $product->getSpecialPrice(), |
| 131 | + "Expected special price for SKU '$sku' in store ID $storeId, but got none." |
| 132 | + ); |
| 133 | + $this->assertEquals( |
| 134 | + 123.45, |
| 135 | + (float)$product->getSpecialPrice(), |
| 136 | + "Special price mismatch for store ID $storeId" |
| 137 | + ); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments