1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AntonyThorpe\Knockout\Tests; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Dev\FunctionalTest; |
6
|
|
|
use SilverStripe\Core\Config\Config; |
7
|
|
|
use SilverStripe\Control\Controller; |
8
|
|
|
use SilverStripe\Control\HTTPRequest; |
9
|
|
|
use SilverStripe\Dev\TestOnly; |
10
|
|
|
use SilverStripe\Forms\FieldList; |
11
|
|
|
use SilverStripe\View\SSViewer; |
12
|
|
|
use SilverStripe\Forms\Form; |
13
|
|
|
use AntonyThorpe\Knockout\KnockoutForm; |
14
|
|
|
use AntonyThorpe\Knockout\KnockoutTextField; |
15
|
|
|
use AntonyThorpe\Knockout\KnockoutDropdownField; |
16
|
|
|
use AntonyThorpe\Knockout\KnockoutNumericField; |
17
|
|
|
use AntonyThorpe\Knockout\KnockoutEmailField; |
18
|
|
|
use AntonyThorpe\Knockout\KnockoutTextareaField; |
19
|
|
|
use AntonyThorpe\Knockout\KnockoutOptionsetField; |
20
|
|
|
use AntonyThorpe\Knockout\KnockoutConfirmedPasswordField; |
21
|
|
|
use AntonyThorpe\Knockout\KnockoutFormAction; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* KnockoutFormTest |
25
|
|
|
* |
26
|
|
|
* Controller tests |
27
|
|
|
*/ |
28
|
|
|
class KnockoutFormTest extends FunctionalTest |
29
|
|
|
{ |
30
|
|
|
protected static $disable_theme = true; |
31
|
|
|
|
32
|
|
|
protected static $extra_controllers = [ |
33
|
|
|
KnockoutFormTest_Controller::class |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
public function setUp() |
37
|
|
|
{ |
38
|
|
|
parent::setUp(); |
39
|
|
|
Config::inst()->update('SSViewer', 'source_file_comments', true); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testKnockoutForm() |
43
|
|
|
{ |
44
|
|
|
$page = $this->get('KnockoutFormTest_Controller'); |
45
|
|
|
$this->assertEquals(200, $page->getStatusCode(), "a page should load"); |
46
|
|
|
$body = $page->getBody(); |
47
|
|
|
|
48
|
|
|
$this->assertContains( |
49
|
|
|
'data-bind="submit: addToCart2"', |
50
|
|
|
$body, |
51
|
|
|
'form element has submit binding to javascript function' |
52
|
|
|
); |
53
|
|
|
$this->assertContains( |
54
|
|
|
'<input data-bind="textInput: spaceship2, hasFocus: true"', |
55
|
|
|
$body, |
56
|
|
|
'Databind attribute in input element' |
57
|
|
|
); |
58
|
|
|
$this->assertContains( |
59
|
|
|
"setKnockout:{value:'Enterprise\'s Voyage'}", |
60
|
|
|
$body, |
61
|
|
|
'Comma escaped in HTML for javascript' |
62
|
|
|
); |
63
|
|
|
$this->assertContains( |
64
|
|
|
'<select data-bind="value: menu"', |
65
|
|
|
$body, |
66
|
|
|
'Databind attribute applied to select element' |
67
|
|
|
); |
68
|
|
|
$this->assertContains( |
69
|
|
|
'<input data-bind="textInput: seatNumber, setKnockout:{value:4}"', |
70
|
|
|
$body, |
71
|
|
|
'KnockoutNumericField works' |
72
|
|
|
); |
73
|
|
|
$this->assertContains( |
74
|
|
|
'<input data-bind="enable: canSaveInterGalacticAction, css:{ \'FormAction_Disabled\': !canSaveInterGalacticAction() }" type="submit"', |
75
|
|
|
$body, |
76
|
|
|
'Databind attribute in submit button' |
77
|
|
|
); |
78
|
|
|
$this->assertContains( |
79
|
|
|
'<input data-bind="textInput: email, setKnockout:{value:\'[email protected]\'}"', |
80
|
|
|
$body, |
81
|
|
|
'Databind attribute applied to input element for email field' |
82
|
|
|
); |
83
|
|
|
$this->assertContains( |
84
|
|
|
'class="email text"', |
85
|
|
|
$body, |
86
|
|
|
'KnockoutEmailField has a class of "email text"' |
87
|
|
|
); |
88
|
|
|
$this->assertContains( |
89
|
|
|
'<textarea data-bind="textInput: comments"', |
90
|
|
|
$body, |
91
|
|
|
'Databind attribute applied to the textareafield' |
92
|
|
|
); |
93
|
|
|
$this->assertContains( |
94
|
|
|
'class="knockouttextarea textarea"', |
95
|
|
|
$body, |
96
|
|
|
'KnockoutTextareaField has a class of "textarea text"' |
97
|
|
|
); |
98
|
|
|
$this->assertContains( |
99
|
|
|
'data-bind="checked: accessories, setKnockout:{value:\'Zero Gravity Pillow\'}, blah: someOtherFunction"', |
100
|
|
|
$body, |
101
|
|
|
'Databind attribute applied to the radio buttons' |
102
|
|
|
); |
103
|
|
|
$this->assertContains( |
104
|
|
|
'class="radio"', |
105
|
|
|
$body, |
106
|
|
|
'KnockoutOptionsetField has a class of "radio"' |
107
|
|
|
); |
108
|
|
|
$this->assertContains( |
109
|
|
|
'data-bind="enable: canSaveInterGalacticAction', |
110
|
|
|
$body, |
111
|
|
|
'KnockoutFormAction has an obserable of "canSaveInterGalacticAction"' |
112
|
|
|
); |
113
|
|
|
$this->assertContains( |
114
|
|
|
'data-bind="textInput: password', |
115
|
|
|
$body, |
116
|
|
|
'KnockoutConfirmedPasswordField has a child with an obserable of "password"' |
117
|
|
|
); |
118
|
|
|
$this->assertContains( |
119
|
|
|
'data-bind="textInput: confirmedPassword', |
120
|
|
|
$body, |
121
|
|
|
'KnockoutConfirmedPasswordField has a child with an obserable of "confirmedPassword"' |
122
|
|
|
); |
123
|
|
|
// add additional tests here after adding to the form below |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
class KnockoutFormTest_Controller extends Controller implements TestOnly |
129
|
|
|
{ |
130
|
|
|
public function __construct() |
131
|
|
|
{ |
132
|
|
|
parent::__construct(); |
133
|
|
|
if (Controller::has_curr()) { |
134
|
|
|
$this->setRequest(Controller::curr()->getRequest()); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
private static $allowed_actions = array('Form'); |
|
|
|
|
139
|
|
|
|
140
|
|
|
private static $url_handlers = array( |
|
|
|
|
141
|
|
|
'$Action//$ID/$OtherID' => "handleAction", |
142
|
|
|
); |
143
|
|
|
|
144
|
|
|
protected $template = 'BlankPage'; |
145
|
|
|
|
146
|
|
|
public function Link($action = null) |
147
|
|
|
{ |
148
|
|
|
return Controller::join_links( |
149
|
|
|
'KnockoutFormTest_Controller', |
150
|
|
|
$this->getRequest()->latestParam('Action'), |
151
|
|
|
$this->getRequest()->latestParam('ID'), |
152
|
|
|
$action |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function Form() |
157
|
|
|
{ |
158
|
|
|
$form = KnockoutForm::create( |
159
|
|
|
$this, |
160
|
|
|
'Form', |
161
|
|
|
FieldList::create( |
162
|
|
|
KnockoutTextField::create('Spaceship', 'Spaceship') |
163
|
|
|
->setObservable('spaceship2') |
164
|
|
|
->setHasFocus(true), |
165
|
|
|
KnockoutTextField::create('FieldWithComma', 'FieldWithComma') |
166
|
|
|
->setObservable('fieldwithcomma') |
167
|
|
|
->setValue("Enterprise's Voyage"), |
168
|
|
|
KnockoutDropdownField::create( |
169
|
|
|
'Menu', |
170
|
|
|
'Space Menu', |
171
|
|
|
array('1'=>'Light Speed Salad','2'=>'Comet Custard') |
172
|
|
|
)->setObservable('menu'), |
173
|
|
|
KnockoutNumericField::create('SeatNumber', 'Seat Number', 4) |
174
|
|
|
->setObservable('seatNumber'), |
175
|
|
|
KnockoutEmailField::create('Email', 'Email') |
176
|
|
|
->setObservable('email') |
177
|
|
|
->setValue('[email protected]'), |
178
|
|
|
KnockoutTextareaField::create('Comments', 'Comments') |
179
|
|
|
->setObservable('comments'), |
180
|
|
|
KnockoutOptionsetField::create( |
181
|
|
|
'Accessories', |
182
|
|
|
'Accessories', |
183
|
|
|
array( |
184
|
|
|
'Flying High DVD' => 'Flying High DVD', |
185
|
|
|
'Zero Gravity Pillow' => 'Zero Gravity Pillow', |
186
|
|
|
'Rocket Replica' => 'Rocket Replica' |
187
|
|
|
), |
188
|
|
|
'Zero Gravity Pillow' |
189
|
|
|
)->setObservable('accessories') |
190
|
|
|
->setOtherBindings("blah: someOtherFunction"), |
191
|
|
|
KnockoutConfirmedPasswordField::create('Password', 'Password') |
192
|
|
|
// add any new knockout fields here and assert above |
193
|
|
|
), |
194
|
|
|
FieldList::create( |
195
|
|
|
KnockoutFormAction::create('doSubmit', 'Submit') |
196
|
|
|
->setObservable('canSaveInterGalacticAction') |
197
|
|
|
) |
198
|
|
|
); |
199
|
|
|
$form->setSubmit('addToCart2'); |
200
|
|
|
|
201
|
|
|
return $form; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function doSubmit($data, Form $form, HTTPRequest $request) |
|
|
|
|
205
|
|
|
{ |
206
|
|
|
$form->sessionMessage('Test save was successful', 'good'); |
207
|
|
|
return $this->redirectBack(); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
public function getViewer($action = null) |
211
|
|
|
{ |
212
|
|
|
return new SSViewer('BlankPage'); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|