Refactor captcha session to use internal session id
[MAILPOET-2343]
This commit is contained in:
committed by
Jack Kitterhing
parent
5a33946ea8
commit
704117d37d
@@ -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]);
|
||||
}
|
||||
}
|
||||
|
@@ -1,61 +1,33 @@
|
||||
<?php
|
||||
namespace MailPoet\Test\Subscription;
|
||||
|
||||
use MailPoet\Config\Session;
|
||||
use MailPoet\Subscription\CaptchaSession;
|
||||
use MailPoet\Util\Cookies;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
use PHPUnit_Framework_MockObject_MockObject as MockObject;
|
||||
|
||||
class CaptchaSessionTest extends \MailPoetTest {
|
||||
const SESSION_ID = 'ABCD';
|
||||
|
||||
/** @var CaptchaSession */
|
||||
private $captcha_session;
|
||||
|
||||
/** @var MockObject */
|
||||
private $cookies_mock;
|
||||
|
||||
function _before() {
|
||||
$this->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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user