Add integration test for captcha session

[MAILPOET-2248]
This commit is contained in:
Rostislav Wolny
2019-08-05 16:19:58 +02:00
committed by Jack Kitterhing
parent 13724898d1
commit 579ce5fc64
2 changed files with 81 additions and 2 deletions

View File

@@ -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]);
}
}

View File

@@ -0,0 +1,78 @@
<?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 {
/** @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();
}
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);
}
}