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