9
9
10
10
use Magento \Catalog \Api \Data \ProductInterface ;
11
11
use Magento \Catalog \Model \Locator \LocatorInterface ;
12
+ use Magento \Catalog \Model \Product \Type as ProductType ;
13
+ use Magento \Downloadable \Api \Data \LinkInterface ;
12
14
use Magento \Downloadable \Helper \File as DownloadableFile ;
13
15
use Magento \Downloadable \Model \Link as LinkModel ;
14
16
use Magento \Downloadable \Model \Product \Type ;
15
17
use Magento \Downloadable \Ui \DataProvider \Product \Form \Modifier \Data \Links ;
16
18
use Magento \Framework \App \Config \ScopeConfigInterface ;
19
+ use Magento \Framework \DataObject ;
17
20
use Magento \Framework \Escaper ;
18
21
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
19
22
use Magento \Framework \UrlInterface ;
@@ -78,11 +81,14 @@ protected function setUp(): void
78
81
{
79
82
$ this ->objectManagerHelper = new ObjectManagerHelper ($ this );
80
83
$ this ->productMock = $ this ->getMockBuilder (ProductInterface::class)
81
- ->setMethods (['getLinksTitle ' , 'getId ' , 'getTypeId ' ])
84
+ ->onlyMethods (['getId ' , 'getTypeId ' ])
85
+ ->addMethods (['getLinksTitle ' , 'getTypeInstance ' , 'getStoreId ' ])
82
86
->getMockForAbstractClass ();
83
87
$ this ->locatorMock = $ this ->getMockForAbstractClass (LocatorInterface::class);
84
88
$ this ->scopeConfigMock = $ this ->getMockForAbstractClass (ScopeConfigInterface::class);
85
- $ this ->escaperMock = $ this ->createMock (Escaper::class);
89
+ $ this ->escaperMock = $ this ->getMockBuilder (Escaper::class)
90
+ ->onlyMethods (['escapeHtml ' ])
91
+ ->getMockForAbstractClass ();
86
92
$ this ->downloadableFileMock = $ this ->createMock (DownloadableFile::class);
87
93
$ this ->urlBuilderMock = $ this ->getMockForAbstractClass (UrlInterface::class);
88
94
$ this ->linkModelMock = $ this ->createMock (LinkModel::class);
@@ -100,6 +106,8 @@ protected function setUp(): void
100
106
}
101
107
102
108
/**
109
+ * Test case for getLinksTitle
110
+ *
103
111
* @param int|null $id
104
112
* @param string $typeId
105
113
* @param InvokedCount $expectedGetTitle
@@ -161,4 +169,183 @@ public function getLinksTitleDataProvider()
161
169
],
162
170
];
163
171
}
172
+
173
+ /**
174
+ * Test case for getLinksData
175
+ *
176
+ * @param $productTypeMock
177
+ * @param string $typeId
178
+ * @param int $storeId
179
+ * @param array $links
180
+ * @param array $expectedLinksData
181
+ * @return void
182
+ * @dataProvider getLinksDataProvider
183
+ */
184
+ public function testGetLinksData (
185
+ $ productTypeMock ,
186
+ string $ typeId ,
187
+ int $ storeId ,
188
+ array $ links ,
189
+ array $ expectedLinksData
190
+ ): void {
191
+ $ this ->locatorMock ->expects ($ this ->any ())
192
+ ->method ('getProduct ' )
193
+ ->willReturn ($ this ->productMock );
194
+ if (!empty ($ expectedLinksData )) {
195
+ $ this ->escaperMock ->expects ($ this ->any ())
196
+ ->method ('escapeHtml ' )
197
+ ->willReturn ($ expectedLinksData ['title ' ]);
198
+ }
199
+ $ this ->productMock ->expects ($ this ->any ())
200
+ ->method ('getTypeId ' )
201
+ ->willReturn ($ typeId );
202
+ $ this ->productMock ->expects ($ this ->any ())
203
+ ->method ('getTypeInstance ' )
204
+ ->willReturn ($ productTypeMock );
205
+ $ this ->productMock ->expects ($ this ->any ())
206
+ ->method ('getStoreId ' )
207
+ ->willReturn ($ storeId );
208
+ $ productTypeMock ->expects ($ this ->any ())
209
+ ->method ('getLinks ' )
210
+ ->willReturn ($ links );
211
+ $ getLinksData = $ this ->links ->getLinksData ();
212
+ if (!empty ($ getLinksData )) {
213
+ $ actualResult = current ($ getLinksData );
214
+ } else {
215
+ $ actualResult = $ getLinksData ;
216
+ }
217
+ $ this ->assertEquals ($ expectedLinksData , $ actualResult );
218
+ }
219
+
220
+ /**
221
+ * Get Links data provider
222
+ *
223
+ * @return array
224
+ */
225
+ public function getLinksDataProvider ()
226
+ {
227
+ $ productData1 = [
228
+ 'link_id ' => '1 ' ,
229
+ 'title ' => 'test ' ,
230
+ 'price ' => '0.00 ' ,
231
+ 'number_of_downloads ' => '0 ' ,
232
+ 'is_shareable ' => '1 ' ,
233
+ 'link_url ' => 'http://cdn.sourcebooks.com/test ' ,
234
+ 'type ' => 'url ' ,
235
+ 'sample ' =>
236
+ [
237
+ 'url ' => null ,
238
+ 'type ' => null ,
239
+ ],
240
+ 'sort_order ' => '1 ' ,
241
+ 'is_unlimited ' => '1 ' ,
242
+ 'use_default_price ' => '0 ' ,
243
+ 'use_default_title ' => '0 ' ,
244
+
245
+ ];
246
+ $ productData2 = $ productData1 ;
247
+ unset($ productData2 ['use_default_price ' ]);
248
+ unset($ productData2 ['use_default_title ' ]);
249
+ $ productData3 = [
250
+ 'link_id ' => '1 ' ,
251
+ 'title ' => 'simple ' ,
252
+ 'price ' => '10.00 ' ,
253
+ 'number_of_downloads ' => '0 ' ,
254
+ 'is_shareable ' => '0 ' ,
255
+ 'link_url ' => '' ,
256
+ 'type ' => 'simple ' ,
257
+ 'sample ' =>
258
+ [
259
+ 'url ' => null ,
260
+ 'type ' => null ,
261
+ ],
262
+ 'sort_order ' => '1 ' ,
263
+ 'is_unlimited ' => '1 ' ,
264
+ 'use_default_price ' => '0 ' ,
265
+ 'use_default_title ' => '0 ' ,
266
+
267
+ ];
268
+ $ linkMock1 = $ this ->getLinkMockObject ($ productData1 , '1 ' , '1 ' );
269
+ $ linkMock2 = $ this ->getLinkMockObject ($ productData1 , '0 ' , '0 ' );
270
+ $ linkMock3 = $ this ->getLinkMockObject ($ productData3 , '0 ' , '0 ' );
271
+ return [
272
+ 'test case for downloadable product for default store ' => [
273
+ 'type ' => $ this ->createMock (Type::class),
274
+ 'type_id ' => Type::TYPE_DOWNLOADABLE ,
275
+ 'store_id ' => 1 ,
276
+ 'links ' => [$ linkMock1 ],
277
+ 'expectedLinksData ' => $ productData1
278
+ ],
279
+ 'test case for downloadable product for all store ' => [
280
+ 'type ' => $ this ->createMock (Type::class),
281
+ 'type_id ' => Type::TYPE_DOWNLOADABLE ,
282
+ 'store_id ' => 0 ,
283
+ 'links ' => [$ linkMock2 ],
284
+ 'expectedLinksData ' => $ productData2
285
+ ],
286
+ 'test case for simple product for default store ' => [
287
+ 'type ' => $ this ->createMock (Type::class),
288
+ 'type_id ' => ProductType::TYPE_SIMPLE ,
289
+ 'store_id ' => 1 ,
290
+ 'links ' => [$ linkMock3 ],
291
+ 'expectedLinksData ' => []
292
+ ],
293
+ ];
294
+ }
295
+
296
+ /**
297
+ * Data provider for getLinks
298
+ *
299
+ * @param array $productData
300
+ * @param string $useDefaultPrice
301
+ * @param string $useDefaultTitle
302
+ * @return MockObject
303
+ */
304
+ private function getLinkMockObject (
305
+ array $ productData ,
306
+ string $ useDefaultPrice ,
307
+ string $ useDefaultTitle
308
+ ): MockObject {
309
+ $ linkMock = $ this ->getMockBuilder (LinkInterface::class)
310
+ ->onlyMethods (['getId ' ])
311
+ ->addMethods (['getWebsitePrice ' , 'getStoreTitle ' ])
312
+ ->getMockForAbstractClass ();
313
+ $ linkMock ->expects ($ this ->any ())
314
+ ->method ('getId ' )
315
+ ->willReturn ($ productData ['link_id ' ]);
316
+ $ linkMock ->expects ($ this ->any ())
317
+ ->method ('getTitle ' )
318
+ ->willReturn ($ productData ['title ' ]);
319
+ $ linkMock ->expects ($ this ->any ())
320
+ ->method ('getPrice ' )
321
+ ->willReturn ($ productData ['price ' ]);
322
+ $ linkMock ->expects ($ this ->any ())
323
+ ->method ('getNumberOfDownloads ' )
324
+ ->willReturn ($ productData ['number_of_downloads ' ]);
325
+ $ linkMock ->expects ($ this ->any ())
326
+ ->method ('getIsShareable ' )
327
+ ->willReturn ($ productData ['is_shareable ' ]);
328
+ $ linkMock ->expects ($ this ->any ())
329
+ ->method ('getLinkUrl ' )
330
+ ->willReturn ($ productData ['link_url ' ]);
331
+ $ linkMock ->expects ($ this ->any ())
332
+ ->method ('getLinkType ' )
333
+ ->willReturn ($ productData ['type ' ]);
334
+ $ linkMock ->expects ($ this ->any ())
335
+ ->method ('getSampleUrl ' )
336
+ ->willReturn ($ productData ['sample ' ]['url ' ]);
337
+ $ linkMock ->expects ($ this ->any ())
338
+ ->method ('getSampleType ' )
339
+ ->willReturn ($ productData ['sample ' ]['type ' ]);
340
+ $ linkMock ->expects ($ this ->any ())
341
+ ->method ('getSortOrder ' )
342
+ ->willReturn ($ productData ['sort_order ' ]);
343
+ $ linkMock ->expects ($ this ->any ())
344
+ ->method ('getWebsitePrice ' )
345
+ ->willReturn ($ useDefaultPrice );
346
+ $ linkMock ->expects ($ this ->any ())
347
+ ->method ('getStoreTitle ' )
348
+ ->willReturn ($ useDefaultTitle );
349
+ return $ linkMock ;
350
+ }
164
351
}
0 commit comments