Skip to content

Commit e597209

Browse files
author
Oleksii Korshenko
committed
MAGETWO-65353: [GitHub][PR] Fix product option files not copying to order dir. #8462
- fixed unit tests
1 parent 7776ccb commit e597209

File tree

2 files changed

+84
-15
lines changed
  • app/code/Magento/Catalog

2 files changed

+84
-15
lines changed

app/code/Magento/Catalog/Model/Product/Option/Type/File.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,16 @@ class File extends \Magento\Catalog\Model\Product\Option\Type\DefaultType
3232
protected $_formattedOptionValue = null;
3333

3434
/**
35-
* @var \Magento\Framework\Filesystem\Directory\Write
35+
* @var \Magento\Framework\Filesystem\Directory\ReadInterface
36+
* @deprecated
37+
* @see $mediaDirectory
3638
*/
37-
protected $_mediaDirectory;
39+
protected $_rootDirectory;
40+
41+
/**
42+
* @var \Magento\Framework\Filesystem\Directory\WriteInterface
43+
*/
44+
private $mediaDirectory;
3845

3946
/**
4047
* Core file storage database
@@ -114,7 +121,9 @@ public function __construct(
114121
$this->_escaper = $escaper;
115122
$this->_coreFileStorageDatabase = $coreFileStorageDatabase;
116123
$this->filesystem = $filesystem ?: \Magento\Framework\App\ObjectManager::getInstance()->get(Filesystem::class);
117-
$this->_mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
124+
/** The _rootDirectory is deprecated. The field is initialized for backward compatibility */
125+
$this->_rootDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
126+
$this->mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
118127
$this->validatorInfo = $validatorInfo;
119128
$this->validatorFile = $validatorFile;
120129
$this->serializer = $serializer ? $serializer : ObjectManager::getInstance()->get(Json::class);
@@ -473,17 +482,17 @@ public function copyQuoteToOrder()
473482
$quotePath = $value['quote_path'];
474483
$orderPath = $value['order_path'];
475484

476-
if (!$this->_mediaDirectory->isFile($quotePath) || !$this->_mediaDirectory->isReadable($quotePath)) {
485+
if (!$this->mediaDirectory->isFile($quotePath) || !$this->mediaDirectory->isReadable($quotePath)) {
477486
throw new \Exception();
478487
}
479-
488+
480489
if ($this->_coreFileStorageDatabase->checkDbUsage()) {
481490
$this->_coreFileStorageDatabase->copyFile(
482-
$this->_mediaDirectory->getAbsolutePath($quotePath),
483-
$this->_mediaDirectory->getAbsolutePath($orderPath)
491+
$this->mediaDirectory->getAbsolutePath($quotePath),
492+
$this->mediaDirectory->getAbsolutePath($orderPath)
484493
);
485494
} else {
486-
$this->_mediaDirectory->copyFile($quotePath, $orderPath);
495+
$this->mediaDirectory->copyFile($quotePath, $orderPath);
487496
}
488497
} catch (\Exception $e) {
489498
return $this;

app/code/Magento/Catalog/Test/Unit/Model/Product/Option/Type/FileTest.php

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ protected function setUp()
8383

8484
$this->coreFileStorageDatabase = $this->getMock(
8585
\Magento\MediaStorage\Helper\File\Storage\Database::class,
86-
['copyFile'],
86+
['copyFile', 'checkDbUsage'],
8787
[],
8888
'',
8989
false
@@ -133,7 +133,7 @@ public function testGetCustomizedView()
133133
);
134134
}
135135

136-
public function testCopyQuoteToOrder()
136+
public function testCopyQuoteToOrderWithDbUsage()
137137
{
138138
$optionMock = $this->getMockBuilder(OptionInterface::class)
139139
->disableOriginalConstructor()
@@ -154,25 +154,29 @@ function ($value) {
154154
}
155155
);
156156

157-
$optionMock->expects($this->any())
157+
$optionMock->expects($this->once())
158158
->method('getValue')
159159
->will($this->returnValue($quoteValue));
160160

161-
$this->mediaDirectory->expects($this->any())
161+
$this->mediaDirectory->expects($this->once())
162162
->method('isFile')
163163
->with($this->equalTo($quotePath))
164164
->will($this->returnValue(true));
165165

166-
$this->mediaDirectory->expects($this->any())
166+
$this->mediaDirectory->expects($this->once())
167167
->method('isReadable')
168168
->with($this->equalTo($quotePath))
169169
->will($this->returnValue(true));
170170

171-
$this->mediaDirectory->expects($this->any())
171+
$this->mediaDirectory->expects($this->exactly(2))
172172
->method('getAbsolutePath')
173173
->will($this->returnValue('/file.path'));
174174

175-
$this->coreFileStorageDatabase->expects($this->any())
175+
$this->coreFileStorageDatabase->expects($this->once())
176+
->method('checkDbUsage')
177+
->willReturn(true);
178+
179+
$this->coreFileStorageDatabase->expects($this->once())
176180
->method('copyFile')
177181
->will($this->returnValue('true'));
178182

@@ -185,6 +189,62 @@ function ($value) {
185189
);
186190
}
187191

192+
public function testCopyQuoteToOrderWithoutUsage()
193+
{
194+
$optionMock = $this->getMockBuilder(OptionInterface::class)
195+
->disableOriginalConstructor()
196+
->setMethods(['getValue'])
197+
->getMockForAbstractClass();
198+
199+
$quotePath = '/quote/path/path/uploaded.file';
200+
$orderPath = '/order/path/path/uploaded.file';
201+
202+
$quoteValue = "{\"quote_path\":\"$quotePath\",\"order_path\":\"$orderPath\"}";
203+
204+
$this->serializer->expects($this->once())
205+
->method('unserialize')
206+
->with($quoteValue)
207+
->willReturnCallback(
208+
function ($value) {
209+
return json_decode($value, true);
210+
}
211+
);
212+
213+
$optionMock->expects($this->once())
214+
->method('getValue')
215+
->will($this->returnValue($quoteValue));
216+
217+
$this->mediaDirectory->expects($this->once())
218+
->method('isFile')
219+
->with($this->equalTo($quotePath))
220+
->will($this->returnValue(true));
221+
222+
$this->mediaDirectory->expects($this->once())
223+
->method('isReadable')
224+
->with($this->equalTo($quotePath))
225+
->will($this->returnValue(true));
226+
227+
$this->mediaDirectory->expects($this->never())
228+
->method('getAbsolutePath')
229+
->will($this->returnValue('/file.path'));
230+
231+
$this->coreFileStorageDatabase->expects($this->once())
232+
->method('checkDbUsage')
233+
->willReturn(false);
234+
235+
$this->coreFileStorageDatabase->expects($this->any())
236+
->method('copyFile')
237+
->willReturn(false);
238+
239+
$fileObject = $this->getFileObject();
240+
$fileObject->setData('configuration_item_option', $optionMock);
241+
242+
$this->assertInstanceOf(
243+
\Magento\Catalog\Model\Product\Option\Type\File::class,
244+
$fileObject->copyQuoteToOrder()
245+
);
246+
}
247+
188248
public function testGetFormattedOptionValue()
189249
{
190250
$resultValue = ['result'];

0 commit comments

Comments
 (0)