2008-05-14 13:09:47 +02:00
|
|
|
<?php
|
2017-09-07 01:32:55 +02:00
|
|
|
|
|
|
|
namespace SilverStripe\MultiForm\Tests;
|
|
|
|
|
2017-09-08 03:57:33 +02:00
|
|
|
use SilverStripe\Control\HTTPRequest;
|
|
|
|
use SilverStripe\Control\Session;
|
2017-09-07 01:32:55 +02:00
|
|
|
use SilverStripe\Core\Config\Config;
|
2017-09-08 03:57:33 +02:00
|
|
|
use SilverStripe\Core\Injector\Injector;
|
2017-09-07 01:32:55 +02:00
|
|
|
use SilverStripe\Dev\FunctionalTest;
|
2017-09-12 01:51:17 +02:00
|
|
|
use SilverStripe\MultiForm\Forms\MultiForm;
|
2017-09-08 03:57:33 +02:00
|
|
|
use SilverStripe\MultiForm\Models\MultiFormSession;
|
2017-09-08 05:59:53 +02:00
|
|
|
use SilverStripe\MultiForm\Tests\Stubs\MultiFormTestController;
|
|
|
|
use SilverStripe\MultiForm\Tests\Stubs\MultiFormTestForm;
|
|
|
|
use SilverStripe\MultiForm\Tests\Stubs\MultiFormTestStepOne;
|
|
|
|
use SilverStripe\MultiForm\Tests\Stubs\MultiFormTestStepTwo;
|
2017-09-07 01:32:55 +02:00
|
|
|
|
2008-05-14 13:09:47 +02:00
|
|
|
/**
|
|
|
|
* MultiFormTest
|
2009-07-20 11:22:49 +02:00
|
|
|
* For testing purposes, we have some test classes:
|
2015-11-02 01:38:50 +01:00
|
|
|
*
|
2017-09-07 01:32:55 +02:00
|
|
|
* - MultiFormTestController (simulation of a real Controller class)
|
|
|
|
* - MultiFormTestForm (subclass of MultiForm)
|
|
|
|
* - MultiFormTestStepOne (subclass of MultiFormStep)
|
|
|
|
* - MultiFormTestStepTwo (subclass of MultiFormStep)
|
|
|
|
* - MultiFormTestStepThree (subclass of MultiFormStep)
|
2009-07-20 11:22:49 +02:00
|
|
|
*
|
|
|
|
* The above classes are used to simulate real-world behaviour
|
2017-09-07 01:32:55 +02:00
|
|
|
* of the multiform module - for example, MultiFormTestController
|
2009-07-20 11:22:49 +02:00
|
|
|
* is a simulation of a page where MultiFormTest_Form is a simple
|
|
|
|
* multi-step contact form it belongs to.
|
2015-11-02 01:38:50 +01:00
|
|
|
*
|
2008-05-14 13:09:47 +02:00
|
|
|
*/
|
2017-09-06 22:47:35 +02:00
|
|
|
class MultiFormTest extends FunctionalTest
|
|
|
|
{
|
2017-09-08 04:24:28 +02:00
|
|
|
protected static $fixture_file = 'MultiFormTest.yml';
|
2017-09-06 22:47:35 +02:00
|
|
|
|
2017-09-08 03:57:33 +02:00
|
|
|
/**
|
|
|
|
* @var MultiFormTestController
|
|
|
|
*/
|
2017-09-06 22:47:35 +02:00
|
|
|
protected $controller;
|
|
|
|
|
2017-09-07 01:32:55 +02:00
|
|
|
/**
|
|
|
|
* @var MultiFormTestForm
|
|
|
|
*/
|
|
|
|
protected $form;
|
|
|
|
|
2023-09-06 10:35:07 +02:00
|
|
|
protected function setUp(): void
|
2017-09-06 22:47:35 +02:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2017-09-07 01:32:55 +02:00
|
|
|
$this->controller = new MultiFormTestController();
|
2017-09-08 03:57:33 +02:00
|
|
|
$this->controller->setRequest(new HTTPRequest('GET', '/'));
|
|
|
|
$this->controller->getRequest()->setSession(new Session([]));
|
|
|
|
$this->controller->pushCurrent();
|
|
|
|
$form = $this->form = $this->controller->Form();
|
|
|
|
Injector::inst()->registerService($form, MultiForm::class);
|
|
|
|
$this->form = $form;
|
2017-09-06 22:47:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testInitialisingForm()
|
|
|
|
{
|
|
|
|
$this->assertTrue(is_numeric($this->form->getCurrentStep()->ID) && ($this->form->getCurrentStep()->ID > 0));
|
2017-09-08 03:57:33 +02:00
|
|
|
$this->assertTrue(
|
|
|
|
is_numeric($this->form->getMultiFormSession()->ID)
|
|
|
|
&& ($this->form->getMultiFormSession()->ID > 0)
|
|
|
|
);
|
2017-09-07 01:32:55 +02:00
|
|
|
$this->assertEquals(MultiFormTestStepOne::class, $this->form->getStartStep());
|
2017-09-06 22:47:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSessionGeneration()
|
|
|
|
{
|
2017-09-08 03:57:33 +02:00
|
|
|
$this->assertTrue($this->form->getMultiFormSession()->ID > 0);
|
2017-09-06 22:47:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testMemberLogging()
|
|
|
|
{
|
|
|
|
// Grab any user to fake being logged in as, and ensure that after a session is written it has
|
|
|
|
// that user as the submitter.
|
|
|
|
|
2017-09-07 01:32:55 +02:00
|
|
|
$userId = $this->logInWithPermission('ADMIN');
|
|
|
|
|
2017-09-08 03:57:33 +02:00
|
|
|
$session = $this->form->getMultiFormSession();
|
2017-09-06 22:47:35 +02:00
|
|
|
$session->write();
|
|
|
|
|
|
|
|
$this->assertEquals($userId, $session->SubmitterID);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSecondStep()
|
|
|
|
{
|
2017-09-07 01:32:55 +02:00
|
|
|
$this->assertEquals(MultiFormTestStepTwo::class, $this->form->getCurrentStep()->getNextStep());
|
2017-09-06 22:47:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testParentForm()
|
|
|
|
{
|
|
|
|
$currentStep = $this->form->getCurrentStep();
|
2017-09-11 05:25:16 +02:00
|
|
|
$this->assertEquals(get_class($currentStep->getForm()), get_class($this->form));
|
2017-09-06 22:47:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testTotalStepCount()
|
|
|
|
{
|
|
|
|
$this->assertEquals(3, $this->form->getAllStepsLinear()->Count());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCompletedSession()
|
|
|
|
{
|
2017-09-08 03:57:33 +02:00
|
|
|
$this->form->setCurrentSessionHash($this->form->getMultiFormSession()->Hash);
|
|
|
|
$this->assertInstanceOf(MultiFormSession::class, $this->form->getCurrentSession());
|
|
|
|
$this->form->getMultiFormSession()->markCompleted();
|
2017-09-06 22:47:35 +02:00
|
|
|
$this->assertNull($this->form->getCurrentSession());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIncorrectSessionIdentifier()
|
|
|
|
{
|
|
|
|
$this->form->setCurrentSessionHash('sdfsdf3432325325sfsdfdf'); // made up!
|
|
|
|
|
|
|
|
// A new session is generated, even though we made up the identifier
|
2017-09-08 03:57:33 +02:00
|
|
|
$this->assertInstanceOf(MultiFormSession::class, $this->form->getMultiFormSession());
|
2017-09-06 22:47:35 +02:00
|
|
|
}
|
|
|
|
|
2017-09-07 01:32:55 +02:00
|
|
|
public function testCustomGetVar()
|
2017-09-06 22:47:35 +02:00
|
|
|
{
|
2017-09-08 03:57:33 +02:00
|
|
|
Config::modify()->set(MultiForm::class, 'get_var', 'SuperSessionID');
|
2017-09-06 22:47:35 +02:00
|
|
|
|
|
|
|
$form = $this->controller->Form();
|
2023-09-06 13:13:29 +02:00
|
|
|
$this->assertContains(
|
2017-09-08 04:24:28 +02:00
|
|
|
'SuperSessionID',
|
|
|
|
$form->config()->get('ignored_fields'),
|
|
|
|
'GET var wasn\'t added to ignored fields'
|
|
|
|
);
|
2023-09-06 10:35:07 +02:00
|
|
|
$this->assertStringContainsString(
|
2017-09-07 01:32:55 +02:00
|
|
|
'SuperSessionID',
|
|
|
|
$form->FormAction(),
|
|
|
|
"Form action doesn't contain correct session ID parameter"
|
2017-09-06 22:47:35 +02:00
|
|
|
);
|
2023-09-06 10:35:07 +02:00
|
|
|
$this->assertStringContainsString(
|
2017-09-07 01:32:55 +02:00
|
|
|
'SuperSessionID',
|
|
|
|
$form->getCurrentStep()->Link(),
|
|
|
|
"Form step doesn't contain correct session ID parameter"
|
2017-09-06 22:47:35 +02:00
|
|
|
);
|
|
|
|
}
|
2009-08-05 12:12:13 +02:00
|
|
|
}
|