silverstripe-multiform/tests/MultiFormTest.php

142 lines
4.6 KiB
PHP
Raw Normal View History

<?php
namespace SilverStripe\MultiForm\Tests;
2017-09-08 03:57:33 +02:00
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\Session;
use SilverStripe\Core\Config\Config;
2017-09-08 03:57:33 +02:00
use SilverStripe\Core\Injector\Injector;
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;
use SilverStripe\MultiForm\Tests\Stubs\MultiFormTestController;
use SilverStripe\MultiForm\Tests\Stubs\MultiFormTestForm;
use SilverStripe\MultiForm\Tests\Stubs\MultiFormTestStepOne;
use SilverStripe\MultiForm\Tests\Stubs\MultiFormTestStepTwo;
/**
* 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
*
* - 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
* 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
*
*/
class MultiFormTest extends FunctionalTest
{
2017-09-08 04:24:28 +02:00
protected static $fixture_file = 'MultiFormTest.yml';
2017-09-08 03:57:33 +02:00
/**
* @var MultiFormTestController
*/
protected $controller;
/**
* @var MultiFormTestForm
*/
protected $form;
2023-09-06 10:35:07 +02:00
protected function setUp(): void
{
parent::setUp();
$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;
}
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)
);
$this->assertEquals(MultiFormTestStepOne::class, $this->form->getStartStep());
}
public function testSessionGeneration()
{
2017-09-08 03:57:33 +02:00
$this->assertTrue($this->form->getMultiFormSession()->ID > 0);
}
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.
$userId = $this->logInWithPermission('ADMIN');
2017-09-08 03:57:33 +02:00
$session = $this->form->getMultiFormSession();
$session->write();
$this->assertEquals($userId, $session->SubmitterID);
}
public function testSecondStep()
{
$this->assertEquals(MultiFormTestStepTwo::class, $this->form->getCurrentStep()->getNextStep());
}
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));
}
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();
$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());
}
public function testCustomGetVar()
{
2017-09-08 03:57:33 +02:00
Config::modify()->set(MultiForm::class, 'get_var', 'SuperSessionID');
$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(
'SuperSessionID',
$form->FormAction(),
"Form action doesn't contain correct session ID parameter"
);
2023-09-06 10:35:07 +02:00
$this->assertStringContainsString(
'SuperSessionID',
$form->getCurrentStep()->Link(),
"Form step doesn't contain correct session ID parameter"
);
}
}