/
MultiFormTest.php
141 lines (120 loc) · 4.63 KB
/
MultiFormTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
namespace SilverStripe\MultiForm\Tests;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\Session;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\FunctionalTest;
use SilverStripe\MultiForm\Forms\MultiForm;
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
* For testing purposes, we have some test classes:
*
* - MultiFormTestController (simulation of a real Controller class)
* - MultiFormTestForm (subclass of MultiForm)
* - MultiFormTestStepOne (subclass of MultiFormStep)
* - MultiFormTestStepTwo (subclass of MultiFormStep)
* - MultiFormTestStepThree (subclass of MultiFormStep)
*
* The above classes are used to simulate real-world behaviour
* of the multiform module - for example, MultiFormTestController
* is a simulation of a page where MultiFormTest_Form is a simple
* multi-step contact form it belongs to.
*
*/
class MultiFormTest extends FunctionalTest
{
protected static $fixture_file = 'MultiFormTest.yml';
/**
* @var MultiFormTestController
*/
protected $controller;
/**
* @var MultiFormTestForm
*/
protected $form;
protected function setUp(): void
{
parent::setUp();
$this->controller = new MultiFormTestController();
$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));
$this->assertTrue(
is_numeric($this->form->getMultiFormSession()->ID)
&& ($this->form->getMultiFormSession()->ID > 0)
);
$this->assertEquals(MultiFormTestStepOne::class, $this->form->getStartStep());
}
public function testSessionGeneration()
{
$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');
$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();
$this->assertEquals(get_class($currentStep->getForm()), get_class($this->form));
}
public function testTotalStepCount()
{
$this->assertEquals(3, $this->form->getAllStepsLinear()->Count());
}
public function testCompletedSession()
{
$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
$this->assertInstanceOf(MultiFormSession::class, $this->form->getMultiFormSession());
}
public function testCustomGetVar()
{
Config::modify()->set(MultiForm::class, 'get_var', 'SuperSessionID');
$form = $this->controller->Form();
$this->assertContains(
'SuperSessionID',
$form->config()->get('ignored_fields'),
'GET var wasn\'t added to ignored fields'
);
$this->assertStringContainsString(
'SuperSessionID',
$form->FormAction(),
"Form action doesn't contain correct session ID parameter"
);
$this->assertStringContainsString(
'SuperSessionID',
$form->getCurrentStep()->Link(),
"Form step doesn't contain correct session ID parameter"
);
}
}