Skip to content

Commit 793d81a

Browse files
author
dyushkin
committed
Merge branch 'MAGETWO-37282' into PR-37282
2 parents 183254f + 7f9477a commit 793d81a

File tree

7 files changed

+106
-31
lines changed

7 files changed

+106
-31
lines changed

app/code/Magento/Payment/Model/Method/AbstractMethod.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ abstract class AbstractMethod extends \Magento\Framework\Model\AbstractExtensibl
208208
protected $_scopeConfig;
209209

210210
/**
211-
* @var \Psr\Log\LoggerInterface
211+
* @var Logger
212212
*/
213213
protected $logger;
214214

@@ -219,9 +219,11 @@ abstract class AbstractMethod extends \Magento\Framework\Model\AbstractExtensibl
219219
* @param \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory
220220
* @param \Magento\Payment\Helper\Data $paymentData
221221
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
222+
* @param Logger $logger
222223
* @param \Magento\Framework\Model\Resource\AbstractResource $resource
223224
* @param \Magento\Framework\Data\Collection\Db $resourceCollection
224225
* @param array $data
226+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
225227
*/
226228
public function __construct(
227229
\Magento\Framework\Model\Context $context,
@@ -230,6 +232,7 @@ public function __construct(
230232
\Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
231233
\Magento\Payment\Helper\Data $paymentData,
232234
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
235+
\Magento\Payment\Model\Method\Logger $logger,
233236
\Magento\Framework\Model\Resource\AbstractResource $resource = null,
234237
\Magento\Framework\Data\Collection\Db $resourceCollection = null,
235238
array $data = []
@@ -245,7 +248,7 @@ public function __construct(
245248
);
246249
$this->_paymentData = $paymentData;
247250
$this->_scopeConfig = $scopeConfig;
248-
$this->logger = $context->getLogger();
251+
$this->logger = $logger;
249252
$this->initializeData($data);
250253
}
251254

@@ -880,14 +883,12 @@ public function getConfigPaymentAction()
880883
/**
881884
* Log debug data to file
882885
*
883-
* @param mixed $debugData
886+
* @param array $debugData
884887
* @return void
885888
*/
886889
protected function _debug($debugData)
887890
{
888-
if ($this->getDebugFlag()) {
889-
$this->logger->debug(var_export($debugData, true));
890-
}
891+
$this->logger->debug($debugData, $this->getDebugReplacePrivateDataKeys(), $this->getDebugFlag());
891892
}
892893

893894
/**
@@ -934,4 +935,14 @@ public function setExtensionAttributes(\Magento\Quote\Api\Data\PaymentMethodExte
934935
{
935936
return $this->_setExtensionAttributes($extensionAttributes);
936937
}
938+
939+
/**
940+
* Return replace keys for debug data
941+
*
942+
* @return array
943+
*/
944+
public function getDebugReplacePrivateDataKeys()
945+
{
946+
return (array) $this->_debugReplacePrivateDataKeys;
947+
}
937948
}

app/code/Magento/Payment/Model/Method/Cc.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class Cc extends \Magento\Payment\Model\Method\AbstractMethod
4343
* @param \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory
4444
* @param \Magento\Payment\Helper\Data $paymentData
4545
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
46+
* @param Logger $logger
4647
* @param \Magento\Framework\Module\ModuleListInterface $moduleList
4748
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
4849
* @param \Magento\Framework\Model\Resource\AbstractResource $resource
@@ -57,6 +58,7 @@ public function __construct(
5758
\Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
5859
\Magento\Payment\Helper\Data $paymentData,
5960
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
61+
\Magento\Payment\Model\Method\Logger $logger,
6062
\Magento\Framework\Module\ModuleListInterface $moduleList,
6163
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
6264
\Magento\Framework\Model\Resource\AbstractResource $resource = null,
@@ -70,6 +72,7 @@ public function __construct(
7072
$customAttributeFactory,
7173
$paymentData,
7274
$scopeConfig,
75+
$logger,
7376
$resource,
7477
$resourceCollection,
7578
$data

app/code/Magento/Payment/Model/Method/Free.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class Free extends \Magento\Payment\Model\Method\AbstractMethod
5050
* @param \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory
5151
* @param \Magento\Payment\Helper\Data $paymentData
5252
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
53+
* @param Logger $logger
5354
* @param PriceCurrencyInterface $priceCurrency
5455
* @param \Magento\Framework\Model\Resource\AbstractResource $resource
5556
* @param \Magento\Framework\Data\Collection\Db $resourceCollection
@@ -63,6 +64,7 @@ public function __construct(
6364
\Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
6465
\Magento\Payment\Helper\Data $paymentData,
6566
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
67+
\Magento\Payment\Model\Method\Logger $logger,
6668
PriceCurrencyInterface $priceCurrency,
6769
\Magento\Framework\Model\Resource\AbstractResource $resource = null,
6870
\Magento\Framework\Data\Collection\Db $resourceCollection = null,
@@ -75,6 +77,7 @@ public function __construct(
7577
$customAttributeFactory,
7678
$paymentData,
7779
$scopeConfig,
80+
$logger,
7881
$resource,
7982
$resourceCollection,
8083
$data

app/code/Magento/Payment/Model/Method/Logger.php

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515
class Logger
1616
{
17+
const DEBUG_KEYS_MASK = '****';
1718
/**
1819
* @var LoggerInterface
1920
*/
@@ -30,15 +31,40 @@ public function __construct(LoggerInterface $logger)
3031
/**
3132
* Logs payment related information used for debug
3233
*
33-
* @param mixed $logData
34-
* @param ConfigInterface $config
35-
*
34+
* @param array $debugData
35+
* @param array $debugReplaceKeys
36+
* @param bool $debugFlag
3637
* @return void
3738
*/
38-
public function debug($logData, ConfigInterface $config)
39+
public function debug(array $debugData, array $debugReplaceKeys, $debugFlag)
3940
{
40-
if ($config->getConfigValue('debug')) {
41-
$this->logger->debug(var_export($logData, true));
41+
if ($debugFlag == true && !empty($debugData) && !empty($debugReplaceKeys)) {
42+
$debugData = $this->filterDebugData(
43+
$debugData,
44+
$debugReplaceKeys
45+
);
46+
$this->logger->debug(var_export($debugData, true));
47+
}
48+
}
49+
50+
/**
51+
* Recursive filter data by private conventions
52+
*
53+
* @param array $debugData
54+
* @param array $debugReplacePrivateDataKeys
55+
* @return array
56+
*/
57+
protected function filterDebugData(array $debugData, array $debugReplacePrivateDataKeys)
58+
{
59+
$debugReplacePrivateDataKeys = array_map('strtolower', $debugReplacePrivateDataKeys);
60+
61+
foreach (array_keys($debugData) as $key) {
62+
if (in_array(strtolower($key), $debugReplacePrivateDataKeys)) {
63+
$debugData[$key] = self::DEBUG_KEYS_MASK;
64+
} elseif (is_array($debugData[$key])) {
65+
$debugData[$key] = $this->filterDebugData($debugData[$key], $debugReplacePrivateDataKeys);
66+
}
4267
}
68+
return $debugData;
4369
}
4470
}

app/code/Magento/Payment/Test/Unit/Model/Method/AbstractMethodTest.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ class AbstractMethodTest extends \PHPUnit_Framework_TestCase
3535
*/
3636
protected $quoteMock;
3737

38+
/**
39+
* @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
40+
*/
41+
protected $loggerMock;
42+
3843
protected function setUp()
3944
{
4045
$this->scopeConfigMock = $this->getMockBuilder('Magento\Framework\App\Config\ScopeConfigInterface')
@@ -50,22 +55,35 @@ protected function setUp()
5055
->disableOriginalConstructor()
5156
->setMethods(['getEventDispatcher'])
5257
->getMock();
53-
5458
$contextMock->expects($this->once())
5559
->method('getEventDispatcher')
5660
->willReturn($this->eventManagerMock);
61+
$this->loggerMock = $this->getMockBuilder('\Magento\Payment\Model\Method\Logger')
62+
->setConstructorArgs([$this->getMockForAbstractClass('Psr\Log\LoggerInterface')])
63+
->setMethods(['debug'])
64+
->getMock();
5765

5866
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
59-
6067
$this->payment = $helper->getObject(
6168
'Magento\Payment\Test\Unit\Model\Method\AbstractMethod\Stub',
6269
[
6370
'scopeConfig' => $this->scopeConfigMock,
64-
'context' => $contextMock
71+
'context' => $contextMock,
72+
'logger' => $this->loggerMock
6573
]
6674
);
6775
}
6876

77+
public function testDebugData()
78+
{
79+
$debugData = ['masked' => '123'];
80+
$this->loggerMock->expects($this->once())
81+
->method('debug')
82+
->with($this->equalTo($debugData));
83+
84+
$this->payment->debugData($debugData);
85+
}
86+
6987
/**
7088
* @param bool $result
7189
*

app/code/Magento/Payment/Test/Unit/Model/Method/FreeTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,18 @@ protected function setUp()
3939
);
4040
$customAttributeFactory = $this->getMock('\Magento\Framework\Api\AttributeValueFactory', [], [], '', false);
4141

42+
$loggerMock = $this->getMockBuilder('\Magento\Payment\Model\Method\Logger')
43+
->setConstructorArgs([$this->getMockForAbstractClass('Psr\Log\LoggerInterface')])
44+
->getMock();
45+
4246
$this->methodFree = new \Magento\Payment\Model\Method\Free(
4347
$context,
4448
$registry,
4549
$extensionAttributesFactory,
4650
$customAttributeFactory,
4751
$paymentData,
4852
$this->scopeConfig,
53+
$loggerMock,
4954
$this->currencyPrice
5055
);
5156
}

app/code/Magento/Payment/Test/Unit/Model/Method/LoggerTest.php

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,48 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
1818
/** @var LoggerInterface | \PHPUnit_Framework_MockObject_MockObject */
1919
private $loggerMock;
2020

21-
/** @var ConfigInterface | \PHPUnit_Framework_MockObject_MockObject */
22-
private $configMock;
23-
2421
protected function setUp()
2522
{
2623
$this->loggerMock = $this->getMockForAbstractClass('Psr\Log\LoggerInterface');
27-
$this->configMock = $this->getMockForAbstractClass('Magento\Payment\Model\Method\ConfigInterface');
28-
2924
$this->logger = new Logger($this->loggerMock);
3025
}
3126

3227
public function testDebugOn()
3328
{
34-
$this->configMock->expects($this->once())
35-
->method('getConfigValue')
36-
->with('debug')
37-
->willReturn(true);
29+
$debugData =
30+
[
31+
'request' => ['masked' => '123', 'unmasked' => '123']
32+
];
33+
$expectedDebugData =
34+
[
35+
'request' => ['masked' => Logger::DEBUG_KEYS_MASK, 'unmasked' => '123']
36+
];
37+
$debugReplaceKeys =
38+
[
39+
'masked'
40+
];
41+
3842
$this->loggerMock->expects($this->once())
3943
->method('debug')
40-
->with("'test_value'");
44+
->with(var_export($expectedDebugData, true));
4145

42-
$this->logger->debug('test_value', $this->configMock);
46+
$this->logger->debug($debugData, $debugReplaceKeys, true);
4347
}
4448

4549
public function testDebugOff()
4650
{
47-
$this->configMock->expects($this->once())
48-
->method('getConfigValue')
49-
->with('debug')
50-
->willReturn(false);
51+
$debugData =
52+
[
53+
'request' => ['masked' => '123', 'unmasked' => '123']
54+
];
55+
$debugReplaceKeys =
56+
[
57+
'masked'
58+
];
59+
5160
$this->loggerMock->expects($this->never())
5261
->method('debug');
5362

54-
$this->logger->debug('', $this->configMock);
63+
$this->logger->debug($debugData, $debugReplaceKeys, false);
5564
}
5665
}

0 commit comments

Comments
 (0)