Skip to content

10058: Tablerate->getCsvFile() fails with non-default PHP upload_tmp_dir #12275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Magento\OfflineShipping\Model\ResourceModel\Carrier;

use Magento\Framework\Filesystem;
use Magento\Framework\Filesystem\DirectoryList;
use Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate\Import;
use Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate\RateQuery;
use Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate\RateQueryFactory;
Expand Down Expand Up @@ -321,9 +320,13 @@ public function getConditionName(\Magento\Framework\DataObject $object)
*/
private function getCsvFile($filePath)
{
$tmpDirectory = $this->filesystem->getDirectoryRead(DirectoryList::SYS_TMP);
$path = $tmpDirectory->getRelativePath($filePath);
return $tmpDirectory->openFile($path);
$pathInfo = pathInfo($filePath);
$dirName = isset($pathInfo['dirname']) ? $pathInfo['dirname'] : '';
$fileName = isset($pathInfo['basename']) ? $pathInfo['basename'] : '';

$directoryRead = $this->filesystem->getDirectoryReadByPath($dirName);

return $directoryRead->openFile($fileName);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\OfflineShipping\Test\Unit\Model\ResourceModel\Carrier;

use Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate;
use Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate\Import;
use Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate\RateQueryFactory;

/**
* Unit test for Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class TablerateTest extends \PHPUnit\Framework\TestCase
{
/**
* @var Tablerate
*/
private $model;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $storeManagerMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $filesystemMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $resource;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $importMock;

protected function setUp()
{
$contextMock = $this->createMock(\Magento\Framework\Model\ResourceModel\Db\Context::class);
$loggerMock = $this->createMock(\Psr\Log\LoggerInterface::class);
$coreConfigMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
$this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
$carrierTablerateMock = $this->createMock(\Magento\OfflineShipping\Model\Carrier\Tablerate::class);
$this->filesystemMock = $this->createMock(\Magento\Framework\Filesystem::class);
$this->importMock = $this->createMock(Import::class);
$rateQueryFactoryMock = $this->createMock(RateQueryFactory::class);
$this->resource = $this->createMock(\Magento\Framework\App\ResourceConnection::class);

$contextMock->expects($this->once())->method('getResources')->willReturn($this->resource);

$this->model = new Tablerate(
$contextMock,
$loggerMock,
$coreConfigMock,
$this->storeManagerMock,
$carrierTablerateMock,
$this->filesystemMock,
$this->importMock,
$rateQueryFactoryMock
);
}

public function testUploadAndImport()
{
$_FILES['groups']['tmp_name']['tablerate']['fields']['import']['value'] = 'some/path/to/file';
$object = $this->createPartialMock(
\Magento\OfflineShipping\Model\Config\Backend\Tablerate::class,
['getScopeId']
);

$websiteMock = $this->createMock(\Magento\Store\Api\Data\WebsiteInterface::class);
$directoryReadMock = $this->createMock(\Magento\Framework\Filesystem\Directory\ReadInterface::class);
$fileReadMock = $this->createMock(\Magento\Framework\Filesystem\File\ReadInterface::class);
$connectionMock = $this->createMock(\Magento\Framework\DB\Adapter\AdapterInterface::class);

$this->storeManagerMock->expects($this->once())->method('getWebsite')->willReturn($websiteMock);
$object->expects($this->once())->method('getScopeId')->willReturn(1);
$websiteMock->expects($this->once())->method('getId')->willReturn(1);

$this->filesystemMock->expects($this->once())->method('getDirectoryReadByPath')
->with('some/path/to')->willReturn($directoryReadMock);
$directoryReadMock->expects($this->once())->method('openFile')
->with('file')->willReturn($fileReadMock);

$this->resource->expects($this->once())->method('getConnection')->willReturn($connectionMock);

$connectionMock->expects($this->once())->method('beginTransaction');
$connectionMock->expects($this->once())->method('delete');
$connectionMock->expects($this->once())->method('commit');

$this->importMock->expects($this->once())->method('getColumns')->willReturn([]);
$this->importMock->expects($this->once())->method('getData')->willReturn([]);

$this->model->uploadAndImport($object);
unset($_FILES['groups']);
}
}
14 changes: 14 additions & 0 deletions lib/internal/Magento/Framework/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,20 @@ public function getDirectoryRead($directoryCode, $driverCode = DriverPool::FILE)
return $this->readInstances[$code];
}

/**
* Create an instance of directory with read permissions by path.
*
* @param string $path
* @param string $driverCode
*
* @return \Magento\Framework\Filesystem\Directory\ReadInterface
*
*/
public function getDirectoryReadByPath($path, $driverCode = DriverPool::FILE)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately we cannot add new public methods to this class as it has been marked with @api would it work if we called getDirectoryRead here instead of making the new call?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dmanners, no it wouldn't. It checks it suggested path is one of the /Magento/Framework/App/Filesystem/DirectoryList::getDefaultConfig().
And what if I'll create the pr with a same code to 2.3?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm ok, If that is not possible then I would suggest that 2.3 would be fine for this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dmanners,link for the pr to 2.3: #12376.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about creating a new class (something like \Magento\Framework\FilesystemV2) and using that one instead (by injecting it as an extra optional parameter at Magento\OfflineShipping\Model\ResourceModel\Carrier\Tablerate::__construct()) and deprecating the old one? Would that be deemed acceptable in such a case?

{
return $this->readFactory->create($path, $driverCode);
}

/**
* Create an instance of directory with write permissions
*
Expand Down
8 changes: 8 additions & 0 deletions lib/internal/Magento/Framework/Test/Unit/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ public function testGetDirectoryRead()
$this->assertEquals($dirReadMock, $this->_filesystem->getDirectoryRead(DirectoryList::ROOT));
}

public function testGetDirectoryReadByPath()
{
/** @var \Magento\Framework\Filesystem\Directory\ReadInterface $dirReadMock */
$dirReadMock = $this->createMock(\Magento\Framework\Filesystem\Directory\ReadInterface::class);
$this->_dirReadFactoryMock->expects($this->once())->method('create')->will($this->returnValue($dirReadMock));
$this->assertEquals($dirReadMock, $this->_filesystem->getDirectoryReadByPath('path/to/some/file'));
}

public function testGetDirectoryWrite()
{
/** @var \Magento\Framework\Filesystem\Directory\WriteInterface $dirWriteMock */
Expand Down