|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\ReCaptchaPaypal\Test\Unit\Plugin; |
| 9 | + |
| 10 | +use Magento\Framework\Webapi\Rest\Request; |
| 11 | +use Magento\Paypal\Model\Config; |
| 12 | +use Magento\Quote\Model\QuoteIdMask; |
| 13 | +use Magento\Quote\Model\QuoteIdMaskFactory; |
| 14 | +use Magento\ReCaptchaCheckout\Model\WebapiConfigProvider; |
| 15 | +use Magento\ReCaptchaPaypal\Model\ReCaptchaSession; |
| 16 | +use Magento\ReCaptchaPaypal\Plugin\ReplayPayflowReCaptchaForPlaceOrder; |
| 17 | +use Magento\ReCaptchaUi\Model\IsCaptchaEnabledInterface; |
| 18 | +use Magento\ReCaptchaValidationApi\Api\Data\ValidationConfigInterface; |
| 19 | +use Magento\ReCaptchaWebapiApi\Api\Data\EndpointInterface; |
| 20 | +use PHPUnit\Framework\MockObject\MockObject; |
| 21 | +use PHPUnit\Framework\TestCase; |
| 22 | + |
| 23 | +class ReplayPayflowReCaptchaForPlaceOrderTest extends TestCase |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @var IsCaptchaEnabledInterface|MockObject |
| 27 | + */ |
| 28 | + private $isCaptchaEnabled; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var Request|MockObject |
| 32 | + */ |
| 33 | + private $request; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var ReCaptchaSession|MockObject |
| 37 | + */ |
| 38 | + private $reCaptchaSession; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var QuoteIdMaskFactory|MockObject |
| 42 | + */ |
| 43 | + private $quoteIdMaskFactory; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var QuoteIdMask|MockObject |
| 47 | + */ |
| 48 | + private $quoteIdMask; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var ReplayPayflowReCaptchaForPlaceOrder |
| 52 | + */ |
| 53 | + private $model; |
| 54 | + |
| 55 | + /** |
| 56 | + * @inheritdoc |
| 57 | + */ |
| 58 | + protected function setUp(): void |
| 59 | + { |
| 60 | + parent::setUp(); |
| 61 | + $this->isCaptchaEnabled = $this->getMockForAbstractClass(IsCaptchaEnabledInterface::class); |
| 62 | + $this->request = $this->createMock(Request::class); |
| 63 | + $this->reCaptchaSession = $this->createMock(ReCaptchaSession::class); |
| 64 | + $this->quoteIdMaskFactory = $this->createMock(QuoteIdMaskFactory::class); |
| 65 | + $this->quoteIdMask = $this->getMockBuilder(QuoteIdMask::class) |
| 66 | + ->onlyMethods(['load']) |
| 67 | + ->addMethods(['getQuoteId']) |
| 68 | + ->disableOriginalConstructor() |
| 69 | + ->getMock(); |
| 70 | + $this->model = new ReplayPayflowReCaptchaForPlaceOrder( |
| 71 | + $this->isCaptchaEnabled, |
| 72 | + $this->request, |
| 73 | + $this->reCaptchaSession, |
| 74 | + $this->quoteIdMaskFactory |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @param array $mocks |
| 80 | + * @param bool $isResultNull |
| 81 | + * @param bool $isReturnNull |
| 82 | + * @dataProvider afterGetConfigForDataProvider |
| 83 | + */ |
| 84 | + public function testAfterGetConfigFor(array $mocks, bool $isResultNull, bool $isReturnNull): void |
| 85 | + { |
| 86 | + $this->configureMock($mocks); |
| 87 | + $subject = $this->createMock(WebapiConfigProvider::class); |
| 88 | + $result = $this->getMockForAbstractClass(ValidationConfigInterface::class); |
| 89 | + $endpoint = $this->getMockForAbstractClass(EndpointInterface::class); |
| 90 | + $this->assertSame( |
| 91 | + $isReturnNull ? null : $result, |
| 92 | + $this->model->afterGetConfigFor($subject, $isResultNull ? null : $result, $endpoint) |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
| 98 | + */ |
| 99 | + public function afterGetConfigForDataProvider(): array |
| 100 | + { |
| 101 | + return [ |
| 102 | + [ |
| 103 | + [ |
| 104 | + 'reCaptchaSession' => [ |
| 105 | + ['method' => 'isValid', 'expects' => $this->never()] |
| 106 | + ] |
| 107 | + ], |
| 108 | + true, |
| 109 | + true |
| 110 | + ], |
| 111 | + [ |
| 112 | + [ |
| 113 | + 'isCaptchaEnabled' => [ |
| 114 | + ['method' => 'isCaptchaEnabledFor', 'with' => 'paypal_payflowpro', 'willReturn' => false] |
| 115 | + ], |
| 116 | + 'reCaptchaSession' => [ |
| 117 | + ['method' => 'isValid', 'expects' => $this->never(),] |
| 118 | + ] |
| 119 | + ], |
| 120 | + false, |
| 121 | + false |
| 122 | + ], |
| 123 | + [ |
| 124 | + [ |
| 125 | + 'isCaptchaEnabled' => [ |
| 126 | + ['method' => 'isCaptchaEnabledFor', 'with' => 'paypal_payflowpro', 'willReturn' => true] |
| 127 | + ], |
| 128 | + 'request' => [ |
| 129 | + ['method' => 'getBodyParams', 'expects' => $this->once(), 'willReturn' => []] |
| 130 | + ], |
| 131 | + 'reCaptchaSession' => [ |
| 132 | + ['method' => 'isValid', 'expects' => $this->never(),] |
| 133 | + ] |
| 134 | + ], |
| 135 | + false, |
| 136 | + false |
| 137 | + ], |
| 138 | + [ |
| 139 | + [ |
| 140 | + 'isCaptchaEnabled' => [ |
| 141 | + ['method' => 'isCaptchaEnabledFor', 'with' => 'paypal_payflowpro', 'willReturn' => true] |
| 142 | + ], |
| 143 | + 'request' => [ |
| 144 | + [ |
| 145 | + 'method' => 'getBodyParams', |
| 146 | + 'expects' => $this->once(), |
| 147 | + 'willReturn' => ['cartId' => 1, 'paymentMethod' => ['method' => 'checkmo']] |
| 148 | + ] |
| 149 | + ], |
| 150 | + 'reCaptchaSession' => [ |
| 151 | + ['method' => 'isValid', 'expects' => $this->never(), 'willReturn' => false] |
| 152 | + ] |
| 153 | + ], |
| 154 | + false, |
| 155 | + false |
| 156 | + ], |
| 157 | + [ |
| 158 | + [ |
| 159 | + 'isCaptchaEnabled' => [ |
| 160 | + ['method' => 'isCaptchaEnabledFor', 'with' => 'paypal_payflowpro', 'willReturn' => true] |
| 161 | + ], |
| 162 | + 'request' => [ |
| 163 | + [ |
| 164 | + 'method' => 'getBodyParams', |
| 165 | + 'expects' => $this->once(), |
| 166 | + 'willReturn' => ['cartId' => 1, 'paymentMethod' => ['method' => Config::METHOD_PAYFLOWPRO]] |
| 167 | + ] |
| 168 | + ], |
| 169 | + 'reCaptchaSession' => [ |
| 170 | + ['method' => 'isValid', 'expects' => $this->once(), 'with' => 1, 'willReturn' => false] |
| 171 | + ] |
| 172 | + ], |
| 173 | + false, |
| 174 | + false |
| 175 | + ], |
| 176 | + [ |
| 177 | + [ |
| 178 | + 'isCaptchaEnabled' => [ |
| 179 | + ['method' => 'isCaptchaEnabledFor', 'with' => 'paypal_payflowpro', 'willReturn' => true] |
| 180 | + ], |
| 181 | + 'request' => [ |
| 182 | + [ |
| 183 | + 'method' => 'getBodyParams', |
| 184 | + 'expects' => $this->once(), |
| 185 | + 'willReturn' => ['cartId' => 1, 'paymentMethod' => ['method' => Config::METHOD_PAYFLOWPRO]] |
| 186 | + ] |
| 187 | + ], |
| 188 | + 'reCaptchaSession' => [ |
| 189 | + ['method' => 'isValid', 'expects' => $this->once(), 'with' => 1, 'willReturn' => true] |
| 190 | + ] |
| 191 | + ], |
| 192 | + false, |
| 193 | + true |
| 194 | + ], |
| 195 | + [ |
| 196 | + [ |
| 197 | + 'isCaptchaEnabled' => [ |
| 198 | + ['method' => 'isCaptchaEnabledFor', 'with' => 'paypal_payflowpro', 'willReturn' => true] |
| 199 | + ], |
| 200 | + 'request' => [ |
| 201 | + [ |
| 202 | + 'method' => 'getBodyParams', |
| 203 | + 'expects' => $this->once(), |
| 204 | + 'willReturn' => [ |
| 205 | + 'cart_id' => 1, |
| 206 | + 'payment_method' => ['method' => Config::METHOD_PAYFLOWPRO] |
| 207 | + ] |
| 208 | + ] |
| 209 | + ], |
| 210 | + 'reCaptchaSession' => [ |
| 211 | + ['method' => 'isValid', 'expects' => $this->once(), 'with' => 1, 'willReturn' => true] |
| 212 | + ] |
| 213 | + ], |
| 214 | + false, |
| 215 | + true |
| 216 | + ], |
| 217 | + [ |
| 218 | + [ |
| 219 | + 'isCaptchaEnabled' => [ |
| 220 | + ['method' => 'isCaptchaEnabledFor', 'with' => 'paypal_payflowpro', 'willReturn' => true] |
| 221 | + ], |
| 222 | + 'request' => [ |
| 223 | + [ |
| 224 | + 'method' => 'getBodyParams', |
| 225 | + 'expects' => $this->once(), |
| 226 | + 'willReturn' => [ |
| 227 | + 'cartId' => '17uc43rge98nc92', |
| 228 | + 'paymentMethod' => ['method' => Config::METHOD_PAYFLOWPRO] |
| 229 | + ] |
| 230 | + ] |
| 231 | + ], |
| 232 | + 'quoteIdMaskFactory' => [ |
| 233 | + [ |
| 234 | + 'method' => 'create', |
| 235 | + 'expects' => $this->once(), |
| 236 | + 'willReturnProperty' => 'quoteIdMask' |
| 237 | + ] |
| 238 | + ], |
| 239 | + 'quoteIdMask' => [ |
| 240 | + [ |
| 241 | + 'method' => 'load', |
| 242 | + 'expects' => $this->once(), |
| 243 | + 'willReturnSelf' => null |
| 244 | + ], |
| 245 | + [ |
| 246 | + 'method' => 'getQuoteId', |
| 247 | + 'expects' => $this->once(), |
| 248 | + 'willReturn' => 2 |
| 249 | + ] |
| 250 | + ], |
| 251 | + 'reCaptchaSession' => [ |
| 252 | + ['method' => 'isValid', 'expects' => $this->once(), 'with' => 2, 'willReturn' => true] |
| 253 | + ] |
| 254 | + ], |
| 255 | + false, |
| 256 | + true |
| 257 | + ], |
| 258 | + ]; |
| 259 | + } |
| 260 | + |
| 261 | + private function configureMock(array $mocks): void |
| 262 | + { |
| 263 | + foreach ($mocks as $prop => $propMocks) { |
| 264 | + foreach ($propMocks as $mock) { |
| 265 | + $builder = $this->$prop->expects($mock['expects'] ?? $this->any()); |
| 266 | + unset($mock['expects']); |
| 267 | + foreach ($mock as $method => $args) { |
| 268 | + if ($method === 'willReturnProperty') { |
| 269 | + $method = 'willReturn'; |
| 270 | + $args = $this->$args; |
| 271 | + } |
| 272 | + $builder->$method(...[$args]); |
| 273 | + } |
| 274 | + } |
| 275 | + } |
| 276 | + } |
| 277 | +} |
0 commit comments