diff --git a/lib/Subscription/CaptchaSession.php b/lib/Subscription/CaptchaSession.php index d2c9e363fc..8fe12de1f1 100644 --- a/lib/Subscription/CaptchaSession.php +++ b/lib/Subscription/CaptchaSession.php @@ -48,9 +48,10 @@ class CaptchaSession { } private function getKey($type) { - if ($this->session->getId() === null) { + $session_id = $this->session->getId(); + if ($session_id === null) { throw new \Exception("MailPoet session not initialized."); } - return implode('_', ['MAILPOET', $this->session->getId(), $type]); + return implode('_', ['MAILPOET', $session_id, $type]); } } diff --git a/tests/integration/Subscription/CaptchaSessionTest.php b/tests/integration/Subscription/CaptchaSessionTest.php new file mode 100644 index 0000000000..f7e5d2490f --- /dev/null +++ b/tests/integration/Subscription/CaptchaSessionTest.php @@ -0,0 +1,78 @@ +cookies_mock = $this->createMock(Cookies::class); + $this->captcha_session = new CaptchaSession(new WPFunctions, new Session($this->cookies_mock)); + } + + function testIsAvailableWhenCookieExists() { + $this->cookies_mock + ->method('get') + ->willReturn('abcd'); + expect($this->captcha_session->isAvailable())->true(); + } + + function testIsNotAvailableWhenCookieDoesntExits() { + $this->cookies_mock + ->method('get') + ->willReturn(null); + expect($this->captcha_session->isAvailable())->false(); + } + + function testItCanStoreAndRetrieveFormData() { + $this->cookies_mock + ->method('get') + ->willReturn('abcd'); + $form_data = ['email' => 'email@example.com']; + $this->captcha_session->setFormData($form_data); + expect($this->captcha_session->getFormData())->equals($form_data); + } + + function testItCanStoreAndRetrieveCaptchaHash() { + $this->cookies_mock + ->method('get') + ->willReturn('abcd'); + $hash = '1234'; + $this->captcha_session->setCaptchaHash($hash); + expect($this->captcha_session->getCaptchaHash())->equals($hash); + } + + function testItCanResetSessionData() { + $this->cookies_mock + ->method('get') + ->willReturn('abcd'); + $this->captcha_session->setFormData(['email' => 'email@example.com']); + $this->captcha_session->setCaptchaHash('hash123'); + $this->captcha_session->reset(); + expect($this->captcha_session->getFormData())->false(); + expect($this->captcha_session->getCaptchaHash())->false(); + } + + function testItAssociatesDataWithSession() { + $session1 = 'abcd'; + $session2 = 'efgh'; + $this->cookies_mock + ->method('get') + ->willReturnOnConsecutiveCalls($session1, $session1, $session2, $session1); + $hash = '1234'; + $this->captcha_session->setCaptchaHash($hash); + expect($this->captcha_session->getCaptchaHash())->equals($hash); + expect($this->captcha_session->getCaptchaHash())->false(); + expect($this->captcha_session->getCaptchaHash())->equals($hash); + } +}