Skip to content

Commit dfd32c1

Browse files
author
Oleksii Korshenko
authored
MAGETWO-65353: [GitHub][PR] Fix product option files not copying to order dir. #8462
2 parents 1c0e85f + e597209 commit dfd32c1

File tree

2 files changed

+92
-18
lines changed
  • app/code/Magento/Catalog

2 files changed

+92
-18
lines changed

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,16 @@ class File extends \Magento\Catalog\Model\Product\Option\Type\DefaultType
3333

3434
/**
3535
* @var \Magento\Framework\Filesystem\Directory\ReadInterface
36+
* @deprecated
37+
* @see $mediaDirectory
3638
*/
3739
protected $_rootDirectory;
3840

41+
/**
42+
* @var \Magento\Framework\Filesystem\Directory\WriteInterface
43+
*/
44+
private $mediaDirectory;
45+
3946
/**
4047
* Core file storage database
4148
*
@@ -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);
124+
/** The _rootDirectory is deprecated. The field is initialized for backward compatibility */
117125
$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);
@@ -466,13 +475,18 @@ public function copyQuoteToOrder()
466475
$quotePath = $value['quote_path'];
467476
$orderPath = $value['order_path'];
468477

469-
if (!$this->_rootDirectory->isFile($quotePath) || !$this->_rootDirectory->isReadable($quotePath)) {
478+
if (!$this->mediaDirectory->isFile($quotePath) || !$this->mediaDirectory->isReadable($quotePath)) {
470479
throw new \Exception();
471480
}
472-
$this->_coreFileStorageDatabase->copyFile(
473-
$this->_rootDirectory->getAbsolutePath($quotePath),
474-
$this->_rootDirectory->getAbsolutePath($orderPath)
475-
);
481+
482+
if ($this->_coreFileStorageDatabase->checkDbUsage()) {
483+
$this->_coreFileStorageDatabase->copyFile(
484+
$this->mediaDirectory->getAbsolutePath($quotePath),
485+
$this->mediaDirectory->getAbsolutePath($orderPath)
486+
);
487+
} else {
488+
$this->mediaDirectory->copyFile($quotePath, $orderPath);
489+
}
476490
} catch (\Exception $e) {
477491
return $this;
478492
}

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

Lines changed: 73 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Magento\Framework\App\Filesystem\DirectoryList;
1010
use Magento\Framework\Exception\SerializationException;
1111
use Magento\Framework\Filesystem;
12-
use Magento\Framework\Filesystem\Directory\ReadInterface;
12+
use Magento\Framework\Filesystem\Directory\WriteInterface;
1313
use Magento\Framework\Filesystem\DriverPool;
1414

1515
/**
@@ -25,9 +25,9 @@ class FileTest extends \PHPUnit_Framework_TestCase
2525
protected $objectManager;
2626

2727
/**
28-
* @var ReadInterface|\PHPUnit_Framework_MockObject_MockObject
28+
* @var WriteInterface|\PHPUnit_Framework_MockObject_MockObject
2929
*/
30-
protected $rootDirectory;
30+
protected $mediaDirectory;
3131

3232
/**
3333
* @var \Magento\MediaStorage\Helper\File\Storage\Database|\PHPUnit_Framework_MockObject_MockObject
@@ -67,13 +67,13 @@ protected function setUp()
6767
->disableOriginalConstructor()
6868
->getMock();
6969

70-
$this->rootDirectory = $this->getMockBuilder(ReadInterface::class)
70+
$this->mediaDirectory = $this->getMockBuilder(WriteInterface::class)
7171
->getMock();
7272

7373
$this->filesystemMock->expects($this->any())
74-
->method('getDirectoryRead')
74+
->method('getDirectoryWrite')
7575
->with(DirectoryList::MEDIA, DriverPool::FILE)
76-
->willReturn($this->rootDirectory);
76+
->willReturn($this->mediaDirectory);
7777

7878
$this->serializer = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
7979
->disableOriginalConstructor()
@@ -95,7 +95,7 @@ protected function setUp()
9595

9696
$this->coreFileStorageDatabase = $this->getMock(
9797
\Magento\MediaStorage\Helper\File\Storage\Database::class,
98-
['copyFile'],
98+
['copyFile', 'checkDbUsage'],
9999
[],
100100
'',
101101
false
@@ -166,7 +166,7 @@ public function testGetCustomizedView()
166166
);
167167
}
168168

169-
public function testCopyQuoteToOrder()
169+
public function testCopyQuoteToOrderWithDbUsage()
170170
{
171171
$optionMock = $this->getMockBuilder(OptionInterface::class)
172172
->disableOriginalConstructor()
@@ -187,25 +187,29 @@ function ($value) {
187187
}
188188
);
189189

190-
$optionMock->expects($this->any())
190+
$optionMock->expects($this->once())
191191
->method('getValue')
192192
->will($this->returnValue($quoteValue));
193193

194-
$this->rootDirectory->expects($this->any())
194+
$this->mediaDirectory->expects($this->once())
195195
->method('isFile')
196196
->with($this->equalTo($quotePath))
197197
->will($this->returnValue(true));
198198

199-
$this->rootDirectory->expects($this->any())
199+
$this->mediaDirectory->expects($this->once())
200200
->method('isReadable')
201201
->with($this->equalTo($quotePath))
202202
->will($this->returnValue(true));
203203

204-
$this->rootDirectory->expects($this->any())
204+
$this->mediaDirectory->expects($this->exactly(2))
205205
->method('getAbsolutePath')
206206
->will($this->returnValue('/file.path'));
207207

208-
$this->coreFileStorageDatabase->expects($this->any())
208+
$this->coreFileStorageDatabase->expects($this->once())
209+
->method('checkDbUsage')
210+
->willReturn(true);
211+
212+
$this->coreFileStorageDatabase->expects($this->once())
209213
->method('copyFile')
210214
->will($this->returnValue('true'));
211215

@@ -218,6 +222,62 @@ function ($value) {
218222
);
219223
}
220224

225+
public function testCopyQuoteToOrderWithoutUsage()
226+
{
227+
$optionMock = $this->getMockBuilder(OptionInterface::class)
228+
->disableOriginalConstructor()
229+
->setMethods(['getValue'])
230+
->getMockForAbstractClass();
231+
232+
$quotePath = '/quote/path/path/uploaded.file';
233+
$orderPath = '/order/path/path/uploaded.file';
234+
235+
$quoteValue = "{\"quote_path\":\"$quotePath\",\"order_path\":\"$orderPath\"}";
236+
237+
$this->serializer->expects($this->once())
238+
->method('unserialize')
239+
->with($quoteValue)
240+
->willReturnCallback(
241+
function ($value) {
242+
return json_decode($value, true);
243+
}
244+
);
245+
246+
$optionMock->expects($this->once())
247+
->method('getValue')
248+
->will($this->returnValue($quoteValue));
249+
250+
$this->mediaDirectory->expects($this->once())
251+
->method('isFile')
252+
->with($this->equalTo($quotePath))
253+
->will($this->returnValue(true));
254+
255+
$this->mediaDirectory->expects($this->once())
256+
->method('isReadable')
257+
->with($this->equalTo($quotePath))
258+
->will($this->returnValue(true));
259+
260+
$this->mediaDirectory->expects($this->never())
261+
->method('getAbsolutePath')
262+
->will($this->returnValue('/file.path'));
263+
264+
$this->coreFileStorageDatabase->expects($this->once())
265+
->method('checkDbUsage')
266+
->willReturn(false);
267+
268+
$this->coreFileStorageDatabase->expects($this->any())
269+
->method('copyFile')
270+
->willReturn(false);
271+
272+
$fileObject = $this->getFileObject();
273+
$fileObject->setData('configuration_item_option', $optionMock);
274+
275+
$this->assertInstanceOf(
276+
\Magento\Catalog\Model\Product\Option\Type\File::class,
277+
$fileObject->copyQuoteToOrder()
278+
);
279+
}
280+
221281
public function testGetFormattedOptionValue()
222282
{
223283
$resultValue = ['result'];

0 commit comments

Comments
 (0)