Skip to content

Commit 0c21527

Browse files
committed
Merge branch 'ACP2E-1948' of https://github.com/magento-l3/magento2ce into PR-05242023
2 parents 46b07d5 + 0702871 commit 0c21527

File tree

2 files changed

+190
-3
lines changed
  • app/code/Magento/Downloadable
    • Test/Unit/Ui/DataProvider/Product/Form/Modifier/Data
    • Ui/DataProvider/Product/Form/Modifier/Data

2 files changed

+190
-3
lines changed

app/code/Magento/Downloadable/Test/Unit/Ui/DataProvider/Product/Form/Modifier/Data/LinksTest.php

Lines changed: 189 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@
99

1010
use Magento\Catalog\Api\Data\ProductInterface;
1111
use Magento\Catalog\Model\Locator\LocatorInterface;
12+
use Magento\Catalog\Model\Product\Type as ProductType;
13+
use Magento\Downloadable\Api\Data\LinkInterface;
1214
use Magento\Downloadable\Helper\File as DownloadableFile;
1315
use Magento\Downloadable\Model\Link as LinkModel;
1416
use Magento\Downloadable\Model\Product\Type;
1517
use Magento\Downloadable\Ui\DataProvider\Product\Form\Modifier\Data\Links;
1618
use Magento\Framework\App\Config\ScopeConfigInterface;
19+
use Magento\Framework\DataObject;
1720
use Magento\Framework\Escaper;
1821
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
1922
use Magento\Framework\UrlInterface;
@@ -78,11 +81,14 @@ protected function setUp(): void
7881
{
7982
$this->objectManagerHelper = new ObjectManagerHelper($this);
8083
$this->productMock = $this->getMockBuilder(ProductInterface::class)
81-
->setMethods(['getLinksTitle', 'getId', 'getTypeId'])
84+
->onlyMethods(['getId', 'getTypeId'])
85+
->addMethods(['getLinksTitle', 'getTypeInstance', 'getStoreId'])
8286
->getMockForAbstractClass();
8387
$this->locatorMock = $this->getMockForAbstractClass(LocatorInterface::class);
8488
$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();
8692
$this->downloadableFileMock = $this->createMock(DownloadableFile::class);
8793
$this->urlBuilderMock = $this->getMockForAbstractClass(UrlInterface::class);
8894
$this->linkModelMock = $this->createMock(LinkModel::class);
@@ -100,6 +106,8 @@ protected function setUp(): void
100106
}
101107

102108
/**
109+
* Test case for getLinksTitle
110+
*
103111
* @param int|null $id
104112
* @param string $typeId
105113
* @param InvokedCount $expectedGetTitle
@@ -161,4 +169,183 @@ public function getLinksTitleDataProvider()
161169
],
162170
];
163171
}
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+
}
164351
}

app/code/Magento/Downloadable/Ui/DataProvider/Product/Form/Modifier/Data/Links.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public function getLinksData()
120120
$linkData = [];
121121
$linkData['link_id'] = $link->getId();
122122
$linkData['title'] = $this->escaper->escapeHtml($link->getTitle());
123-
$linkData['price'] = $this->getPriceValue($link->getPrice());
123+
$linkData['price'] = $this->getPriceValue((float) $link->getPrice());
124124
$linkData['number_of_downloads'] = $link->getNumberOfDownloads();
125125
$linkData['is_shareable'] = $link->getIsShareable();
126126
$linkData['link_url'] = $link->getLinkUrl();

0 commit comments

Comments
 (0)