| Total Complexity | 68 |
| Total Lines | 1200 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like NfgTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use NfgTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @codeCoverageIgnore |
||
| 36 | */ |
||
| 37 | class NfgTest extends TestCase |
||
| 38 | { |
||
| 39 | use TestsUtil; |
||
| 40 | |||
| 41 | private function getBreaker($exception = null) |
||
| 42 | { |
||
| 43 | $breaker = $this->getMockBuilder(Breaker::class)->disableOriginalConstructor()->getMock(); |
||
| 44 | |||
| 45 | if ($exception instanceof \Exception) { |
||
| 46 | $breaker->expects($this->once())->method('protect') |
||
| 47 | ->willThrowException($exception); |
||
| 48 | } else { |
||
| 49 | $breaker->expects($this->once())->method('protect') |
||
| 50 | ->willReturnCallback(function (\Closure $closure) { |
||
| 51 | return $closure(); |
||
| 52 | }); |
||
| 53 | } |
||
| 54 | |||
| 55 | return $breaker; |
||
| 56 | } |
||
| 57 | |||
| 58 | public function testLoginRedirectUnavailableAccessId() |
||
| 59 | { |
||
| 60 | $soapService = $this->createMock(NfgSoapInterface::class); |
||
| 61 | $soapService->expects($this->once())->method('getAccessID') |
||
| 62 | ->willThrowException(new NfgServiceUnavailableException()); |
||
| 63 | |||
| 64 | $this->expectException(NfgServiceUnavailableException::class); |
||
| 65 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 66 | |||
| 67 | $circuitBreaker = $this->getBreaker(); |
||
| 68 | |||
| 69 | /** @var LoggerInterface|MockObject $logger */ |
||
| 70 | $logger = $this->createMock('Psr\Log\LoggerInterface'); |
||
| 71 | $logger->expects($this->once())->method('error'); |
||
| 72 | |||
| 73 | $nfg = $this->getNfgService( |
||
| 74 | [ |
||
| 75 | 'session' => $this->getSession($accessId), |
||
| 76 | 'soap' => $soapService, |
||
| 77 | ] |
||
| 78 | ); |
||
| 79 | $nfg->setCircuitBreaker($circuitBreaker); |
||
| 80 | $nfg->setLogger($logger); |
||
| 81 | $nfg->login(); |
||
| 82 | } |
||
| 83 | |||
| 84 | public function testLoginRedirectUnavailableUnknownError() |
||
| 85 | { |
||
| 86 | $soapService = $this->createMock('\PROCERGS\LoginCidadao\NfgBundle\Service\NfgSoapInterface'); |
||
| 87 | $soapService->expects($this->once())->method('getAccessID') |
||
| 88 | ->willThrowException(new \RuntimeException()); |
||
| 89 | |||
| 90 | $this->expectException(NfgServiceUnavailableException::class); |
||
| 91 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 92 | |||
| 93 | $circuitBreaker = $this->getBreaker(); |
||
| 94 | |||
| 95 | $nfg = $this->getNfgService( |
||
| 96 | [ |
||
| 97 | 'session' => $this->getSession($accessId), |
||
| 98 | 'soap' => $soapService, |
||
| 99 | ] |
||
| 100 | ); |
||
| 101 | $nfg->setCircuitBreaker($circuitBreaker); |
||
| 102 | $nfg->login(); |
||
| 103 | } |
||
| 104 | |||
| 105 | public function testLoginRedirectUnavailable() |
||
| 106 | { |
||
| 107 | $this->expectException(NfgServiceUnavailableException::class); |
||
| 108 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 109 | |||
| 110 | $circuitBreaker = $this->getBreaker(new NfgServiceUnavailableException()); |
||
| 111 | |||
| 112 | $nfg = $this->getNfgService( |
||
| 113 | [ |
||
| 114 | 'session' => $this->getSession($accessId), |
||
| 115 | 'soap' => $this->getSoapService($accessId), |
||
| 116 | ] |
||
| 117 | ); |
||
| 118 | $nfg->setCircuitBreaker($circuitBreaker); |
||
| 119 | $nfg->login(); |
||
| 120 | } |
||
| 121 | |||
| 122 | public function testConnectRedirectUnavailable() |
||
| 123 | { |
||
| 124 | $this->expectException(NfgServiceUnavailableException::class); |
||
| 125 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 126 | |||
| 127 | $circuitBreaker = $this->getBreaker(new NfgServiceUnavailableException()); |
||
| 128 | |||
| 129 | $nfg = $this->getNfgService( |
||
| 130 | [ |
||
| 131 | 'session' => $this->getSession($accessId), |
||
| 132 | 'soap' => $this->getSoapService($accessId), |
||
| 133 | ] |
||
| 134 | ); |
||
| 135 | $nfg->setCircuitBreaker($circuitBreaker); |
||
| 136 | $nfg->connect(); |
||
| 137 | } |
||
| 138 | |||
| 139 | public function testLoginRedirect() |
||
| 140 | { |
||
| 141 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 142 | |||
| 143 | $circuitBreaker = $this->getBreaker(); |
||
| 144 | |||
| 145 | /** @var LoggerInterface|\PHPUnit_Framework_MockObject_MockObject $logger */ |
||
| 146 | $logger = $this->createMock('Psr\Log\LoggerInterface'); |
||
| 147 | $logger->expects($this->once())->method('info'); |
||
| 148 | |||
| 149 | $nfg = $this->getNfgService( |
||
| 150 | [ |
||
| 151 | 'session' => $this->getSession($accessId, 'set'), |
||
| 152 | 'soap' => $this->getSoapService($accessId), |
||
| 153 | ] |
||
| 154 | ); |
||
| 155 | $nfg->setCircuitBreaker($circuitBreaker); |
||
| 156 | $nfg->setLogger($logger); |
||
| 157 | |||
| 158 | $response = $nfg->login(); |
||
| 159 | $this->assertInstanceOf('\Symfony\Component\HttpFoundation\Response', $response); |
||
| 160 | $this->assertContains($accessId, $response->getContent()); |
||
| 161 | $this->assertContains('nfg_login_callback', $response->getContent()); |
||
| 162 | } |
||
| 163 | |||
| 164 | public function testLoginCallback() |
||
| 165 | { |
||
| 166 | $cpf = '12345678901'; |
||
| 167 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 168 | $secret = "my very super secret secret"; |
||
| 169 | $prsec = hash_hmac('sha256', "$cpf$accessId", $secret); |
||
| 170 | |||
| 171 | $person = new Person(); |
||
| 172 | $person->setCpf($cpf); |
||
| 173 | $personMeuRS = new PersonMeuRS(); |
||
| 174 | $personMeuRS->setPerson($person) |
||
| 175 | ->setNfgAccessToken('dummy'); |
||
| 176 | $meuRSHelper = $this->getMeuRSHelper($cpf, $personMeuRS); |
||
| 177 | |||
| 178 | $nfg = $this->getNfgService( |
||
| 179 | [ |
||
| 180 | 'session' => $this->getSession($accessId, 'get'), |
||
| 181 | 'soap' => $this->getSoapService($accessId), |
||
| 182 | 'meurs_helper' => $meuRSHelper, |
||
| 183 | 'login_manager' => $this->getLoginManager(true), |
||
| 184 | ] |
||
| 185 | ); |
||
| 186 | |||
| 187 | /** @var RedirectResponse $response */ |
||
| 188 | $response = $nfg->loginCallback(compact('cpf', 'accessId', 'prsec'), $secret); |
||
| 189 | |||
| 190 | $this->assertInstanceOf('\Symfony\Component\HttpFoundation\RedirectResponse', $response); |
||
| 191 | $this->assertEquals('lc_home', $response->getTargetUrl()); |
||
| 192 | } |
||
| 193 | |||
| 194 | public function testLoginCallbackMissingParams() |
||
| 195 | { |
||
| 196 | $this->expectException('Symfony\Component\HttpKernel\Exception\BadRequestHttpException'); |
||
| 197 | $cpf = '12345678901'; |
||
| 198 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 199 | $secret = "my very super secret secret"; |
||
| 200 | $prsec = hash_hmac('sha256', "$cpf$accessId", $secret); |
||
| 201 | |||
| 202 | $nfg = $this->getNfgService( |
||
| 203 | [ |
||
| 204 | 'session' => $this->getSession($accessId, 'none'), |
||
| 205 | 'soap' => $this->getSoapService($accessId), |
||
| 206 | ] |
||
| 207 | ); |
||
| 208 | |||
| 209 | $nfg->loginCallback(compact('accessId', 'prsec'), $secret); |
||
| 210 | } |
||
| 211 | |||
| 212 | public function testLoginCallbackInvalidSignature() |
||
| 213 | { |
||
| 214 | $this->expectException('Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException'); |
||
| 215 | $cpf = '12345678901'; |
||
| 216 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 217 | $secret = "my very super secret secret"; |
||
| 218 | $prsec = hash_hmac('sha256', "$cpf$accessId", $secret).'_INVALID'; |
||
| 219 | |||
| 220 | $nfg = $this->getNfgService( |
||
| 221 | [ |
||
| 222 | 'session' => $this->getSession($accessId, 'none'), |
||
| 223 | 'soap' => $this->getSoapService($accessId), |
||
| 224 | ] |
||
| 225 | ); |
||
| 226 | |||
| 227 | $nfg->loginCallback(compact('cpf', 'accessId', 'prsec'), $secret); |
||
| 228 | } |
||
| 229 | |||
| 230 | public function testLoginCallbackInvalidAccessId() |
||
| 231 | { |
||
| 232 | $this->expectException('Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException'); |
||
| 233 | $cpf = '12345678901'; |
||
| 234 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 235 | $secret = "my very super secret secret"; |
||
| 236 | $prsec = hash_hmac('sha256', "$cpf$accessId", $secret); |
||
| 237 | |||
| 238 | $nfg = $this->getNfgService( |
||
| 239 | [ |
||
| 240 | 'session' => $this->getSession($accessId.'_INVALID', 'none'), |
||
| 241 | 'soap' => $this->getSoapService($accessId), |
||
| 242 | ] |
||
| 243 | ); |
||
| 244 | |||
| 245 | $nfg->loginCallback(compact('cpf', 'accessId', 'prsec'), $secret); |
||
| 246 | } |
||
| 247 | |||
| 248 | public function testLoginCallbackInactiveUser() |
||
| 249 | { |
||
| 250 | $this->expectException('Symfony\Component\Security\Core\Exception\AccountStatusException'); |
||
| 251 | |||
| 252 | $cpf = '12345678901'; |
||
| 253 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 254 | $secret = "my very super secret secret"; |
||
| 255 | $prsec = hash_hmac('sha256', "$cpf$accessId", $secret); |
||
| 256 | |||
| 257 | $person = new Person(); |
||
| 258 | $person->setCpf($cpf); |
||
| 259 | $personMeuRS = new PersonMeuRS(); |
||
| 260 | $personMeuRS->setPerson($person) |
||
| 261 | ->setNfgAccessToken('dummy'); |
||
| 262 | $meuRSHelper = $this->getMeuRSHelper($cpf, $personMeuRS); |
||
| 263 | |||
| 264 | $loginManager = $this->getLoginManager(); |
||
| 265 | $loginManager->method('logInUser')->willThrowException(new AccountExpiredException()); |
||
| 266 | |||
| 267 | $nfg = $this->getNfgService( |
||
| 268 | [ |
||
| 269 | 'session' => $this->getSession($accessId, 'get'), |
||
| 270 | 'soap' => $this->getSoapService($accessId), |
||
| 271 | 'login_manager' => $loginManager, |
||
| 272 | 'meurs_helper' => $meuRSHelper, |
||
| 273 | ] |
||
| 274 | ); |
||
| 275 | |||
| 276 | $nfg->loginCallback(compact('cpf', 'accessId', 'prsec'), $secret); |
||
| 277 | } |
||
| 278 | |||
| 279 | public function testLoginNonexistentUser() |
||
| 280 | { |
||
| 281 | $this->expectException('PROCERGS\LoginCidadao\NfgBundle\Exception\ConnectionNotFoundException'); |
||
| 282 | |||
| 283 | $cpf = '12345678901'; |
||
| 284 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 285 | $secret = "my very super secret secret"; |
||
| 286 | $prsec = hash_hmac('sha256', "$cpf$accessId", $secret); |
||
| 287 | |||
| 288 | $person = new Person(); |
||
| 289 | $person->setCpf($cpf); |
||
| 290 | $meuRSHelper = $this->getMeuRSHelper($cpf, null); |
||
| 291 | |||
| 292 | $nfg = $this->getNfgService( |
||
| 293 | [ |
||
| 294 | 'session' => $this->getSession($accessId, 'get'), |
||
| 295 | 'soap' => $this->getSoapService($accessId), |
||
| 296 | 'meurs_helper' => $meuRSHelper, |
||
| 297 | 'login_manager' => $this->getLoginManager(false), |
||
| 298 | ] |
||
| 299 | ); |
||
| 300 | |||
| 301 | $nfg->loginCallback(compact('cpf', 'accessId', 'prsec'), $secret); |
||
| 302 | } |
||
| 303 | |||
| 304 | public function testPreUserInfoUnavailable() |
||
| 305 | { |
||
| 306 | $this->expectException('PROCERGS\LoginCidadao\NfgBundle\Exception\NfgServiceUnavailableException'); |
||
| 307 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 308 | $soapService = $this->getSoapService($accessId); |
||
| 309 | |||
| 310 | $cpf = '01234567890'; |
||
| 311 | $person = new Person(); |
||
| 312 | $person->setCpf($cpf); |
||
| 313 | $personMeuRS = new PersonMeuRS(); |
||
| 314 | $personMeuRS |
||
| 315 | ->setVoterRegistration('1234567890') |
||
| 316 | ->setPerson($person); |
||
| 317 | |||
| 318 | $circuitBreaker = $this->getBreaker(new NfgServiceUnavailableException()); |
||
| 319 | |||
| 320 | $nfg = $this->getNfgService( |
||
| 321 | [ |
||
| 322 | 'session' => $this->getSession($accessId, 'none'), |
||
| 323 | 'soap' => $soapService, |
||
| 324 | ] |
||
| 325 | ); |
||
| 326 | $nfg->setCircuitBreaker($circuitBreaker); |
||
| 327 | |||
| 328 | $accessToken = 'access_token'.random_int(10, 9999); |
||
| 329 | $request = $this->getRequest($accessToken); |
||
| 330 | |||
| 331 | $nfg->connectCallback($request, $personMeuRS); |
||
| 332 | } |
||
| 333 | |||
| 334 | public function testConnectCallbackMissingAccessToken() |
||
| 335 | { |
||
| 336 | $this->expectException('Symfony\Component\HttpKernel\Exception\BadRequestHttpException'); |
||
| 337 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 338 | $soapService = $this->getSoapService($accessId); |
||
| 339 | |||
| 340 | $cpf = '01234567890'; |
||
| 341 | $person = new Person(); |
||
| 342 | $person->setCpf($cpf); |
||
| 343 | $personMeuRS = new PersonMeuRS(); |
||
| 344 | $personMeuRS |
||
| 345 | ->setVoterRegistration('1234567890') |
||
| 346 | ->setPerson($person); |
||
| 347 | |||
| 348 | $nfg = $this->getNfgService( |
||
| 349 | [ |
||
| 350 | 'session' => $this->getSession($accessId, 'none'), |
||
| 351 | 'soap' => $soapService, |
||
| 352 | ] |
||
| 353 | ); |
||
| 354 | |||
| 355 | $request = $this->getRequest(null); |
||
| 356 | |||
| 357 | $nfg->connectCallback($request, $personMeuRS); |
||
| 358 | } |
||
| 359 | |||
| 360 | public function testUserInfoUnavailable() |
||
| 361 | { |
||
| 362 | $this->expectException('PROCERGS\LoginCidadao\NfgBundle\Exception\NfgServiceUnavailableException'); |
||
| 363 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 364 | |||
| 365 | $soapService = $this->createMock('\PROCERGS\LoginCidadao\NfgBundle\Service\NfgSoapInterface'); |
||
| 366 | $soapService->expects($this->once())->method('getUserInfo') |
||
| 367 | ->willThrowException(new NfgServiceUnavailableException()); |
||
| 368 | |||
| 369 | $cpf = '01234567890'; |
||
| 370 | $person = new Person(); |
||
| 371 | $person->setCpf($cpf); |
||
| 372 | $personMeuRS = new PersonMeuRS(); |
||
| 373 | $personMeuRS |
||
| 374 | ->setVoterRegistration('1234567890') |
||
| 375 | ->setPerson($person); |
||
| 376 | |||
| 377 | $circuitBreaker = $this->getBreaker(); |
||
| 378 | |||
| 379 | $nfg = $this->getNfgService( |
||
| 380 | [ |
||
| 381 | 'session' => $this->getSession($accessId, 'none'), |
||
| 382 | 'soap' => $soapService, |
||
| 383 | ] |
||
| 384 | ); |
||
| 385 | $nfg->setCircuitBreaker($circuitBreaker); |
||
| 386 | |||
| 387 | $accessToken = 'access_token'.random_int(10, 9999); |
||
| 388 | $request = $this->getRequest($accessToken); |
||
| 389 | |||
| 390 | $nfg->connectCallback($request, $personMeuRS); |
||
| 391 | } |
||
| 392 | |||
| 393 | public function testUserInfoError() |
||
| 394 | { |
||
| 395 | $this->expectException('PROCERGS\LoginCidadao\NfgBundle\Exception\NfgServiceUnavailableException'); |
||
| 396 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 397 | |||
| 398 | $soapService = $this->createMock('\PROCERGS\LoginCidadao\NfgBundle\Service\NfgSoapInterface'); |
||
| 399 | $soapService->expects($this->once())->method('getUserInfo') |
||
| 400 | ->willThrowException(new \RuntimeException()); |
||
| 401 | |||
| 402 | $cpf = '01234567890'; |
||
| 403 | $person = new Person(); |
||
| 404 | $person->setCpf($cpf); |
||
| 405 | $personMeuRS = new PersonMeuRS(); |
||
| 406 | $personMeuRS |
||
| 407 | ->setVoterRegistration('1234567890') |
||
| 408 | ->setPerson($person); |
||
| 409 | |||
| 410 | $circuitBreaker = $this->getBreaker(); |
||
| 411 | |||
| 412 | $nfg = $this->getNfgService( |
||
| 413 | [ |
||
| 414 | 'session' => $this->getSession($accessId, 'none'), |
||
| 415 | 'soap' => $soapService, |
||
| 416 | ] |
||
| 417 | ); |
||
| 418 | $nfg->setCircuitBreaker($circuitBreaker); |
||
| 419 | |||
| 420 | $accessToken = 'access_token'.random_int(10, 9999); |
||
| 421 | $request = $this->getRequest($accessToken); |
||
| 422 | |||
| 423 | $nfg->connectCallback($request, $personMeuRS); |
||
| 424 | } |
||
| 425 | |||
| 426 | public function testIncompleteInfo() |
||
| 427 | { |
||
| 428 | $this->expectException('PROCERGS\LoginCidadao\NfgBundle\Exception\MissingRequiredInformationException'); |
||
| 429 | |||
| 430 | $nfgProfile = $this->getNfgProfile(); |
||
| 431 | $nfgProfile->setCpf(null) |
||
| 432 | ->setEmail(null) |
||
| 433 | ->setName(null) |
||
| 434 | ->setBirthdate(null) |
||
| 435 | ->setMobile(null); |
||
| 436 | |||
| 437 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 438 | $soapService = $this->getSoapService($accessId); |
||
| 439 | |||
| 440 | $cpf = '01234567890'; |
||
| 441 | $person = new Person(); |
||
| 442 | $person->setCpf($cpf); |
||
| 443 | $personMeuRS = new PersonMeuRS(); |
||
| 444 | $personMeuRS |
||
| 445 | ->setVoterRegistration('1234567890') |
||
| 446 | ->setPerson($person); |
||
| 447 | |||
| 448 | $soapService->expects($this->atLeastOnce())->method('getUserInfo')->willReturn($nfgProfile); |
||
| 449 | |||
| 450 | $nfg = $this->getNfgService( |
||
| 451 | [ |
||
| 452 | 'session' => $this->getSession($accessId, 'none'), |
||
| 453 | 'soap' => $soapService, |
||
| 454 | ] |
||
| 455 | ); |
||
| 456 | |||
| 457 | $accessToken = 'access_token'.random_int(10, 9999); |
||
| 458 | $request = $this->getRequest($accessToken); |
||
| 459 | |||
| 460 | $nfg->connectCallback($request, $personMeuRS); |
||
| 461 | } |
||
| 462 | |||
| 463 | public function testMinimalInfo() |
||
| 464 | { |
||
| 465 | $cpf = '01234567890'; |
||
| 466 | $nfgProfile = $this->getNfgProfile(); |
||
| 467 | $nfgProfile->setCpf($cpf) |
||
| 468 | ->setEmail('[email protected]') |
||
| 469 | ->setName('Fulano') |
||
| 470 | ->setBirthdate(null) |
||
| 471 | ->setMobile(null); |
||
| 472 | |||
| 473 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 474 | $soapService = $this->getSoapService($accessId); |
||
| 475 | |||
| 476 | $person = new Person(); |
||
| 477 | $person->setCpf($cpf); |
||
| 478 | $personMeuRS = new PersonMeuRS(); |
||
| 479 | $personMeuRS |
||
| 480 | ->setVoterRegistration('1234567890') |
||
| 481 | ->setPerson($person); |
||
| 482 | |||
| 483 | $soapService->expects($this->atLeastOnce())->method('getUserInfo')->willReturn($nfgProfile); |
||
| 484 | |||
| 485 | $nfg = $this->getNfgService( |
||
| 486 | [ |
||
| 487 | 'session' => $this->getSession($accessId, 'none'), |
||
| 488 | 'soap' => $soapService, |
||
| 489 | ] |
||
| 490 | ); |
||
| 491 | |||
| 492 | $accessToken = 'access_token'.random_int(10, 9999); |
||
| 493 | $request = $this->getRequest($accessToken); |
||
| 494 | |||
| 495 | $nfg->connectCallback($request, $personMeuRS); |
||
| 496 | } |
||
| 497 | |||
| 498 | public function testRegistration() |
||
| 499 | { |
||
| 500 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 501 | $soapService = $this->getSoapService($accessId); |
||
| 502 | |||
| 503 | $nfgProfile = $this->getNfgProfile(); |
||
| 504 | $soapService->expects($this->atLeastOnce())->method('getUserInfo')->willReturn($nfgProfile); |
||
| 505 | |||
| 506 | $meuRSHelper = $this->getMeuRSHelper($nfgProfile->getCpf(), null); |
||
| 507 | |||
| 508 | $dispatcher = $this->getDispatcher(); |
||
| 509 | $dispatcher->expects($this->atLeastOnce())->method('dispatch') |
||
| 510 | ->willReturnCallback(function ($eventName, $event) { |
||
| 511 | if ($eventName === FOSUserEvents::REGISTRATION_INITIALIZE |
||
| 512 | && $event instanceof GetResponseUserEvent |
||
| 513 | ) { |
||
| 514 | $event->setResponse(new RedirectResponse('dummy')); |
||
| 515 | } |
||
| 516 | }); |
||
| 517 | |||
| 518 | $nfg = $this->getNfgService( |
||
| 519 | [ |
||
| 520 | 'session' => $this->getSession($accessId, 'none'), |
||
| 521 | 'soap' => $soapService, |
||
| 522 | 'meurs_helper' => $meuRSHelper, |
||
| 523 | 'dispatcher' => $dispatcher, |
||
| 524 | ] |
||
| 525 | ); |
||
| 526 | |||
| 527 | $personMeuRS = new PersonMeuRS(); |
||
| 528 | |||
| 529 | $accessToken = 'access_token'.random_int(10, 9999); |
||
| 530 | $request = $this->getRequest($accessToken); |
||
| 531 | /** @var RedirectResponse $response */ |
||
| 532 | $response = $nfg->connectCallback($request, $personMeuRS); |
||
| 533 | |||
| 534 | $this->assertInstanceOf('\Symfony\Component\HttpFoundation\RedirectResponse', $response); |
||
| 535 | $this->assertEquals('dummy', $response->getTargetUrl()); |
||
| 536 | } |
||
| 537 | |||
| 538 | public function testRegistrationWithPreexistingNfgProfile() |
||
| 539 | { |
||
| 540 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 541 | $soapService = $this->getSoapService($accessId); |
||
| 542 | |||
| 543 | $nfgProfile = $this->getNfgProfile(); |
||
| 544 | $soapService->expects($this->atLeastOnce())->method('getUserInfo')->willReturn($nfgProfile); |
||
| 545 | |||
| 546 | $meuRSHelper = $this->getMeuRSHelper($nfgProfile->getCpf(), null); |
||
| 547 | |||
| 548 | $oldNfgProfile = $this->getNfgProfile(); |
||
| 549 | $oldNfgProfile->setEmail('[email protected]'); |
||
| 550 | $nfgProfileRepo = $this->getNfgProfileRepository(); |
||
| 551 | $nfgProfileRepo->expects($this->atLeastOnce()) |
||
| 552 | ->method('findByCpf')->with($nfgProfile->getCpf())->willReturn($oldNfgProfile); |
||
| 553 | |||
| 554 | $nfg = $this->getNfgService( |
||
| 555 | [ |
||
| 556 | 'session' => $this->getSession($accessId, 'none'), |
||
| 557 | 'soap' => $soapService, |
||
| 558 | 'meurs_helper' => $meuRSHelper, |
||
| 559 | 'nfgprofile_repository' => $nfgProfileRepo, |
||
| 560 | ] |
||
| 561 | ); |
||
| 562 | |||
| 563 | $personMeuRS = new PersonMeuRS(); |
||
| 564 | |||
| 565 | $accessToken = 'access_token'.random_int(10, 9999); |
||
| 566 | $request = $this->getRequest($accessToken); |
||
| 567 | /** @var RedirectResponse $response */ |
||
| 568 | $response = $nfg->connectCallback($request, $personMeuRS); |
||
| 569 | |||
| 570 | $this->assertInstanceOf('\Symfony\Component\HttpFoundation\RedirectResponse', $response); |
||
| 571 | $this->assertEquals('fos_user_registration_confirmed', $response->getTargetUrl()); |
||
| 572 | |||
| 573 | $this->assertEquals($nfgProfile->getEmail(), $oldNfgProfile->getEmail()); |
||
| 574 | } |
||
| 575 | |||
| 576 | public function testRegistrationCpfCollision() |
||
| 577 | { |
||
| 578 | $this->expectException('PROCERGS\LoginCidadao\NfgBundle\Exception\CpfInUseException'); |
||
| 579 | |||
| 580 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 581 | $soapService = $this->getSoapService($accessId); |
||
| 582 | |||
| 583 | $nfgProfile = $this->getNfgProfile(); |
||
| 584 | $soapService->expects($this->atLeastOnce())->method('getUserInfo')->willReturn($nfgProfile); |
||
| 585 | |||
| 586 | $otherPerson = new Person(); |
||
| 587 | $otherPerson->setCpf($nfgProfile->getCpf()); |
||
| 588 | $otherPersonMeuRS = new PersonMeuRS(); |
||
| 589 | $otherPersonMeuRS->setPerson($otherPerson); |
||
| 590 | $otherPersonMeuRS->setNfgAccessToken('token'); |
||
| 591 | $meuRSHelper = $this->getMeuRSHelper($nfgProfile->getCpf(), $otherPersonMeuRS); |
||
| 592 | |||
| 593 | $nfg = $this->getNfgService( |
||
| 594 | [ |
||
| 595 | 'session' => $this->getSession($accessId, 'none'), |
||
| 596 | 'soap' => $soapService, |
||
| 597 | 'meurs_helper' => $meuRSHelper, |
||
| 598 | ] |
||
| 599 | ); |
||
| 600 | |||
| 601 | $accessToken = 'access_token'.random_int(10, 9999); |
||
| 602 | $request = $this->getRequest($accessToken); |
||
| 603 | $nfg->connectCallback($request, new PersonMeuRS()); |
||
| 604 | } |
||
| 605 | |||
| 606 | public function testRegistrationTurnsIntoLogin() |
||
| 607 | { |
||
| 608 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 609 | $soapService = $this->getSoapService($accessId); |
||
| 610 | |||
| 611 | $nfgProfile = $this->getNfgProfile(); |
||
| 612 | $soapService->expects($this->atLeastOnce())->method('getUserInfo')->willReturn($nfgProfile); |
||
| 613 | |||
| 614 | $otherPersonMeuRS = new PersonMeuRS(); |
||
| 615 | $otherPersonMeuRS |
||
| 616 | ->setNfgProfile($nfgProfile) |
||
| 617 | ->setNfgAccessToken('access_token') |
||
| 618 | ->setPerson(new Person()); |
||
| 619 | $meuRSHelper = $this->getMeuRSHelper($nfgProfile->getCpf(), $otherPersonMeuRS); |
||
| 620 | |||
| 621 | $nfg = $this->getNfgService( |
||
| 622 | [ |
||
| 623 | 'session' => $this->getSession($accessId, 'none'), |
||
| 624 | 'soap' => $soapService, |
||
| 625 | 'meurs_helper' => $meuRSHelper, |
||
| 626 | 'login_manager' => $this->getLoginManager(true), |
||
| 627 | ] |
||
| 628 | ); |
||
| 629 | |||
| 630 | $accessToken = 'access_token'.random_int(10, 9999); |
||
| 631 | $request = $this->getRequest($accessToken); |
||
| 632 | $nfg->connectCallback($request, new PersonMeuRS()); |
||
| 633 | } |
||
| 634 | |||
| 635 | public function testRegistrationEmailCollision() |
||
| 636 | { |
||
| 637 | $this->expectException('PROCERGS\LoginCidadao\NfgBundle\Exception\EmailInUseException'); |
||
| 638 | |||
| 639 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 640 | $soapService = $this->getSoapService($accessId); |
||
| 641 | |||
| 642 | $nfgProfile = $this->getNfgProfile(); |
||
| 643 | $soapService->expects($this->atLeastOnce())->method('getUserInfo')->willReturn($nfgProfile); |
||
| 644 | |||
| 645 | $meuRSHelper = $this->getMeuRSHelper(); |
||
| 646 | $meuRSHelper->expects($this->atLeastOnce())->method('getPersonByEmail')->willReturn(new Person()); |
||
| 647 | |||
| 648 | $nfg = $this->getNfgService( |
||
| 649 | [ |
||
| 650 | 'session' => $this->getSession($accessId, 'none'), |
||
| 651 | 'soap' => $soapService, |
||
| 652 | 'meurs_helper' => $meuRSHelper, |
||
| 653 | ] |
||
| 654 | ); |
||
| 655 | |||
| 656 | $accessToken = 'access_token'.random_int(10, 9999); |
||
| 657 | $request = $this->getRequest($accessToken); |
||
| 658 | $nfg->connectCallback($request, new PersonMeuRS()); |
||
| 659 | } |
||
| 660 | |||
| 661 | public function testLevel1Registration() |
||
| 662 | { |
||
| 663 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 664 | $soapService = $this->getSoapService($accessId); |
||
| 665 | |||
| 666 | $nfgProfile = $this->getNfgProfile(); |
||
| 667 | $nfgProfile->setAccessLvl(1); |
||
| 668 | $soapService->expects($this->atLeastOnce())->method('getUserInfo')->willReturn($nfgProfile); |
||
| 669 | |||
| 670 | $meuRSHelper = $this->getMeuRSHelper($nfgProfile->getCpf(), null); |
||
| 671 | |||
| 672 | $nfg = $this->getNfgService( |
||
| 673 | [ |
||
| 674 | 'session' => $this->getSession($accessId, 'none'), |
||
| 675 | 'soap' => $soapService, |
||
| 676 | 'meurs_helper' => $meuRSHelper, |
||
| 677 | ] |
||
| 678 | ); |
||
| 679 | |||
| 680 | $personMeuRS = new PersonMeuRS(); |
||
| 681 | |||
| 682 | $accessToken = 'access_token'.random_int(10, 9999); |
||
| 683 | $request = $this->getRequest($accessToken); |
||
| 684 | /** @var RedirectResponse $response */ |
||
| 685 | $response = $nfg->connectCallback($request, $personMeuRS); |
||
| 686 | |||
| 687 | $this->assertInstanceOf('\Symfony\Component\HttpFoundation\RedirectResponse', $response); |
||
| 688 | $this->assertEquals('fos_user_registration_confirmed', $response->getTargetUrl()); |
||
| 689 | |||
| 690 | // Assert that the CPF was moved to $person |
||
| 691 | $this->assertNotNull($personMeuRS->getNfgAccessToken()); |
||
| 692 | $this->assertNotNull($personMeuRS->getNfgProfile()); |
||
| 693 | } |
||
| 694 | |||
| 695 | /** |
||
| 696 | * This tests a user with CPF filled and with no CPF collision |
||
| 697 | */ |
||
| 698 | public function testConnectCallbackWithCpf() |
||
| 699 | { |
||
| 700 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 701 | $soapService = $this->getSoapService($accessId); |
||
| 702 | |||
| 703 | $cpf = '01234567890'; |
||
| 704 | $person = new Person(); |
||
| 705 | $person->setCpf($cpf); |
||
| 706 | $personMeuRS = new PersonMeuRS(); |
||
| 707 | $personMeuRS |
||
| 708 | ->setVoterRegistration('1234567890') |
||
| 709 | ->setPerson($person); |
||
| 710 | |||
| 711 | $nfgProfile = $this->getNfgProfile($personMeuRS->getVoterRegistration()); |
||
| 712 | $soapService->expects($this->atLeastOnce())->method('getUserInfo')->willReturn($nfgProfile); |
||
| 713 | |||
| 714 | $nfg = $this->getNfgService( |
||
| 715 | [ |
||
| 716 | 'session' => $this->getSession($accessId, 'none'), |
||
| 717 | 'soap' => $soapService, |
||
| 718 | ] |
||
| 719 | ); |
||
| 720 | |||
| 721 | $accessToken = 'access_token'.random_int(10, 9999); |
||
| 722 | $request = $this->getRequest($accessToken); |
||
| 723 | /** @var RedirectResponse $response */ |
||
| 724 | $response = $nfg->connectCallback($request, $personMeuRS); |
||
| 725 | |||
| 726 | $this->assertInstanceOf('\Symfony\Component\HttpFoundation\RedirectResponse', $response); |
||
| 727 | $this->assertEquals('fos_user_profile_edit', $response->getTargetUrl()); |
||
| 728 | |||
| 729 | // Assert that the CPF was moved to $person |
||
| 730 | $this->assertNotNull($personMeuRS->getNfgAccessToken()); |
||
| 731 | } |
||
| 732 | |||
| 733 | /** |
||
| 734 | * This tests a user with CPF filled that does not match NFG's CPF |
||
| 735 | */ |
||
| 736 | public function testConnectCallbackCpfMismatch() |
||
| 737 | { |
||
| 738 | $this->expectException('PROCERGS\LoginCidadao\NfgBundle\Exception\CpfMismatchException'); |
||
| 739 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 740 | $soapService = $this->getSoapService($accessId); |
||
| 741 | |||
| 742 | $cpf = '01234567891'; |
||
| 743 | $person = new Person(); |
||
| 744 | $person->setCpf($cpf); |
||
| 745 | $personMeuRS = new PersonMeuRS(); |
||
| 746 | $personMeuRS |
||
| 747 | ->setVoterRegistration('1234567890') |
||
| 748 | ->setPerson($person); |
||
| 749 | |||
| 750 | $nfgProfile = $this->getNfgProfile($personMeuRS->getVoterRegistration()); |
||
| 751 | $soapService->expects($this->atLeastOnce())->method('getUserInfo')->willReturn($nfgProfile); |
||
| 752 | |||
| 753 | $nfg = $this->getNfgService( |
||
| 754 | [ |
||
| 755 | 'session' => $this->getSession($accessId, 'none'), |
||
| 756 | 'soap' => $soapService, |
||
| 757 | ] |
||
| 758 | ); |
||
| 759 | |||
| 760 | $accessToken = 'access_token'.random_int(10, 9999); |
||
| 761 | $request = $this->getRequest($accessToken); |
||
| 762 | $nfg->connectCallback($request, $personMeuRS); |
||
| 763 | } |
||
| 764 | |||
| 765 | /** |
||
| 766 | * Test scenario where the person making the connection does not have a CPF in the profile but there is another |
||
| 767 | * account that uses the user's CPF but without NFG connection. |
||
| 768 | * |
||
| 769 | * The other user's account should have the CPF set to NULL and the current user will be connected to NFG |
||
| 770 | */ |
||
| 771 | public function testConnectCallbackWithoutCpfAndSimpleCollision() |
||
| 772 | { |
||
| 773 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 774 | $soapService = $this->getSoapService($accessId); |
||
| 775 | |||
| 776 | $cpf = '01234567890'; |
||
| 777 | $person = new Person(); |
||
| 778 | $personMeuRS = new PersonMeuRS(); |
||
| 779 | $personMeuRS |
||
| 780 | ->setId(1) |
||
| 781 | ->setVoterRegistration('1234567890') |
||
| 782 | ->setPerson($person); |
||
| 783 | |||
| 784 | $nfgProfile = $this->getNfgProfile($personMeuRS->getVoterRegistration()); |
||
| 785 | $soapService->expects($this->atLeastOnce())->method('getUserInfo')->willReturn($nfgProfile); |
||
| 786 | |||
| 787 | $otherPerson = new Person(); |
||
| 788 | $otherPerson->setCpf($cpf); |
||
| 789 | $otherPersonMeuRS = new PersonMeuRS(); |
||
| 790 | $otherPersonMeuRS |
||
| 791 | ->setPerson($otherPerson) |
||
| 792 | ->setId(2); |
||
| 793 | $meuRSHelper = $this->getMeuRSHelper($cpf, $otherPersonMeuRS); |
||
| 794 | |||
| 795 | $nfg = $this->getNfgService( |
||
| 796 | [ |
||
| 797 | // flush() and persist() should be called twice since we change both $person and $otherPerson |
||
| 798 | 'em' => $this->getEntityManager(['flush' => $this->exactly(2), 'persist' => $this->exactly(2)]), |
||
| 799 | 'session' => $this->getSession($accessId, 'none'), |
||
| 800 | 'soap' => $soapService, |
||
| 801 | 'meurs_helper' => $meuRSHelper, |
||
| 802 | ] |
||
| 803 | ); |
||
| 804 | |||
| 805 | $accessToken = 'access_token'.random_int(10, 9999); |
||
| 806 | $request = $this->getRequest($accessToken); |
||
| 807 | /** @var RedirectResponse $response */ |
||
| 808 | $response = $nfg->connectCallback($request, $personMeuRS); |
||
| 809 | |||
| 810 | $this->assertInstanceOf('\Symfony\Component\HttpFoundation\RedirectResponse', $response); |
||
| 811 | $this->assertEquals('fos_user_profile_edit', $response->getTargetUrl()); |
||
| 812 | |||
| 813 | // Assert the connection to NFG was made |
||
| 814 | $this->assertNotNull($personMeuRS->getNfgAccessToken()); |
||
| 815 | |||
| 816 | // Assert that the CPF was moved to $person |
||
| 817 | $this->assertNull($otherPerson->getCpf()); |
||
| 818 | $this->assertEquals($cpf, $person->getCpf()); |
||
| 819 | } |
||
| 820 | |||
| 821 | /** |
||
| 822 | * Test scenario where the person making the connection does not have a CPF in the profile but there is another |
||
| 823 | * account that user's CPF. Also, this other account is linked to the same NFG account. |
||
| 824 | */ |
||
| 825 | public function testConnectCallbackWithoutCpfAndCollision() |
||
| 826 | { |
||
| 827 | $this->expectException('PROCERGS\LoginCidadao\NfgBundle\Exception\NfgAccountCollisionException'); |
||
| 828 | |||
| 829 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 830 | $accessToken = 'access_token'.random_int(10, 9999); |
||
| 831 | $soapService = $this->getSoapService($accessId); |
||
| 832 | |||
| 833 | $cpf = '01234567890'; |
||
| 834 | $voterRegistration = '1234567890'; |
||
| 835 | $nfgProfile = $this->getNfgProfile($voterRegistration); |
||
| 836 | $person = new Person(); |
||
| 837 | $personMeuRS = new PersonMeuRS(); |
||
| 838 | $personMeuRS |
||
| 839 | ->setId(1) |
||
| 840 | ->setVoterRegistration($voterRegistration) |
||
| 841 | ->setPerson($person); |
||
| 842 | |||
| 843 | $soapService->expects($this->atLeastOnce())->method('getUserInfo')->willReturn($nfgProfile); |
||
| 844 | |||
| 845 | $otherPerson = new Person(); |
||
| 846 | $otherPerson->setCpf($cpf); |
||
| 847 | $otherPersonMeuRS = new PersonMeuRS(); |
||
| 848 | $otherPersonMeuRS |
||
| 849 | ->setId(2) |
||
| 850 | ->setNfgAccessToken($accessToken) |
||
| 851 | ->setNfgProfile($nfgProfile) |
||
| 852 | ->setPerson($otherPerson); |
||
| 853 | $meuRSHelper = $this->getMeuRSHelper($cpf, $otherPersonMeuRS); |
||
| 854 | |||
| 855 | $nfg = $this->getNfgService( |
||
| 856 | [ |
||
| 857 | 'session' => $this->getSession($accessId, 'none'), |
||
| 858 | 'soap' => $soapService, |
||
| 859 | 'meurs_helper' => $meuRSHelper, |
||
| 860 | ] |
||
| 861 | ); |
||
| 862 | |||
| 863 | $request = $this->getRequest($accessToken); |
||
| 864 | $nfg->connectCallback($request, $personMeuRS); |
||
| 865 | } |
||
| 866 | |||
| 867 | /** |
||
| 868 | * Test scenario where the person making the connection does not have a CPF in the profile but there is another |
||
| 869 | * account that user's CPF. Also, this other account is linked to the same NFG account. |
||
| 870 | * |
||
| 871 | * In this scenario, the used opted to override the existing connection |
||
| 872 | */ |
||
| 873 | public function testConnectCallbackWithoutCpfAndCollisionWithOverride() |
||
| 874 | { |
||
| 875 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 876 | $accessToken = 'access_token'.random_int(10, 9999); |
||
| 877 | $soapService = $this->getSoapService($accessId); |
||
| 878 | |||
| 879 | $cpf = '01234567890'; |
||
| 880 | $voterRegistration = '1234567890'; |
||
| 881 | $nfgProfile = $this->getNfgProfile($voterRegistration); |
||
| 882 | $person = new Person(); |
||
| 883 | $personMeuRS = new PersonMeuRS(); |
||
| 884 | $personMeuRS |
||
| 885 | ->setId(1) |
||
| 886 | ->setVoterRegistration($voterRegistration) |
||
| 887 | ->setPerson($person); |
||
| 888 | |||
| 889 | $soapService->expects($this->atLeastOnce())->method('getUserInfo')->willReturn($nfgProfile); |
||
| 890 | |||
| 891 | $otherPerson = new Person(); |
||
| 892 | $otherPerson->setCpf($cpf); |
||
| 893 | $otherPersonMeuRS = new PersonMeuRS(); |
||
| 894 | $otherPersonMeuRS |
||
| 895 | ->setId(2) |
||
| 896 | ->setNfgAccessToken($accessToken) |
||
| 897 | ->setNfgProfile($nfgProfile) |
||
| 898 | ->setPerson($otherPerson); |
||
| 899 | $meuRSHelper = $this->getMeuRSHelper($cpf, $otherPersonMeuRS); |
||
| 900 | |||
| 901 | $nfg = $this->getNfgService( |
||
| 902 | [ |
||
| 903 | 'session' => $this->getSession($accessId, 'none'), |
||
| 904 | 'soap' => $soapService, |
||
| 905 | 'meurs_helper' => $meuRSHelper, |
||
| 906 | ] |
||
| 907 | ); |
||
| 908 | |||
| 909 | $request = $this->getRequest($accessToken); |
||
| 910 | $nfg->connectCallback($request, $personMeuRS, true); |
||
| 911 | |||
| 912 | $this->assertNull($otherPersonMeuRS->getNfgProfile()); |
||
| 913 | $this->assertNull($otherPersonMeuRS->getNfgAccessToken()); |
||
| 914 | } |
||
| 915 | |||
| 916 | public function testDisconnection() |
||
| 917 | { |
||
| 918 | $em = $this->getEntityManager(['flush' => $this->atLeastOnce()]); |
||
| 919 | $em->expects($this->atLeastOnce())->method('remove')->willReturn(true); |
||
| 920 | |||
| 921 | $person = new Person(); |
||
| 922 | $personMeuRS = new PersonMeuRS(); |
||
| 923 | $personMeuRS |
||
| 924 | ->setNfgAccessToken('not null') |
||
| 925 | ->setNfgProfile($this->getNfgProfile()) |
||
| 926 | ->setPerson($person); |
||
| 927 | |||
| 928 | // Run the disconnection method |
||
| 929 | $this |
||
| 930 | ->getNfgService(compact('em')) |
||
| 931 | ->disconnect($personMeuRS); |
||
| 932 | |||
| 933 | $this->assertNull($personMeuRS->getNfgProfile()); |
||
| 934 | $this->assertNull($personMeuRS->getNfgAccessToken()); |
||
| 935 | } |
||
| 936 | |||
| 937 | private function getNfgService(array $collaborators) |
||
| 938 | { |
||
| 939 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 940 | if (false === array_key_exists('em', $collaborators)) { |
||
| 941 | $collaborators['em'] = $this->getEntityManager(); |
||
| 942 | } |
||
| 943 | if (false === array_key_exists('soap', $collaborators)) { |
||
| 944 | $collaborators['soap'] = $this->getSoapService($accessId); |
||
| 945 | } |
||
| 946 | if (false === array_key_exists('router', $collaborators)) { |
||
| 947 | $collaborators['router'] = $this->getRouter($this); |
||
| 948 | } |
||
| 949 | if (false === array_key_exists('session', $collaborators)) { |
||
| 950 | $collaborators['session'] = $this->getSession($accessId, 'none'); |
||
| 951 | } |
||
| 952 | if (false === array_key_exists('login_manager', $collaborators)) { |
||
| 953 | $collaborators['login_manager'] = $this->getLoginManager(false); |
||
| 954 | } |
||
| 955 | if (false === array_key_exists('meurs_helper', $collaborators)) { |
||
| 956 | $collaborators['meurs_helper'] = $this->getMeuRSHelper(); |
||
| 957 | } |
||
| 958 | if (false === array_key_exists('dispatcher', $collaborators)) { |
||
| 959 | $collaborators['dispatcher'] = $this->getDispatcher(); |
||
| 960 | } |
||
| 961 | if (false === array_key_exists('form_factory', $collaborators)) { |
||
| 962 | $collaborators['form_factory'] = $this->getFormFactory(); |
||
| 963 | } |
||
| 964 | if (false === array_key_exists('user_manager', $collaborators)) { |
||
| 965 | $collaborators['user_manager'] = $this->getUserManager(); |
||
| 966 | } |
||
| 967 | if (false === array_key_exists('firewall', $collaborators)) { |
||
| 968 | $collaborators['firewall'] = 'firewall'; |
||
| 969 | } |
||
| 970 | if (false === array_key_exists('login_endpoint', $collaborators)) { |
||
| 971 | $collaborators['login_endpoint'] = 'https://dum.my/login'; |
||
| 972 | } |
||
| 973 | if (false === array_key_exists('auth_endpoint', $collaborators)) { |
||
| 974 | $collaborators['auth_endpoint'] = 'https://dum.my/auth'; |
||
| 975 | } |
||
| 976 | if (false === array_key_exists('nfgprofile_repository', $collaborators)) { |
||
| 977 | $collaborators['nfgprofile_repository'] = $this->getNfgProfileRepository(); |
||
| 978 | } |
||
| 979 | if (false === array_key_exists('mailer', $collaborators)) { |
||
| 980 | $collaborators['mailer'] = $this->getMailer(); |
||
| 981 | } |
||
| 982 | |||
| 983 | $canonicalizer = new Canonicalizer(); |
||
| 984 | |||
| 985 | $nfg = new Nfg( |
||
| 986 | $collaborators['em'], |
||
| 987 | $collaborators['soap'], |
||
| 988 | $collaborators['router'], |
||
| 989 | $collaborators['session'], |
||
| 990 | $collaborators['login_manager'], |
||
| 991 | $collaborators['meurs_helper'], |
||
| 992 | $collaborators['dispatcher'], |
||
| 993 | $collaborators['user_manager'], |
||
| 994 | $collaborators['form_factory'], |
||
| 995 | $collaborators['nfgprofile_repository'], |
||
| 996 | $collaborators['mailer'], |
||
| 997 | $canonicalizer, |
||
| 998 | $collaborators['firewall'], |
||
| 999 | $collaborators['login_endpoint'], |
||
| 1000 | $collaborators['auth_endpoint'] |
||
| 1001 | ); |
||
| 1002 | |||
| 1003 | return $nfg; |
||
| 1004 | } |
||
| 1005 | |||
| 1006 | private function getSoapService($accessId) |
||
| 1007 | { |
||
| 1008 | $soapService = $this->createMock('\PROCERGS\LoginCidadao\NfgBundle\Service\NfgSoapInterface'); |
||
| 1009 | $soapService->expects($this->any())->method('getAccessID')->willReturn($accessId); |
||
| 1010 | |||
| 1011 | return $soapService; |
||
| 1012 | } |
||
| 1013 | |||
| 1014 | /** |
||
| 1015 | * @param bool $shouldCallLogInUser |
||
| 1016 | * @return \PHPUnit_Framework_MockObject_MockObject |
||
| 1017 | */ |
||
| 1018 | private function getLoginManager($shouldCallLogInUser = false) |
||
| 1019 | { |
||
| 1020 | if ($shouldCallLogInUser) { |
||
| 1021 | $frequency = $this->atLeastOnce(); |
||
| 1022 | } else { |
||
| 1023 | $frequency = $this->any(); |
||
| 1024 | } |
||
| 1025 | $loginManager = $this->createMock('\FOS\UserBundle\Security\LoginManagerInterface'); |
||
| 1026 | $loginManager->expects($frequency) |
||
| 1027 | ->method('logInUser')->with( |
||
| 1028 | $this->isType('string'), |
||
| 1029 | $this->isInstanceOf('\FOS\UserBundle\Model\UserInterface'), |
||
| 1030 | $this->isInstanceOf('\Symfony\Component\HttpFoundation\Response') |
||
| 1031 | ); |
||
| 1032 | |||
| 1033 | return $loginManager; |
||
| 1034 | } |
||
| 1035 | |||
| 1036 | /** |
||
| 1037 | * @param $accessId |
||
| 1038 | * @param null $shouldCall |
||
| 1039 | * @return \PHPUnit_Framework_MockObject_MockObject|SessionInterface |
||
| 1040 | */ |
||
| 1041 | private function getSession($accessId, $shouldCall = null) |
||
| 1042 | { |
||
| 1043 | $session = $this->createMock('\Symfony\Component\HttpFoundation\Session\SessionInterface'); |
||
| 1044 | switch ($shouldCall) { |
||
| 1045 | case 'get': |
||
| 1046 | $session->expects($this->atLeastOnce()) |
||
| 1047 | ->method('get')->with(Nfg::ACCESS_ID_SESSION_KEY)->willReturn($accessId); |
||
| 1048 | break; |
||
| 1049 | case 'set': |
||
| 1050 | $session->expects($this->atLeastOnce()) |
||
| 1051 | ->method('set')->with(Nfg::ACCESS_ID_SESSION_KEY, $accessId); |
||
| 1052 | break; |
||
| 1053 | default: |
||
| 1054 | |||
| 1055 | } |
||
| 1056 | |||
| 1057 | return $session; |
||
| 1058 | } |
||
| 1059 | |||
| 1060 | private function getEntityManager(array $matchers = []) |
||
| 1061 | { |
||
| 1062 | $matchers = array_merge( |
||
| 1063 | [ |
||
| 1064 | 'persist' => $this->any(), |
||
| 1065 | 'flush' => $this->any(), |
||
| 1066 | ], |
||
| 1067 | $matchers |
||
| 1068 | ); |
||
| 1069 | |||
| 1070 | $em = $this->getMockBuilder('Doctrine\ORM\EntityManager') |
||
| 1071 | ->disableOriginalConstructor() |
||
| 1072 | ->getMock(); |
||
| 1073 | $em->expects($matchers['persist'])->method('persist')->willReturn(true); |
||
| 1074 | $em->expects($matchers['flush'])->method('flush')->willReturn(true); |
||
| 1075 | |||
| 1076 | return $em; |
||
| 1077 | } |
||
| 1078 | |||
| 1079 | /** |
||
| 1080 | * @param $accessToken |
||
| 1081 | * @return \PHPUnit_Framework_MockObject_MockObject|Request |
||
| 1082 | */ |
||
| 1083 | private function getRequest($accessToken) |
||
| 1084 | { |
||
| 1085 | $request = $this->createMock('Symfony\Component\HttpFoundation\Request'); |
||
| 1086 | $request->expects($this->atLeastOnce())->method('get')->with('paccessid')->willReturn($accessToken); |
||
| 1087 | |||
| 1088 | return $request; |
||
| 1089 | } |
||
| 1090 | |||
| 1091 | /** |
||
| 1092 | * @return \PHPUnit_Framework_MockObject_MockObject |
||
| 1093 | */ |
||
| 1094 | private function getDispatcher() |
||
| 1095 | { |
||
| 1096 | return $this->createMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'); |
||
| 1097 | } |
||
| 1098 | |||
| 1099 | /** |
||
| 1100 | * @return FormFactory|\PHPUnit_Framework_MockObject_MockObject |
||
| 1101 | */ |
||
| 1102 | private function getFormFactory() |
||
| 1103 | { |
||
| 1104 | $formFactory = $this->getMockBuilder('FOS\UserBundle\Form\Factory\FormFactory') |
||
| 1105 | ->disableOriginalConstructor() |
||
| 1106 | ->getMock(); |
||
| 1107 | |||
| 1108 | $formFactory->expects($this->any())->method('createForm') |
||
| 1109 | ->willReturnCallback(function () { |
||
| 1110 | return $this->createMock('Symfony\Component\Form\FormInterface'); |
||
| 1111 | }); |
||
| 1112 | |||
| 1113 | return $formFactory; |
||
| 1114 | } |
||
| 1115 | |||
| 1116 | private function getNfgProfile($voterRegistration = null) |
||
| 1117 | { |
||
| 1118 | $nfgProfile = new NfgProfile(); |
||
| 1119 | $nfgProfile->setName('John Doe') |
||
| 1120 | ->setEmail('[email protected]') |
||
| 1121 | ->setBirthdate('1970-01-01T00:00:00') |
||
| 1122 | ->setMobile('+555193333333') |
||
| 1123 | ->setVoterRegistration($voterRegistration) |
||
| 1124 | ->setCpf('1234567890')// NFG treats CPF as integers, hence no leading 0s |
||
| 1125 | ->setAccessLvl(2); |
||
| 1126 | |||
| 1127 | return $nfgProfile; |
||
| 1128 | } |
||
| 1129 | |||
| 1130 | /** |
||
| 1131 | * @return UserManagerInterface|\PHPUnit_Framework_MockObject_MockObject |
||
| 1132 | */ |
||
| 1133 | private function getUserManager() |
||
| 1134 | { |
||
| 1135 | $userManager = $this->createMock('FOS\UserBundle\Model\UserManagerInterface'); |
||
| 1136 | $userManager->expects($this->any())->method('createUser')->willReturnCallback(function () { |
||
| 1137 | return new Person(); |
||
| 1138 | }); |
||
| 1139 | |||
| 1140 | return $userManager; |
||
| 1141 | } |
||
| 1142 | |||
| 1143 | private function getMeuRSHelper($expectedCpf = null, $response = null) |
||
| 1144 | { |
||
| 1145 | $meuRSHelper = $this->getMockBuilder('PROCERGS\LoginCidadao\CoreBundle\Helper\MeuRSHelper') |
||
| 1146 | ->disableOriginalConstructor() |
||
| 1147 | ->getMock(); |
||
| 1148 | |||
| 1149 | if ($expectedCpf) { |
||
| 1150 | $meuRSHelper->expects($this->atLeastOnce()) |
||
| 1151 | ->method('getPersonByCpf')->with($expectedCpf)->willReturn($response); |
||
| 1152 | } |
||
| 1153 | |||
| 1154 | return $meuRSHelper; |
||
| 1155 | } |
||
| 1156 | |||
| 1157 | private function getNfgProfileRepository() |
||
| 1158 | { |
||
| 1159 | return $this->getMockBuilder('PROCERGS\LoginCidadao\NfgBundle\Entity\NfgProfileRepository') |
||
| 1160 | ->disableOriginalConstructor() |
||
| 1161 | ->getMock(); |
||
| 1162 | } |
||
| 1163 | |||
| 1164 | private function getMailer() |
||
| 1165 | { |
||
| 1166 | return $this->createMock('PROCERGS\LoginCidadao\NfgBundle\Mailer\MailerInterface'); |
||
| 1167 | } |
||
| 1168 | |||
| 1169 | public function testRegisterCpfTakeOver() |
||
| 1170 | { |
||
| 1171 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 1172 | $soapService = $this->getSoapService($accessId); |
||
| 1173 | |||
| 1174 | $nfgProfile = $this->getNfgProfile(); |
||
| 1175 | $soapService->expects($this->atLeastOnce())->method('getUserInfo')->willReturn($nfgProfile); |
||
| 1176 | |||
| 1177 | $otherPersonMeuRS = new PersonMeuRS(); |
||
| 1178 | $otherPersonMeuRS->setPerson(new Person()); |
||
| 1179 | $otherPersonMeuRS->getPerson()->setCpf($nfgProfile->getCpf()); |
||
| 1180 | $meuRSHelper = $this->getMeuRSHelper($nfgProfile->getCpf(), $otherPersonMeuRS); |
||
| 1181 | |||
| 1182 | $dispatcher = $this->getDispatcher(); |
||
| 1183 | $dispatcher->expects($this->atLeastOnce())->method('dispatch') |
||
| 1184 | ->willReturnCallback(function ($eventName, $event) { |
||
| 1185 | if ($eventName === FOSUserEvents::REGISTRATION_INITIALIZE |
||
| 1186 | && $event instanceof GetResponseUserEvent |
||
| 1187 | ) { |
||
| 1188 | $event->setResponse(new RedirectResponse('dummy')); |
||
| 1189 | } |
||
| 1190 | }); |
||
| 1191 | |||
| 1192 | $mailer = $this->getMailer(); |
||
| 1193 | $mailer->expects($this->atLeastOnce())->method('notifyCpfLost'); |
||
| 1194 | |||
| 1195 | $nfg = $this->getNfgService( |
||
| 1196 | [ |
||
| 1197 | 'session' => $this->getSession($accessId, 'none'), |
||
| 1198 | 'soap' => $soapService, |
||
| 1199 | 'meurs_helper' => $meuRSHelper, |
||
| 1200 | 'dispatcher' => $dispatcher, |
||
| 1201 | 'mailer' => $mailer, |
||
| 1202 | ] |
||
| 1203 | ); |
||
| 1204 | |||
| 1205 | $personMeuRS = new PersonMeuRS(); |
||
| 1206 | |||
| 1207 | $accessToken = 'access_token'.random_int(10, 9999); |
||
| 1208 | $request = $this->getRequest($accessToken); |
||
| 1209 | /** @var RedirectResponse $response */ |
||
| 1210 | $response = $nfg->connectCallback($request, $personMeuRS); |
||
| 1211 | |||
| 1212 | $this->assertInstanceOf('\Symfony\Component\HttpFoundation\RedirectResponse', $response); |
||
| 1213 | $this->assertEquals('dummy', $response->getTargetUrl()); |
||
| 1214 | } |
||
| 1215 | |||
| 1216 | public function testRegisterEmailMissing() |
||
| 1217 | { |
||
| 1218 | $this->expectException('PROCERGS\LoginCidadao\NfgBundle\Exception\MissingRequiredInformationException'); |
||
| 1219 | |||
| 1220 | $accessId = 'access_id'.random_int(10, 9999); |
||
| 1221 | $soapService = $this->getSoapService($accessId); |
||
| 1222 | |||
| 1223 | $nfgProfile = $this->getNfgProfile(); |
||
| 1224 | $nfgProfile->setEmail(null); |
||
| 1225 | $soapService->expects($this->atLeastOnce())->method('getUserInfo')->willReturn($nfgProfile); |
||
| 1226 | |||
| 1227 | $nfg = $this->getNfgService( |
||
| 1228 | [ |
||
| 1229 | 'session' => $this->getSession($accessId, 'none'), |
||
| 1230 | 'soap' => $soapService, |
||
| 1231 | ] |
||
| 1232 | ); |
||
| 1233 | |||
| 1239 |