Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FraudHandlerTest.php
Go to the documentation of this file.
1 <?php
7 
13 
14 class FraudHandlerTest extends \PHPUnit\Framework\TestCase
15 {
19  private $paymentMock;
20 
24  private $responseMock;
25 
29  private $fraudHandler;
30 
34  private $paypalInfoManagerMock;
35 
36  protected function setUp()
37  {
38  $this->paymentMock = $this->getMockBuilder(\Magento\Payment\Model\InfoInterface::class)
39  ->getMock();
40  $this->responseMock = $this->getMockBuilder(\Magento\Framework\DataObject::class)
41  ->disableOriginalConstructor()
42  ->getMock();
43  $this->paypalInfoManagerMock = $this->getMockBuilder(\Magento\Paypal\Model\Info::class)
44  ->disableOriginalConstructor()
45  ->getMock();
46 
47  $this->fraudHandler = new FraudHandler(
48  $this->paypalInfoManagerMock,
49  new \Magento\Framework\Xml\Security()
50  );
51  }
52 
54  {
55  $this->responseMock->expects($this->once())
56  ->method('getData')
57  ->with('result')
58  ->willReturn(Payflowpro::STATUS_APPROVED);
59 
60  $this->paypalInfoManagerMock->expects($this->never())
61  ->method('importToPayment');
62 
63  $this->fraudHandler->handle($this->paymentMock, $this->responseMock);
64  }
65 
69  public function testHandle($message, $rulesString, $existingFrauds, $expectedMessage)
70  {
71  $this->responseMock->expects($this->atLeastOnce())
72  ->method('getData')
73  ->willReturnMap(
74  [
76  [FraudHandler::FRAUD_RULES_XML, null, $rulesString],
78  ]
79  );
80 
81  $this->paymentMock->expects($this->once())
82  ->method('getAdditionalInformation')
83  ->with(Info::FRAUD_FILTERS)
84  ->willReturn($existingFrauds);
85  $this->paypalInfoManagerMock->expects($this->once())
86  ->method('importToPayment')
87  ->with(
88  [
89  Info::FRAUD_FILTERS => $expectedMessage
90  ]
91  );
92 
93  $this->fraudHandler->handle($this->paymentMock, $this->responseMock);
94  }
95 
99  public function handleMessagesDataProvider()
100  {
101  return [
102  ['Fraud message', null, null, ['RESPMSG' => 'Fraud message']],
103  [
104  'New fraud message',
105  '<?xml version="1.0"?>',
106  ['RESPMSG' => 'Existing fraud message'],
107  ['RESPMSG' => 'Existing fraud message']
108  ],
109  [
110  'New fraud message',
111  $this->getRulesXmlString(),
112  [
113  'Total Purchase Price Ceiling' => 'Existing fraud message',
114  'RESPMSG' => 'Existing fraud message'
115  ],
116  array_merge(
117  $this->getRulesExpectedDictionary(),
118  [
119  'Total Purchase Price Ceiling' => 'Existing fraud message',
120  'RESPMSG' => 'Existing fraud message'
121  ]
122  )
123  ]
124  ];
125  }
126 
133  private function getRulesXmlString($fileName = 'fps_prexmldata.xml')
134  {
135  return file_get_contents(__DIR__ . '/_files/' . $fileName);
136  }
137 
143  private function getRulesExpectedDictionary()
144  {
145  return [
146  'Total Purchase Price Ceiling' =>
147  'The purchase amount of 7501 is greater than the ceiling value set of 7500',
148  'Total ItemCeiling' =>
149  '16 items were ordered, which is overthe maximum allowed quantity of 15',
150  'Shipping/BillingMismatch' =>
151  'Thebilling and shipping addresses did not match',
152  'BIN Risk List Match' =>
153  'The card number is in a high risk bin list',
154  'Zip Risk List Match' =>
155  'High risk shipping zip',
156  'USPS Address Validation Failure' =>
157  'The billing address is not a valid USAddress'
158  ];
159  }
160 
164  public function testHandleXXEXml()
165  {
166  $file = __DIR__ . '/_files/xxe-xml.txt';
167  $rulesString = str_replace('{file}', $file, $this->getRulesXmlString('xxe_fps_prexmldata.xml'));
168 
169  $this->responseMock->expects($this->atLeastOnce())
170  ->method('getData')
171  ->willReturnMap(
172  [
173  [FraudHandler::RESPONSE_MESSAGE, null, 'New fraud message'],
174  [FraudHandler::FRAUD_RULES_XML, null, $rulesString],
176  ]
177  );
178  $this->paymentMock->expects($this->once())
179  ->method('getAdditionalInformation')
180  ->with(Info::FRAUD_FILTERS)
181  ->willReturn(
182  [
183  'Total Purchase Price Ceiling' => 'Existing fraud message',
184  'RESPMSG' => 'Existing fraud message'
185  ]
186  );
187 
188  $this->paypalInfoManagerMock->expects($this->once())
189  ->method('importToPayment')
190  ->with(
191  [
193  'RESPMSG' => 'Existing fraud message',
194  'Total Purchase Price Ceiling' => 'Existing fraud message'
195  ]
196  ]
197  );
198 
199  $this->fraudHandler->handle($this->paymentMock, $this->responseMock);
200  }
201 }
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
$message
$fileName
Definition: translate.phtml:15
testHandle($message, $rulesString, $existingFrauds, $expectedMessage)