1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* PAYONE Magento 2 Connector is free software: you can redistribute it and/or modify |
5
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
6
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
7
|
|
|
* (at your option) any later version. |
8
|
|
|
* |
9
|
|
|
* PAYONE Magento 2 Connector is distributed in the hope that it will be useful, |
10
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
11
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12
|
|
|
* GNU Lesser General Public License for more details. |
13
|
|
|
* |
14
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
15
|
|
|
* along with PAYONE Magento 2 Connector. If not, see <http://www.gnu.org/licenses/>. |
16
|
|
|
* |
17
|
|
|
* PHP version 5 |
18
|
|
|
* |
19
|
|
|
* @category Payone |
20
|
|
|
* @package Payone_Magento2_Plugin |
21
|
|
|
* @author FATCHIP GmbH <[email protected]> |
22
|
|
|
* @copyright 2003 - 2017 Payone GmbH |
23
|
|
|
* @license <http://www.gnu.org/licenses/> GNU Lesser General Public License |
24
|
|
|
* @link http://www.payone.de |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace Payone\Core\Test\Unit\Model\Risk; |
28
|
|
|
|
29
|
|
|
use Payone\Core\Model\Risk\Addresscheck as ClassToTest; |
30
|
|
|
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
31
|
|
|
use Payone\Core\Model\Api\Request\Addresscheck; |
32
|
|
|
use Payone\Core\Helper\Database; |
33
|
|
|
use Payone\Core\Helper\Toolkit; |
34
|
|
|
use Magento\Checkout\Model\Session; |
35
|
|
|
use Magento\Quote\Model\Quote\Address; |
36
|
|
|
use Magento\Quote\Model\Quote; |
37
|
|
|
use Magento\Framework\Exception\LocalizedException; |
38
|
|
|
use Payone\Core\Test\Unit\BaseTestCase; |
39
|
|
|
use Payone\Core\Model\Test\PayoneObjectManager; |
40
|
|
|
|
41
|
|
|
class AddresscheckTest extends BaseTestCase |
42
|
|
|
{ |
43
|
|
|
/** |
44
|
|
|
* @var ClassToTest |
45
|
|
|
*/ |
46
|
|
|
private $classToTest; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var ObjectManager|PayoneObjectManager |
50
|
|
|
*/ |
51
|
|
|
private $objectManager; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var Addresscheck|\PHPUnit_Framework_MockObject_MockObject |
55
|
|
|
*/ |
56
|
|
|
private $addresscheck; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var Database|\PHPUnit_Framework_MockObject_MockObject |
60
|
|
|
*/ |
61
|
|
|
private $databaseHelper; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var Quote|\PHPUnit_Framework_MockObject_MockObject |
65
|
|
|
*/ |
66
|
|
|
private $quote; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var Toolkit |
70
|
|
|
*/ |
71
|
|
|
private $toolkitHelper; |
72
|
|
|
|
73
|
|
|
protected function setUp() |
74
|
|
|
{ |
75
|
|
|
$this->objectManager = $this->getObjectManager(); |
76
|
|
|
|
77
|
|
|
$this->addresscheck = $this->getMockBuilder(Addresscheck::class)->disableOriginalConstructor()->getMock(); |
78
|
|
|
$this->databaseHelper = $this->getMockBuilder(Database::class)->disableOriginalConstructor()->getMock(); |
79
|
|
|
|
80
|
|
|
$this->quote = $this->getMockBuilder(Quote::class) |
81
|
|
|
->disableOriginalConstructor() |
82
|
|
|
->setMethods(['isVirtual', 'getSubtotal']) |
83
|
|
|
->getMock(); |
84
|
|
|
$this->quote->method('isVirtual')->willReturn(false); |
85
|
|
|
$this->quote->method('getSubtotal')->willReturn(100); |
86
|
|
|
|
87
|
|
|
#$this->toolkitHelper = $this->getMockBuilder(Toolkit::class)->disableOriginalConstructor()->getMock(); |
|
|
|
|
88
|
|
|
#$this->toolkitHelper->method('handleSubstituteReplacement')->willReturn('Invalid message'); |
|
|
|
|
89
|
|
|
$this->toolkitHelper = $this->objectManager->getObject(Toolkit::class); |
90
|
|
|
|
91
|
|
|
$checkoutSession = $this->getMockBuilder(Session::class) |
92
|
|
|
->disableOriginalConstructor() |
93
|
|
|
->setMethods([ |
94
|
|
|
'getPayoneBillingAddresscheckScore', |
95
|
|
|
'getPayoneShippingAddresscheckScore', |
96
|
|
|
'unsPayoneBillingAddresscheckScore', |
97
|
|
|
'unsPayoneShippingAddresscheckScore']) |
98
|
|
|
->getMock(); |
99
|
|
|
$checkoutSession->method('getPayoneShippingAddresscheckScore')->willReturn(null); |
100
|
|
|
|
101
|
|
|
$this->classToTest = $this->objectManager->getObject(ClassToTest::class, [ |
102
|
|
|
'addresscheck' => $this->addresscheck, |
103
|
|
|
'databaseHelper' => $this->databaseHelper, |
104
|
|
|
'toolkitHelper' => $this->toolkitHelper, |
105
|
|
|
'checkoutSession' => $checkoutSession |
106
|
|
|
]); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
View Code Duplication |
public function testIsCheckNeededForQuoteMinBasket() |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
$this->databaseHelper->expects($this->any()) |
|
|
|
|
112
|
|
|
->method('getConfigParam') |
113
|
|
|
->willReturnMap([['min_order_total', 'address_check', 'payone_protect', null, 10]]); |
114
|
|
|
|
115
|
|
|
$result = $this->classToTest->isCheckNeededForQuote(true, true, 5); |
116
|
|
|
$this->assertFalse($result); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
View Code Duplication |
public function testIsCheckNeededForQuoteMaxBasket() |
|
|
|
|
120
|
|
|
{ |
121
|
|
|
$this->databaseHelper->expects($this->any()) |
|
|
|
|
122
|
|
|
->method('getConfigParam') |
123
|
|
|
->willReturnMap([['max_order_total', 'address_check', 'payone_protect', null, 10]]); |
124
|
|
|
|
125
|
|
|
$result = $this->classToTest->isCheckNeededForQuote(true, true, 15); |
126
|
|
|
$this->assertFalse($result); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
View Code Duplication |
public function testIsCheckNeededForQuoteVirtual() |
|
|
|
|
130
|
|
|
{ |
131
|
|
|
$this->databaseHelper->expects($this->any()) |
|
|
|
|
132
|
|
|
->method('getConfigParam') |
133
|
|
|
->willReturnMap([['check_billing_for_virtual_order', 'address_check', 'payone_protect', null, 0]]); |
134
|
|
|
|
135
|
|
|
$result = $this->classToTest->isCheckNeededForQuote(true, true, 15); |
136
|
|
|
$this->assertFalse($result); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
View Code Duplication |
public function testIsCheckNeededForQuote() |
|
|
|
|
140
|
|
|
{ |
141
|
|
|
$this->databaseHelper->expects($this->any()) |
|
|
|
|
142
|
|
|
->method('getConfigParam') |
143
|
|
|
->willReturnMap([['check_billing_for_virtual_order', 'address_check', 'payone_protect', null, 0]]); |
144
|
|
|
|
145
|
|
|
$result = $this->classToTest->isCheckNeededForQuote(true, false, 15); |
146
|
|
|
$this->assertTrue($result); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
View Code Duplication |
public function testCorrectAddress() |
|
|
|
|
150
|
|
|
{ |
151
|
|
|
$address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock(); |
152
|
|
|
$this->addresscheck->method('sendRequest')->willReturn(true); |
153
|
|
|
|
154
|
|
|
$result = $this->classToTest->correctAddress($address); |
155
|
|
|
$this->assertEquals($address, $result); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function testHandleAddressManagement() |
159
|
|
|
{ |
160
|
|
|
$address = $this->getMockBuilder(Address::class) |
161
|
|
|
->disableOriginalConstructor() |
162
|
|
|
->setMethods(['getPayoneAddresscheckScore', 'setPayoneAddresscheckScore', 'getStreet', 'setStreet', 'getData', 'setData']) |
163
|
|
|
->getMock(); |
164
|
|
|
$address->method('getPayoneAddresscheckScore')->willReturn(null); |
165
|
|
|
$address->method('getStreet')->willReturn(['Teststr. 12', '3rd floor']); |
166
|
|
|
|
167
|
|
|
$this->addresscheck->method('sendRequest')->willReturn(['status' => 'VALID', 'street' => 'Another str. 7', 'firstname' => 'Patrick']); |
168
|
|
|
|
169
|
|
|
$result = $this->classToTest->handleAddressManagement($address, $this->quote, false); |
170
|
|
|
$this->assertInstanceOf(Address::class, $result); |
171
|
|
|
|
172
|
|
|
$result = $this->classToTest->isAddressCorrected(); |
173
|
|
|
$this->assertTrue($result); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function testHandleAddressManagementExceptionError() |
177
|
|
|
{ |
178
|
|
|
$address = $this->getMockBuilder(Address::class) |
179
|
|
|
->disableOriginalConstructor() |
180
|
|
|
->setMethods(['getPayoneAddresscheckScore', 'setPayoneAddresscheckScore', 'getStreet', 'setStreet', 'getData', 'setData']) |
181
|
|
|
->getMock(); |
182
|
|
|
$address->method('getPayoneAddresscheckScore')->willReturn(null); |
183
|
|
|
|
184
|
|
|
$this->addresscheck->method('sendRequest')->willReturn(['status' => 'ERROR']); |
185
|
|
|
$this->databaseHelper->expects($this->any()) |
|
|
|
|
186
|
|
|
->method('getConfigParam') |
187
|
|
|
->willReturnMap([ |
188
|
|
|
['handle_response_error', 'address_check', 'payone_protect', null, 'stop_checkout'], |
189
|
|
|
['stop_checkout_message', 'address_check', 'payone_protect', null, null] |
190
|
|
|
]); |
191
|
|
|
|
192
|
|
|
$this->expectException(LocalizedException::class); |
193
|
|
|
$this->classToTest->handleAddressManagement($address, $this->quote, false); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
View Code Duplication |
public function testHandleAddressManagementExceptionInvalid() |
|
|
|
|
197
|
|
|
{ |
198
|
|
|
$address = $this->getMockBuilder(Address::class) |
199
|
|
|
->disableOriginalConstructor() |
200
|
|
|
->setMethods(['getPayoneAddresscheckScore', 'setPayoneAddresscheckScore', 'getStreet', 'setStreet', 'getData', 'setData']) |
201
|
|
|
->getMock(); |
202
|
|
|
$address->method('getPayoneAddresscheckScore')->willReturn(null); |
203
|
|
|
|
204
|
|
|
$this->addresscheck->method('sendRequest')->willReturn(['status' => 'INVALID', 'customermessage' => 'Address invalid']); |
205
|
|
|
$this->databaseHelper->expects($this->any()) |
|
|
|
|
206
|
|
|
->method('getConfigParam') |
207
|
|
|
->willReturnMap([ |
208
|
|
|
['message_response_invalid', 'address_check', 'payone_protect', null, null] |
209
|
|
|
]); |
210
|
|
|
|
211
|
|
|
$this->expectException(LocalizedException::class); |
212
|
|
|
$this->classToTest->handleAddressManagement($address, $this->quote, false); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public function testHandleAddressManagementExceptionNoStatus() |
216
|
|
|
{ |
217
|
|
|
$address = $this->getMockBuilder(Address::class) |
218
|
|
|
->disableOriginalConstructor() |
219
|
|
|
->setMethods(['getPayoneAddresscheckScore', 'setPayoneAddresscheckScore', 'getStreet', 'setStreet', 'getData', 'setData']) |
220
|
|
|
->getMock(); |
221
|
|
|
$address->method('getPayoneAddresscheckScore')->willReturn(null); |
222
|
|
|
|
223
|
|
|
$this->addresscheck->method('sendRequest')->willReturn([]); |
224
|
|
|
$this->databaseHelper->expects($this->any()) |
|
|
|
|
225
|
|
|
->method('getConfigParam') |
226
|
|
|
->willReturnMap([ |
227
|
|
|
['message_response_invalid', 'address_check', 'payone_protect', null, null] |
228
|
|
|
]); |
229
|
|
|
|
230
|
|
|
$this->expectException(LocalizedException::class); |
231
|
|
|
$this->classToTest->handleAddressManagement($address, $this->quote, false); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
View Code Duplication |
public function testHandleAddressManagementExceptionInvalidDefault() |
|
|
|
|
235
|
|
|
{ |
236
|
|
|
$address = $this->getMockBuilder(Address::class) |
237
|
|
|
->disableOriginalConstructor() |
238
|
|
|
->setMethods(['getPayoneAddresscheckScore', 'setPayoneAddresscheckScore', 'getStreet', 'setStreet', 'getData', 'setData']) |
239
|
|
|
->getMock(); |
240
|
|
|
$address->method('getPayoneAddresscheckScore')->willReturn(null); |
241
|
|
|
|
242
|
|
|
$this->addresscheck->method('sendRequest')->willReturn(['status' => 'INVALID', 'customermessage' => 'Address invalid']); |
243
|
|
|
$this->databaseHelper->expects($this->any()) |
|
|
|
|
244
|
|
|
->method('getConfigParam') |
245
|
|
|
->willReturnMap([ |
246
|
|
|
['message_response_invalid', 'address_check', 'payone_protect', null, 'Address invalid: {{payone_customermessage}}'] |
247
|
|
|
]); |
248
|
|
|
|
249
|
|
|
$this->expectException(LocalizedException::class); |
250
|
|
|
$this->classToTest->handleAddressManagement($address, $this->quote, false); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
View Code Duplication |
public function testGetScoreR() |
|
|
|
|
254
|
|
|
{ |
255
|
|
|
$this->addresscheck->method('sendRequest')->willReturn(['status' => 'INVALID']); |
256
|
|
|
$address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock(); |
257
|
|
|
|
258
|
|
|
$result = $this->classToTest->getScore($address); |
259
|
|
|
$expected = 'R'; |
260
|
|
|
$this->assertEquals($expected, $result); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
public function testGetScorePersonstatusNoMapping() |
264
|
|
|
{ |
265
|
|
|
$status = 'x'; |
266
|
|
|
$this->databaseHelper->expects($this->any()) |
|
|
|
|
267
|
|
|
->method('getConfigParam') |
268
|
|
|
->willReturnMap([['mapping_personstatus', 'address_check', 'payone_protect', null, $this->toolkitHelper->serialize($status)]]); |
269
|
|
|
|
270
|
|
|
$this->addresscheck->method('sendRequest')->willReturn(['personstatus' => 'ABC']); |
271
|
|
|
$address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock(); |
272
|
|
|
|
273
|
|
|
$result = $this->classToTest->getScore($address); |
274
|
|
|
$expected = 'G'; |
275
|
|
|
$this->assertEquals($expected, $result); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
public function testGetScorePersonstatus() |
279
|
|
|
{ |
280
|
|
|
$status = [ |
281
|
|
|
['personstatus' => 'ABC', 'score' => 'D'] |
282
|
|
|
]; |
283
|
|
|
$this->databaseHelper->expects($this->any()) |
|
|
|
|
284
|
|
|
->method('getConfigParam') |
285
|
|
|
->willReturnMap([['mapping_personstatus', 'address_check', 'payone_protect', null, $this->toolkitHelper->serialize($status)]]); |
286
|
|
|
|
287
|
|
|
$this->addresscheck->method('sendRequest')->willReturn(['personstatus' => 'ABC']); |
288
|
|
|
$address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock(); |
289
|
|
|
|
290
|
|
|
$result = $this->classToTest->getScore($address); |
291
|
|
|
$expected = 'D'; |
292
|
|
|
$this->assertEquals($expected, $result); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
View Code Duplication |
public function testGetScoreStillValid() |
|
|
|
|
296
|
|
|
{ |
297
|
|
|
$this->addresscheck->method('sendRequest')->willReturn(true); |
298
|
|
|
$address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock(); |
299
|
|
|
|
300
|
|
|
$expected = 'X'; |
301
|
|
|
$this->databaseHelper->method('getOldAddressStatus')->willReturn($expected); |
302
|
|
|
|
303
|
|
|
$result = $this->classToTest->getScore($address); |
304
|
|
|
$this->assertEquals($expected, $result); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
View Code Duplication |
public function testGetResponse() |
|
|
|
|
308
|
|
|
{ |
309
|
|
|
$this->addresscheck->method('sendRequest')->willReturn(true); |
310
|
|
|
|
311
|
|
|
$address = $this->getMockBuilder(Address::class)->disableOriginalConstructor()->getMock(); |
312
|
|
|
|
313
|
|
|
$result = $this->classToTest->getResponse($address); |
314
|
|
|
$this->assertTrue($result); |
|
|
|
|
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.