Skip to content

Commit 70d2287

Browse files
authored
Merge pull request magento#4726 from magento-chaika/Chaika-PR-2019-09-03-2.3
Chaika-PR-2019-09-03-2.3
2 parents a0c6ad6 + 7529fab commit 70d2287

File tree

7 files changed

+124
-28
lines changed

7 files changed

+124
-28
lines changed

app/code/Magento/OfflinePayments/Observer/BeforeOrderPaymentSaveObserver.php

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,38 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

7-
/**
8-
* OfflinePayments Observer
9-
*/
108
namespace Magento\OfflinePayments\Observer;
119

1210
use Magento\Framework\Event\ObserverInterface;
1311
use Magento\OfflinePayments\Model\Banktransfer;
1412
use Magento\OfflinePayments\Model\Cashondelivery;
1513
use Magento\OfflinePayments\Model\Checkmo;
14+
use Magento\Sales\Model\Order\Payment;
1615

16+
/**
17+
* Sets payment additional information.
18+
*/
1719
class BeforeOrderPaymentSaveObserver implements ObserverInterface
1820
{
1921
/**
20-
* Sets current instructions for bank transfer account
22+
* Sets current instructions for bank transfer account.
2123
*
2224
* @param \Magento\Framework\Event\Observer $observer
2325
* @return void
26+
* @throws \Magento\Framework\Exception\LocalizedException
2427
*/
25-
public function execute(\Magento\Framework\Event\Observer $observer)
28+
public function execute(\Magento\Framework\Event\Observer $observer): void
2629
{
27-
/** @var \Magento\Sales\Model\Order\Payment $payment */
30+
/** @var Payment $payment */
2831
$payment = $observer->getEvent()->getPayment();
2932
$instructionMethods = [
3033
Banktransfer::PAYMENT_METHOD_BANKTRANSFER_CODE,
3134
Cashondelivery::PAYMENT_METHOD_CASHONDELIVERY_CODE
3235
];
3336
if (in_array($payment->getMethod(), $instructionMethods)) {
34-
$payment->setAdditionalInformation(
35-
'instructions',
36-
$payment->getMethodInstance()->getInstructions()
37-
);
37+
$payment->setAdditionalInformation('instructions', $this->getInstructions($payment));
3838
} elseif ($payment->getMethod() === Checkmo::PAYMENT_METHOD_CHECKMO_CODE) {
3939
$methodInstance = $payment->getMethodInstance();
4040
if (!empty($methodInstance->getPayableTo())) {
@@ -45,4 +45,17 @@ public function execute(\Magento\Framework\Event\Observer $observer)
4545
}
4646
}
4747
}
48+
49+
/**
50+
* Retrieve store-specific payment method instructions, or already saved if exists.
51+
*
52+
* @param Payment $payment
53+
* @return string|null
54+
* @throws \Magento\Framework\Exception\LocalizedException
55+
*/
56+
private function getInstructions(Payment $payment): ?string
57+
{
58+
return $payment->getAdditionalInformation('instructions')
59+
?: $payment->getMethodInstance()->getConfigData('instructions', $payment->getOrder()->getStoreId());
60+
}
4861
}

app/code/Magento/OfflinePayments/Test/Unit/Observer/BeforeOrderPaymentSaveObserverTest.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\OfflinePayments\Model\Banktransfer;
1212
use Magento\OfflinePayments\Model\Cashondelivery;
1313
use Magento\OfflinePayments\Observer\BeforeOrderPaymentSaveObserver;
14+
use Magento\Sales\Model\Order;
1415
use Magento\Sales\Model\Order\Payment;
1516
use PHPUnit_Framework_MockObject_MockObject as MockObject;
1617
use Magento\OfflinePayments\Model\Checkmo;
@@ -76,19 +77,12 @@ public function testBeforeOrderPaymentSaveWithInstructions($methodCode)
7677
$this->payment->expects(self::once())
7778
->method('getMethod')
7879
->willReturn($methodCode);
80+
$this->payment->method('getAdditionalInformation')
81+
->with('instructions')
82+
->willReturn('payment configuration');
7983
$this->payment->expects(self::once())
8084
->method('setAdditionalInformation')
8185
->with('instructions', 'payment configuration');
82-
$method = $this->getMockBuilder(Banktransfer::class)
83-
->disableOriginalConstructor()
84-
->getMock();
85-
86-
$method->expects(self::once())
87-
->method('getInstructions')
88-
->willReturn('payment configuration');
89-
$this->payment->expects(self::once())
90-
->method('getMethodInstance')
91-
->willReturn($method);
9286

9387
$this->_model->execute($this->observer);
9488
}

app/code/Magento/OfflinePayments/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"php": "~7.1.3||~7.2.0||~7.3.0",
99
"magento/framework": "*",
1010
"magento/module-checkout": "*",
11-
"magento/module-payment": "*"
11+
"magento/module-payment": "*",
12+
"magento/module-sales": "*"
1213
},
1314
"suggest": {
1415
"magento/module-config": "*"

app/code/Magento/Payment/Block/Info/Instructions.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ class Instructions extends \Magento\Payment\Block\Info
2525
*/
2626
protected $_template = 'Magento_Payment::info/instructions.phtml';
2727

28+
/**
29+
* Gets payment method title for appropriate store.
30+
*
31+
* @return string
32+
* @throws \Magento\Framework\Exception\LocalizedException
33+
*/
34+
public function getTitle()
35+
{
36+
return $this->getInfo()->getAdditionalInformation('method_title')
37+
?: $this->getMethod()->getConfigData('title', $this->getInfo()->getOrder()->getStoreId());
38+
}
39+
2840
/**
2941
* Get instructions text from order payment
3042
* (or from config, if instructions are missed in payment)

app/code/Magento/Payment/Test/Unit/Block/Info/InstructionsTest.php

Lines changed: 81 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
* See COPYING.txt for license details.
55
*/
66

7-
/**
8-
* Test class for \Magento\Payment\Block\Info\Instructions
9-
*/
107
namespace Magento\Payment\Test\Unit\Block\Info;
118

9+
use Magento\Payment\Model\MethodInterface;
10+
use Magento\Sales\Model\Order;
11+
use PHPUnit\Framework\MockObject\MockObject;
12+
1213
class InstructionsTest extends \PHPUnit\Framework\TestCase
1314
{
1415
/**
@@ -25,10 +26,59 @@ protected function setUp()
2526
{
2627
$context = $this->createMock(\Magento\Framework\View\Element\Template\Context::class);
2728
$this->_instructions = new \Magento\Payment\Block\Info\Instructions($context);
28-
$this->_info = $this->createMock(\Magento\Payment\Model\Info::class);
29+
$this->_info = $this->getMockBuilder(\Magento\Payment\Model\Info::class)
30+
->setMethods(
31+
[
32+
'getOrder',
33+
'getAdditionalInformation',
34+
'getMethodInstance'
35+
]
36+
)
37+
->disableOriginalConstructor()
38+
->getMock();
2939
$this->_instructions->setData('info', $this->_info);
3040
}
3141

42+
/**
43+
* @return void
44+
* @throws \Magento\Framework\Exception\LocalizedException
45+
*/
46+
public function testGetTitleFromPaymentAdditionalData()
47+
{
48+
$this->_info->method('getAdditionalInformation')
49+
->with('method_title')
50+
->willReturn('payment_method_title');
51+
52+
$this->getMethod()->expects($this->never())
53+
->method('getConfigData');
54+
55+
$this->assertEquals($this->_instructions->getTitle(), 'payment_method_title');
56+
}
57+
58+
/**
59+
* @return void
60+
* @throws \Magento\Framework\Exception\LocalizedException
61+
*/
62+
public function testGetTitleFromPaymentMethodConfig()
63+
{
64+
$this->_info->method('getAdditionalInformation')
65+
->with('method_title')
66+
->willReturn(null);
67+
68+
$this->getMethod()->expects($this->once())
69+
->method('getConfigData')
70+
->with('title', null)
71+
->willReturn('payment_method_title');
72+
73+
$order = $this->getOrder();
74+
$this->_info->method('getOrder')->willReturn($order);
75+
76+
$this->assertEquals($this->_instructions->getTitle(), 'payment_method_title');
77+
}
78+
79+
/**
80+
* @return void
81+
*/
3282
public function testGetInstructionAdditionalInformation()
3383
{
3484
$this->_info->expects($this->once())
@@ -41,10 +91,13 @@ public function testGetInstructionAdditionalInformation()
4191
$this->assertEquals('get the instruction here', $this->_instructions->getInstructions());
4292
}
4393

94+
/**
95+
* @return void
96+
*/
4497
public function testGetInstruction()
4598
{
4699
$methodInstance = $this->getMockBuilder(
47-
\Magento\Payment\Model\MethodInterface::class
100+
MethodInterface::class
48101
)->getMockForAbstractClass();
49102
$methodInstance->expects($this->once())
50103
->method('getConfigData')
@@ -59,4 +112,27 @@ public function testGetInstruction()
59112
->willReturn($methodInstance);
60113
$this->assertEquals('get the instruction here', $this->_instructions->getInstructions());
61114
}
115+
116+
/**
117+
* @return MethodInterface|MockObject
118+
*/
119+
private function getMethod()
120+
{
121+
$method = $this->getMockBuilder(MethodInterface::class)
122+
->getMockForAbstractClass();
123+
$this->_info->method('getMethodInstance')
124+
->willReturn($method);
125+
126+
return $method;
127+
}
128+
129+
/**
130+
* @return Order|MockObject
131+
*/
132+
private function getOrder()
133+
{
134+
return $this->getMockBuilder(Order::class)
135+
->disableOriginalConstructor()
136+
->getMock();
137+
}
62138
}

app/code/Magento/Payment/view/adminhtml/templates/info/instructions.phtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* @see \Magento\Payment\Block\Info
1010
*/
1111
?>
12-
<p><?= $block->escapeHtml($block->getMethod()->getTitle()) ?></p>
12+
<p><?= $block->escapeHtml($block->getTitle()) ?></p>
1313
<?php if ($block->getInstructions()) : ?>
1414
<table>
1515
<tbody>

app/code/Magento/Payment/view/frontend/templates/info/instructions.phtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*/
1111
?>
1212
<dl class="payment-method">
13-
<dt class="title"><?= $block->escapeHtml($block->getMethod()->getTitle()) ?></dt>
13+
<dt class="title"><?= $block->escapeHtml($block->getTitle()) ?></dt>
1414
<?php if ($block->getInstructions()) : ?>
1515
<dd class="content"><?= /* @noEscape */ nl2br($block->escapeHtml($block->getInstructions())) ?></dd>
1616
<?php endif; ?>

0 commit comments

Comments
 (0)