8
8
9
9
namespace Magento \Downloadable \Api ;
10
10
11
+ use Magento \Downloadable \Test \Fixture \DownloadableProduct ;
11
12
use Magento \Framework \Webapi \Rest \Request ;
12
13
use Magento \TestFramework \Helper \Bootstrap ;
13
14
use Magento \TestFramework \TestCase \WebapiAbstract ;
@@ -25,7 +26,6 @@ class StockItemUpdatePreservesLinksTest extends WebapiAbstract
25
26
{
26
27
private const ADMIN_TOKEN_RESOURCE_PATH = '/V1/integration/admin/token ' ;
27
28
private const PRODUCT_RESOURCE_PATH = '/V1/products ' ;
28
- private const TEST_PRODUCT_SKU = 'downloadable-product ' ;
29
29
30
30
/**
31
31
* @var DataFixtureStorage
@@ -47,28 +47,67 @@ protected function setUp(): void
47
47
* Verify that REST-API updating product stock_item does not delete downloadable_product_links
48
48
*/
49
49
#[DataFixture(User::class, ['role_id ' => 1 ], 'admin_user ' )]
50
- #[DataFixture('Magento/Downloadable/_files/downloadable_product_with_files_and_sample_url.php ' )]
50
+ #[DataFixture(DownloadableProduct::class, [
51
+ 'sku ' => 'downloadable-product ' ,
52
+ 'name ' => 'Downloadable Product Test ' ,
53
+ 'price ' => 50.00 ,
54
+ 'type_id ' => 'downloadable ' ,
55
+ 'links_purchased_separately ' => 1 ,
56
+ 'links_title ' => 'Downloadable Links ' ,
57
+ 'extension_attributes ' => [
58
+ 'website_ids ' => [1 ],
59
+ 'stock_item ' => [
60
+ 'use_config_manage_stock ' => true ,
61
+ 'qty ' => 100 ,
62
+ 'is_qty_decimal ' => false ,
63
+ 'is_in_stock ' => true ,
64
+ ],
65
+ 'downloadable_product_links ' => [
66
+ [
67
+ 'title ' => 'Downloadable Product Link ' ,
68
+ 'price ' => 10.00 ,
69
+ 'link_type ' => 'url ' ,
70
+ 'is_shareable ' => 0 ,
71
+ 'number_of_downloads ' => 5 ,
72
+ 'sort_order ' => 1
73
+ ],
74
+ [
75
+ 'title ' => 'Another Link ' ,
76
+ 'price ' => 15.00 ,
77
+ 'link_type ' => 'file ' ,
78
+ 'link_file ' => 'test-file.txt ' ,
79
+ 'is_shareable ' => 1 ,
80
+ 'number_of_downloads ' => 10 ,
81
+ 'sort_order ' => 2
82
+ ]
83
+ ]
84
+ ]
85
+ ], 'downloadable_product ' )]
51
86
public function testStockItemUpdatePreservesDownloadableLinks ()
52
87
{
53
88
// Steps 1-7: Generate admin access token
54
89
$ adminToken = $ this ->generateAdminAccessToken ();
55
90
91
+ // Get the product SKU from the fixture
92
+ $ product = $ this ->fixtures ->get ('downloadable_product ' );
93
+ $ productSku = $ product ->getSku ();
94
+
56
95
// Get original product and verify it has downloadable links
57
- $ originalProduct = $ this ->getProductBySku (self :: TEST_PRODUCT_SKU );
96
+ $ originalProduct = $ this ->getProductBySku ($ productSku );
58
97
$ this ->verifyProductHasDownloadableLinks ($ originalProduct , 'Original product should have downloadable links ' );
59
98
$ originalLinks = $ originalProduct ['extension_attributes ' ]['downloadable_product_links ' ];
60
99
61
100
// Steps 8-14: Update product stock_item via catalogProductRepositoryV1 PUT endpoint
62
- $ updatedProduct = $ this ->updateProductStockItem ($ adminToken );
101
+ $ updatedProduct = $ this ->updateProductStockItem ($ adminToken, $ productSku );
63
102
64
103
// Verify the API call was successful (Step 14: Server response Code=200)
65
104
$ this ->assertNotEmpty ($ updatedProduct , 'API response should not be empty ' );
66
- $ this ->assertEquals (self :: TEST_PRODUCT_SKU , $ updatedProduct ['sku ' ]);
105
+ $ this ->assertEquals ($ productSku , $ updatedProduct ['sku ' ]);
67
106
$ this ->assertEquals ('99.99 ' , $ updatedProduct ['price ' ]);
68
107
$ this ->assertEquals ('1 ' , $ updatedProduct ['status ' ]);
69
108
70
109
// Steps 15-16: Verify downloadable product links are preserved
71
- $ this ->verifyDownloadableLinksPreserved ($ originalLinks );
110
+ $ this ->verifyDownloadableLinksPreserved ($ originalLinks, $ productSku );
72
111
}
73
112
74
113
/**
@@ -100,19 +139,19 @@ private function generateAdminAccessToken(): string
100
139
/**
101
140
* Update Product Stock Item
102
141
*/
103
- private function updateProductStockItem (string $ adminToken ): array
142
+ private function updateProductStockItem (string $ adminToken, string $ productSku ): array
104
143
{
105
144
$ serviceInfo = [
106
145
'rest ' => [
107
- 'resourcePath ' => self ::PRODUCT_RESOURCE_PATH . '/ ' . self :: TEST_PRODUCT_SKU ,
146
+ 'resourcePath ' => self ::PRODUCT_RESOURCE_PATH . '/ ' . $ productSku ,
108
147
'httpMethod ' => Request::HTTP_METHOD_PUT ,
109
148
'token ' => $ adminToken ,
110
149
],
111
150
];
112
151
113
152
$ productData = [
114
153
'product ' => [
115
- 'sku ' => self :: TEST_PRODUCT_SKU ,
154
+ 'sku ' => $ productSku ,
116
155
'status ' => '1 ' ,
117
156
'price ' => '99.99 ' ,
118
157
'type_id ' => 'downloadable ' ,
@@ -130,9 +169,9 @@ private function updateProductStockItem(string $adminToken): array
130
169
/**
131
170
* Verify Downloadable Links are Preserved
132
171
*/
133
- private function verifyDownloadableLinksPreserved (array $ originalLinks ): void
172
+ private function verifyDownloadableLinksPreserved (array $ originalLinks, string $ productSku ): void
134
173
{
135
- $ updatedProduct = $ this ->getProductBySku (self :: TEST_PRODUCT_SKU );
174
+ $ updatedProduct = $ this ->getProductBySku ($ productSku );
136
175
$ this ->verifyProductHasDownloadableLinks ($ updatedProduct , 'Updated product should preserve downloadable links ' );
137
176
138
177
$ preservedLinks = $ updatedProduct ['extension_attributes ' ]['downloadable_product_links ' ];
0 commit comments