Source of file GridFieldSortableRowsAutoSortTest.php
Size: 15,066 Bytes - Last Modified: 2021-12-23T10:56:57+00:00
/var/www/docs.ssmods.com/process/src/tests/forms/GridFieldSortableRowsAutoSortTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377 | <?php namespace UndefinedOffset\SortableGridField\Tests; use SilverStripe\Control\Controller; use SilverStripe\Control\HTTPRequest; use SilverStripe\Dev\SapphireTest; use SilverStripe\Dev\TestOnly; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\Form; use SilverStripe\Forms\GridField\GridField; use SilverStripe\Forms\GridField\GridFieldConfig; use SilverStripe\ORM\DB; use SilverStripe\ORM\DataObject; use SilverStripe\ORM\FieldType\DBInt; use SilverStripe\ORM\FieldType\DBVarchar; use SilverStripe\Security\Security; use SilverStripe\Versioned\Versioned; use UndefinedOffset\SortableGridField\Forms\GridFieldSortableRows; /** * Class GridFieldSortableRowsAutoSortTest * * @package SortableGridField\Tests */ class GridFieldSortableRowsAutoSortTest extends SapphireTest { /** @var string */ public static $fixture_file = 'GridFieldSortableRowsAutoSortTest.yml'; /** @var array */ protected static $extra_dataobjects = array( GridFieldAction_SortOrder_Player::class, GridFieldAction_SortOrder_VPlayer::class, GridFieldAction_SortOrder_TestParent::class, GridFieldAction_SortOrder_BaseObject::class, GridFieldAction_SortOrder_ChildObject::class ); public function testAutoSort() { if (Security::getCurrentUser()) { $this->logOut(); } $list = GridFieldAction_SortOrder_Player::get(); $config = GridFieldConfig::create()->addComponent(new GridFieldSortableRows('SortOrder')); $gridField = new GridField('testfield', 'testfield', $list, $config); $form = new Form(new SortableGridField_DummyController(), 'mockform', new FieldList(array($gridField)), new FieldList()); $stateID = 'testGridStateActionField'; $request = new HTTPRequest('POST', 'url', array(), array('action_gridFieldAlterAction?StateID=' . $stateID => true, $form->getSecurityToken()->getName() => $form->getSecurityToken()->getValue())); $session = Controller::curr()->getRequest()->getSession(); $session->set($form->getSecurityToken()->getName(), $form->getSecurityToken()->getValue()); $session->set($stateID, array('grid' => '', 'actionName' => 'sortableRowsToggle', 'args' => array('GridFieldSortableRows' => array('sortableToggle' => true)))); $request->setSession($session); $gridField->gridFieldAlterAction(array('StateID' => $stateID), $form, $request); //Insure sort ran $this->assertEquals(3, $list->last()->SortOrder, 'Auto sort should have run'); //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'); } public function testAppendToTopAutoSort() { if (Security::getCurrentUser()) { $this->logOut(); } $list = GridFieldAction_SortOrder_Player::get(); $config = GridFieldConfig::create()->addComponent(new GridFieldSortableRows('SortOrder')); $gridField = new GridField('testfield', 'testfield', $list, $config); $form = new Form(new SortableGridField_DummyController(), 'mockform', new FieldList(array($gridField)), new FieldList()); /** @var GridFieldSortableRows $sortableRows */ $sortableRows = $gridField->getConfig()->getComponentByType(GridFieldSortableRows::class); $sortableRows->setAppendToTop(true); $this->assertEquals(0, $list->last()->SortOrder, 'Auto sort should not have run'); $stateID = 'testGridStateActionField'; $request = new HTTPRequest('POST', 'url', array(), array('action_gridFieldAlterAction?StateID=' . $stateID => true, $form->getSecurityToken()->getName() => $form->getSecurityToken()->getValue())); $session = Controller::curr()->getRequest()->getSession(); $session->set($form->getSecurityToken()->getName(), $form->getSecurityToken()->getValue()); $session->set($stateID, array('grid' => '', 'actionName' => 'sortableRowsToggle', 'args' => array('GridFieldSortableRows' => array('sortableToggle' => true)))); $request->setSession($session); $gridField->gridFieldAlterAction(array('StateID' => $stateID), $form, $request); //Insure sort ran $this->assertEquals(3, $list->last()->SortOrder, 'Auto sort should have run'); //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'); } public function testAutoSortVersioned() { if (Security::getCurrentUser()) { $this->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 SortableGridField_DummyController(), 'mockform', new FieldList(array($gridField)), new FieldList()); $stateID = 'testGridStateActionField'; $request = new HTTPRequest('POST', 'url', array(), array('action_gridFieldAlterAction?StateID=' . $stateID => true, $form->getSecurityToken()->getName() => $form->getSecurityToken()->getValue())); $session = Controller::curr()->getRequest()->getSession(); $session->set($form->getSecurityToken()->getName(), $form->getSecurityToken()->getValue()); $session->set($stateID, array('grid' => '', 'actionName' => 'sortableRowsToggle', 'args' => array('GridFieldSortableRows' => array('sortableToggle' => true)))); $request->setSession($session); $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::set_reading_mode('Live'); //Get live instance $obj = Versioned::get_one_by_stage(GridFieldAction_SortOrder_VPlayer::class, 'Live', '"ID"=' . $list->last()->ID); //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::class, '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 (Security::getCurrentUser()) { $this->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 SortableGridField_DummyController(), 'mockform', new FieldList(array($gridField)), new FieldList()); /** @var GridFieldSortableRows $sortableRows */ $sortableRows = $gridField->getConfig()->getComponentByType(GridFieldSortableRows::class); $sortableRows->setAppendToTop(true); $this->assertEquals(0, $list->last()->SortOrder, 'Auto sort should not have run on Versioned stage "Stage"'); $stateID = 'testGridStateActionField'; $request = new HTTPRequest('POST', 'url', array(), array('action_gridFieldAlterAction?StateID=' . $stateID => true, $form->getSecurityToken()->getName() => $form->getSecurityToken()->getValue())); $session = Controller::curr()->getRequest()->getSession(); $session->set($form->getSecurityToken()->getName(), $form->getSecurityToken()->getValue()); $session->set($stateID, array('grid' => '', 'actionName' => 'sortableRowsToggle', 'args' => array('GridFieldSortableRows' => array('sortableToggle' => true)))); $request->setSession($session); $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::set_reading_mode('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"'); } public function testAppendToTopAutoSortChild() { if (Security::getCurrentUser()) { $this->logOut(); } //Push the edit date into the past, we're checking this later DB::query('UPDATE "GridFieldAction_SortOrder_BaseObject" SET "LastEdited"=\'' . date('Y-m-d 00:00:00', strtotime('yesterday')) . '\''); /** @var GridFieldAction_SortOrder_TestParent $parent */ $parent = GridFieldAction_SortOrder_TestParent::get()->first(); /** @var DataList $list */ $list = $parent->TestRelation(); $config = GridFieldConfig::create()->addComponent(new GridFieldSortableRows('SortOrder')); $gridField = new GridField('testfield', 'testfield', $list, $config); $form = new Form(new SortableGridField_DummyController(), 'mockform', new FieldList(array($gridField)), new FieldList()); $form->loadDataFrom($parent); /** @var GridFieldSortableRows $sortableRows */ $sortableRows = $gridField->getConfig()->getComponentByType(GridFieldSortableRows::class); $sortableRows->setAppendToTop(true); $this->assertEquals(0, $list->last()->SortOrder, 'Auto sort should not have run'); $stateID = 'testGridStateActionField'; $request = new HTTPRequest('POST', 'url', array(), array('action_gridFieldAlterAction?StateID=' . $stateID => true, $form->getSecurityToken()->getName() => $form->getSecurityToken()->getValue())); $session = Controller::curr()->getRequest()->getSession(); $session->set($form->getSecurityToken()->getName(), $form->getSecurityToken()->getValue()); $session->set($stateID, array('grid' => '', 'actionName' => 'sortableRowsToggle', 'args' => array('GridFieldSortableRows' => array('sortableToggle' => true)))); $request->setSession($session); $gridField->gridFieldAlterAction(array('StateID' => $stateID), $form, $request); //Insure sort ran $this->assertEquals(3, $list->last()->SortOrder, 'Auto sort should have run'); //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'); //Make sure the last edited is today for all records $this->assertEquals(3, $list->filter('LastEdited:GreaterThan', date('Y-m-d 00:00:00'))->count()); } } /** * Class GridFieldAction_SortOrder_Player * * @package SortableGridField\Tests * @property string Name * @property int SortOrder */ class GridFieldAction_SortOrder_Player extends DataObject implements TestOnly { private static $table_name = 'GridFieldAction_SortOrder_Player'; private static $db = array( 'Name' => DBVarchar::class, 'SortOrder' => DBInt::class ); private static $default_sort = 'SortOrder'; } /** * Class GridFieldAction_SortOrder_VPlayer * * @package SortableGridField\Tests * @property string Name * @property int SortOrder */ class GridFieldAction_SortOrder_VPlayer extends DataObject implements TestOnly { private static $table_name = 'GridFieldAction_SortOrder_VPlayer'; private static $db = array( 'Name' => DBVarchar::class, 'SortOrder' => DBInt::class ); private static $default_sort = 'SortOrder'; private static $extensions = array( "SilverStripe\\Versioned\\Versioned('Stage', 'Live')" ); } /** * Class GridFieldAction_SortOrder_TestParent * * @package SortableGridField\Tests * @property string Name * @method GridFieldAction_SortOrder_ChildObject TestRelation */ class GridFieldAction_SortOrder_TestParent extends DataObject implements TestOnly { private static $table_name = 'GridFieldAction_SortOrder_TestParent'; private static $db = array( 'Name' => DBVarchar::class ); private static $has_many = array( 'TestRelation' => GridFieldAction_SortOrder_ChildObject::class ); } /** * Class GridFieldAction_SortOrder_BaseObject * * @package SortableGridField\Tests * @property string Name */ class GridFieldAction_SortOrder_BaseObject extends DataObject implements TestOnly { private static $table_name = 'GridFieldAction_SortOrder_BaseObject'; private static $db = array( 'Name' => DBVarchar::class ); } /** * Class GridFieldAction_SortOrder_ChildObject * * @package SortableGridField\Tests * @property int SortOrder * @method GridFieldAction_SortOrder_TestParent Parent */ class GridFieldAction_SortOrder_ChildObject extends GridFieldAction_SortOrder_BaseObject implements TestOnly { private static $table_name = 'GridFieldAction_SortOrder_ChildObject'; private static $db = array( 'SortOrder' => DBInt::class ); private static $has_one = array( 'Parent' => GridFieldAction_SortOrder_TestParent::class ); private static $default_sort = 'SortOrder'; } /** * Class SortableGridField_DummyController * * @package SortableGridField\Tests */ class SortableGridField_DummyController extends Controller { private static $url_segment = 'sortable-grid-field'; } |