2008-08-12 02:12:29 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class TableListFieldTest extends SapphireTest {
|
|
|
|
static $fixture_file = 'sapphire/tests/forms/TableListFieldTest.yml';
|
2010-01-13 00:16:14 +01:00
|
|
|
|
|
|
|
protected $extraDataObjects = array(
|
|
|
|
'TableListFieldTest_Obj',
|
|
|
|
'TableListFieldTest_CsvExport',
|
|
|
|
);
|
2008-08-12 02:12:29 +02:00
|
|
|
|
2008-08-13 03:39:46 +02:00
|
|
|
function testCanReferenceCustomMethodsAndFieldsOnObject() {
|
2008-08-12 02:12:29 +02:00
|
|
|
$table = new TableListField("Tester", "TableListFieldTest_Obj", array(
|
|
|
|
"A" => "Col A",
|
|
|
|
"B" => "Col B",
|
|
|
|
"C" => "Col C",
|
|
|
|
"D" => "Col D",
|
|
|
|
"E" => "Col E",
|
|
|
|
));
|
2008-10-08 04:00:12 +02:00
|
|
|
// A TableListField must be inside a form for its links to be generated
|
|
|
|
$form = new Form(new TableListFieldTest_TestController(), "TestForm", new FieldSet(
|
|
|
|
$table
|
|
|
|
), new FieldSet());
|
|
|
|
|
2008-08-12 02:12:29 +02:00
|
|
|
$result = $table->FieldHolder();
|
2008-08-13 03:39:46 +02:00
|
|
|
|
2008-08-12 02:12:29 +02:00
|
|
|
// Do a quick check to ensure that some of the D() and getE() values got through
|
|
|
|
$this->assertRegExp('/>\s*a2\s*</', $result);
|
|
|
|
$this->assertRegExp('/>\s*a2\/b2\/c2\s*</', $result);
|
|
|
|
$this->assertRegExp('/>\s*a2-e</', $result);
|
|
|
|
}
|
2008-08-13 03:39:46 +02:00
|
|
|
|
|
|
|
function testUnpaginatedSourceItemGeneration() {
|
2009-10-01 23:51:58 +02:00
|
|
|
$item1 = $this->objFromFixture('TableListFieldTest_Obj', 'one');
|
|
|
|
$item2 = $this->objFromFixture('TableListFieldTest_Obj', 'two');
|
|
|
|
$item3 = $this->objFromFixture('TableListFieldTest_Obj', 'three');
|
|
|
|
$item4 = $this->objFromFixture('TableListFieldTest_Obj', 'four');
|
|
|
|
$item5 = $this->objFromFixture('TableListFieldTest_Obj', 'five');
|
|
|
|
|
2008-08-13 03:39:46 +02:00
|
|
|
/* In this simple case, the source items should just list all the data objects specified */
|
|
|
|
$table = new TableListField("Tester", "TableListFieldTest_Obj", array(
|
|
|
|
"A" => "Col A",
|
|
|
|
"B" => "Col B",
|
|
|
|
"C" => "Col C",
|
|
|
|
"D" => "Col D",
|
|
|
|
"E" => "Col E",
|
|
|
|
));
|
2008-10-08 04:00:12 +02:00
|
|
|
// A TableListField must be inside a form for its links to be generated
|
|
|
|
$form = new Form(new TableListFieldTest_TestController(), "TestForm", new FieldSet(
|
|
|
|
$table
|
|
|
|
), new FieldSet());
|
|
|
|
|
2008-08-13 03:39:46 +02:00
|
|
|
$items = $table->sourceItems();
|
|
|
|
$this->assertNotNull($items);
|
|
|
|
|
2009-10-01 23:51:58 +02:00
|
|
|
$itemMap = $items->toDropdownMap("ID", "A") ;
|
|
|
|
$this->assertEquals(array(
|
|
|
|
$item1->ID => "a1",
|
|
|
|
$item2->ID => "a2",
|
|
|
|
$item3->ID => "a3",
|
|
|
|
$item4->ID => "a4",
|
|
|
|
$item5->ID => "a5"
|
|
|
|
), $itemMap);
|
2008-08-13 03:39:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function testFirstPageOfPaginatedSourceItemGeneration() {
|
2009-10-01 23:51:58 +02:00
|
|
|
$item1 = $this->objFromFixture('TableListFieldTest_Obj', 'one');
|
|
|
|
$item2 = $this->objFromFixture('TableListFieldTest_Obj', 'two');
|
|
|
|
$item3 = $this->objFromFixture('TableListFieldTest_Obj', 'three');
|
|
|
|
$item4 = $this->objFromFixture('TableListFieldTest_Obj', 'four');
|
|
|
|
$item5 = $this->objFromFixture('TableListFieldTest_Obj', 'five');
|
|
|
|
|
2008-08-13 03:39:46 +02:00
|
|
|
/* With pagination enabled, only the first page of items should be shown */
|
|
|
|
$table = new TableListField("Tester", "TableListFieldTest_Obj", array(
|
|
|
|
"A" => "Col A",
|
|
|
|
"B" => "Col B",
|
|
|
|
"C" => "Col C",
|
|
|
|
"D" => "Col D",
|
|
|
|
"E" => "Col E",
|
|
|
|
));
|
2008-10-08 04:00:12 +02:00
|
|
|
// A TableListField must be inside a form for its links to be generated
|
|
|
|
$form = new Form(new TableListFieldTest_TestController(), "TestForm", new FieldSet(
|
|
|
|
$table
|
|
|
|
), new FieldSet());
|
|
|
|
|
2008-08-13 03:39:46 +02:00
|
|
|
$table->ShowPagination = true;
|
|
|
|
$table->PageSize = 2;
|
|
|
|
|
|
|
|
$items = $table->sourceItems();
|
|
|
|
$this->assertNotNull($items);
|
|
|
|
|
|
|
|
$itemMap = $items->toDropdownMap("ID", "A") ;
|
2009-10-01 23:51:58 +02:00
|
|
|
$this->assertEquals(array(
|
|
|
|
$item1->ID => "a1",
|
|
|
|
$item2->ID => "a2"
|
|
|
|
), $itemMap);
|
2008-08-13 03:39:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function testSecondPageOfPaginatedSourceItemGeneration() {
|
2009-10-01 23:51:58 +02:00
|
|
|
$item1 = $this->objFromFixture('TableListFieldTest_Obj', 'one');
|
|
|
|
$item2 = $this->objFromFixture('TableListFieldTest_Obj', 'two');
|
|
|
|
$item3 = $this->objFromFixture('TableListFieldTest_Obj', 'three');
|
|
|
|
$item4 = $this->objFromFixture('TableListFieldTest_Obj', 'four');
|
|
|
|
$item5 = $this->objFromFixture('TableListFieldTest_Obj', 'five');
|
|
|
|
|
2008-08-13 03:39:46 +02:00
|
|
|
/* With pagination enabled, only the first page of items should be shown */
|
|
|
|
$table = new TableListField("Tester", "TableListFieldTest_Obj", array(
|
|
|
|
"A" => "Col A",
|
|
|
|
"B" => "Col B",
|
|
|
|
"C" => "Col C",
|
|
|
|
"D" => "Col D",
|
|
|
|
"E" => "Col E",
|
|
|
|
));
|
2008-10-08 04:00:12 +02:00
|
|
|
// A TableListField must be inside a form for its links to be generated
|
|
|
|
$form = new Form(new TableListFieldTest_TestController(), "TestForm", new FieldSet(
|
|
|
|
$table
|
|
|
|
), new FieldSet());
|
|
|
|
|
2008-08-13 03:39:46 +02:00
|
|
|
$table->ShowPagination = true;
|
|
|
|
$table->PageSize = 2;
|
|
|
|
$_REQUEST['ctf']['Tester']['start'] = 2;
|
|
|
|
|
|
|
|
$items = $table->sourceItems();
|
|
|
|
$this->assertNotNull($items);
|
|
|
|
|
2009-10-01 23:51:58 +02:00
|
|
|
$itemMap = $items->toDropdownMap("ID", "A") ;
|
|
|
|
$this->assertEquals(array($item3->ID => "a3", $item4->ID => "a4"), $itemMap);
|
2008-08-13 03:39:46 +02:00
|
|
|
}
|
2008-10-08 04:00:12 +02:00
|
|
|
|
2008-10-08 05:32:33 +02:00
|
|
|
/**
|
|
|
|
* Get that visiting the field's URL returns the content of the field.
|
|
|
|
* This capability is used by ajax
|
|
|
|
*/
|
|
|
|
function testAjaxRefreshing() {
|
|
|
|
$controller = new TableListFieldTest_TestController();
|
|
|
|
$table = $controller->TestForm()->Fields()->First();
|
|
|
|
|
|
|
|
$ajaxResponse = Director::test($table->Link())->getBody();
|
|
|
|
|
|
|
|
// Check that the column headings have been rendered
|
|
|
|
$this->assertRegExp('/<th[^>]*>\s*Col A\s*<\/th>/', $ajaxResponse);
|
|
|
|
$this->assertRegExp('/<th[^>]*>\s*Col B\s*<\/th>/', $ajaxResponse);
|
|
|
|
$this->assertRegExp('/<th[^>]*>\s*Col C\s*<\/th>/', $ajaxResponse);
|
|
|
|
$this->assertRegExp('/<th[^>]*>\s*Col D\s*<\/th>/', $ajaxResponse);
|
|
|
|
$this->assertRegExp('/<th[^>]*>\s*Col E\s*<\/th>/', $ajaxResponse);
|
|
|
|
}
|
|
|
|
|
2008-10-08 04:00:12 +02:00
|
|
|
function testCsvExport() {
|
|
|
|
$table = new TableListField("Tester", "TableListFieldTest_CsvExport", array(
|
|
|
|
"A" => "Col A",
|
|
|
|
"B" => "Col B"
|
|
|
|
));
|
|
|
|
|
|
|
|
$form = new Form(new TableListFieldTest_TestController(), "TestForm", new FieldSet(
|
|
|
|
$table
|
|
|
|
), new FieldSet());
|
|
|
|
|
|
|
|
$csvResponse = $table->export();
|
|
|
|
|
|
|
|
$csvOutput = $csvResponse->getBody();
|
|
|
|
|
|
|
|
$this->assertNotEquals($csvOutput, false);
|
|
|
|
|
|
|
|
// Create a temporary file and write the CSV to it.
|
|
|
|
$csvFileName = tempnam(TEMP_FOLDER, 'csv-export');
|
|
|
|
$csvFile = fopen($csvFileName, 'w');
|
|
|
|
fwrite($csvFile, $csvOutput);
|
|
|
|
fclose($csvFile);
|
|
|
|
|
|
|
|
$csvFile = fopen($csvFileName, 'r');
|
|
|
|
$csvRow = fgetcsv($csvFile);
|
|
|
|
$this->assertEquals(
|
|
|
|
$csvRow,
|
|
|
|
array('Col A', 'Col B')
|
|
|
|
);
|
|
|
|
|
|
|
|
$csvRow = fgetcsv($csvFile);
|
|
|
|
$this->assertEquals(
|
|
|
|
$csvRow,
|
|
|
|
array('"A field, with a comma"', 'A second field')
|
|
|
|
);
|
|
|
|
|
|
|
|
fclose($csvFile);
|
|
|
|
|
|
|
|
unlink($csvFileName);
|
|
|
|
}
|
2008-08-12 02:12:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class TableListFieldTest_Obj extends DataObject implements TestOnly {
|
|
|
|
static $db = array(
|
|
|
|
"A" => "Varchar",
|
|
|
|
"B" => "Varchar",
|
|
|
|
"C" => "Varchar",
|
|
|
|
);
|
2009-06-16 06:03:47 +02:00
|
|
|
static $default_sort = "A";
|
2008-08-12 02:12:29 +02:00
|
|
|
|
|
|
|
function D() {
|
|
|
|
return $this->A . '/' . $this->B . '/' . $this->C;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getE() {
|
|
|
|
return $this->A . '-e';
|
|
|
|
}
|
|
|
|
}
|
2008-10-08 04:00:12 +02:00
|
|
|
|
|
|
|
class TableListFieldTest_CsvExport extends DataObject implements TestOnly {
|
|
|
|
static $db = array(
|
|
|
|
"A" => "Varchar",
|
|
|
|
"B" => "Varchar"
|
|
|
|
);
|
2009-06-16 06:03:47 +02:00
|
|
|
static $default_sort = "A";
|
2008-10-08 04:00:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class TableListFieldTest_TestController extends Controller {
|
2009-10-11 02:07:16 +02:00
|
|
|
function Link($action = null) {
|
|
|
|
return Controller::join_links("TableListFieldTest_TestController/", $action);
|
2008-10-08 04:00:12 +02:00
|
|
|
}
|
2008-10-08 05:32:33 +02:00
|
|
|
function TestForm() {
|
|
|
|
$table = new TableListField("Table", "TableListFieldTest_Obj", array(
|
|
|
|
"A" => "Col A",
|
|
|
|
"B" => "Col B",
|
|
|
|
"C" => "Col C",
|
|
|
|
"D" => "Col D",
|
|
|
|
"E" => "Col E",
|
|
|
|
));
|
2009-10-15 23:58:15 +02:00
|
|
|
$table->disableSorting();
|
2008-10-08 05:32:33 +02:00
|
|
|
|
|
|
|
// A TableListField must be inside a form for its links to be generated
|
|
|
|
return new Form($this, "TestForm", new FieldSet(
|
|
|
|
$table
|
|
|
|
), new FieldSet());
|
|
|
|
}
|
2008-10-08 04:00:12 +02:00
|
|
|
}
|