2012-09-06 17:27:23 +02:00
|
|
|
<?php
|
|
|
|
class GridFieldSortableRowsAutoSortTest extends SapphireTest {
|
|
|
|
/** @var string */
|
|
|
|
public static $fixture_file = 'GridFieldSortableRowsAutoSortTest.yml';
|
|
|
|
|
|
|
|
/** @var array */
|
2016-02-06 22:00:23 +01:00
|
|
|
protected $extraDataObjects = array('GridFieldAction_SortOrder_Player', 'GridFieldAction_SortOrder_VPlayer');
|
2012-09-06 17:27:23 +02:00
|
|
|
|
|
|
|
public function testAutoSort() {
|
|
|
|
if(Member::currentUser()) { Member::currentUser()->logOut(); }
|
|
|
|
|
2016-02-06 22:00:23 +01:00
|
|
|
$list = GridFieldAction_SortOrder_Player::get();
|
|
|
|
$config = GridFieldConfig::create()->addComponent(new GridFieldSortableRows('SortOrder'));
|
|
|
|
$gridField = new GridField('testfield', 'testfield', $list, $config);
|
|
|
|
$form = new Form(new Controller(), 'mockform', new FieldList(array($gridField)), new FieldList());
|
|
|
|
|
2012-09-06 17:27:23 +02:00
|
|
|
$stateID = 'testGridStateActionField';
|
2013-07-19 02:01:41 +02:00
|
|
|
Session::set($stateID, array('grid'=>'', 'actionName'=>'sortableRowsToggle', 'args'=>array('GridFieldSortableRows'=>array('sortableToggle'=>true))));
|
2016-03-03 14:47:28 +01:00
|
|
|
$request = new SS_HTTPRequest('POST', 'url', array(), array('action_gridFieldAlterAction?StateID='.$stateID=>true, $form->getSecurityToken()->getName()=>$form->getSecurityToken()->getValue()));
|
2016-02-06 22:00:23 +01:00
|
|
|
$gridField->gridFieldAlterAction(array('StateID'=>$stateID), $form, $request);
|
2013-10-22 15:36:08 +02:00
|
|
|
|
|
|
|
//Insure sort ran
|
2016-02-06 22:00:23 +01:00
|
|
|
$this->assertEquals(3, $list->last()->SortOrder, 'Auto sort should have run');
|
2013-10-22 15:36:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
//Check for duplicates (there shouldn't be any)
|
2016-02-06 22:00:23 +01:00
|
|
|
$count=$list->Count();
|
|
|
|
$indexes=count(array_unique($list->column('SortOrder')));
|
2013-10-22 15:36:08 +02:00
|
|
|
$this->assertEquals(0, $count-$indexes, 'Duplicate indexes detected');
|
2012-09-06 17:27:23 +02:00
|
|
|
}
|
2013-10-19 19:34:45 +02:00
|
|
|
|
|
|
|
public function testAppendToTopAutoSort() {
|
|
|
|
if(Member::currentUser()) { Member::currentUser()->logOut(); }
|
|
|
|
|
2016-02-06 22:00:23 +01:00
|
|
|
$list = GridFieldAction_SortOrder_Player::get();
|
|
|
|
$config = GridFieldConfig::create()->addComponent(new GridFieldSortableRows('SortOrder'));
|
|
|
|
$gridField = new GridField('testfield', 'testfield', $list, $config);
|
|
|
|
$form = new Form(new Controller(), 'mockform', new FieldList(array($gridField)), new FieldList());
|
2013-10-19 19:34:45 +02:00
|
|
|
|
2016-02-06 22:00:23 +01:00
|
|
|
$gridField->getConfig()->getComponentByType('GridFieldSortableRows')->setAppendToTop(true);
|
|
|
|
|
|
|
|
$this->assertEquals(0, $list->last()->SortOrder, 'Auto sort should not have run');
|
2013-10-19 19:34:45 +02:00
|
|
|
|
|
|
|
$stateID = 'testGridStateActionField';
|
|
|
|
Session::set($stateID, array('grid'=>'', 'actionName'=>'sortableRowsToggle', 'args'=>array('GridFieldSortableRows'=>array('sortableToggle'=>true))));
|
2016-03-03 14:47:28 +01:00
|
|
|
$request = new SS_HTTPRequest('POST', 'url', array(), array('action_gridFieldAlterAction?StateID='.$stateID=>true, $form->getSecurityToken()->getName()=>$form->getSecurityToken()->getValue()));
|
2016-02-06 22:00:23 +01:00
|
|
|
$gridField->gridFieldAlterAction(array('StateID'=>$stateID), $form, $request);
|
2013-10-22 15:36:08 +02:00
|
|
|
|
|
|
|
//Insure sort ran
|
2016-02-06 22:00:23 +01:00
|
|
|
$this->assertEquals(3, $list->last()->SortOrder, 'Auto sort should have run');
|
2013-10-22 15:36:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
//Check for duplicates (there shouldn't be any)
|
2016-02-06 22:00:23 +01:00
|
|
|
$count=$list->Count();
|
|
|
|
$indexes=count(array_unique($list->column('SortOrder')));
|
2013-10-22 15:36:08 +02:00
|
|
|
$this->assertEquals(0, $count-$indexes, 'Duplicate indexes detected');
|
2013-10-19 19:34:45 +02:00
|
|
|
}
|
2016-02-06 22:00:23 +01:00
|
|
|
|
|
|
|
public function testAutoSortVersioned() {
|
|
|
|
if(Member::currentUser()) { Member::currentUser()->logOut(); }
|
|
|
|
|
|
|
|
//Force versioned to reset
|
|
|
|
Versioned::reset();
|
|
|
|
|
|
|
|
$list = GridFieldAction_SortOrder_VPlayer::get();
|
|
|
|
|
|
|
|
//Publish all records
|
|
|
|
foreach($list as $item) {
|
|
|
|
$item->publish('Stage', 'Live');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$config = GridFieldConfig::create()->addComponent(new GridFieldSortableRows('SortOrder', true, 'Live'));
|
|
|
|
$gridField = new GridField('testfield', 'testfield', $list, $config);
|
|
|
|
$form = new Form(new Controller(), 'mockform', new FieldList(array($gridField)), new FieldList());
|
|
|
|
|
|
|
|
$stateID = 'testGridStateActionField';
|
|
|
|
Session::set($stateID, array('grid'=>'', 'actionName'=>'sortableRowsToggle', 'args'=>array('GridFieldSortableRows'=>array('sortableToggle'=>true))));
|
2016-03-03 14:47:28 +01:00
|
|
|
$request = new SS_HTTPRequest('POST', 'url', array(), array('action_gridFieldAlterAction?StateID='.$stateID=>true, $form->getSecurityToken()->getName()=>$form->getSecurityToken()->getValue()));
|
2016-02-06 22:00:23 +01:00
|
|
|
$gridField->gridFieldAlterAction(array('StateID'=>$stateID), $form, $request);
|
|
|
|
|
|
|
|
|
|
|
|
//Insure sort ran
|
|
|
|
$this->assertEquals(3, $list->last()->SortOrder, 'Auto sort should have run on Versioned stage "Stage"');
|
|
|
|
|
|
|
|
|
|
|
|
//Check for duplicates (there shouldn't be any)
|
|
|
|
$count=$list->Count();
|
|
|
|
$indexes=count(array_unique($list->column('SortOrder')));
|
|
|
|
$this->assertEquals(0, $count-$indexes, 'Duplicate indexes detected on Versioned stage "Stage"');
|
|
|
|
|
|
|
|
|
|
|
|
//Force versioned over to Live stage
|
|
|
|
Versioned::reading_stage('Live');
|
|
|
|
|
|
|
|
//Get live instance
|
2016-02-13 18:51:01 +01:00
|
|
|
$obj=Versioned::get_one_by_stage('GridFieldAction_SortOrder_VPlayer', 'Live', '"ID"='.$list->last()->ID);
|
2016-02-06 22:00:23 +01:00
|
|
|
|
|
|
|
//Insure sort ran
|
|
|
|
$this->assertEquals(3, $obj->SortOrder, 'Auto sort should have run on Versioned stage "Live"');
|
|
|
|
|
|
|
|
|
|
|
|
//Check for duplicates (there shouldn't be any)
|
|
|
|
$list=Versioned::get_by_stage('GridFieldAction_SortOrder_VPlayer', 'Live');
|
|
|
|
$count=$list->Count();
|
|
|
|
$indexes=count(array_unique($list->column('SortOrder')));
|
|
|
|
$this->assertEquals(0, $count-$indexes, 'Duplicate indexes detected on Versioned stage "Live"');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAppendToTopAutoSortVersioned() {
|
|
|
|
if(Member::currentUser()) { Member::currentUser()->logOut(); }
|
|
|
|
|
|
|
|
//Force versioned to reset
|
|
|
|
Versioned::reset();
|
|
|
|
|
|
|
|
$list = GridFieldAction_SortOrder_VPlayer::get();
|
|
|
|
|
|
|
|
//Publish all records
|
|
|
|
foreach($list as $item) {
|
|
|
|
$item->publish('Stage', 'Live');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$config = GridFieldConfig::create()->addComponent(new GridFieldSortableRows('SortOrder', true, 'Live'));
|
|
|
|
$gridField = new GridField('testfield', 'testfield', $list, $config);
|
|
|
|
$form = new Form(new Controller(), 'mockform', new FieldList(array($gridField)), new FieldList());
|
|
|
|
|
|
|
|
$gridField->getConfig()->getComponentByType('GridFieldSortableRows')->setAppendToTop(true);
|
|
|
|
|
|
|
|
$this->assertEquals(0, $list->last()->SortOrder, 'Auto sort should not have run on Versioned stage "Stage"');
|
|
|
|
|
|
|
|
$stateID = 'testGridStateActionField';
|
|
|
|
Session::set($stateID, array('grid'=>'', 'actionName'=>'sortableRowsToggle', 'args'=>array('GridFieldSortableRows'=>array('sortableToggle'=>true))));
|
2016-03-03 14:47:28 +01:00
|
|
|
$request = new SS_HTTPRequest('POST', 'url', array(), array('action_gridFieldAlterAction?StateID='.$stateID=>true, $form->getSecurityToken()->getName()=>$form->getSecurityToken()->getValue()));
|
2016-02-06 22:00:23 +01:00
|
|
|
$gridField->gridFieldAlterAction(array('StateID'=>$stateID), $form, $request);
|
|
|
|
|
|
|
|
|
|
|
|
//Insure sort ran
|
|
|
|
$this->assertEquals(3, $list->last()->SortOrder, 'Auto sort should have run on Versioned stage "Stage"');
|
|
|
|
|
|
|
|
|
|
|
|
//Check for duplicates (there shouldn't be any)
|
|
|
|
$count=$list->Count();
|
|
|
|
$indexes=count(array_unique($list->column('SortOrder')));
|
|
|
|
$this->assertEquals(0, $count-$indexes, 'Duplicate indexes detected on Versioned stage "Stage"');
|
|
|
|
|
|
|
|
|
|
|
|
//Force versioned over to Live stage
|
|
|
|
Versioned::reading_stage('Live');
|
|
|
|
|
|
|
|
//Insure sort ran
|
|
|
|
$this->assertEquals(3, $list->last()->SortOrder, 'Auto sort should have run on Versioned stage "Live"');
|
|
|
|
|
|
|
|
|
|
|
|
//Check for duplicates (there shouldn't be any)
|
|
|
|
$count=$list->Count();
|
|
|
|
$indexes=count(array_unique($list->column('SortOrder')));
|
|
|
|
$this->assertEquals(0, $count-$indexes, 'Duplicate indexes detected on Versioned stage "Live"');
|
|
|
|
}
|
2012-09-06 17:27:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class GridFieldAction_SortOrder_Player extends DataObject implements TestOnly {
|
|
|
|
static $db = array(
|
|
|
|
'Name' => 'Varchar',
|
|
|
|
'SortOrder' => 'Int'
|
|
|
|
);
|
|
|
|
|
|
|
|
static $default_sort='SortOrder';
|
|
|
|
}
|
2016-02-06 22:00:23 +01:00
|
|
|
|
|
|
|
class GridFieldAction_SortOrder_VPlayer extends DataObject implements TestOnly {
|
|
|
|
static $db = array(
|
|
|
|
'Name' => 'Varchar',
|
|
|
|
'SortOrder' => 'Int'
|
|
|
|
);
|
|
|
|
|
|
|
|
static $default_sort='SortOrder';
|
|
|
|
|
|
|
|
static $extensions=array(
|
|
|
|
"Versioned('Stage', 'Live')"
|
|
|
|
);
|
|
|
|
}
|
2012-09-06 17:27:23 +02:00
|
|
|
?>
|