|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2013-2017 Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Authorizenet\Controller\Directpost\Payment; |
| 7 | + |
| 8 | +/** |
| 9 | + * Class ResponseTest |
| 10 | + * |
| 11 | + * @magentoAppArea frontend |
| 12 | + */ |
| 13 | +class ResponseTest extends \Magento\TestFramework\TestCase\AbstractController |
| 14 | +{ |
| 15 | + /** |
| 16 | + * Tests the controller for declines |
| 17 | + * |
| 18 | + * @param int $invoiceNum |
| 19 | + * @param string $hash |
| 20 | + * @param string $errorMsg |
| 21 | + * @param string[] $params |
| 22 | + * |
| 23 | + * @dataProvider responseActionAuthorizeCaptureDeclineDataProvider |
| 24 | + */ |
| 25 | + public function testResponseActionAuthorizeCaptureDecline($invoiceNum, $hash, $errorMsg, $params) |
| 26 | + { |
| 27 | + $controllerName = 'directpost_payment'; |
| 28 | + $controllerModule = 'authorizenet'; |
| 29 | + $controllerAction = 'response'; |
| 30 | + $params['x_invoice_num'] = $invoiceNum; |
| 31 | + $params['x_MD5_Hash'] = $hash; |
| 32 | + $this->getRequest()->setControllerName( |
| 33 | + $controllerName |
| 34 | + )->setControllerModule( |
| 35 | + $controllerModule |
| 36 | + )->setActionName( |
| 37 | + $controllerAction |
| 38 | + )->setRouteName( |
| 39 | + $controllerModule |
| 40 | + )->setRequestUri("/{$controllerModule}/{$controllerName}/{$controllerAction}") |
| 41 | + ->setParams($params); |
| 42 | + |
| 43 | + $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); |
| 44 | + |
| 45 | + /** @var \Magento\Authorizenet\Controller\Directpost\Payment\Response */ |
| 46 | + $controller = $objectManager->create(\Magento\Authorizenet\Controller\Directpost\Payment\Response::class); |
| 47 | + |
| 48 | + $response = $controller->execute(); |
| 49 | + $output = $response->getLayout()->getOutput(); |
| 50 | + |
| 51 | + $expectedString = "{$controllerModule}/{$controllerName}/redirect/x_invoice_num/{$params['x_invoice_num']}/" |
| 52 | + . "success/0/error_msg/{$errorMsg}/controller_action_name/{$controllerName}/"; |
| 53 | + |
| 54 | + $this->assertContains('window.location', $output); |
| 55 | + $this->assertContains($expectedString, $output); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Tests the controller for created blocks used for sending emails that should not affect layout response |
| 60 | + * |
| 61 | + * @param string $hash |
| 62 | + * @param string[] $params |
| 63 | + * |
| 64 | + * @dataProvider responseActionAuthorizeCaptureSuccessDataProvider |
| 65 | + */ |
| 66 | + public function testBlockCreationAffectingResult($hash, $params) |
| 67 | + { |
| 68 | + $controllerName = 'directpost_payment'; |
| 69 | + $controllerModule = 'authorizenet'; |
| 70 | + $controllerAction = 'response'; |
| 71 | + $params['x_invoice_num'] = 100000002; |
| 72 | + $params['x_MD5_Hash'] = $hash; |
| 73 | + $this->getRequest()->setControllerName( |
| 74 | + $controllerName |
| 75 | + )->setControllerModule( |
| 76 | + $controllerModule |
| 77 | + )->setActionName( |
| 78 | + $controllerAction |
| 79 | + )->setRouteName( |
| 80 | + $controllerModule |
| 81 | + )->setRequestUri("/{$controllerModule}/{$controllerName}/{$controllerAction}") |
| 82 | + ->setParams($params); |
| 83 | + |
| 84 | + $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); |
| 85 | + |
| 86 | + $directpostMock = $this->getMockBuilder(\Magento\Authorizenet\Model\Directpost::class) |
| 87 | + ->disableOriginalConstructor() |
| 88 | + ->getMock(); |
| 89 | + $objectManagerMock = $this->getMockBuilder(\Magento\Framework\ObjectManagerInterface::class) |
| 90 | + ->setMethods(['create']) |
| 91 | + ->getMockForAbstractClass(); |
| 92 | + $objectManagerMock->expects($this->atLeastOnce()) |
| 93 | + ->method('create') |
| 94 | + ->with(\Magento\Authorizenet\Model\Directpost::class) |
| 95 | + ->willReturn($directpostMock); |
| 96 | + $context = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create( |
| 97 | + \Magento\Backend\App\Action\Context::class, |
| 98 | + [ |
| 99 | + 'objectManager' => $objectManagerMock |
| 100 | + ] |
| 101 | + ); |
| 102 | + |
| 103 | + /** @var \Magento\Authorizenet\Controller\Directpost\Payment\Response $controller */ |
| 104 | + $controller = $objectManager->create( |
| 105 | + \Magento\Authorizenet\Controller\Directpost\Payment\Response::class, |
| 106 | + [ |
| 107 | + 'context' => $context |
| 108 | + ] |
| 109 | + ); |
| 110 | + |
| 111 | + // create one block for potential layout stack modification that should not affect response |
| 112 | + /** @var \Magento\Authorizenet\Block\Adminhtml\Order\View\Info\FraudDetails $block */ |
| 113 | + $block = $objectManager->get(\Magento\Framework\View\LayoutInterface::class) |
| 114 | + ->createBlock(\Magento\Authorizenet\Block\Adminhtml\Order\View\Info\FraudDetails::class); |
| 115 | + $block->setTemplate('Magento_Payment::order/view/info/fraud_details.phtml'); |
| 116 | + |
| 117 | + $response = $controller->execute(); |
| 118 | + $output = $response->getLayout()->getOutput(); |
| 119 | + |
| 120 | + $expectedString = "{$controllerModule}/{$controllerName}/redirect/x_invoice_num/{$params['x_invoice_num']}/" |
| 121 | + . "success/1/controller_action_name/{$controllerName}/"; |
| 122 | + |
| 123 | + $this->assertContains('window.location', $output); |
| 124 | + $this->assertContains($expectedString, $output); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * @return array |
| 129 | + */ |
| 130 | + public function responseActionAuthorizeCaptureDeclineDataProvider() |
| 131 | + { |
| 132 | + $postArray = [ |
| 133 | + 'x_response_code' => 1, |
| 134 | + 'x_response_reason_code' => 1, |
| 135 | + 'x_response_reason_text' => 'This transaction has been approved.', |
| 136 | + 'x_avs_code' => 'Y', |
| 137 | + 'x_auth_code' => 'G0L0XR', |
| 138 | + 'x_trans_id' => '60016479791', |
| 139 | + 'x_method' => 'CC', |
| 140 | + 'x_card_type' => 'American Express', |
| 141 | + 'x_account_number' => 'XXXX0002', |
| 142 | + 'x_first_name' => 'Name', |
| 143 | + 'x_last_name' => 'Surname', |
| 144 | + 'x_company' => null, |
| 145 | + 'x_address' => 'Address', |
| 146 | + 'x_city' => 'Austin', |
| 147 | + 'x_state' => 'Texas', |
| 148 | + 'x_zip' => '78753', |
| 149 | + 'x_country' => 'US', |
| 150 | + 'x_phone' => '5127242323', |
| 151 | + 'x_fax' => null, |
| 152 | + 'x_email' => 'customer@example.com', |
| 153 | + 'x_description' => null, |
| 154 | + 'x_type' => 'auth_capture', |
| 155 | + 'x_cust_id' => null, |
| 156 | + 'x_ship_to_first_name' => null, |
| 157 | + 'x_ship_to_last_name' => null, |
| 158 | + 'x_ship_to_company' => null, |
| 159 | + 'x_ship_to_address' => null, |
| 160 | + 'x_ship_to_city' => null, |
| 161 | + 'x_ship_to_state' => null, |
| 162 | + 'x_ship_to_zip' => null, |
| 163 | + 'x_ship_to_country' => null, |
| 164 | + 'x_amount' => 100.00, |
| 165 | + 'x_tax' => 0.00, |
| 166 | + 'x_duty' => 0.00, |
| 167 | + 'x_freight' => 0.00, |
| 168 | + 'x_tax_exempt' => false, |
| 169 | + 'x_po_num' => null, |
| 170 | + 'x_SHA2_Hash' => null, |
| 171 | + 'x_cvv2_resp_code' => 'P', |
| 172 | + 'x_cavv_response' => 2, |
| 173 | + 'x_test_request' => false, |
| 174 | + 'controller_action_name' => 'directpost_payment', |
| 175 | + 'is_secure' => null |
| 176 | + ]; |
| 177 | + return [ |
| 178 | + 'error_hash' => [ |
| 179 | + 'invoice_num' => '1231231', |
| 180 | + 'x_MD5_Hash' => 'F9AE81A5DA36057D1312D71C904FCCF2', |
| 181 | + 'error_msg' => 'The%20transaction%20was%20declined%20because%20the%20' |
| 182 | + . 'response%20hash%20validation%20failed.', |
| 183 | + 'post' => $postArray |
| 184 | + ] |
| 185 | + ]; |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * @return array |
| 190 | + */ |
| 191 | + public function responseActionAuthorizeCaptureSuccessDataProvider() |
| 192 | + { |
| 193 | + $postArray = [ |
| 194 | + 'x_response_code' => 1, |
| 195 | + 'x_response_reason_code' => 1, |
| 196 | + 'x_response_reason_text' => 'This transaction has been approved.', |
| 197 | + 'x_avs_code' => 'Y', |
| 198 | + 'x_auth_code' => 'G0L0XR', |
| 199 | + 'x_trans_id' => '60016479791', |
| 200 | + 'x_method' => 'CC', |
| 201 | + 'x_card_type' => 'American Express', |
| 202 | + 'x_account_number' => 'XXXX0002', |
| 203 | + 'x_first_name' => 'Name', |
| 204 | + 'x_last_name' => 'Surname', |
| 205 | + 'x_company' => null, |
| 206 | + 'x_address' => 'Address', |
| 207 | + 'x_city' => 'Austin', |
| 208 | + 'x_state' => 'Texas', |
| 209 | + 'x_zip' => '78753', |
| 210 | + 'x_country' => 'US', |
| 211 | + 'x_phone' => '5127242323', |
| 212 | + 'x_fax' => null, |
| 213 | + 'x_email' => 'integrationtest@magento.com', |
| 214 | + 'x_description' => null, |
| 215 | + 'x_type' => 'auth_capture', |
| 216 | + 'x_cust_id' => null, |
| 217 | + 'x_ship_to_first_name' => null, |
| 218 | + 'x_ship_to_last_name' => null, |
| 219 | + 'x_ship_to_company' => null, |
| 220 | + 'x_ship_to_address' => null, |
| 221 | + 'x_ship_to_city' => null, |
| 222 | + 'x_ship_to_state' => null, |
| 223 | + 'x_ship_to_zip' => null, |
| 224 | + 'x_ship_to_country' => null, |
| 225 | + 'x_amount' => 120.15, |
| 226 | + 'x_tax' => 0.00, |
| 227 | + 'x_duty' => 0.00, |
| 228 | + 'x_freight' => 0.00, |
| 229 | + 'x_tax_exempt' => false, |
| 230 | + 'x_po_num' => null, |
| 231 | + 'x_SHA2_Hash' => null, |
| 232 | + 'x_cvv2_resp_code' => 'P', |
| 233 | + 'x_cavv_response' => 2, |
| 234 | + 'x_test_request' => false, |
| 235 | + 'controller_action_name' => 'directpost_payment', |
| 236 | + 'is_secure' => null |
| 237 | + ]; |
| 238 | + return [ |
| 239 | + 'success' => [ |
| 240 | + 'x_MD5_Hash' => '35DCF749F7760193FB8254886E1D1522', |
| 241 | + 'post' => $postArray |
| 242 | + ], |
| 243 | + ]; |
| 244 | + } |
| 245 | +} |
0 commit comments