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

Fix "area code is not set" in all commands that depend on Payment/Helper/Data #33726

Merged
merged 10 commits into from
Oct 28, 2021
93 changes: 57 additions & 36 deletions app/code/Magento/Payment/Helper/Data.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
<?php
declare(strict_types=1);
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Payment\Helper;

use Exception;
use Magento\Framework\App\Area;
use Magento\Framework\App\Config\Initial;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Framework\Exception\LocalizedException;
use Magento\Payment\Model\Config;
use Magento\Payment\Model\Method\Factory;
use Magento\Payment\Model\Method\Free;
use Magento\Quote\Model\Quote;
use Magento\Store\Model\App\Emulation;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\Store;
use Magento\Payment\Block\Form;
use Magento\Payment\Model\InfoInterface;
Expand All @@ -14,6 +27,7 @@
use Magento\Framework\View\LayoutFactory;
use Magento\Payment\Model\Method\AbstractMethod;
use Magento\Payment\Model\MethodInterface;
use UnexpectedValueException;

/**
* Payment module base helper
Expand All @@ -22,61 +36,68 @@
* @api
* @since 100.0.2
*/
class Data extends \Magento\Framework\App\Helper\AbstractHelper
class Data extends AbstractHelper
{
const XML_PATH_PAYMENT_METHODS = 'payment';

/**
* @var \Magento\Payment\Model\Config
* @var Config
*/
protected $_paymentConfig;

/**
* Layout
*
* @var \Magento\Framework\View\LayoutInterface
* @deprecated
* @var LayoutInterface
*/
protected $_layout;

/**
* LayoutFactory
*
* @var LayoutFactory
*/
private $layoutFactory;

/**
* Factory for payment method models
*
* @var \Magento\Payment\Model\Method\Factory
* @var Factory
*/
protected $_methodFactory;

/**
* App emulation model
*
* @var \Magento\Store\Model\App\Emulation
* @var Emulation
*/
protected $_appEmulation;

/**
* @var \Magento\Framework\App\Config\Initial
* @var Initial
*/
protected $_initialConfig;

/**
* Construct
*
* @param \Magento\Framework\App\Helper\Context $context
* @param Context $context
* @param LayoutFactory $layoutFactory
* @param \Magento\Payment\Model\Method\Factory $paymentMethodFactory
* @param \Magento\Store\Model\App\Emulation $appEmulation
* @param \Magento\Payment\Model\Config $paymentConfig
* @param \Magento\Framework\App\Config\Initial $initialConfig
* @param Factory $paymentMethodFactory
* @param Emulation $appEmulation
* @param Config $paymentConfig
* @param Initial $initialConfig
*/
public function __construct(
\Magento\Framework\App\Helper\Context $context,
Context $context,
LayoutFactory $layoutFactory,
\Magento\Payment\Model\Method\Factory $paymentMethodFactory,
\Magento\Store\Model\App\Emulation $appEmulation,
\Magento\Payment\Model\Config $paymentConfig,
\Magento\Framework\App\Config\Initial $initialConfig
Factory $paymentMethodFactory,
Emulation $appEmulation,
Config $paymentConfig,
Initial $initialConfig
) {
parent::__construct($context);
$this->_layout = $layoutFactory->create();
$this->layoutFactory = $layoutFactory;
$this->_methodFactory = $paymentMethodFactory;
$this->_appEmulation = $appEmulation;
$this->_paymentConfig = $paymentConfig;
Expand All @@ -99,18 +120,18 @@ protected function getMethodModelConfigName($code)
*
* @param string $code
*
* @throws \Magento\Framework\Exception\LocalizedException
* @return MethodInterface
* @throws LocalizedException
*/
public function getMethodInstance($code)
{
$class = $this->scopeConfig->getValue(
$this->getMethodModelConfigName($code),
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
ScopeInterface::SCOPE_STORE
);

if (!$class) {
throw new \UnexpectedValueException('Payment model name is not provided in config!');
throw new UnexpectedValueException('Payment model name is not provided in config!');
}

return $this->_methodFactory->create($class);
Expand All @@ -133,7 +154,7 @@ public function getStoreMethods($store = null, $quote = null)
foreach (array_keys($methods) as $code) {
$model = $this->scopeConfig->getValue(
$this->getMethodModelConfigName($code),
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
ScopeInterface::SCOPE_STORE,
$store
);
if (!$model) {
Expand Down Expand Up @@ -178,12 +199,12 @@ public function getMethodFormBlock(MethodInterface $method, LayoutInterface $lay
* Retrieve payment information block
*
* @param InfoInterface $info
* @param \Magento\Framework\View\LayoutInterface $layout
* @param LayoutInterface $layout
* @return Template
*/
public function getInfoBlock(InfoInterface $info, LayoutInterface $layout = null)
{
$layout = $layout ?: $this->_layout;
$layout = $layout ?: $this->layoutFactory->create();
$blockType = $info->getMethodInstance()->getInfoBlockType();
$block = $layout->createBlock($blockType);
$block->setInfo($info);
Expand All @@ -196,21 +217,21 @@ public function getInfoBlock(InfoInterface $info, LayoutInterface $layout = null
* @param InfoInterface $info
* @param int $storeId
* @return string
* @throws \Exception
* @throws Exception
*/
public function getInfoBlockHtml(InfoInterface $info, $storeId)
{
$this->_appEmulation->startEnvironmentEmulation($storeId, \Magento\Framework\App\Area::AREA_FRONTEND, true);
$this->_appEmulation->startEnvironmentEmulation($storeId, Area::AREA_FRONTEND, true);

try {
// Retrieve specified view block from appropriate design package (depends on emulated store)
$paymentBlock = $this->getInfoBlock($info);
$paymentBlock->setArea(\Magento\Framework\App\Area::AREA_FRONTEND)
$paymentBlock->setArea(Area::AREA_FRONTEND)
->setIsSecureMode(true);
$paymentBlock->getMethod()
->setStore($storeId);
$paymentBlockHtml = $paymentBlock->toHtml();
} catch (\Exception $exception) {
} catch (Exception $exception) {
$this->_appEmulation->stopEnvironmentEmulation();
throw $exception;
}
Expand Down Expand Up @@ -313,8 +334,8 @@ public function getPaymentMethodList($sorted = true, $asLabelValue = false, $wit
public function isZeroSubTotal($store = null)
{
return $this->scopeConfig->getValue(
\Magento\Payment\Model\Method\Free::XML_PATH_PAYMENT_FREE_ACTIVE,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
Free::XML_PATH_PAYMENT_FREE_ACTIVE,
ScopeInterface::SCOPE_STORE,
$store
);
}
Expand All @@ -328,8 +349,8 @@ public function isZeroSubTotal($store = null)
public function getZeroSubTotalOrderStatus($store = null)
{
return $this->scopeConfig->getValue(
\Magento\Payment\Model\Method\Free::XML_PATH_PAYMENT_FREE_ORDER_STATUS,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
Free::XML_PATH_PAYMENT_FREE_ORDER_STATUS,
ScopeInterface::SCOPE_STORE,
$store
);
}
Expand All @@ -343,8 +364,8 @@ public function getZeroSubTotalOrderStatus($store = null)
public function getZeroSubTotalPaymentAutomaticInvoice($store = null)
{
return $this->scopeConfig->getValue(
\Magento\Payment\Model\Method\Free::XML_PATH_PAYMENT_FREE_PAYMENT_ACTION,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
Free::XML_PATH_PAYMENT_FREE_PAYMENT_ACTION,
ScopeInterface::SCOPE_STORE,
$store
);
}
Expand All @@ -359,9 +380,9 @@ public function getZeroSubTotalPaymentAutomaticInvoice($store = null)
private function getMethodStoreTitle(string $code, ?int $storeId = null): string
{
$configPath = sprintf('%s/%s/title', self::XML_PATH_PAYMENT_METHODS, $code);
return (string) $this->scopeConfig->getValue(
return (string)$this->scopeConfig->getValue(
$configPath,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
ScopeInterface::SCOPE_STORE,
$storeId
);
}
Expand Down
11 changes: 9 additions & 2 deletions app/code/Magento/Payment/Test/Unit/Helper/DataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Framework\TestFramework\Unit\Matcher\MethodInvokedAtIndex;
use Magento\Framework\View\Element\BlockInterface;
use Magento\Framework\View\LayoutFactory;
use Magento\Framework\View\LayoutInterface;
use Magento\Payment\Helper\Data;
use Magento\Payment\Model\Info;
Expand Down Expand Up @@ -44,6 +45,11 @@ class DataTest extends TestCase
*/
private $appEmulation;

/**
* @var LayoutFactory|MockObject
*/
private $layoutFactoryMock;

protected function setUp(): void
{
$objectManagerHelper = new ObjectManager($this);
Expand All @@ -53,8 +59,7 @@ protected function setUp(): void
$context = $arguments['context'];
$this->scopeConfig = $context->getScopeConfig();
$this->layoutMock = $this->getMockForAbstractClass(LayoutInterface::class);
$layoutFactoryMock = $arguments['layoutFactory'];
$layoutFactoryMock->expects($this->once())->method('create')->willReturn($this->layoutMock);
$this->layoutFactoryMock = $arguments['layoutFactory'];

$this->methodFactory = $arguments['paymentMethodFactory'];
$this->appEmulation = $arguments['appEmulation'];
Expand Down Expand Up @@ -214,6 +219,7 @@ public function testGetInfoBlock()
$this->layoutMock->expects($this->once())->method('createBlock')
->with($blockType)
->willReturn($blockMock);
$this->layoutFactoryMock->expects($this->once())->method('create')->willReturn($this->layoutMock);
$blockMock->expects($this->once())->method('setInfo')->with($infoMock);

$this->assertSame($blockMock, $this->helper->getInfoBlock($infoMock));
Expand Down Expand Up @@ -242,6 +248,7 @@ public function testGetInfoBlockHtml()
$this->layoutMock->expects($this->once())->method('createBlock')
->with($blockType)
->willReturn($paymentBlockMock);
$this->layoutFactoryMock->expects($this->once())->method('create')->willReturn($this->layoutMock);
$paymentBlockMock->expects($this->once())->method('setInfo')->with($infoMock);
$paymentBlockMock->expects($this->once())->method('setArea')
->with(Area::AREA_FRONTEND)
Expand Down