2008-05-14 13:09:47 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* MultiFormTest
|
2009-07-20 11:22:49 +02:00
|
|
|
* For testing purposes, we have some test classes:
|
2008-05-14 13:09:47 +02:00
|
|
|
*
|
2009-07-20 11:22:49 +02:00
|
|
|
* - MultiFormTest_Controller (simulation of a real Controller class)
|
|
|
|
* - MultiFormTest_Form (subclass of MultiForm)
|
|
|
|
* - MultiFormTest_StepOne (subclass of MultiFormStep)
|
|
|
|
* - MultiFormTest_StepTwo (subclass of MultiFormStep)
|
|
|
|
* - MultiFormTest_StepThree (subclass of MultiFormStep)
|
|
|
|
*
|
|
|
|
* The above classes are used to simulate real-world behaviour
|
|
|
|
* of the multiform module - for example, MultiFormTest_Controller
|
|
|
|
* is a simulation of a page where MultiFormTest_Form is a simple
|
|
|
|
* multi-step contact form it belongs to.
|
2008-05-14 13:09:47 +02:00
|
|
|
*
|
2009-07-20 11:22:49 +02:00
|
|
|
* @package multiform
|
|
|
|
* @subpackage tests
|
2008-05-14 13:09:47 +02:00
|
|
|
*/
|
2009-07-20 11:22:49 +02:00
|
|
|
class MultiFormTest extends FunctionalTest {
|
|
|
|
|
2009-07-21 00:36:32 +02:00
|
|
|
public static $fixture_file = 'multiform/tests/MultiFormTest.yml';
|
|
|
|
|
2009-07-20 11:22:49 +02:00
|
|
|
protected $controller;
|
|
|
|
|
2008-05-14 13:09:47 +02:00
|
|
|
function setUp() {
|
2009-07-20 11:22:49 +02:00
|
|
|
parent::setUp();
|
2009-08-05 12:12:13 +02:00
|
|
|
|
2009-07-20 11:22:49 +02:00
|
|
|
$this->controller = new MultiFormTest_Controller();
|
|
|
|
$this->form = $this->controller->Form();
|
2008-05-14 13:09:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function testInitialisingForm() {
|
|
|
|
$this->assertTrue(is_numeric($this->form->getCurrentStep()->ID) && ($this->form->getCurrentStep()->ID > 0));
|
2008-08-21 10:17:04 +02:00
|
|
|
$this->assertTrue(is_numeric($this->form->getSession()->ID) && ($this->form->getSession()->ID > 0));
|
2009-07-20 11:22:49 +02:00
|
|
|
$this->assertEquals('MultiFormTest_StepOne', $this->form->getStartStep());
|
2008-05-23 07:47:07 +02:00
|
|
|
}
|
|
|
|
|
2009-07-21 00:36:32 +02:00
|
|
|
function testSessionGeneration() {
|
|
|
|
$this->assertTrue($this->form->session->ID > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
function testMemberLogging() {
|
2010-05-04 04:14:17 +02:00
|
|
|
// Grab any user to fake being logged in as, and ensure that after a session is written it has
|
|
|
|
// that user as the submitter.
|
|
|
|
$userId = Member::get_one("Member")->ID;
|
|
|
|
$this->session()->inst_set('loggedInAs', $userId);
|
2009-07-21 00:36:32 +02:00
|
|
|
|
|
|
|
$session = $this->form->session;
|
|
|
|
$session->write();
|
|
|
|
|
2010-05-04 04:14:17 +02:00
|
|
|
$this->assertEquals($userId, $session->SubmitterID);
|
2009-07-21 00:36:32 +02:00
|
|
|
}
|
|
|
|
|
2008-05-23 07:47:07 +02:00
|
|
|
function testSecondStep() {
|
2009-07-20 11:22:49 +02:00
|
|
|
$this->assertEquals('MultiFormTest_StepTwo', $this->form->getCurrentStep()->getNextStep());
|
|
|
|
}
|
|
|
|
|
|
|
|
function testParentForm() {
|
|
|
|
$currentStep = $this->form->getCurrentStep();
|
|
|
|
$this->assertEquals($currentStep->getForm()->class, $this->form->class);
|
2008-05-23 07:47:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function testTotalStepCount() {
|
|
|
|
$this->assertEquals(3, $this->form->getAllStepsLinear()->Count());
|
2008-05-14 13:09:47 +02:00
|
|
|
}
|
2012-05-14 05:23:01 +02:00
|
|
|
|
2009-08-05 12:12:13 +02:00
|
|
|
function testCompletedSession() {
|
|
|
|
$this->form->setCurrentSessionHash($this->form->session->Hash);
|
2012-05-14 05:23:01 +02:00
|
|
|
$this->assertInstanceOf('MultiFormSession', $this->form->getCurrentSession());
|
2009-08-05 12:12:13 +02:00
|
|
|
$this->form->session->markCompleted();
|
2014-07-02 10:34:44 +02:00
|
|
|
$this->assertNull($this->form->getCurrentSession());
|
2009-08-05 12:12:13 +02:00
|
|
|
}
|
|
|
|
|
2009-08-05 12:31:47 +02:00
|
|
|
function testIncorrectSessionIdentifier() {
|
|
|
|
$this->form->setCurrentSessionHash('sdfsdf3432325325sfsdfdf'); // made up!
|
|
|
|
|
|
|
|
// A new session is generated, even though we made up the identifier
|
2012-05-14 05:23:01 +02:00
|
|
|
$this->assertInstanceOf('MultiFormSession', $this->form->session);
|
2009-08-05 12:31:47 +02:00
|
|
|
}
|
2015-02-03 16:17:42 +01:00
|
|
|
|
|
|
|
function testCustomGetVar() {
|
|
|
|
Config::nest();
|
|
|
|
Config::inst()->update('MultiForm', 'get_var', 'SuperSessionID');
|
|
|
|
|
|
|
|
$form = $this->controller->Form();
|
|
|
|
$this->assertContains('SuperSessionID', $form::$ignored_fields, "GET var wasn't added to ignored fields");
|
|
|
|
$this->assertContains('SuperSessionID', $form->FormAction(), "Form action doesn't contain correct session
|
|
|
|
ID parameter");
|
|
|
|
$this->assertContains('SuperSessionID', $form->getCurrentStep()->Link(), "Form step doesn't contain correct
|
|
|
|
session ID parameter");
|
|
|
|
|
|
|
|
Config::unnest();
|
2014-06-17 06:55:07 +02:00
|
|
|
}
|
2009-08-05 12:31:47 +02:00
|
|
|
|
2009-07-20 11:22:49 +02:00
|
|
|
}
|
2014-07-02 10:34:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @package multiform
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2009-07-20 11:22:49 +02:00
|
|
|
class MultiFormTest_Controller extends Controller implements TestOnly {
|
|
|
|
|
2009-07-20 12:57:00 +02:00
|
|
|
function Link() {
|
|
|
|
return 'MultiFormTest_Controller';
|
2009-07-20 11:22:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function Form($request = null) {
|
|
|
|
$form = new MultiFormTest_Form($this, 'Form');
|
|
|
|
$form->setHTMLID('MultiFormTest_Form');
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
}
|
2014-07-02 10:34:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @package multiform
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2009-07-20 11:22:49 +02:00
|
|
|
class MultiFormTest_Form extends MultiForm implements TestOnly {
|
|
|
|
|
2010-05-11 05:30:55 +02:00
|
|
|
public static $start_step = 'MultiFormTest_StepOne';
|
2009-07-20 11:22:49 +02:00
|
|
|
|
|
|
|
function getStartStep() {
|
2013-05-25 09:04:59 +02:00
|
|
|
return self::$start_step;
|
2009-07-20 11:22:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-07-02 10:34:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @package multiform
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2009-07-20 11:22:49 +02:00
|
|
|
class MultiFormTest_StepOne extends MultiFormStep implements TestOnly {
|
|
|
|
|
2010-05-11 05:35:40 +02:00
|
|
|
public static $next_steps = 'MultiFormTest_StepTwo';
|
2009-07-20 11:22:49 +02:00
|
|
|
|
|
|
|
function getFields() {
|
2013-01-03 10:05:21 +01:00
|
|
|
$class = (class_exists('FieldList')) ? 'FieldList' : 'FieldSet';
|
|
|
|
return new $class(
|
2009-07-20 11:22:49 +02:00
|
|
|
new TextField('FirstName', 'First name'),
|
|
|
|
new TextField('Surname', 'Surname'),
|
|
|
|
new EmailField('Email', 'Email address')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2014-06-17 06:55:07 +02:00
|
|
|
|
2014-07-02 10:34:44 +02:00
|
|
|
/**
|
|
|
|
* @package multiform
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2009-07-20 11:22:49 +02:00
|
|
|
class MultiFormTest_StepTwo extends MultiFormStep implements TestOnly {
|
|
|
|
|
2010-05-11 05:35:40 +02:00
|
|
|
public static $next_steps = 'MultiFormTest_StepThree';
|
2009-07-20 11:22:49 +02:00
|
|
|
|
|
|
|
function getFields() {
|
2013-01-03 10:05:21 +01:00
|
|
|
$class = (class_exists('FieldList')) ? 'FieldList' : 'FieldSet';
|
|
|
|
return new $class(
|
2009-07-20 11:22:49 +02:00
|
|
|
new TextareaField('Comments', 'Tell us a bit about yourself...')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2014-06-17 06:55:07 +02:00
|
|
|
|
2014-07-02 10:34:44 +02:00
|
|
|
/**
|
|
|
|
* @package multiform
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2009-07-20 11:22:49 +02:00
|
|
|
class MultiFormTest_StepThree extends MultiFormStep implements TestOnly {
|
|
|
|
|
2010-05-11 05:37:51 +02:00
|
|
|
public static $is_final_step = true;
|
2009-07-20 11:22:49 +02:00
|
|
|
|
|
|
|
function getFields() {
|
2013-01-03 10:05:21 +01:00
|
|
|
$class = (class_exists('FieldList')) ? 'FieldList' : 'FieldSet';
|
|
|
|
return new $class(
|
2009-07-20 11:22:49 +02:00
|
|
|
new TextField('Test', 'Anything else you\'d like to tell us?')
|
|
|
|
);
|
2008-05-14 13:09:47 +02:00
|
|
|
}
|
|
|
|
|
2009-08-05 12:12:13 +02:00
|
|
|
}
|