|
| 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\Framework\Webapi\Rest\Request; |
| 13 | +use Magento\Integration\Api\AdminTokenServiceInterface; |
| 14 | +use Magento\Store\Model\StoreManagerInterface; |
| 15 | +use Magento\TestFramework\Fixture\DataFixture; |
| 16 | +use Magento\TestFramework\Fixture\DataFixtureStorage; |
| 17 | +use Magento\TestFramework\Fixture\DataFixtureStorageManager; |
| 18 | +use Magento\TestFramework\Helper\Bootstrap; |
| 19 | +use Magento\Catalog\Test\Fixture\Product as ProductFixture; |
| 20 | +use Magento\TestFramework\TestCase\WebapiAbstract; |
| 21 | +use Magento\User\Test\Fixture\User as UserFixture; |
| 22 | + |
| 23 | +/** |
| 24 | + * WebAPI test to validate SpecialPriceStoragePlugin behavior |
| 25 | + */ |
| 26 | +class SpecialPriceStoragePluginTest extends WebapiAbstract |
| 27 | +{ |
| 28 | + /** |
| 29 | + * @var DataFixtureStorage |
| 30 | + */ |
| 31 | + private $fixtures; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var ProductRepositoryInterface |
| 35 | + */ |
| 36 | + private ProductRepositoryInterface $productRepository; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var StoreManagerInterface |
| 40 | + */ |
| 41 | + private StoreManagerInterface $storeManager; |
| 42 | + |
| 43 | + protected function setUp(): void |
| 44 | + { |
| 45 | + $this->fixtures = Bootstrap::getObjectManager()->get(DataFixtureStorageManager::class)->getStorage(); |
| 46 | + $objectManager = Bootstrap::getObjectManager(); |
| 47 | + $this->productRepository = $objectManager->get(ProductRepositoryInterface::class); |
| 48 | + $this->storeManager = $objectManager->get(StoreManagerInterface::class); |
| 49 | + } |
| 50 | + |
| 51 | + #[ |
| 52 | + DataFixture(ProductFixture::class, as: 'product'), |
| 53 | + DataFixture(RoleFixture::class, as: 'restrictedRole'), |
| 54 | + DataFixture(UserFixture::class, ['role_id' => '$restrictedRole.id$'], 'restrictedUser'), |
| 55 | + ] |
| 56 | + public function testSpecialPriceIsAppliedToAllStoresInWebsite(): void |
| 57 | + { |
| 58 | + $this->_markTestAsRestOnly(); |
| 59 | + $product = $this->fixtures->get('product'); |
| 60 | + $sku = $product->getSku(); |
| 61 | + $storeId= $product->getStoreId(); |
| 62 | + $product->setSpecialPrice(123.45); |
| 63 | + |
| 64 | + $Store = $this->storeManager->getStore($storeId); |
| 65 | + $website = $Store->getWebsite(); |
| 66 | + $storeIds = $website->getStoreIds(); |
| 67 | + |
| 68 | + $restrictedUser = $this->fixtures->get('restrictedUser'); |
| 69 | + |
| 70 | + $adminTokens = Bootstrap::getObjectManager()->get(AdminTokenServiceInterface::class); |
| 71 | + $accessToken = $adminTokens->createAdminAccessToken( |
| 72 | + $restrictedUser->getData('username'), |
| 73 | + \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD |
| 74 | + ); |
| 75 | + |
| 76 | + $data = [ |
| 77 | + 'sku' => $sku, |
| 78 | + 'price' =>123.45, |
| 79 | + 'store_id' => $storeId |
| 80 | + ]; |
| 81 | + |
| 82 | + $serviceInfo = [ |
| 83 | + 'rest' => [ |
| 84 | + 'resourcePath' => '/V1/products/special-price', |
| 85 | + 'httpMethod' => Request::HTTP_METHOD_POST, |
| 86 | + 'token' => $accessToken, |
| 87 | + ], |
| 88 | + ]; |
| 89 | + $this->_webApiCall( |
| 90 | + $serviceInfo, |
| 91 | + [ |
| 92 | + 'prices' => [ |
| 93 | + $data |
| 94 | + ] |
| 95 | + ] |
| 96 | + ); |
| 97 | + foreach ($storeIds as $storeId) { |
| 98 | + $product = $this->productRepository->get($sku, false, $storeId); |
| 99 | + $this->assertNotNull( |
| 100 | + $product->getSpecialPrice(), |
| 101 | + "Expected special price for SKU '$sku' in store ID $storeId, but got none." |
| 102 | + ); |
| 103 | + $this->assertEquals( |
| 104 | + 123.45, |
| 105 | + (float)$product->getSpecialPrice(), |
| 106 | + "Special price mismatch for store ID $storeId" |
| 107 | + ); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments