1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* MultiFormTest |
4
|
|
|
* For testing purposes, we have some test classes: |
5
|
|
|
* |
6
|
|
|
* - MultiFormTest_Controller (simulation of a real Controller class) |
7
|
|
|
* - MultiFormTest_Form (subclass of MultiForm) |
8
|
|
|
* - MultiFormTest_StepOne (subclass of MultiFormStep) |
9
|
|
|
* - MultiFormTest_StepTwo (subclass of MultiFormStep) |
10
|
|
|
* - MultiFormTest_StepThree (subclass of MultiFormStep) |
11
|
|
|
* |
12
|
|
|
* The above classes are used to simulate real-world behaviour |
13
|
|
|
* of the multiform module - for example, MultiFormTest_Controller |
14
|
|
|
* is a simulation of a page where MultiFormTest_Form is a simple |
15
|
|
|
* multi-step contact form it belongs to. |
16
|
|
|
* |
17
|
|
|
* @package multiform |
18
|
|
|
* @subpackage tests |
19
|
|
|
*/ |
20
|
|
|
class MultiFormTest extends FunctionalTest { |
|
|
|
|
21
|
|
|
|
22
|
|
|
public static $fixture_file = 'multiform/tests/MultiFormTest.yml'; |
23
|
|
|
|
24
|
|
|
protected $controller; |
25
|
|
|
|
26
|
|
|
public function setUp() { |
27
|
|
|
parent::setUp(); |
28
|
|
|
|
29
|
|
|
$this->controller = new MultiFormTest_Controller(); |
30
|
|
|
$this->form = $this->controller->Form(); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testInitialisingForm() { |
34
|
|
|
$this->assertTrue(is_numeric($this->form->getCurrentStep()->ID) && ($this->form->getCurrentStep()->ID > 0)); |
|
|
|
|
35
|
|
|
$this->assertTrue(is_numeric($this->form->getSession()->ID) && ($this->form->getSession()->ID > 0)); |
|
|
|
|
36
|
|
|
$this->assertEquals('MultiFormTest_StepOne', $this->form->getStartStep()); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testSessionGeneration() { |
40
|
|
|
$this->assertTrue($this->form->session->ID > 0); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testMemberLogging() { |
44
|
|
|
// Grab any user to fake being logged in as, and ensure that after a session is written it has |
45
|
|
|
// that user as the submitter. |
46
|
|
|
$userId = Member::get_one("Member")->ID; |
47
|
|
|
$this->session()->inst_set('loggedInAs', $userId); |
48
|
|
|
|
49
|
|
|
$session = $this->form->session; |
50
|
|
|
$session->write(); |
51
|
|
|
|
52
|
|
|
$this->assertEquals($userId, $session->SubmitterID); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testSecondStep() { |
56
|
|
|
$this->assertEquals('MultiFormTest_StepTwo', $this->form->getCurrentStep()->getNextStep()); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testParentForm() { |
60
|
|
|
$currentStep = $this->form->getCurrentStep(); |
61
|
|
|
$this->assertEquals($currentStep->getForm()->class, $this->form->class); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testTotalStepCount() { |
65
|
|
|
$this->assertEquals(3, $this->form->getAllStepsLinear()->Count()); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testCompletedSession() { |
69
|
|
|
$this->form->setCurrentSessionHash($this->form->session->Hash); |
70
|
|
|
$this->assertInstanceOf('MultiFormSession', $this->form->getCurrentSession()); |
|
|
|
|
71
|
|
|
$this->form->session->markCompleted(); |
72
|
|
|
$this->assertNull($this->form->getCurrentSession()); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function testIncorrectSessionIdentifier() { |
76
|
|
|
$this->form->setCurrentSessionHash('sdfsdf3432325325sfsdfdf'); // made up! |
77
|
|
|
|
78
|
|
|
// A new session is generated, even though we made up the identifier |
79
|
|
|
$this->assertInstanceOf('MultiFormSession', $this->form->session); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
function testCustomGetVar() { |
|
|
|
|
83
|
|
|
Config::nest(); |
84
|
|
|
Config::inst()->update('MultiForm', 'get_var', 'SuperSessionID'); |
85
|
|
|
|
86
|
|
|
$form = $this->controller->Form(); |
87
|
|
|
$this->assertContains('SuperSessionID', $form::$ignored_fields, "GET var wasn't added to ignored fields"); |
88
|
|
|
$this->assertContains('SuperSessionID', $form->FormAction(), "Form action doesn't contain correct session |
89
|
|
|
ID parameter"); |
90
|
|
|
$this->assertContains('SuperSessionID', $form->getCurrentStep()->Link(), "Form step doesn't contain correct |
91
|
|
|
session ID parameter"); |
92
|
|
|
|
93
|
|
|
Config::unnest(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @package multiform |
100
|
|
|
* @subpackage tests |
101
|
|
|
*/ |
102
|
|
|
class MultiFormTest_Controller extends Controller implements TestOnly { |
|
|
|
|
103
|
|
|
|
104
|
|
|
public function Link() { |
105
|
|
|
return 'MultiFormTest_Controller'; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function Form($request = null) { |
|
|
|
|
109
|
|
|
$form = new MultiFormTest_Form($this, 'Form'); |
|
|
|
|
110
|
|
|
$form->setHTMLID('MultiFormTest_Form'); |
111
|
|
|
return $form; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @package multiform |
117
|
|
|
* @subpackage tests |
118
|
|
|
*/ |
119
|
|
|
class MultiFormTest_Form extends MultiForm implements TestOnly { |
|
|
|
|
120
|
|
|
|
121
|
|
|
public static $start_step = 'MultiFormTest_StepOne'; |
122
|
|
|
|
123
|
|
|
public function getStartStep() { |
124
|
|
|
return self::$start_step; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @package multiform |
131
|
|
|
* @subpackage tests |
132
|
|
|
*/ |
133
|
|
|
class MultiFormTest_StepOne extends MultiFormStep implements TestOnly { |
|
|
|
|
134
|
|
|
|
135
|
|
|
public static $next_steps = 'MultiFormTest_StepTwo'; |
136
|
|
|
|
137
|
|
|
public function getFields() { |
138
|
|
|
$class = (class_exists('FieldList')) ? 'FieldList' : 'FieldSet'; |
139
|
|
|
return new $class( |
140
|
|
|
new TextField('FirstName', 'First name'), |
141
|
|
|
new TextField('Surname', 'Surname'), |
142
|
|
|
new EmailField('Email', 'Email address') |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @package multiform |
149
|
|
|
* @subpackage tests |
150
|
|
|
*/ |
151
|
|
View Code Duplication |
class MultiFormTest_StepTwo extends MultiFormStep implements TestOnly { |
|
|
|
|
152
|
|
|
|
153
|
|
|
public static $next_steps = 'MultiFormTest_StepThree'; |
154
|
|
|
|
155
|
|
|
public function getFields() { |
156
|
|
|
$class = (class_exists('FieldList')) ? 'FieldList' : 'FieldSet'; |
157
|
|
|
return new $class( |
158
|
|
|
new TextareaField('Comments', 'Tell us a bit about yourself...') |
159
|
|
|
); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @package multiform |
165
|
|
|
* @subpackage tests |
166
|
|
|
*/ |
167
|
|
View Code Duplication |
class MultiFormTest_StepThree extends MultiFormStep implements TestOnly { |
|
|
|
|
168
|
|
|
|
169
|
|
|
public static $is_final_step = true; |
170
|
|
|
|
171
|
|
|
public function getFields() { |
172
|
|
|
$class = (class_exists('FieldList')) ? 'FieldList' : 'FieldSet'; |
173
|
|
|
return new $class( |
174
|
|
|
new TextField('Test', 'Anything else you\'d like to tell us?') |
175
|
|
|
); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
} |
179
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.