diff --git a/lib/Subscription/CaptchaSession.php b/lib/Subscription/CaptchaSession.php index b1cd634afe..99bb442c12 100644 --- a/lib/Subscription/CaptchaSession.php +++ b/lib/Subscription/CaptchaSession.php @@ -2,11 +2,12 @@ namespace MailPoet\Subscription; -use MailPoet\Config\Session; +use MailPoet\Util\Security; use MailPoet\WP\Functions as WPFunctions; class CaptchaSession { const EXPIRATION = 1800; // 30 minutes + const ID_LENGTH = 32; const SESSION_HASH_KEY = 'hash'; const SESSION_FORM_KEY = 'form'; @@ -14,16 +15,22 @@ class CaptchaSession { /** @var WPFunctions */ private $wp; - /** @var Session */ - private $session; + /** @var string */ + private $id; - function __construct(WPFunctions $wp, Session $session) { + function __construct(WPFunctions $wp) { $this->wp = $wp; - $this->session = $session; } - function isAvailable() { - return $this->session->getId() !== null; + function init($id = null) { + $this->id = $id ?: Security::generateRandomString(self::ID_LENGTH); + } + + function getId() { + if ($this->id === null) { + throw new \Exception("MailPoet captcha session not initialized."); + } + return $this->id; } function reset() { @@ -48,10 +55,6 @@ class CaptchaSession { } private function getKey($type) { - $session_id = $this->session->getId(); - if ($session_id === null) { - throw new \Exception("MailPoet session not initialized."); - } - return implode('_', ['MAILPOET', $session_id, $type]); + return implode('_', ['MAILPOET', $this->getId(), $type]); } } diff --git a/tests/integration/Subscription/CaptchaSessionTest.php b/tests/integration/Subscription/CaptchaSessionTest.php index f7e5d2490f..50dd50b22b 100644 --- a/tests/integration/Subscription/CaptchaSessionTest.php +++ b/tests/integration/Subscription/CaptchaSessionTest.php @@ -1,61 +1,33 @@ 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(); + $this->captcha_session = new CaptchaSession(new WPFunctions); + $this->captcha_session->init(self::SESSION_ID); } 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(); @@ -64,15 +36,12 @@ class CaptchaSessionTest extends \MailPoetTest { } 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); + $this->captcha_session->init(); expect($this->captcha_session->getCaptchaHash())->false(); + $this->captcha_session->init(self::SESSION_ID); expect($this->captcha_session->getCaptchaHash())->equals($hash); } }