GridField_URLHandlerTest.php
3.88 KB
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
142
143
144
<?php
/**
* Test the API for creating GridField_URLHandler compeonnts
*/
class GridField_URLHandlerTest extends FunctionalTest {
public function testFormSubmission() {
$result = $this->get("GridField_URLHandlerTest_Controller/Form/field/Grid/showform");
$formResult = $this->submitForm('Form_Form', 'action_doAction', array('Test' => 'foo bar') );
$this->assertEquals("Submitted foo bar to component", $formResult->getBody());
}
public function testNestedRequestHandlerFormSubmission() {
$result = $this->get("GridField_URLHandlerTest_Controller/Form/field/Grid/item/3/showform");
$formResult = $this->submitForm('Form_Form', 'action_doAction', array('Test' => 'foo bar') );
$this->assertEquals("Submitted foo bar to item #3", $formResult->getBody());
}
public function testURL() {
$result = $this->get("GridField_URLHandlerTest_Controller/Form/field/Grid/testpage");
$this->assertEquals("Test page for component", $result->getBody());
}
public function testNestedRequestHandlerURL() {
$result = $this->get("GridField_URLHandlerTest_Controller/Form/field/Grid/item/5/testpage");
$this->assertEquals("Test page for item #5", $result->getBody());
}
}
class GridField_URLHandlerTest_Controller extends Controller implements TestOnly {
private static $allowed_actions = array('Form');
public function Link() {
return get_class($this) ."/";
}
public function Form() {
$gridConfig = GridFieldConfig::create();
$gridConfig->addComponent(new GridField_URLHandlerTest_Component());
$gridData = new ArrayList();
$gridField = new GridField('Grid', 'My grid', $gridData, $gridConfig);
return new Form($this, 'Form', new FieldList(
$gridField
), new FieldList());
}
}
/**
* Test URLHandler with a nested request handler
*/
class GridField_URLHandlerTest_Component extends RequestHandler implements GridField_URLHandler {
private static $allowed_actions = array('Form', 'showform', 'testpage', 'handleItem');
protected $gridField;
public function getURLHandlers($gridField) {
return array(
'showform' => 'showform',
'testpage' => 'testpage',
'Form' => 'Form',
'item/$ID' => 'handleItem',
);
}
public function handleItem($gridField, $request) {
$id = $request->param("ID");
return new GridField_URLHandlerTest_Component_ItemRequest(
$gridField, $id,
Controller::join_links($gridField->Link(), 'item/' . $id));
}
public function Link() {
return $this->gridField->Link();
}
public function showform($gridField, $request) {
return "<head>" . SSViewer::get_base_tag("") . "</head>" . $this->Form($gridField, $request)->forTemplate();
}
public function Form($gridField, $request) {
$this->gridField = $gridField;
return new Form($this, 'Form', new FieldList(
new TextField("Test")
), new FieldList(
new FormAction('doAction', 'Go')
));
}
public function doAction($data, $form) {
return "Submitted " . $data['Test'] . " to component";
}
public function testpage($gridField, $request) {
return "Test page for component";
}
}
class GridField_URLHandlerTest_Component_ItemRequest extends RequestHandler {
private static $allowed_actions = array('Form', 'showform', 'testpage');
protected $gridField;
protected $link;
protected $id;
public function __construct($gridField, $id, $link) {
$this->gridField = $gridField;
$this->id = $id;
$this->link = $link;
parent::__construct();
}
public function Link() {
return $this->link;
}
public function showform() {
return "<head>" . SSViewer::get_base_tag("") . "</head>" . $this->Form()->forTemplate();
}
public function Form() {
return new Form($this, 'Form', new FieldList(
new TextField("Test")
), new FieldList(
new FormAction('doAction', 'Go')
));
}
public function doAction($data, $form) {
return "Submitted " . $data['Test'] . " to item #" . $this->id;
}
public function testpage() {
return "Test page for item #" . $this->id;
}
}