2013-10-23 22:59:06 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @package framework
|
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
|
|
|
|
class GridFieldSortableHeaderTest extends SapphireTest {
|
|
|
|
|
|
|
|
protected static $fixture_file = 'GridFieldSortableHeaderTest.yml';
|
|
|
|
|
|
|
|
protected $extraDataObjects = array(
|
|
|
|
'GridFieldSortableHeaderTest_Team',
|
2015-09-17 00:19:03 +02:00
|
|
|
'GridFieldSortableHeaderTest_TeamGroup',
|
2013-10-23 22:59:06 +02:00
|
|
|
'GridFieldSortableHeaderTest_Cheerleader',
|
2015-09-17 00:19:03 +02:00
|
|
|
'GridFieldSortableHeaderTest_CheerleaderHat',
|
|
|
|
'GridFieldSortableHeaderTest_Mom'
|
2013-10-23 22:59:06 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-11-25 04:38:46 +01:00
|
|
|
/**
|
|
|
|
* Tests that the appropriate sortable headers are generated
|
|
|
|
*/
|
|
|
|
public function testRenderHeaders() {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-11-25 04:38:46 +01:00
|
|
|
// Generate sortable header and extract HTML
|
|
|
|
$list = new DataList('GridFieldSortableHeaderTest_Team');
|
|
|
|
$config = new GridFieldConfig_RecordEditor();
|
|
|
|
$form = new Form(Controller::curr(), 'Form', new FieldList(), new FieldList());
|
|
|
|
$gridField = new GridField('testfield', 'testfield', $list, $config);
|
|
|
|
$gridField->setForm($form);
|
|
|
|
$compontent = $gridField->getConfig()->getComponentByType('GridFieldSortableHeader');
|
|
|
|
$htmlFragment = $compontent->getHTMLFragments($gridField);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-11-25 04:38:46 +01:00
|
|
|
// Check that the output shows name and hat as sortable fields, but not city
|
|
|
|
$this->assertContains('<span class="non-sortable">City</span>', $htmlFragment['header']);
|
2014-03-30 08:37:54 +02:00
|
|
|
$this->assertContains('value="Name" class="action ss-gridfield-sort" id="action_SetOrderName"',
|
|
|
|
$htmlFragment['header']);
|
|
|
|
$this->assertContains(
|
|
|
|
'value="Cheerleader Hat" class="action ss-gridfield-sort" id="action_SetOrderCheerleader-Hat-Colour"',
|
|
|
|
$htmlFragment['header']);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-11-25 04:38:46 +01:00
|
|
|
// Check inverse of above
|
2014-03-30 08:37:54 +02:00
|
|
|
$this->assertNotContains('value="City" class="action ss-gridfield-sort" id="action_SetOrderCity"',
|
|
|
|
$htmlFragment['header']);
|
2013-11-25 04:38:46 +01:00
|
|
|
$this->assertNotContains('<span class="non-sortable">Name</span>', $htmlFragment['header']);
|
|
|
|
$this->assertNotContains('<span class="non-sortable">Cheerleader Hat</span>', $htmlFragment['header']);
|
|
|
|
}
|
2013-10-23 22:59:06 +02:00
|
|
|
|
|
|
|
public function testGetManipulatedData() {
|
|
|
|
$list = new DataList('GridFieldSortableHeaderTest_Team');
|
|
|
|
$config = new GridFieldConfig_RecordEditor();
|
|
|
|
$gridField = new GridField('testfield', 'testfield', $list, $config);
|
|
|
|
|
|
|
|
// Test normal sorting
|
|
|
|
$state = $gridField->State->GridFieldSortableHeader;
|
|
|
|
$state->SortColumn = 'City';
|
|
|
|
$state->SortDirection = 'asc';
|
|
|
|
|
|
|
|
$compontent = $gridField->getConfig()->getComponentByType('GridFieldSortableHeader');
|
|
|
|
$listA = $compontent->getManipulatedData($gridField, $list);
|
|
|
|
|
|
|
|
$state->SortDirection = 'desc';
|
|
|
|
$listB = $compontent->getManipulatedData($gridField, $list);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
array('Auckland', 'Cologne', 'Melbourne', 'Wellington'),
|
|
|
|
$listA->column('City')
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
array('Wellington', 'Melbourne', 'Cologne', 'Auckland'),
|
|
|
|
$listB->column('City')
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test one relation 'deep'
|
|
|
|
$state->SortColumn = 'Cheerleader.Name';
|
|
|
|
$state->SortDirection = 'asc';
|
|
|
|
$relationListA = $compontent->getManipulatedData($gridField, $list);
|
|
|
|
|
|
|
|
$state->SortDirection = 'desc';
|
|
|
|
$relationListB = $compontent->getManipulatedData($gridField, $list);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
array('Wellington', 'Melbourne', 'Cologne', 'Auckland'),
|
|
|
|
$relationListA->column('City')
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
array('Auckland', 'Cologne', 'Melbourne', 'Wellington'),
|
|
|
|
$relationListB->column('City')
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test two relations 'deep'
|
|
|
|
$state->SortColumn = 'Cheerleader.Hat.Colour';
|
|
|
|
$state->SortDirection = 'asc';
|
|
|
|
$relationListC = $compontent->getManipulatedData($gridField, $list);
|
|
|
|
|
|
|
|
$state->SortDirection = 'desc';
|
|
|
|
$relationListD = $compontent->getManipulatedData($gridField, $list);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
array('Cologne', 'Auckland', 'Wellington', 'Melbourne'),
|
|
|
|
$relationListC->column('City')
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
array('Melbourne', 'Wellington', 'Auckland', 'Cologne'),
|
|
|
|
$relationListD->column('City')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-09-17 00:19:03 +02:00
|
|
|
/**
|
|
|
|
* Test getManipulatedData on subclassed dataobjects
|
|
|
|
*/
|
|
|
|
public function testInheritedGetManiplatedData() {
|
|
|
|
$list = GridFieldSortableHeaderTest_TeamGroup::get();
|
|
|
|
$config = new GridFieldConfig_RecordEditor();
|
|
|
|
$gridField = new GridField('testfield', 'testfield', $list, $config);
|
|
|
|
$state = $gridField->State->GridFieldSortableHeader;
|
|
|
|
$compontent = $gridField->getConfig()->getComponentByType('GridFieldSortableHeader');
|
|
|
|
|
|
|
|
// Test that inherited dataobjects will work correctly
|
|
|
|
$state->SortColumn = 'Cheerleader.Hat.Colour';
|
|
|
|
$state->SortDirection = 'asc';
|
|
|
|
$relationListA = $compontent->getManipulatedData($gridField, $list);
|
|
|
|
$relationListAsql = Convert::nl2os($relationListA->sql(), ' ');
|
|
|
|
|
|
|
|
// Assert that all tables are joined properly
|
|
|
|
$this->assertContains('FROM "GridFieldSortableHeaderTest_Team"', $relationListAsql);
|
|
|
|
$this->assertContains(
|
|
|
|
'LEFT JOIN "GridFieldSortableHeaderTest_TeamGroup" '
|
|
|
|
. 'ON "GridFieldSortableHeaderTest_TeamGroup"."ID" = "GridFieldSortableHeaderTest_Team"."ID"',
|
|
|
|
$relationListAsql
|
|
|
|
);
|
|
|
|
$this->assertContains(
|
|
|
|
'LEFT JOIN "GridFieldSortableHeaderTest_Cheerleader" '
|
|
|
|
. 'ON "GridFieldSortableHeaderTest_Cheerleader"."ID" = "GridFieldSortableHeaderTest_Team"."CheerleaderID"',
|
|
|
|
$relationListAsql
|
|
|
|
);
|
|
|
|
$this->assertContains(
|
|
|
|
'LEFT JOIN "GridFieldSortableHeaderTest_CheerleaderHat" '
|
|
|
|
. 'ON "GridFieldSortableHeaderTest_CheerleaderHat"."ID" = "GridFieldSortableHeaderTest_Cheerleader"."HatID"', $relationListAsql);
|
|
|
|
|
|
|
|
// Test sorting is correct
|
|
|
|
$this->assertEquals(
|
|
|
|
array('Cologne', 'Auckland', 'Wellington', 'Melbourne'),
|
|
|
|
$relationListA->column('City')
|
|
|
|
);
|
|
|
|
$state->SortDirection = 'desc';
|
|
|
|
$relationListAdesc = $compontent->getManipulatedData($gridField, $list);
|
|
|
|
$this->assertEquals(
|
|
|
|
array('Melbourne', 'Wellington', 'Auckland', 'Cologne'),
|
|
|
|
$relationListAdesc->column('City')
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test subclasses of tables
|
|
|
|
$state->SortColumn = 'CheerleadersMom.Hat.Colour';
|
|
|
|
$state->SortDirection = 'asc';
|
|
|
|
$relationListB = $compontent->getManipulatedData($gridField, $list);
|
|
|
|
$relationListBsql = $relationListB->sql();
|
|
|
|
|
|
|
|
// Assert that subclasses are included in the query
|
|
|
|
$this->assertContains('FROM "GridFieldSortableHeaderTest_Team"', $relationListBsql);
|
|
|
|
$this->assertContains(
|
|
|
|
'LEFT JOIN "GridFieldSortableHeaderTest_TeamGroup" '
|
|
|
|
. 'ON "GridFieldSortableHeaderTest_TeamGroup"."ID" = "GridFieldSortableHeaderTest_Team"."ID"',
|
|
|
|
$relationListBsql
|
|
|
|
);
|
2015-10-29 06:37:25 +01:00
|
|
|
// Joined tables are joined basetable first
|
2015-09-17 00:19:03 +02:00
|
|
|
$this->assertContains(
|
2015-10-29 06:37:25 +01:00
|
|
|
'LEFT JOIN "GridFieldSortableHeaderTest_Cheerleader" '
|
|
|
|
. 'ON "GridFieldSortableHeaderTest_Cheerleader"."ID" = "GridFieldSortableHeaderTest_Team"."CheerleadersMomID"',
|
2015-09-17 00:19:03 +02:00
|
|
|
$relationListBsql
|
|
|
|
);
|
2015-10-29 06:37:25 +01:00
|
|
|
// Then the basetable of the joined record is joined to the specific subtable
|
2015-09-17 00:19:03 +02:00
|
|
|
$this->assertContains(
|
2015-10-29 06:37:25 +01:00
|
|
|
'LEFT JOIN "GridFieldSortableHeaderTest_Mom" '
|
|
|
|
. 'ON "GridFieldSortableHeaderTest_Cheerleader"."ID" = "GridFieldSortableHeaderTest_Mom"."ID"',
|
2015-09-17 00:19:03 +02:00
|
|
|
$relationListBsql
|
|
|
|
);
|
|
|
|
$this->assertContains(
|
|
|
|
'LEFT JOIN "GridFieldSortableHeaderTest_CheerleaderHat" '
|
|
|
|
. 'ON "GridFieldSortableHeaderTest_CheerleaderHat"."ID" = "GridFieldSortableHeaderTest_Cheerleader"."HatID"',
|
|
|
|
$relationListBsql
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// Test sorting is correct
|
|
|
|
$this->assertEquals(
|
|
|
|
array('Cologne', 'Auckland', 'Wellington', 'Melbourne'),
|
|
|
|
$relationListB->column('City')
|
|
|
|
);
|
|
|
|
$state->SortDirection = 'desc';
|
|
|
|
$relationListBdesc = $compontent->getManipulatedData($gridField, $list);
|
|
|
|
$this->assertEquals(
|
|
|
|
array('Melbourne', 'Wellington', 'Auckland', 'Cologne'),
|
|
|
|
$relationListBdesc->column('City')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-10-23 22:59:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class GridFieldSortableHeaderTest_Team extends DataObject implements TestOnly {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-11-25 04:38:46 +01:00
|
|
|
private static $summary_fields = array(
|
|
|
|
'Name' => 'Name',
|
|
|
|
'City.Initial' => 'City',
|
|
|
|
'Cheerleader.Hat.Colour' => 'Cheerleader Hat'
|
|
|
|
);
|
2013-10-23 22:59:06 +02:00
|
|
|
|
|
|
|
private static $db = array(
|
|
|
|
'Name' => 'Varchar',
|
|
|
|
'City' => 'Varchar'
|
|
|
|
);
|
|
|
|
|
|
|
|
private static $has_one = array(
|
2015-09-17 00:19:03 +02:00
|
|
|
'Cheerleader' => 'GridFieldSortableHeaderTest_Cheerleader',
|
|
|
|
'CheerleadersMom' => 'GridFieldSortableHeaderTest_Mom'
|
2013-10-23 22:59:06 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-09-17 00:19:03 +02:00
|
|
|
class GridFieldSortableHeaderTest_TeamGroup extends GridFieldSortableHeaderTest_Team implements TestOnly {
|
|
|
|
private static $db = array(
|
|
|
|
'GroupName' => 'Varchar'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-10-23 22:59:06 +02:00
|
|
|
class GridFieldSortableHeaderTest_Cheerleader extends DataObject implements TestOnly {
|
|
|
|
|
|
|
|
private static $db = array(
|
|
|
|
'Name' => 'Varchar'
|
|
|
|
);
|
|
|
|
|
|
|
|
private static $has_one = array(
|
|
|
|
'Team' => 'GridFieldSortableHeaderTest_Team',
|
|
|
|
'Hat' => 'GridFieldSortableHeaderTest_CheerleaderHat'
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-09-17 00:19:03 +02:00
|
|
|
/**
|
|
|
|
* Should have access to same properties as cheerleader
|
|
|
|
*/
|
|
|
|
class GridFieldSortableHeaderTest_Mom extends GridFieldSortableHeaderTest_Cheerleader implements TestOnly {
|
|
|
|
private static $db = array(
|
|
|
|
'NumberOfCookiesBaked' => 'Int'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-10-23 22:59:06 +02:00
|
|
|
class GridFieldSortableHeaderTest_CheerleaderHat extends DataObject implements TestOnly {
|
|
|
|
|
|
|
|
private static $db = array(
|
|
|
|
'Colour' => 'Varchar'
|
|
|
|
);
|
|
|
|
|
|
|
|
private static $has_one = array(
|
|
|
|
'Cheerleader' => 'GridFieldSortableHeaderTest_Cheerleader'
|
|
|
|
);
|
|
|
|
|
2014-03-30 08:37:54 +02:00
|
|
|
}
|