FIX multiple session records being created per page request

This commit is contained in:
Will Rossiter 2014-07-02 20:34:44 +12:00
parent 26bf585550
commit 8485666273
2 changed files with 44 additions and 16 deletions

View File

@ -89,10 +89,6 @@ abstract class MultiForm extends Form {
* @param string $name The form name, typically the same as the method name
*/
public function __construct($controller, $name) {
if(isset($_GET['MultiFormSessionID'])) {
$this->setCurrentSessionHash($_GET['MultiFormSessionID']);
}
// First set the controller and name manually so they are available for
// field construction.
$this->controller = $controller;
@ -222,7 +218,7 @@ abstract class MultiForm extends Form {
*
* @return MultiFormSession
*/
function getSession() {
public function getSession() {
return $this->session;
}
@ -244,7 +240,6 @@ abstract class MultiForm extends Form {
// If there was no session found, create a new one instead
if(!$this->session) {
$this->session = new MultiFormSession();
$this->session->write();
}
// Create encrypted identification to the session instance if it doesn't exist
@ -260,18 +255,30 @@ abstract class MultiForm extends Form {
*
* @param string $hash Encrypted identification to session
*/
function setCurrentSessionHash($hash) {
public function setCurrentSessionHash($hash) {
$this->currentSessionHash = $hash;
$this->setSession();
}
/**
* Return the currently used {@link MultiFormSession}
* @return MultiFormSession|boolean FALSE
*/
function getCurrentSession() {
if(!$this->currentSessionHash) return false;
$SQL_hash = Convert::raw2sql($this->currentSessionHash);
return DataObject::get_one('MultiFormSession', "\"Hash\" = '$SQL_hash' AND \"IsComplete\" = 0");
public function getCurrentSession() {
if(!$this->currentSessionHash) {
$this->currentSessionHash = $this->controller->request->getVar('MultiFormSessionID');
if(!$this->currentSessionHash) {
return false;
}
}
$this->session = MultiFormSession::get()->filter(array(
"Hash" => $this->currentSessionHash,
"IsComplete" => 0
))->first();
return $this->session;
}
/**

View File

@ -69,18 +69,22 @@ class MultiFormTest extends FunctionalTest {
$this->form->setCurrentSessionHash($this->form->session->Hash);
$this->assertInstanceOf('MultiFormSession', $this->form->getCurrentSession());
$this->form->session->markCompleted();
$this->assertFalse($this->form->getCurrentSession());
$this->assertNull($this->form->getCurrentSession());
}
function testIncorrectSessionIdentifier() {
$this->form->setCurrentSessionHash('sdfsdf3432325325sfsdfdf'); // made up!
$this->assertFalse($this->form->getCurrentSession());
// A new session is generated, even though we made up the identifier
$this->assertInstanceOf('MultiFormSession', $this->form->session);
}
}
/**
* @package multiform
* @subpackage tests
*/
class MultiFormTest_Controller extends Controller implements TestOnly {
function Link() {
@ -92,8 +96,12 @@ class MultiFormTest_Controller extends Controller implements TestOnly {
$form->setHTMLID('MultiFormTest_Form');
return $form;
}
}
/**
* @package multiform
* @subpackage tests
*/
class MultiFormTest_Form extends MultiForm implements TestOnly {
public static $start_step = 'MultiFormTest_StepOne';
@ -103,6 +111,11 @@ class MultiFormTest_Form extends MultiForm implements TestOnly {
}
}
/**
* @package multiform
* @subpackage tests
*/
class MultiFormTest_StepOne extends MultiFormStep implements TestOnly {
public static $next_steps = 'MultiFormTest_StepTwo';
@ -115,8 +128,12 @@ class MultiFormTest_StepOne extends MultiFormStep implements TestOnly {
new EmailField('Email', 'Email address')
);
}
}
/**
* @package multiform
* @subpackage tests
*/
class MultiFormTest_StepTwo extends MultiFormStep implements TestOnly {
public static $next_steps = 'MultiFormTest_StepThree';
@ -127,8 +144,12 @@ class MultiFormTest_StepTwo extends MultiFormStep implements TestOnly {
new TextareaField('Comments', 'Tell us a bit about yourself...')
);
}
}
/**
* @package multiform
* @subpackage tests
*/
class MultiFormTest_StepThree extends MultiFormStep implements TestOnly {
public static $is_final_step = true;