2012-03-06 22:48:09 +01:00
|
|
|
<?php
|
2012-01-09 23:10:03 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the API for creating GridField_URLHandler compeonnts
|
|
|
|
*/
|
|
|
|
class GridField_URLHandlerTest extends FunctionalTest {
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testFormSubmission() {
|
2012-01-09 23:10:03 +01:00
|
|
|
$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());
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testNestedRequestHandlerFormSubmission() {
|
2012-01-09 23:10:03 +01:00
|
|
|
$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());
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testURL() {
|
2012-01-09 23:10:03 +01:00
|
|
|
$result = $this->get("GridField_URLHandlerTest_Controller/Form/field/Grid/testpage");
|
|
|
|
$this->assertEquals("Test page for component", $result->getBody());
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testNestedRequestHandlerURL() {
|
2012-01-09 23:10:03 +01:00
|
|
|
$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 {
|
2013-06-20 11:40:55 +02:00
|
|
|
|
|
|
|
private static $allowed_actions = array('Form');
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Link() {
|
2012-01-09 23:10:03 +01:00
|
|
|
return get_class($this) ."/";
|
|
|
|
}
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Form() {
|
2012-01-09 23:10:03 +01:00
|
|
|
$gridConfig = GridFieldConfig::create();
|
|
|
|
$gridConfig->addComponent(new GridField_URLHandlerTest_Component());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-01-09 23:10:03 +01:00
|
|
|
$gridData = new ArrayList();
|
|
|
|
$gridField = new GridField('Grid', 'My grid', $gridData, $gridConfig);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-01-09 23:10:03 +01:00
|
|
|
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 {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-20 11:40:55 +02:00
|
|
|
private static $allowed_actions = array('Form', 'showform', 'testpage', 'handleItem');
|
|
|
|
|
2012-01-09 23:10:03 +01:00
|
|
|
protected $gridField;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getURLHandlers($gridField) {
|
2012-01-09 23:10:03 +01:00
|
|
|
return array(
|
|
|
|
'showform' => 'showform',
|
|
|
|
'testpage' => 'testpage',
|
|
|
|
'Form' => 'Form',
|
|
|
|
'item/$ID' => 'handleItem',
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function handleItem($gridField, $request) {
|
2012-01-09 23:10:03 +01:00
|
|
|
$id = $request->param("ID");
|
|
|
|
return new GridField_URLHandlerTest_Component_ItemRequest(
|
|
|
|
$gridField, $id,
|
|
|
|
Controller::join_links($gridField->Link(), 'item/' . $id));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Link() {
|
2012-01-09 23:10:03 +01:00
|
|
|
return $this->gridField->Link();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function showform($gridField, $request) {
|
2012-01-09 23:10:03 +01:00
|
|
|
return "<head>" . SSViewer::get_base_tag("") . "</head>" . $this->Form($gridField, $request)->forTemplate();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Form($gridField, $request) {
|
2012-01-09 23:10:03 +01:00
|
|
|
$this->gridField = $gridField;
|
|
|
|
return new Form($this, 'Form', new FieldList(
|
|
|
|
new TextField("Test")
|
|
|
|
), new FieldList(
|
|
|
|
new FormAction('doAction', 'Go')
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function doAction($data, $form) {
|
2012-01-09 23:10:03 +01:00
|
|
|
return "Submitted " . $data['Test'] . " to component";
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testpage($gridField, $request) {
|
2012-01-09 23:10:03 +01:00
|
|
|
return "Test page for component";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class GridField_URLHandlerTest_Component_ItemRequest extends RequestHandler {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-06-20 11:40:55 +02:00
|
|
|
private static $allowed_actions = array('Form', 'showform', 'testpage');
|
|
|
|
|
2012-01-09 23:10:03 +01:00
|
|
|
protected $gridField;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-01-09 23:10:03 +01:00
|
|
|
protected $link;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-01-09 23:10:03 +01:00
|
|
|
protected $id;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct($gridField, $id, $link) {
|
2012-01-09 23:10:03 +01:00
|
|
|
$this->gridField = $gridField;
|
|
|
|
$this->id = $id;
|
|
|
|
$this->link = $link;
|
|
|
|
parent::__construct();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Link() {
|
2012-01-09 23:10:03 +01:00
|
|
|
return $this->link;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function showform() {
|
2012-01-09 23:10:03 +01:00
|
|
|
return "<head>" . SSViewer::get_base_tag("") . "</head>" . $this->Form()->forTemplate();
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Form() {
|
2012-01-09 23:10:03 +01:00
|
|
|
return new Form($this, 'Form', new FieldList(
|
|
|
|
new TextField("Test")
|
|
|
|
), new FieldList(
|
|
|
|
new FormAction('doAction', 'Go')
|
|
|
|
));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function doAction($data, $form) {
|
2012-01-09 23:10:03 +01:00
|
|
|
return "Submitted " . $data['Test'] . " to item #" . $this->id;
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testpage() {
|
2012-01-09 23:10:03 +01:00
|
|
|
return "Test page for item #" . $this->id;
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|