Skip to content

Commit 68b2a24

Browse files
author
Yu Tang
committed
MAGETWO-28253: Downloadable Integration API
- Fix build failures
1 parent cdff9fd commit 68b2a24

File tree

7 files changed

+187
-65
lines changed

7 files changed

+187
-65
lines changed

app/code/Magento/Downloadable/Model/LinkRepository.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,15 @@ protected function saveLink(
253253
} elseif ($link->getLinkType() === 'url') {
254254
$linkData['link_url'] = $link->getLinkUrl();
255255
} else {
256-
$linkData['link_file'] = $link->getLinkFile();
256+
//existing link file
257+
$linkData['file'] = $this->jsonEncoder->encode(
258+
[
259+
[
260+
'file' => $link->getLinkFile(),
261+
'status' => 'old',
262+
]
263+
]
264+
);
257265
}
258266

259267
if ($link->getSampleType() == 'file' && $link->getSampleFile() === null) {

app/code/Magento/Downloadable/Model/Product/TypeHandler/Link.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,12 @@ protected function setFiles(ComponentInterface $model, array $files)
168168
}
169169
}
170170
if ($model->getLinkType() == \Magento\Downloadable\Helper\Download::LINK_TYPE_FILE) {
171-
if ($model->getLinkFile() === null) {
172-
$linkFileName = $this->downloadableFile->moveFileFromTmp(
173-
$this->createItem()->getBaseTmpPath(),
174-
$this->createItem()->getBasePath(),
175-
$files
176-
);
177-
$model->setLinkFile($linkFileName);
178-
}
171+
$linkFileName = $this->downloadableFile->moveFileFromTmp(
172+
$this->createItem()->getBaseTmpPath(),
173+
$this->createItem()->getBasePath(),
174+
$files
175+
);
176+
$model->setLinkFile($linkFileName);
179177
}
180178
if ($model->getSampleType() == \Magento\Downloadable\Helper\Download::LINK_TYPE_FILE) {
181179
$linkSampleFileName = $this->downloadableFile->moveFileFromTmp(

app/code/Magento/Downloadable/Model/Product/TypeHandler/Sample.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,12 @@ protected function setDataToModel(ComponentInterface $model, array $data, Produc
102102
protected function setFiles(ComponentInterface $model, array $files)
103103
{
104104
if ($model->getSampleType() == \Magento\Downloadable\Helper\Download::LINK_TYPE_FILE) {
105-
if ($model->getSampleFile() === null) {
106-
$fileName = $this->downloadableFile->moveFileFromTmp(
107-
$model->getBaseTmpPath(),
108-
$model->getBasePath(),
109-
$files
110-
);
111-
$model->setSampleFile($fileName);
112-
}
105+
$fileName = $this->downloadableFile->moveFileFromTmp(
106+
$model->getBaseTmpPath(),
107+
$model->getBasePath(),
108+
$files
109+
);
110+
$model->setSampleFile($fileName);
113111
}
114112
return $this;
115113
}

app/code/Magento/Downloadable/Model/SampleRepository.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
use Magento\Framework\Exception\NoSuchEntityException;
1515
use Magento\Framework\Json\EncoderInterface;
1616

17+
/**
18+
* Class SampleRepository
19+
* @package Magento\Downloadable\Model
20+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
21+
*/
1722
class SampleRepository implements \Magento\Downloadable\Api\SampleRepositoryInterface
1823
{
1924
/**
@@ -133,7 +138,15 @@ protected function saveSample(
133138
} elseif ($sample->getSampleType() === 'url') {
134139
$sampleData['sample_url'] = $sample->getSampleUrl();
135140
} else {
136-
$sampleData['sample_file'] = $sample->getSampleFile();
141+
//existing file
142+
$sampleData['file'] = $this->jsonEncoder->encode(
143+
[
144+
[
145+
'file' => $sample->getSampleFile(),
146+
'status' => 'old',
147+
],
148+
]
149+
);
137150
}
138151

139152
$downloadableData = ['sample' => [$sampleData]];

app/code/Magento/Downloadable/Test/Unit/Model/LinkRepositoryTest.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,13 @@ protected function getLinkMock(array $linkData)
184184
)
185185
);
186186
}
187+
if (isset($linkData['link_file'])) {
188+
$linkMock->expects($this->any())->method('getLinkFile')->will(
189+
$this->returnValue(
190+
$linkData['link_file']
191+
)
192+
);
193+
}
187194
return $linkMock;
188195
}
189196

@@ -323,6 +330,84 @@ public function testUpdate()
323330
$this->assertEquals($linkId, $this->service->save($productSku, $linkMock));
324331
}
325332

333+
public function testUpdateWithExistingFile()
334+
{
335+
$websiteId = 1;
336+
$linkId = 1;
337+
$productSku = 'simple';
338+
$productId = 1;
339+
$linkFile = '/l/i/link.jpg';
340+
$encodedFiles = "something";
341+
$linkData = [
342+
'id' => $linkId,
343+
'title' => 'Updated Title',
344+
'sort_order' => 1,
345+
'price' => 10.1,
346+
'is_shareable' => true,
347+
'number_of_downloads' => 100,
348+
'link_type' => 'file',
349+
'link_file' => $linkFile,
350+
];
351+
$this->repositoryMock->expects($this->any())->method('get')->with($productSku, true)
352+
->will($this->returnValue($this->productMock));
353+
$this->productMock->expects($this->any())->method('getId')->will($this->returnValue($productId));
354+
$storeMock = $this->getMock('\Magento\Store\Model\Store', [], [], '', false);
355+
$storeMock->expects($this->any())->method('getWebsiteId')->will($this->returnValue($websiteId));
356+
$this->productMock->expects($this->any())->method('getStore')->will($this->returnValue($storeMock));
357+
$existingLinkMock = $this->getMock(
358+
'\Magento\Downloadable\Model\Link',
359+
[
360+
'__wakeup',
361+
'getId',
362+
'load',
363+
'getProductId'
364+
],
365+
[],
366+
'',
367+
false
368+
);
369+
$this->linkFactoryMock->expects($this->once())->method('create')->will($this->returnValue($existingLinkMock));
370+
$linkMock = $this->getLinkMock($linkData);
371+
$this->contentValidatorMock->expects($this->any())->method('isValid')->with($linkMock)
372+
->will($this->returnValue(true));
373+
374+
$existingLinkMock->expects($this->any())->method('getId')->will($this->returnValue($linkId));
375+
$existingLinkMock->expects($this->any())->method('getProductId')->will($this->returnValue($productId));
376+
$existingLinkMock->expects($this->once())->method('load')->with($linkId)->will($this->returnSelf());
377+
378+
$this->jsonEncoderMock->expects($this->once())
379+
->method('encode')
380+
->with(
381+
[
382+
[
383+
'file' => $linkFile,
384+
'status' => 'old'
385+
]
386+
]
387+
)->willReturn($encodedFiles);
388+
$this->productMock->expects($this->once())->method('setDownloadableData')->with(
389+
[
390+
'link' => [
391+
[
392+
'link_id' => $linkId,
393+
'is_delete' => 0,
394+
'type' => $linkData['link_type'],
395+
'sort_order' => $linkData['sort_order'],
396+
'title' => $linkData['title'],
397+
'price' => $linkData['price'],
398+
'number_of_downloads' => $linkData['number_of_downloads'],
399+
'is_shareable' => $linkData['is_shareable'],
400+
'file' => $encodedFiles,
401+
],
402+
],
403+
]
404+
);
405+
$this->productTypeMock->expects($this->once())->method('save')
406+
->with($this->productMock);
407+
408+
$this->assertEquals($linkId, $this->service->save($productSku, $linkMock));
409+
}
410+
326411
/**
327412
* @expectedException \Magento\Framework\Exception\InputException
328413
* @expectedExceptionMessage Link title cannot be empty.

app/code/Magento/Downloadable/Test/Unit/Model/Product/TypeHandler/LinkTest.php

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ public function testSave($product, array $data, array $modelData)
6868

6969
/**
7070
* @return array
71-
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
7271
*/
7372
public function saveDataProvider()
7473
{
@@ -135,52 +134,7 @@ public function saveDataProvider()
135134
'number_of_downloads' => 15,
136135
'price' => 15.00,
137136
]
138-
],
139-
[
140-
'product' => $this->createProductMock(100500, 1, 10, [10]),
141-
'data' => [
142-
'link' => [
143-
[
144-
'link_id' => 0,
145-
'product_id' => 1,
146-
'sort_order' => '0',
147-
'title' => 'Downloadable Product Link',
148-
'sample' => [
149-
'type' => \Magento\Downloadable\Helper\Download::LINK_TYPE_FILE,
150-
'url' => null,
151-
'sample_file' => '/n/d/jellyfish_1_3.jpg',
152-
],
153-
'type' => \Magento\Downloadable\Helper\Download::LINK_TYPE_FILE,
154-
'is_shareable' => \Magento\Downloadable\Model\Link::LINK_SHAREABLE_CONFIG,
155-
'link_url' => null,
156-
'is_delete' => 0,
157-
'number_of_downloads' => 15,
158-
'price' => 15.00,
159-
],
160-
],
161-
'sample' => [
162-
[
163-
'is_delete' => 0,
164-
'sample_id' => 0,
165-
'title' => 'Downloadable Product Sample Title',
166-
'type' => \Magento\Downloadable\Helper\Download::LINK_TYPE_FILE,
167-
'sample_file' => '/f/u/jellyfish_1_4.jpg',
168-
'sample_url' => null,
169-
'sort_order' => '0',
170-
],
171-
],
172-
],
173-
'modelData' => [
174-
'product_id' => 1,
175-
'sort_order' => '0',
176-
'title' => 'Downloadable Product Link',
177-
'type' => \Magento\Downloadable\Helper\Download::LINK_TYPE_FILE,
178-
'is_shareable' => \Magento\Downloadable\Model\Link::LINK_SHAREABLE_CONFIG,
179-
'link_url' => null,
180-
'number_of_downloads' => 15,
181-
'price' => 15.00,
182-
]
183-
],
137+
]
184138
];
185139
}
186140

app/code/Magento/Downloadable/Test/Unit/Model/SampleRepositoryTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ protected function getSampleMock(array $sampleData)
118118
$sampleData['sample_url']
119119
));
120120
}
121+
if (isset($sampleData['sample_file'])) {
122+
$sampleMock->expects($this->any())->method('getSampleFile')->will($this->returnValue(
123+
$sampleData['sample_file']
124+
));
125+
}
121126
return $sampleMock;
122127
}
123128

@@ -228,6 +233,67 @@ public function testUpdate()
228233
$this->assertEquals($sampleId, $this->service->save($productSku, $sampleMock));
229234
}
230235

236+
public function testUpdateWithExistingFile()
237+
{
238+
$sampleId = 1;
239+
$productId = 1;
240+
$productSku = 'simple';
241+
$sampleFile = '/s/a/sample.jpg';
242+
$encodedFile = 'something';
243+
$sampleData = [
244+
'id' => $sampleId,
245+
'title' => 'Updated Title',
246+
'sort_order' => 1,
247+
'sample_type' => 'file',
248+
'sample_file' => $sampleFile,
249+
];
250+
$this->repositoryMock->expects($this->any())->method('get')->with($productSku, true)
251+
->will($this->returnValue($this->productMock));
252+
$this->productMock->expects($this->any())->method('getId')->will($this->returnValue($productId));
253+
$existingSampleMock = $this->getMock(
254+
'\Magento\Downloadable\Model\Sample',
255+
['__wakeup', 'getId', 'load', 'getProductId'],
256+
[],
257+
'',
258+
false
259+
);
260+
$this->sampleFactoryMock->expects($this->once())->method('create')
261+
->will($this->returnValue($existingSampleMock));
262+
$sampleMock = $this->getSampleMock($sampleData);
263+
$this->contentValidatorMock->expects($this->any())->method('isValid')->with($sampleMock)
264+
->will($this->returnValue(true));
265+
266+
$existingSampleMock->expects($this->any())->method('getId')->will($this->returnValue($sampleId));
267+
$existingSampleMock->expects($this->any())->method('getProductId')->will($this->returnValue($productId));
268+
$existingSampleMock->expects($this->once())->method('load')->with($sampleId)->will($this->returnSelf());
269+
270+
$this->jsonEncoderMock->expects($this->once())
271+
->method('encode')
272+
->with(
273+
[
274+
[
275+
'file' => $sampleFile,
276+
'status' => 'old',
277+
]
278+
]
279+
)->willReturn($encodedFile);
280+
$this->productMock->expects($this->once())->method('setDownloadableData')->with([
281+
'sample' => [
282+
[
283+
'sample_id' => $sampleId,
284+
'is_delete' => 0,
285+
'type' => $sampleData['sample_type'],
286+
'sort_order' => $sampleData['sort_order'],
287+
'title' => $sampleData['title'],
288+
'file' => $encodedFile,
289+
],
290+
],
291+
]);
292+
$this->productTypeMock->expects($this->once())->method('save')->with($this->productMock);
293+
294+
$this->assertEquals($sampleId, $this->service->save($productSku, $sampleMock));
295+
}
296+
231297
/**
232298
* @expectedException \Magento\Framework\Exception\InputException
233299
* @expectedExceptionMessage Sample title cannot be empty.

0 commit comments

Comments
 (0)