Skip to content

Commit ae4cff0

Browse files
author
Oleksii Korshenko
authored
MAGETWO-84222: 10058: Tablerate->getCsvFile() fails with non-default PHP upload_tmp_dir(2.3) #12376
2 parents a99a338 + 8add938 commit ae4cff0

File tree

4 files changed

+134
-4
lines changed

4 files changed

+134
-4
lines changed

app/code/Magento/OfflineShipping/Model/ResourceModel/Carrier/Tablerate.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Magento\OfflineShipping\Model\ResourceModel\Carrier;
1313

1414
use Magento\Framework\Filesystem;
15-
use Magento\Framework\Filesystem\DirectoryList;
1615
use Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate\Import;
1716
use Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate\RateQuery;
1817
use Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate\RateQueryFactory;
@@ -321,9 +320,13 @@ public function getConditionName(\Magento\Framework\DataObject $object)
321320
*/
322321
private function getCsvFile($filePath)
323322
{
324-
$tmpDirectory = $this->filesystem->getDirectoryRead(DirectoryList::SYS_TMP);
325-
$path = $tmpDirectory->getRelativePath($filePath);
326-
return $tmpDirectory->openFile($path);
323+
$pathInfo = pathInfo($filePath);
324+
$dirName = isset($pathInfo['dirname']) ? $pathInfo['dirname'] : '';
325+
$fileName = isset($pathInfo['basename']) ? $pathInfo['basename'] : '';
326+
327+
$directoryRead = $this->filesystem->getDirectoryReadByPath($dirName);
328+
329+
return $directoryRead->openFile($fileName);
327330
}
328331

329332
/**
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\OfflineShipping\Test\Unit\Model\ResourceModel\Carrier;
8+
9+
use Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate;
10+
use Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate\Import;
11+
use Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate\RateQueryFactory;
12+
13+
/**
14+
* Unit test for Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate
15+
*
16+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
17+
*/
18+
class TablerateTest extends \PHPUnit\Framework\TestCase
19+
{
20+
/**
21+
* @var Tablerate
22+
*/
23+
private $model;
24+
25+
/**
26+
* @var \PHPUnit_Framework_MockObject_MockObject
27+
*/
28+
private $storeManagerMock;
29+
30+
/**
31+
* @var \PHPUnit_Framework_MockObject_MockObject
32+
*/
33+
private $filesystemMock;
34+
35+
/**
36+
* @var \PHPUnit_Framework_MockObject_MockObject
37+
*/
38+
private $resource;
39+
40+
/**
41+
* @var \PHPUnit_Framework_MockObject_MockObject
42+
*/
43+
private $importMock;
44+
45+
protected function setUp()
46+
{
47+
$contextMock = $this->createMock(\Magento\Framework\Model\ResourceModel\Db\Context::class);
48+
$loggerMock = $this->createMock(\Psr\Log\LoggerInterface::class);
49+
$coreConfigMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
50+
$this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
51+
$carrierTablerateMock = $this->createMock(\Magento\OfflineShipping\Model\Carrier\Tablerate::class);
52+
$this->filesystemMock = $this->createMock(\Magento\Framework\Filesystem::class);
53+
$this->importMock = $this->createMock(Import::class);
54+
$rateQueryFactoryMock = $this->createMock(RateQueryFactory::class);
55+
$this->resource = $this->createMock(\Magento\Framework\App\ResourceConnection::class);
56+
57+
$contextMock->expects($this->once())->method('getResources')->willReturn($this->resource);
58+
59+
$this->model = new Tablerate(
60+
$contextMock,
61+
$loggerMock,
62+
$coreConfigMock,
63+
$this->storeManagerMock,
64+
$carrierTablerateMock,
65+
$this->filesystemMock,
66+
$this->importMock,
67+
$rateQueryFactoryMock
68+
);
69+
}
70+
71+
public function testUploadAndImport()
72+
{
73+
$_FILES['groups']['tmp_name']['tablerate']['fields']['import']['value'] = 'some/path/to/file';
74+
$object = $this->createPartialMock(
75+
\Magento\OfflineShipping\Model\Config\Backend\Tablerate::class,
76+
['getScopeId']
77+
);
78+
79+
$websiteMock = $this->createMock(\Magento\Store\Api\Data\WebsiteInterface::class);
80+
$directoryReadMock = $this->createMock(\Magento\Framework\Filesystem\Directory\ReadInterface::class);
81+
$fileReadMock = $this->createMock(\Magento\Framework\Filesystem\File\ReadInterface::class);
82+
$connectionMock = $this->createMock(\Magento\Framework\DB\Adapter\AdapterInterface::class);
83+
84+
$this->storeManagerMock->expects($this->once())->method('getWebsite')->willReturn($websiteMock);
85+
$object->expects($this->once())->method('getScopeId')->willReturn(1);
86+
$websiteMock->expects($this->once())->method('getId')->willReturn(1);
87+
88+
$this->filesystemMock->expects($this->once())->method('getDirectoryReadByPath')
89+
->with('some/path/to')->willReturn($directoryReadMock);
90+
$directoryReadMock->expects($this->once())->method('openFile')
91+
->with('file')->willReturn($fileReadMock);
92+
93+
$this->resource->expects($this->once())->method('getConnection')->willReturn($connectionMock);
94+
95+
$connectionMock->expects($this->once())->method('beginTransaction');
96+
$connectionMock->expects($this->once())->method('delete');
97+
$connectionMock->expects($this->once())->method('commit');
98+
99+
$this->importMock->expects($this->once())->method('getColumns')->willReturn([]);
100+
$this->importMock->expects($this->once())->method('getData')->willReturn([]);
101+
102+
$this->model->uploadAndImport($object);
103+
unset($_FILES['groups']);
104+
}
105+
}

lib/internal/Magento/Framework/Filesystem.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,20 @@ public function getDirectoryRead($directoryCode, $driverCode = DriverPool::FILE)
7070
return $this->readInstances[$code];
7171
}
7272

73+
/**
74+
* Create an instance of directory with read permissions by path.
75+
*
76+
* @param string $path
77+
* @param string $driverCode
78+
*
79+
* @return \Magento\Framework\Filesystem\Directory\ReadInterface
80+
*
81+
*/
82+
public function getDirectoryReadByPath($path, $driverCode = DriverPool::FILE)
83+
{
84+
return $this->readFactory->create($path, $driverCode);
85+
}
86+
7387
/**
7488
* Create an instance of directory with write permissions
7589
*

lib/internal/Magento/Framework/Test/Unit/FilesystemTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ public function testGetDirectoryRead()
4343
$this->assertEquals($dirReadMock, $this->_filesystem->getDirectoryRead(DirectoryList::ROOT));
4444
}
4545

46+
public function testGetDirectoryReadByPath()
47+
{
48+
/** @var \Magento\Framework\Filesystem\Directory\ReadInterface $dirReadMock */
49+
$dirReadMock = $this->createMock(\Magento\Framework\Filesystem\Directory\ReadInterface::class);
50+
$this->_dirReadFactoryMock->expects($this->once())->method('create')->will($this->returnValue($dirReadMock));
51+
$this->assertEquals($dirReadMock, $this->_filesystem->getDirectoryReadByPath('path/to/some/file'));
52+
}
53+
4654
public function testGetDirectoryWrite()
4755
{
4856
/** @var \Magento\Framework\Filesystem\Directory\WriteInterface $dirWriteMock */

0 commit comments

Comments
 (0)