|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2025 Adobe |
| 4 | + * All Rights Reserved. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\Sales\Service\V1; |
| 10 | + |
| 11 | +use Magento\Authorization\Test\Fixture\Role; |
| 12 | +use Magento\Catalog\Test\Fixture\Product as ProductFixture; |
| 13 | +use Magento\Checkout\Test\Fixture\PlaceOrder as PlaceOrderFixture; |
| 14 | +use Magento\Checkout\Test\Fixture\SetBillingAddress as SetBillingAddressFixture; |
| 15 | +use Magento\Checkout\Test\Fixture\SetDeliveryMethod as SetDeliveryMethodFixture; |
| 16 | +use Magento\Checkout\Test\Fixture\SetGuestEmail as SetGuestEmailFixture; |
| 17 | +use Magento\Checkout\Test\Fixture\SetPaymentMethod as SetPaymentMethodFixture; |
| 18 | +use Magento\Checkout\Test\Fixture\SetShippingAddress as SetShippingAddressFixture; |
| 19 | +use Magento\Framework\Exception\AuthenticationException; |
| 20 | +use Magento\Framework\Exception\InputException; |
| 21 | +use Magento\Framework\Exception\LocalizedException; |
| 22 | +use Magento\Integration\Api\AdminTokenServiceInterface; |
| 23 | +use Magento\Quote\Test\Fixture\AddProductToCart as AddProductToCartFixture; |
| 24 | +use Magento\Quote\Test\Fixture\GuestCart as GuestCartFixture; |
| 25 | +use Magento\TestFramework\Fixture\Config as ConfigFixture; |
| 26 | +use Magento\TestFramework\Fixture\DataFixture; |
| 27 | +use Magento\TestFramework\Fixture\DataFixtureStorage; |
| 28 | +use Magento\TestFramework\Fixture\DataFixtureStorageManager; |
| 29 | +use Magento\TestFramework\Helper\Bootstrap as BootstrapHelper; |
| 30 | +use Magento\TestFramework\TestCase\WebapiAbstract; |
| 31 | +use Magento\User\Test\Fixture\User; |
| 32 | +use Magento\TestFramework\Helper\Bootstrap; |
| 33 | +use Magento\Framework\App\ResourceConnection; |
| 34 | +use Magento\Framework\DB\Sql\Expression; |
| 35 | + |
| 36 | +class OrderResponseNullKeysTest extends WebapiAbstract |
| 37 | +{ |
| 38 | + /** |
| 39 | + * @var DataFixtureStorage |
| 40 | + */ |
| 41 | + private $fixtures; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var AdminTokenServiceInterface |
| 45 | + */ |
| 46 | + private $adminToken; |
| 47 | + |
| 48 | + /** |
| 49 | + * @inheritdoc |
| 50 | + */ |
| 51 | + protected function setUp(): void |
| 52 | + { |
| 53 | + parent::setUp(); |
| 54 | + $this->_markTestAsRestOnly(); |
| 55 | + $this->fixtures = BootstrapHelper::getObjectManager()->get(DataFixtureStorageManager::class)->getStorage(); |
| 56 | + $this->adminToken = BootstrapHelper::getObjectManager()->get(AdminTokenServiceInterface::class); |
| 57 | + } |
| 58 | + |
| 59 | + #[ |
| 60 | + DataFixture(Role::class, as: 'allRole'), |
| 61 | + DataFixture(User::class, ['role_id' => '$allRole.id$'], as: 'allUser'), |
| 62 | + ConfigFixture('cataloginventory/item_options/auto_return', 0), |
| 63 | + ConfigFixture('payment/checkmo/active', '1'), |
| 64 | + ConfigFixture('carriers/flatrate/active', '1'), |
| 65 | + DataFixture(ProductFixture::class, [ |
| 66 | + 'price' => 10.00, |
| 67 | + 'quantity_and_stock_status' => ['qty' => 100, 'is_in_stock' => true] |
| 68 | + ], as: 'product'), |
| 69 | + DataFixture(GuestCartFixture::class, as: 'cart'), |
| 70 | + DataFixture(SetGuestEmailFixture::class, [ |
| 71 | + 'cart_id' => '$cart.id$', |
| 72 | + |
| 73 | + ]), |
| 74 | + DataFixture(AddProductToCartFixture::class, [ |
| 75 | + 'cart_id' => '$cart.id$', |
| 76 | + 'product_id' => '$product.id$', |
| 77 | + 'qty' => 1 |
| 78 | + ]), |
| 79 | + DataFixture(SetBillingAddressFixture::class, ['cart_id' => '$cart.id$']), |
| 80 | + DataFixture(SetShippingAddressFixture::class, ['cart_id' => '$cart.id$']), |
| 81 | + DataFixture(SetDeliveryMethodFixture::class, [ |
| 82 | + 'cart_id' => '$cart.id$', |
| 83 | + 'carrier_code' => 'flatrate', |
| 84 | + 'method_code' => 'flatrate' |
| 85 | + ]), |
| 86 | + DataFixture(SetPaymentMethodFixture::class, [ |
| 87 | + 'cart_id' => '$cart.id$', |
| 88 | + 'method' => 'checkmo' |
| 89 | + ]), |
| 90 | + DataFixture(PlaceOrderFixture::class, ['cart_id' => '$cart.id$'], as: 'order'), |
| 91 | + ] |
| 92 | + public function testUserWithRestrictedWebsiteAndStoreGroup() |
| 93 | + { |
| 94 | + $order = $this->fixtures->get('order'); |
| 95 | + $orderId = (int) $order->getId(); |
| 96 | + $this->nullifyOrderStateStatus($orderId); |
| 97 | + |
| 98 | + $user = $this->fixtures->get('allUser'); |
| 99 | + $accessToken = $this->getAccessToken($user->getUsername()); |
| 100 | + $serviceInfo = [ |
| 101 | + 'rest' => [ |
| 102 | + 'resourcePath' => '/V1/orders/' . $orderId, |
| 103 | + 'httpMethod' => 'GET', |
| 104 | + 'token' => $accessToken |
| 105 | + ] |
| 106 | + ]; |
| 107 | + $result = $this->_webApiCall($serviceInfo); |
| 108 | + |
| 109 | + $this->assertIsArray($result); |
| 110 | + $this->assertSame($orderId, (int)$result['entity_id']); |
| 111 | + $this->assertArrayHasKey('state', $result); |
| 112 | + $this->assertArrayHasKey('status', $result); |
| 113 | + $this->assertNull($result['state']); |
| 114 | + $this->assertNull($result['status']); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Update order status and state field as null |
| 119 | + * |
| 120 | + * @param int $orderId |
| 121 | + * @return void |
| 122 | + */ |
| 123 | + private function nullifyOrderStateStatus(int $orderId): void |
| 124 | + { |
| 125 | + $om = Bootstrap::getObjectManager(); |
| 126 | + $resource = $om->get(ResourceConnection::class); |
| 127 | + $connection = $resource->getConnection(); |
| 128 | + $table = $resource->getTableName('sales_order'); |
| 129 | + $connection->update( |
| 130 | + $table, |
| 131 | + ['state' => new Expression('NULL'), 'status' => new Expression('NULL')], |
| 132 | + ['entity_id = ?' => $orderId] |
| 133 | + ); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Get admin access token |
| 138 | + * |
| 139 | + * @param string $username |
| 140 | + * @param string $password |
| 141 | + * @return string |
| 142 | + * @throws AuthenticationException |
| 143 | + * @throws InputException |
| 144 | + * @throws LocalizedException |
| 145 | + */ |
| 146 | + private function getAccessToken(string $username, string $password = 'password1'): string |
| 147 | + { |
| 148 | + return $this->adminToken->createAdminAccessToken($username, $password); |
| 149 | + } |
| 150 | +} |
0 commit comments