From 54597ebef7f9c4e4e9071698c2052ef3bc4435c1 Mon Sep 17 00:00:00 2001 From: Yaroslav Rogoza Date: Thu, 14 Jun 2018 10:49:14 +0200 Subject: [PATCH 1/2] Added unit test for CheckRegisterCheckoutObserver --- .../CheckRegisterCheckoutObserverTest.php | 211 ++++++++++++++++++ 1 file changed, 211 insertions(+) create mode 100644 app/code/Magento/Captcha/Test/Unit/Observer/CheckRegisterCheckoutObserverTest.php diff --git a/app/code/Magento/Captcha/Test/Unit/Observer/CheckRegisterCheckoutObserverTest.php b/app/code/Magento/Captcha/Test/Unit/Observer/CheckRegisterCheckoutObserverTest.php new file mode 100644 index 0000000000000..6c0618197403f --- /dev/null +++ b/app/code/Magento/Captcha/Test/Unit/Observer/CheckRegisterCheckoutObserverTest.php @@ -0,0 +1,211 @@ +createMock(Onepage::class); + $captchaDataHelperMock = $this->createMock(CaptchaDataHelper::class); + $this->objectManager = new ObjectManager($this); + $this->actionFlagMock = $this->createMock(ActionFlag::class); + $this->captchaStringResolverMock = $this->createMock(CaptchaStringResolver::class); + $this->captchaModelMock = $this->createMock(CaptchaModel::class); + $this->quoteModelMock = $this->createMock(Quote::class); + $this->controllerMock = $this->createMock(Action::class); + $this->requestMock = $this->createMock(Http::class); + $this->responseMock = $this->createMock(HttpResponse::class); + $this->observer = new Observer(['controller_action' => $this->controllerMock]); + $this->jsonHelperMock = $this->createMock(JsonHelper::class); + + $this->checkRegisterCheckoutObserver = $this->objectManager->getObject( + CheckRegisterCheckoutObserver::class, + [ + 'helper' => $captchaDataHelperMock, + 'actionFlag' => $this->actionFlagMock, + 'captchaStringResolver' => $this->captchaStringResolverMock, + 'typeOnepage' => $onepageModelTypeMock, + 'jsonHelper' => $this->jsonHelperMock + ] + ); + + $captchaDataHelperMock->expects($this->once()) + ->method('getCaptcha') + ->with(self::FORM_ID) + ->willReturn($this->captchaModelMock); + $onepageModelTypeMock->expects($this->once()) + ->method('getQuote') + ->willReturn($this->quoteModelMock); + } + + public function testCheckRegisterCheckoutForGuest() + { + $this->quoteModelMock->expects($this->once()) + ->method('getCheckoutMethod') + ->willReturn(Onepage::METHOD_GUEST); + $this->captchaModelMock->expects($this->never()) + ->method('isRequired'); + + $this->checkRegisterCheckoutObserver->execute($this->observer); + } + + public function testCheckRegisterCheckoutWithNoCaptchaRequired() + { + $this->quoteModelMock->expects($this->once()) + ->method('getCheckoutMethod') + ->willReturn(Onepage::METHOD_REGISTER); + $this->captchaModelMock->expects($this->once()) + ->method('isRequired') + ->willReturn(false); + $this->captchaModelMock->expects($this->never()) + ->method('isCorrect'); + + $this->checkRegisterCheckoutObserver->execute($this->observer); + } + + public function testCheckRegisterCheckoutWithIncorrectCaptcha() + { + $captchaValue = 'some_word'; + $encodedJsonValue = '{}'; + + $this->quoteModelMock->expects($this->once()) + ->method('getCheckoutMethod') + ->willReturn(Onepage::METHOD_REGISTER); + $this->captchaModelMock->expects($this->once()) + ->method('isRequired') + ->willReturn(true); + $this->controllerMock->expects($this->once()) + ->method('getRequest') + ->willReturn($this->requestMock); + $this->controllerMock->expects($this->once()) + ->method('getResponse') + ->willReturn($this->responseMock); + $this->controllerMock->expects($this->once()) + ->method('getResponse') + ->willReturn($this->responseMock); + $this->captchaStringResolverMock->expects($this->once()) + ->method('resolve') + ->with($this->requestMock, self::FORM_ID) + ->willReturn($captchaValue); + $this->captchaModelMock->expects($this->once()) + ->method('isCorrect') + ->with($captchaValue) + ->willReturn(false); + $this->actionFlagMock->expects($this->once()) + ->method('set') + ->with('', Action::FLAG_NO_DISPATCH, true); + $this->jsonHelperMock->expects($this->once()) + ->method('jsonEncode') + ->willReturn($encodedJsonValue); + $this->responseMock->expects($this->once()) + ->method('representJson') + ->with($encodedJsonValue); + + $this->checkRegisterCheckoutObserver->execute($this->observer); + } + + public function testCheckRegisterCheckoutWithCorrectCaptcha() + { + $this->quoteModelMock->expects($this->once()) + ->method('getCheckoutMethod') + ->willReturn(Onepage::METHOD_REGISTER); + $this->captchaModelMock->expects($this->once()) + ->method('isRequired') + ->willReturn(true); + $this->controllerMock->expects($this->once()) + ->method('getRequest') + ->willReturn($this->requestMock); + $this->captchaStringResolverMock->expects($this->once()) + ->method('resolve') + ->with($this->requestMock, self::FORM_ID) + ->willReturn('some_word'); + $this->captchaModelMock->expects($this->once()) + ->method('isCorrect') + ->with('some_word') + ->willReturn(true); + $this->actionFlagMock->expects($this->never()) + ->method('set'); + + $this->checkRegisterCheckoutObserver->execute($this->observer); + } +} From 51e12bf50a7d8fbac3814df7be693b0e56cac0c8 Mon Sep 17 00:00:00 2001 From: Yaroslav Rogoza Date: Thu, 14 Jun 2018 11:53:34 +0200 Subject: [PATCH 2/2] Fixed long variable name --- .../Unit/Observer/CheckRegisterCheckoutObserverTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/Captcha/Test/Unit/Observer/CheckRegisterCheckoutObserverTest.php b/app/code/Magento/Captcha/Test/Unit/Observer/CheckRegisterCheckoutObserverTest.php index 6c0618197403f..89012ef653838 100644 --- a/app/code/Magento/Captcha/Test/Unit/Observer/CheckRegisterCheckoutObserverTest.php +++ b/app/code/Magento/Captcha/Test/Unit/Observer/CheckRegisterCheckoutObserverTest.php @@ -86,7 +86,7 @@ class CheckRegisterCheckoutObserverTest extends \PHPUnit\Framework\TestCase protected function setUp() { $onepageModelTypeMock = $this->createMock(Onepage::class); - $captchaDataHelperMock = $this->createMock(CaptchaDataHelper::class); + $captchaHelperMock = $this->createMock(CaptchaDataHelper::class); $this->objectManager = new ObjectManager($this); $this->actionFlagMock = $this->createMock(ActionFlag::class); $this->captchaStringResolverMock = $this->createMock(CaptchaStringResolver::class); @@ -101,7 +101,7 @@ protected function setUp() $this->checkRegisterCheckoutObserver = $this->objectManager->getObject( CheckRegisterCheckoutObserver::class, [ - 'helper' => $captchaDataHelperMock, + 'helper' => $captchaHelperMock, 'actionFlag' => $this->actionFlagMock, 'captchaStringResolver' => $this->captchaStringResolverMock, 'typeOnepage' => $onepageModelTypeMock, @@ -109,7 +109,7 @@ protected function setUp() ] ); - $captchaDataHelperMock->expects($this->once()) + $captchaHelperMock->expects($this->once()) ->method('getCaptcha') ->with(self::FORM_ID) ->willReturn($this->captchaModelMock);