Skip to content
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

MAG2-274 - Refactored Backend FileUpload to work for Magento Cloud too #498

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
38 changes: 36 additions & 2 deletions Helper/ApplePay.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,53 @@
namespace Payone\Core\Helper;

use Payone\Core\Model\PayoneConfig;
use Magento\Framework\App\Filesystem\DirectoryList;

/**
* Helper class for sending emails
*/
class ApplePay extends \Payone\Core\Helper\Base
{
/**
* @var \Magento\Framework\Filesystem
*/
protected $filesystem;

/**
* Constructor
*
* @param \Magento\Framework\App\Helper\Context $context
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Payone\Core\Helper\Shop $shopHelper
* @param \Magento\Framework\App\State $state
* @param \Magento\Framework\Filesystem $filesystem
*/
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Payone\Core\Helper\Shop $shopHelper,
\Magento\Framework\App\State $state,
\Magento\Framework\Filesystem $filesystem
) {
parent::__construct($context, $storeManager, $shopHelper, $state);
$this->storeManager = $storeManager;
$this->shopHelper = $shopHelper;
$this->state = $state;
$this->filesystem = $filesystem;
}

/**
* Returns ApplePay file upload path
*
* @param bool $blForceOldPath
* @return string
*/
public function getApplePayUploadPath()
public function getApplePayUploadPath($blForceOldPath = false)
{
$sNewPath = $this->filesystem->getDirectoryRead(DirectoryList::VAR_DIR)->getAbsolutePath('payone-gmbh/ApplePay/');
if($blForceOldPath === false && file_exists($sNewPath)) { // file upload path was refactored to work for Magento cloud too
return $sNewPath;
}
return __DIR__.DIRECTORY_SEPARATOR."..".DIRECTORY_SEPARATOR."ApplePay".DIRECTORY_SEPARATOR;
}

Expand Down Expand Up @@ -81,7 +115,7 @@ protected function isFileExisting($sFile)
return false;
}

if (file_exists($this->getApplePayUploadPath().$sFile)) {
if (file_exists($this->getApplePayUploadPath().$sFile) || file_exists($this->getApplePayUploadPath(true).$sFile)) {
return true;
}
return false;
Expand Down
16 changes: 10 additions & 6 deletions Model/ApplePay/SessionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,14 @@ protected function getShopDomain()
*/
protected function getCertificateFilePath()
{
$sCertFile = $this->shopHelper->getConfigParam("certificate_file", PayoneConfig::METHOD_APPLEPAY, "payment");
$sCertFilePath = $this->applePayHelper->getApplePayUploadPath().$sCertFile;

if ($this->applePayHelper->hasCertificateFile() === false) {
throw new \Exception("Certificate file not configured or missing");
}
$sCertFile = $this->shopHelper->getConfigParam("certificate_file", PayoneConfig::METHOD_APPLEPAY, "payment");
$sCertFilePath = $this->applePayHelper->getApplePayUploadPath().$sCertFile;
if (!file_exists($sCertFilePath)) {
$sCertFilePath = $this->applePayHelper->getApplePayUploadPath(true).$sCertFile;
}
return $sCertFilePath;
}

Expand All @@ -90,12 +92,14 @@ protected function getCertificateFilePath()
*/
protected function getPrivateKeyFilePath()
{
$sPrivateKeyFile = $this->shopHelper->getConfigParam("private_key_file", PayoneConfig::METHOD_APPLEPAY, "payment");
$sPrivateKeyFilePath = $this->applePayHelper->getApplePayUploadPath().$sPrivateKeyFile;

if ($this->applePayHelper->hasPrivateKeyFile() === false) {
throw new \Exception("Private key file not configured or missing");
}
$sPrivateKeyFile = $this->shopHelper->getConfigParam("private_key_file", PayoneConfig::METHOD_APPLEPAY, "payment");
$sPrivateKeyFilePath = $this->applePayHelper->getApplePayUploadPath().$sPrivateKeyFile;
if (!file_exists($sPrivateKeyFilePath)) {
$sPrivateKeyFilePath = $this->applePayHelper->getApplePayUploadPath(true).$sPrivateKeyFile;
}
return $sPrivateKeyFilePath;
}

Expand Down
35 changes: 29 additions & 6 deletions Model/System/Config/Backend/Upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Payone\Core\Model\System\Config\Backend;

use Magento\Framework\Filesystem;
use Magento\Framework\Filesystem\DirectoryList;

class Upload extends \Magento\Framework\App\Config\Value
{
Expand All @@ -12,6 +11,16 @@ class Upload extends \Magento\Framework\App\Config\Value
*/
protected $_tmpDirectory;

/**
* @var \Magento\Framework\Filesystem\Directory\ReadInterface
*/
protected $varDirectory;

/**
* @var \Magento\Framework\Filesystem\Directory\WriteFactory
*/
protected $writeFactory;

/**
* @var \Payone\Core\Helper\ApplePay
*/
Expand All @@ -23,6 +32,7 @@ class Upload extends \Magento\Framework\App\Config\Value
* @param \Magento\Framework\App\Config\ScopeConfigInterface $config
* @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
* @param \Magento\Framework\Filesystem $filesystem
* @param \Magento\Framework\Filesystem\Directory\WriteFactory $writeFactory
* @param \Payone\Core\Helper\ApplePay $applePayHelper
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
Expand All @@ -34,14 +44,17 @@ public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $config,
\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
\Magento\Framework\Filesystem $filesystem,
\Magento\Framework\Filesystem\Directory\WriteFactory $writeFactory,
\Payone\Core\Helper\ApplePay $applePayHelper,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = []
)
{
parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
$this->_tmpDirectory = $filesystem->getDirectoryRead(DirectoryList::SYS_TMP);
$this->_tmpDirectory = $filesystem->getDirectoryRead(\Magento\Framework\Filesystem\DirectoryList::SYS_TMP);
$this->varDirectory = $filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::VAR_DIR);
$this->writeFactory = $writeFactory;
$this->applePayHelper = $applePayHelper;
}

Expand All @@ -65,6 +78,11 @@ public function beforeSave()
$sCurrentFile = $this->applePayHelper->getApplePayUploadPath().$value['value'];
if (file_exists($sCurrentFile)) {
unlink($sCurrentFile);
} else {
$sCurrentFile = $this->applePayHelper->getApplePayUploadPath(true).$value['value'];
if (file_exists($sCurrentFile)) {
unlink($sCurrentFile);
}
}
$this->setValue('');
return $this;
Expand All @@ -88,17 +106,22 @@ public function beforeSave()

/**
* Copies file to upload path
* New version which should be compatible to Magento Cloud too
*
* @param string $sFilePath
* @param string $sFileName
* @return void
*/
protected function moveFile($sFilePath, $sFileName)
{
$sUploadPath = $this->applePayHelper->getApplePayUploadPath();
if (!file_exists($sUploadPath)) {
mkdir($sUploadPath);
$sModuleDirPath = $this->varDirectory->getAbsolutePath('payone-gmbh/ApplePay/');

$oVarModuleDir = $this->writeFactory->create($sModuleDirPath);
$oWriteDriver = $oVarModuleDir->getDriver();
if ($oWriteDriver->isExists($sModuleDirPath) === false) {
$oWriteDriver->createDirectory($sModuleDirPath);
}
move_uploaded_file($sFilePath, $sUploadPath.$sFileName);

$oWriteDriver->copy($sFilePath, $oVarModuleDir->getAbsolutePath($sFileName));
}
}
20 changes: 15 additions & 5 deletions Test/Unit/Helper/ApplePayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
use Magento\Framework\App\Helper\Context;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Framework\Filesystem;
use Magento\Framework\Filesystem\Directory\Read;

class ApplePayTest extends BaseTestCase
{
Expand All @@ -60,21 +62,24 @@ protected function setUp(): void
$storeManager = $this->getMockBuilder(StoreManagerInterface::class)->disableOriginalConstructor()->getMock();
$storeManager->method('getStore')->willReturn($store);

$read = $this->getMockBuilder(Read::class)->disableOriginalConstructor()->getMock();
$read->method('getAbsolutePath')->willReturn("Upload Path");

$filesystem = $this->getMockBuilder(Filesystem::class)->disableOriginalConstructor()->getMock();
$filesystem->method('getDirectoryRead')->willReturn($read);

$this->classToTest = $objectManager->getObject(ClassToTest::class, [
'context' => $context,
'storeManager' => $storeManager,
'filesystem' => $filesystem,
]);

if (!file_exists($this->classToTest->getApplePayUploadPath())) {
mkdir($this->classToTest->getApplePayUploadPath());
}
}

public function testGetApplePayUploadPath()
{
$result = $this->classToTest->getApplePayUploadPath();

$this->assertStringEndsWith(DIRECTORY_SEPARATOR."..".DIRECTORY_SEPARATOR."ApplePay".DIRECTORY_SEPARATOR, $result);
$this->assertNotEmpty($result);
}

public function testHasMerchantId()
Expand All @@ -101,6 +106,11 @@ public function testHasFiles()
$testFile = "unit.test";
$this->scopeConfig->method('getValue')->willReturn($testFile);

$uploadPath = $this->classToTest->getApplePayUploadPath();
if (!file_exists($uploadPath)) {
mkdir($uploadPath);
}

$path = $this->classToTest->getApplePayUploadPath().$testFile;
file_put_contents($path, "test");

Expand Down
21 changes: 17 additions & 4 deletions Test/Unit/Model/System/Config/Backend/UploadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
use Payone\Core\Helper\ApplePay;
use Magento\Framework\Filesystem;
use Magento\Framework\Filesystem\Directory\Read;
use Magento\Framework\Filesystem\Directory\Write;
use Magento\Framework\Filesystem\Directory\WriteFactory;
use Magento\Framework\Filesystem\DriverInterface;

class UploadTest extends BaseTestCase
{
Expand Down Expand Up @@ -68,9 +71,19 @@ protected function setUp(): void
$filesystem = $this->getMockBuilder(Filesystem::class)->disableOriginalConstructor()->getMock();
$filesystem->method('getDirectoryRead')->willReturn($this->tmpDirectory);

$driver = $this->getMockBuilder(DriverInterface::class)->disableOriginalConstructor()->getMock();
$driver->method('isExists')->willReturn(false);

$write = $this->getMockBuilder(Write::class)->disableOriginalConstructor()->getMock();
$write->method('getDriver')->willReturn($driver);

$writeFactory = $this->getMockBuilder(WriteFactory::class)->disableOriginalConstructor()->getMock();
$writeFactory->method('create')->willReturn($write);

$this->classToTest = $this->objectManager->getObject(ClassToTest::class, [
'applePayHelper' => $this->applePayHelper,
'filesystem' => $filesystem
'filesystem' => $filesystem,
'writeFactory' => $writeFactory,
]);
}

Expand All @@ -80,6 +93,7 @@ public function testBeforeSave()

$this->applePayHelper->method('getApplePayUploadPath')->willReturn($uploadPath);
$this->tmpDirectory->method('getRelativePath')->willReturn("Existing path");
$this->tmpDirectory->method('getAbsolutePath')->willReturn($uploadPath);
$this->tmpDirectory->method('isExist')->willReturn(true);
$this->tmpDirectory->method('stat')->willReturn(['size' => 100]);

Expand All @@ -89,11 +103,9 @@ public function testBeforeSave()
'tmp_name' => $uploadPath."upload.tmp",
];
$this->classToTest->setValue($fileUpload);

$result = $this->classToTest->beforeSave();
$this->assertInstanceOf(ClassToTest::class, $result);

rmdir($uploadPath);
}

public function testBeforeSaveException()
Expand All @@ -102,6 +114,7 @@ public function testBeforeSaveException()

$this->applePayHelper->method('getApplePayUploadPath')->willReturn($uploadPath);
$this->tmpDirectory->method('getRelativePath')->willReturn("Existing path");
$this->tmpDirectory->method('getAbsolutePath')->willReturn($uploadPath);
$this->tmpDirectory->method('isExist')->willReturn(true);
$this->tmpDirectory->method('stat')->willReturn(['size' => false]);

Expand Down