From 8ed1621f8b24a92968f014fd563a817961700b2c Mon Sep 17 00:00:00 2001 From: UndefinedOffset Date: Wed, 16 Feb 2022 16:15:33 -0400 Subject: [PATCH] Split unit test classes into separate files --- .gitignore | 3 +- src/Forms/GridFieldSortableRows.php | 3 - .../PHPUnit/Forms/AutoSortTest/BaseObject.php | 19 ++ .../Forms/AutoSortTest/ChildObject.php | 27 +++ .../Forms/AutoSortTest/DummyController.php | 12 ++ tests/PHPUnit/Forms/AutoSortTest/Player.php | 23 ++ .../PHPUnit/Forms/AutoSortTest/TestParent.php | 25 +++ tests/PHPUnit/Forms/AutoSortTest/VPlayer.php | 28 +++ .../GridFieldSortableRowsAutoSortTest.php | 198 ++++-------------- .../GridFieldSortableRowsAutoSortTest.yml | 14 +- .../Forms/GridFieldSortableRowsPageTest.php | 129 ++++-------- .../Forms/GridFieldSortableRowsPageTest.yml | 4 +- .../Forms/GridFieldSortableRowsTest.php | 111 +++------- .../Forms/GridFieldSortableRowsTest.yml | 4 +- tests/PHPUnit/Forms/RowsPageTest/Team.php | 25 +++ tests/PHPUnit/Forms/RowsPageTest/VTeam.php | 30 +++ tests/PHPUnit/Forms/RowsTest/Team.php | 25 +++ tests/PHPUnit/Forms/RowsTest/VTeam.php | 29 +++ 18 files changed, 373 insertions(+), 336 deletions(-) create mode 100644 tests/PHPUnit/Forms/AutoSortTest/BaseObject.php create mode 100644 tests/PHPUnit/Forms/AutoSortTest/ChildObject.php create mode 100644 tests/PHPUnit/Forms/AutoSortTest/DummyController.php create mode 100644 tests/PHPUnit/Forms/AutoSortTest/Player.php create mode 100644 tests/PHPUnit/Forms/AutoSortTest/TestParent.php create mode 100644 tests/PHPUnit/Forms/AutoSortTest/VPlayer.php create mode 100644 tests/PHPUnit/Forms/RowsPageTest/Team.php create mode 100644 tests/PHPUnit/Forms/RowsPageTest/VTeam.php create mode 100644 tests/PHPUnit/Forms/RowsTest/Team.php create mode 100644 tests/PHPUnit/Forms/RowsTest/VTeam.php diff --git a/.gitignore b/.gitignore index 3f45de0..11f86fe 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ /.buildpath /.project /.settings -/.settings/* \ No newline at end of file +/.settings/* +.phpunit.result.cache diff --git a/src/Forms/GridFieldSortableRows.php b/src/Forms/GridFieldSortableRows.php index 925a871..156667b 100644 --- a/src/Forms/GridFieldSortableRows.php +++ b/src/Forms/GridFieldSortableRows.php @@ -1,5 +1,4 @@ 'Varchar', + ]; +} diff --git a/tests/PHPUnit/Forms/AutoSortTest/ChildObject.php b/tests/PHPUnit/Forms/AutoSortTest/ChildObject.php new file mode 100644 index 0000000..7223094 --- /dev/null +++ b/tests/PHPUnit/Forms/AutoSortTest/ChildObject.php @@ -0,0 +1,27 @@ + DBInt::class, + ]; + + private static $has_one = [ + 'Parent' => TestParent::class, + ]; + + private static $default_sort = 'SortOrder'; +} diff --git a/tests/PHPUnit/Forms/AutoSortTest/DummyController.php b/tests/PHPUnit/Forms/AutoSortTest/DummyController.php new file mode 100644 index 0000000..c0ff458 --- /dev/null +++ b/tests/PHPUnit/Forms/AutoSortTest/DummyController.php @@ -0,0 +1,12 @@ + 'Varchar', + 'SortOrder' => 'Int', + ]; + + private static $default_sort = 'SortOrder'; +} diff --git a/tests/PHPUnit/Forms/AutoSortTest/TestParent.php b/tests/PHPUnit/Forms/AutoSortTest/TestParent.php new file mode 100644 index 0000000..2537d2f --- /dev/null +++ b/tests/PHPUnit/Forms/AutoSortTest/TestParent.php @@ -0,0 +1,25 @@ + 'Varchar', + ]; + + private static $has_many = [ + 'TestRelation' => ChildObject::class, + ]; +} diff --git a/tests/PHPUnit/Forms/AutoSortTest/VPlayer.php b/tests/PHPUnit/Forms/AutoSortTest/VPlayer.php new file mode 100644 index 0000000..e3ce08e --- /dev/null +++ b/tests/PHPUnit/Forms/AutoSortTest/VPlayer.php @@ -0,0 +1,28 @@ + 'Varchar', + 'SortOrder' => 'Int', + ]; + + private static $default_sort = 'SortOrder'; + + private static $extensions = [ + Versioned::class, + ]; +} diff --git a/tests/PHPUnit/Forms/GridFieldSortableRowsAutoSortTest.php b/tests/PHPUnit/Forms/GridFieldSortableRowsAutoSortTest.php index 8b4bcb0..4a6afab 100644 --- a/tests/PHPUnit/Forms/GridFieldSortableRowsAutoSortTest.php +++ b/tests/PHPUnit/Forms/GridFieldSortableRowsAutoSortTest.php @@ -1,27 +1,26 @@ logOut(); } - $list = GridFieldAction_SortOrder_Player::get(); + $list = 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()); + $form = new Form(new DummyController(), 'mockform', new FieldList([$gridField]), new FieldList()); $stateID = 'testGridStateActionField'; - $request = new HTTPRequest('POST', 'url', array(), array('action_gridFieldAlterAction?StateID=' . $stateID => true, $form->getSecurityToken()->getName() => $form->getSecurityToken()->getValue())); + $request = new HTTPRequest('POST', 'url', [], ['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)))); + $session->set($stateID, ['grid' => '', 'actionName' => 'sortableRowsToggle', 'args' => ['GridFieldSortableRows' => ['sortableToggle' => true]]]); $request->setSession($session); - - $gridField->gridFieldAlterAction(array('StateID' => $stateID), $form, $request); + + $gridField->gridFieldAlterAction(['StateID' => $stateID], $form, $request); //Insure sort ran $this->assertEquals(3, $list->last()->SortOrder, 'Auto sort should have run'); @@ -73,10 +72,10 @@ class GridFieldSortableRowsAutoSortTest extends SapphireTest $this->logOut(); } - $list = GridFieldAction_SortOrder_Player::get(); + $list = 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()); + $form = new Form(new DummyController(), 'mockform', new FieldList([$gridField]), new FieldList()); /** @var GridFieldSortableRows $sortableRows */ $sortableRows = $gridField->getConfig()->getComponentByType(GridFieldSortableRows::class); @@ -85,12 +84,12 @@ class GridFieldSortableRowsAutoSortTest extends SapphireTest $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())); + $request = new HTTPRequest('POST', 'url', [], ['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)))); + $session->set($stateID, ['grid' => '', 'actionName' => 'sortableRowsToggle', 'args' => ['GridFieldSortableRows' => ['sortableToggle' => true]]]); $request->setSession($session); - $gridField->gridFieldAlterAction(array('StateID' => $stateID), $form, $request); + $gridField->gridFieldAlterAction(['StateID' => $stateID], $form, $request); //Insure sort ran $this->assertEquals(3, $list->last()->SortOrder, 'Auto sort should have run'); @@ -111,7 +110,7 @@ class GridFieldSortableRowsAutoSortTest extends SapphireTest //Force versioned to reset Versioned::reset(); - $list = GridFieldAction_SortOrder_VPlayer::get(); + $list = VPlayer::get(); //Publish all records foreach ($list as $item) { @@ -121,15 +120,15 @@ class GridFieldSortableRowsAutoSortTest extends SapphireTest $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()); + $form = new Form(new DummyController(), 'mockform', new FieldList([$gridField]), new FieldList()); $stateID = 'testGridStateActionField'; - $request = new HTTPRequest('POST', 'url', array(), array('action_gridFieldAlterAction?StateID=' . $stateID => true, $form->getSecurityToken()->getName() => $form->getSecurityToken()->getValue())); + $request = new HTTPRequest('POST', 'url', [], ['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)))); + $session->set($stateID, ['grid' => '', 'actionName' => 'sortableRowsToggle', 'args' => ['GridFieldSortableRows' => ['sortableToggle' => true]]]); $request->setSession($session); - $gridField->gridFieldAlterAction(array('StateID' => $stateID), $form, $request); + $gridField->gridFieldAlterAction(['StateID' => $stateID], $form, $request); //Insure sort ran @@ -146,14 +145,14 @@ class GridFieldSortableRowsAutoSortTest extends SapphireTest Versioned::set_reading_mode('Live'); //Get live instance - $obj = Versioned::get_one_by_stage(GridFieldAction_SortOrder_VPlayer::class, 'Live', '"ID"=' . $list->last()->ID); + $obj = Versioned::get_one_by_stage(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'); + $list = Versioned::get_by_stage(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"'); @@ -168,7 +167,7 @@ class GridFieldSortableRowsAutoSortTest extends SapphireTest //Force versioned to reset Versioned::reset(); - $list = GridFieldAction_SortOrder_VPlayer::get(); + $list = VPlayer::get(); //Publish all records foreach ($list as $item) { @@ -178,7 +177,7 @@ class GridFieldSortableRowsAutoSortTest extends SapphireTest $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()); + $form = new Form(new DummyController(), 'mockform', new FieldList([$gridField]), new FieldList()); /** @var GridFieldSortableRows $sortableRows */ $sortableRows = $gridField->getConfig()->getComponentByType(GridFieldSortableRows::class); @@ -187,12 +186,12 @@ class GridFieldSortableRowsAutoSortTest extends SapphireTest $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())); + $request = new HTTPRequest('POST', 'url', [], ['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)))); + $session->set($stateID, ['grid' => '', 'actionName' => 'sortableRowsToggle', 'args' => ['GridFieldSortableRows' => ['sortableToggle' => true]]]); $request->setSession($session); - $gridField->gridFieldAlterAction(array('StateID' => $stateID), $form, $request); + $gridField->gridFieldAlterAction(['StateID' => $stateID], $form, $request); //Insure sort ran @@ -227,15 +226,15 @@ class GridFieldSortableRowsAutoSortTest extends SapphireTest //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 TestParent $parent */ + $parent = 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 = new Form(new DummyController(), 'mockform', new FieldList([$gridField]), new FieldList()); $form->loadDataFrom($parent); /** @var GridFieldSortableRows $sortableRows */ @@ -245,12 +244,12 @@ class GridFieldSortableRowsAutoSortTest extends SapphireTest $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())); + $request = new HTTPRequest('POST', 'url', [], ['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)))); + $session->set($stateID, ['grid' => '', 'actionName' => 'sortableRowsToggle', 'args' => ['GridFieldSortableRows' => ['sortableToggle' => true]]]); $request->setSession($session); - $gridField->gridFieldAlterAction(array('StateID' => $stateID), $form, $request); + $gridField->gridFieldAlterAction(['StateID' => $stateID], $form, $request); //Insure sort ran $this->assertEquals(3, $list->last()->SortOrder, 'Auto sort should have run'); @@ -266,112 +265,3 @@ class GridFieldSortableRowsAutoSortTest extends SapphireTest $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'; -} \ No newline at end of file diff --git a/tests/PHPUnit/Forms/GridFieldSortableRowsAutoSortTest.yml b/tests/PHPUnit/Forms/GridFieldSortableRowsAutoSortTest.yml index 826514c..849cdf6 100644 --- a/tests/PHPUnit/Forms/GridFieldSortableRowsAutoSortTest.yml +++ b/tests/PHPUnit/Forms/GridFieldSortableRowsAutoSortTest.yml @@ -1,4 +1,4 @@ -UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_Player: +UndefinedOffset\SortableGridField\Tests\Forms\AutoSortTest\Player: player1: Name: Player 1 SortOrder: 0 @@ -9,7 +9,7 @@ UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_Player: Name: Player 3 SortOrder: 0 -UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_VPlayer: +UndefinedOffset\SortableGridField\Tests\Forms\AutoSortTest\VPlayer: player1: Name: Player 1 SortOrder: 0 @@ -20,20 +20,20 @@ UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_VPlayer: Name: Player 3 SortOrder: 0 -UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_TestParent: +UndefinedOffset\SortableGridField\Tests\Forms\AutoSortTest\TestParent: testparent: Name: Test -UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_ChildObject: +UndefinedOffset\SortableGridField\Tests\Forms\AutoSortTest\ChildObject: testitem1: Name: Test 1 SortOrder: 0 - Parent: =>UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_TestParent.testparent + Parent: =>UndefinedOffset\SortableGridField\Tests\Forms\AutoSortTest\TestParent.testparent testitem2: Name: Test 2 SortOrder: 0 - Parent: =>UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_TestParent.testparent + Parent: =>UndefinedOffset\SortableGridField\Tests\Forms\AutoSortTest\TestParent.testparent testitem3: Name: Test 3 SortOrder: 0 - Parent: =>UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_TestParent.testparent \ No newline at end of file + Parent: =>UndefinedOffset\SortableGridField\Tests\Forms\AutoSortTest\TestParent.testparent diff --git a/tests/PHPUnit/Forms/GridFieldSortableRowsPageTest.php b/tests/PHPUnit/Forms/GridFieldSortableRowsPageTest.php index 8147e8b..15c26f6 100644 --- a/tests/PHPUnit/Forms/GridFieldSortableRowsPageTest.php +++ b/tests/PHPUnit/Forms/GridFieldSortableRowsPageTest.php @@ -1,25 +1,21 @@ list = GridFieldAction_PageSortOrder_Team::get(); + + $this->list = Team::get(); $config = GridFieldConfig_Base::create(5)->addComponent(new GridFieldSortableRows('SortOrder')); $this->gridField = new GridField('testfield', 'testfield', $this->list, $config); - $this->form = new Form(new SortableGridField_DummyController(), 'mockform', FieldList::create(array($this->gridField)), FieldList::create()); + $this->form = new Form(new DummyController(), 'mockform', FieldList::create([$this->gridField]), FieldList::create()); } public function testSortToNextPage() { $this->gridField->State->GridFieldPaginator->currentPage = 1; - $team3 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_PageSortOrder_Team', 'team3'); + $team3 = $this->objFromFixture(Team::class, 'team3'); $this->logInWithPermission('ADMIN'); $stateID = 'testGridStateActionField'; - $request = new HTTPRequest('POST', 'url', array('ItemID' => $team3->ID, 'Target' => 'nextpage'), array('action_gridFieldAlterAction?StateID=' . $stateID => true, $this->form->getSecurityToken()->getName() => $this->form->getSecurityToken()->getValue())); + $request = new HTTPRequest('POST', 'url', ['ItemID' => $team3->ID, 'Target' => 'nextpage'], ['action_gridFieldAlterAction?StateID=' . $stateID => true, $this->form->getSecurityToken()->getName() => $this->form->getSecurityToken()->getValue()]); $session = Controller::curr()->getRequest()->getSession(); $session->set($this->form->getSecurityToken()->getName(), $this->form->getSecurityToken()->getValue()); - $session->set($stateID, array('grid' => '', 'actionName' => 'sortToPage', 'args' => array('GridFieldSortableRows' => array('sortableToggle' => true), 'GridFieldPaginator' => array('currentPage' => 1)))); + $session->set($stateID, ['grid' => '', 'actionName' => 'sortToPage', 'args' => ['GridFieldSortableRows' => ['sortableToggle' => true], 'GridFieldPaginator' => ['currentPage' => 1]]]); $request->setSession($session); - $this->gridField->gridFieldAlterAction(array('StateID' => $stateID), $this->form, $request); + $this->gridField->gridFieldAlterAction(['StateID' => $stateID], $this->form, $request); - $team6 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_PageSortOrder_Team', 'team6'); + $team6 = $this->objFromFixture(Team::class, 'team6'); $this->assertEquals(5, $team6->SortOrder, 'Team 6 Should have moved to the bottom of the first page'); - $team3 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_PageSortOrder_Team', 'team3'); + $team3 = $this->objFromFixture(Team::class, 'team3'); $this->assertEquals(6, $team3->SortOrder, 'Team 3 Should have moved to the top of the second page'); } @@ -76,22 +73,22 @@ class GridFieldSortableRowsPageTest extends SapphireTest { $this->gridField->State->GridFieldPaginator->currentPage = 2; - $team7 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_PageSortOrder_Team', 'team7'); + $team7 = $this->objFromFixture(Team::class, 'team7'); $this->logInWithPermission('ADMIN'); $stateID = 'testGridStateActionField'; - $request = new HTTPRequest('POST', 'url', array('ItemID' => $team7->ID, 'Target' => 'previouspage'), array('action_gridFieldAlterAction?StateID=' . $stateID => true, $this->form->getSecurityToken()->getName() => $this->form->getSecurityToken()->getValue())); + $request = new HTTPRequest('POST', 'url', ['ItemID' => $team7->ID, 'Target' => 'previouspage'], ['action_gridFieldAlterAction?StateID=' . $stateID => true, $this->form->getSecurityToken()->getName() => $this->form->getSecurityToken()->getValue()]); $session = Controller::curr()->getRequest()->getSession(); $session->set($this->form->getSecurityToken()->getName(), $this->form->getSecurityToken()->getValue()); - $session->set($stateID, array('grid' => '', 'actionName' => 'sortToPage', 'args' => array('GridFieldSortableRows' => array('sortableToggle' => true), 'GridFieldPaginator' => array('currentPage' => 1)))); + $session->set($stateID, ['grid' => '', 'actionName' => 'sortToPage', 'args' => ['GridFieldSortableRows' => ['sortableToggle' => true], 'GridFieldPaginator' => ['currentPage' => 1]]]); $request->setSession($session); - $this->gridField->gridFieldAlterAction(array('StateID' => $stateID), $this->form, $request); + $this->gridField->gridFieldAlterAction(['StateID' => $stateID], $this->form, $request); - $team7 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_PageSortOrder_Team', 'team7'); + $team7 = $this->objFromFixture(Team::class, 'team7'); $this->assertEquals(5, $team7->SortOrder, 'Team 7 Should have moved to the bottom of the first page'); - $team5 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_PageSortOrder_Team', 'team5'); + $team5 = $this->objFromFixture(Team::class, 'team5'); $this->assertEquals(6, $team5->SortOrder, 'Team 5 Should have moved to the top of the second page'); } @@ -100,7 +97,7 @@ class GridFieldSortableRowsPageTest extends SapphireTest //Force versioned to reset Versioned::reset(); - $list = GridFieldAction_PageSortOrder_VTeam::get(); + $list = VTeam::get(); $this->gridField->setList($list); /** @var GridFieldSortableRows $sortableGrid */ @@ -114,26 +111,26 @@ class GridFieldSortableRowsPageTest extends SapphireTest } - $team3 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_PageSortOrder_VTeam', 'team3'); + $team3 = $this->objFromFixture(VTeam::class, 'team3'); $this->logInWithPermission('ADMIN'); $stateID = 'testGridStateActionField'; - $request = new HTTPRequest('POST', 'url', array('ItemID' => $team3->ID, 'Target' => 'nextpage'), array('action_gridFieldAlterAction?StateID=' . $stateID => true, $this->form->getSecurityToken()->getName() => $this->form->getSecurityToken()->getValue())); + $request = new HTTPRequest('POST', 'url', ['ItemID' => $team3->ID, 'Target' => 'nextpage'], ['action_gridFieldAlterAction?StateID=' . $stateID => true, $this->form->getSecurityToken()->getName() => $this->form->getSecurityToken()->getValue()]); $session = Controller::curr()->getRequest()->getSession(); $session->set($this->form->getSecurityToken()->getName(), $this->form->getSecurityToken()->getValue()); - $session->set($stateID, array('grid' => '', 'actionName' => 'sortToPage', 'args' => array('GridFieldSortableRows' => array('sortableToggle' => true), 'GridFieldPaginator' => array('currentPage' => 1)))); + $session->set($stateID, ['grid' => '', 'actionName' => 'sortToPage', 'args' => ['GridFieldSortableRows' => ['sortableToggle' => true], 'GridFieldPaginator' => ['currentPage' => 1]]]); $request->setSession($session); - $this->gridField->gridFieldAlterAction(array('StateID' => $stateID), $this->form, $request); + $this->gridField->gridFieldAlterAction(['StateID' => $stateID], $this->form, $request); - $team6 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_PageSortOrder_VTeam', 'team6'); + $team6 = $this->objFromFixture(VTeam::class, 'team6'); $this->assertEquals(5, $team6->SortOrder, 'Team 6 Should have moved to the bottom of the first page on Versioned stage "Stage"'); - $team3 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_PageSortOrder_VTeam', 'team3'); + $team3 = $this->objFromFixture(VTeam::class, 'team3'); $this->assertEquals(6, $team3->SortOrder, 'Team 3 Should have moved to the top of the second page on Versioned stage "Stage"'); - $list = Versioned::get_by_stage(GridFieldAction_PageSortOrder_VTeam::class, 'Live'); + $list = Versioned::get_by_stage(VTeam::class, 'Live'); $team6 = $list->byID($team6->ID); $this->assertEquals(5, $team6->SortOrder, 'Team 6 Should have moved to the bottom of the first page on Versioned stage "Live"'); @@ -147,7 +144,7 @@ class GridFieldSortableRowsPageTest extends SapphireTest //Force versioned to reset Versioned::reset(); - $list = GridFieldAction_PageSortOrder_VTeam::get(); + $list = VTeam::get(); $this->gridField->setList($list); /** @var GridFieldSortableRows $sortableGrid */ @@ -161,26 +158,26 @@ class GridFieldSortableRowsPageTest extends SapphireTest } - $team7 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_PageSortOrder_VTeam', 'team7'); + $team7 = $this->objFromFixture(VTeam::class, 'team7'); $this->logInWithPermission('ADMIN'); $stateID = 'testGridStateActionField'; - $request = new HTTPRequest('POST', 'url', array('ItemID' => $team7->ID, 'Target' => 'previouspage'), array('action_gridFieldAlterAction?StateID=' . $stateID => true, $this->form->getSecurityToken()->getName() => $this->form->getSecurityToken()->getValue())); + $request = new HTTPRequest('POST', 'url', ['ItemID' => $team7->ID, 'Target' => 'previouspage'], ['action_gridFieldAlterAction?StateID=' . $stateID => true, $this->form->getSecurityToken()->getName() => $this->form->getSecurityToken()->getValue()]); $session = Controller::curr()->getRequest()->getSession(); $session->set($this->form->getSecurityToken()->getName(), $this->form->getSecurityToken()->getValue()); - $session->set($stateID, array('grid' => '', 'actionName' => 'sortToPage', 'args' => array('GridFieldSortableRows' => array('sortableToggle' => true), 'GridFieldPaginator' => array('currentPage' => 1)))); + $session->set($stateID, ['grid' => '', 'actionName' => 'sortToPage', 'args' => ['GridFieldSortableRows' => ['sortableToggle' => true], 'GridFieldPaginator' => ['currentPage' => 1]]]); $request->setSession($session); - $this->gridField->gridFieldAlterAction(array('StateID' => $stateID), $this->form, $request); + $this->gridField->gridFieldAlterAction(['StateID' => $stateID], $this->form, $request); - $team7 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_PageSortOrder_VTeam', 'team7'); + $team7 = $this->objFromFixture(VTeam::class, 'team7'); $this->assertEquals(5, $team7->SortOrder, 'Team 7 Should have moved to the bottom of the first page on Versioned stage "Stage"'); - $team5 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_PageSortOrder_VTeam', 'team5'); + $team5 = $this->objFromFixture(VTeam::class, 'team5'); $this->assertEquals(6, $team5->SortOrder, 'Team 5 Should have moved to the top of the second page on Versioned stage "Stage"'); - $list = Versioned::get_by_stage(GridFieldAction_PageSortOrder_VTeam::class, 'Live'); + $list = Versioned::get_by_stage(VTeam::class, 'Live'); $team7 = $list->byID($team7->ID); $this->assertEquals(5, $team7->SortOrder, 'Team 7 Should have moved to the bottom of the first page on Versioned stage "Live"'); @@ -189,49 +186,3 @@ class GridFieldSortableRowsPageTest extends SapphireTest $this->assertEquals(6, $team5->SortOrder, 'Team 5 Should have moved to the top of the second page on Versioned stage "Live"'); } } - -/** - * Class GridFieldAction_PageSortOrder_Team - * - * @package SortableGridField\Tests - * @property string Name - * @property string City - * @property int SortOrder - */ -class GridFieldAction_PageSortOrder_Team extends DataObject implements TestOnly -{ - private static $table_name = 'GridFieldAction_PageSortOrder_Team'; - - private static $db = array( - 'Name' => DBVarchar::class, - 'City' => DBVarchar::class, - 'SortOrder' => DBInt::class - ); - - private static $default_sort = 'SortOrder'; -} - -/** - * Class GridFieldAction_PageSortOrder_VTeam - * - * @package SortableGridField\Tests - * @property string Name - * @property string City - * @property int SortOrder - */ -class GridFieldAction_PageSortOrder_VTeam extends DataObject implements TestOnly -{ - private static $table_name = 'GridFieldAction_PageSortOrder_VTeam'; - - private static $db = array( - 'Name' => DBVarchar::class, - 'City' => DBVarchar::class, - 'SortOrder' => DBInt::class - ); - - private static $default_sort = 'SortOrder'; - - private static $extensions = array( - "SilverStripe\\Versioned\\Versioned('Stage', 'Live')" - ); -} diff --git a/tests/PHPUnit/Forms/GridFieldSortableRowsPageTest.yml b/tests/PHPUnit/Forms/GridFieldSortableRowsPageTest.yml index 0f6ffac..52ba7ab 100644 --- a/tests/PHPUnit/Forms/GridFieldSortableRowsPageTest.yml +++ b/tests/PHPUnit/Forms/GridFieldSortableRowsPageTest.yml @@ -1,4 +1,4 @@ -UndefinedOffset\SortableGridField\Tests\GridFieldAction_PageSortOrder_Team: +UndefinedOffset\SortableGridField\Tests\Forms\RowsPageTest\Team: team1: Name: Team 1 City: Cologne @@ -36,7 +36,7 @@ UndefinedOffset\SortableGridField\Tests\GridFieldAction_PageSortOrder_Team: City: Auckland SortOrder: 9 -UndefinedOffset\SortableGridField\Tests\GridFieldAction_PageSortOrder_VTeam: +UndefinedOffset\SortableGridField\Tests\Forms\RowsPageTest\VTeam: team1: Name: Team 1 City: Cologne diff --git a/tests/PHPUnit/Forms/GridFieldSortableRowsTest.php b/tests/PHPUnit/Forms/GridFieldSortableRowsTest.php index 682c9b5..fbd4ada 100644 --- a/tests/PHPUnit/Forms/GridFieldSortableRowsTest.php +++ b/tests/PHPUnit/Forms/GridFieldSortableRowsTest.php @@ -1,22 +1,20 @@ list = GridFieldAction_SortOrder_Team::get(); + + $this->list = Team::get(); $config = GridFieldConfig::create()->addComponent(new GridFieldSortableRows('SortOrder')); $this->gridField = new GridField('testfield', 'testfield', $this->list, $config); - $this->form = new Form(new SortableGridField_DummyController(), 'mockform', new FieldList(array($this->gridField)), new FieldList()); + $this->form = new Form(new DummyController(), 'mockform', new FieldList([$this->gridField]), new FieldList()); } public function testSortActionWithoutCorrectPermission() @@ -52,34 +51,35 @@ class GridFieldSortableRowsTest extends SapphireTest if (Member::currentUser()) { Member::currentUser()->logOut(); } - $this->setExpectedException(ValidationException::class); - $team1 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_Team', 'team1'); - $team2 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_Team', 'team2'); - $team3 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_Team', 'team3'); + + $this->expectException(ValidationException::class); + $team1 = $this->objFromFixture(Team::class, 'team1'); + $team2 = $this->objFromFixture(Team::class, 'team2'); + $team3 = $this->objFromFixture(Team::class, 'team3'); $stateID = 'testGridStateActionField'; - $request = new HTTPRequest('POST', 'url', array('ItemIDs' => "$team1->ID, $team3->ID, $team2->ID"), array('action_gridFieldAlterAction?StateID=' . $stateID => true, $this->form->getSecurityToken()->getName() => $this->form->getSecurityToken()->getValue())); + $request = new HTTPRequest('POST', 'url', ['ItemIDs' => "$team1->ID, $team3->ID, $team2->ID"], ['action_gridFieldAlterAction?StateID=' . $stateID => true, $this->form->getSecurityToken()->getName() => $this->form->getSecurityToken()->getValue()]); $session = Controller::curr()->getRequest()->getSession(); $session->set($this->form->getSecurityToken()->getName(), $this->form->getSecurityToken()->getValue()); - $session->set($stateID, array('grid' => '', 'actionName' => 'saveGridRowSort', 'args' => array('GridFieldSortableRows' => array('sortableToggle' => true)))); + $session->set($stateID, ['grid' => '', 'actionName' => 'saveGridRowSort', 'args' => ['GridFieldSortableRows' => ['sortableToggle' => true]]]); $request->setSession($session); - $this->gridField->gridFieldAlterAction(array('StateID' => $stateID), $this->form, $request); + $this->gridField->gridFieldAlterAction(['StateID' => $stateID], $this->form, $request); $this->assertEquals($team3->ID, $this->list->last()->ID, 'User should\'t be able to sort records without correct permissions.'); } public function testSortActionWithAdminPermission() { - $team1 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_Team', 'team1'); - $team2 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_Team', 'team2'); - $team3 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_Team', 'team3'); + $team1 = $this->objFromFixture(Team::class, 'team1'); + $team2 = $this->objFromFixture(Team::class, 'team2'); + $team3 = $this->objFromFixture(Team::class, 'team3'); $this->logInWithPermission('ADMIN'); $stateID = 'testGridStateActionField'; - $request = new HTTPRequest('POST', 'url', array('ItemIDs' => "$team1->ID, $team3->ID, $team2->ID"), array('action_gridFieldAlterAction?StateID=' . $stateID => true, $this->form->getSecurityToken()->getName() => $this->form->getSecurityToken()->getValue())); + $request = new HTTPRequest('POST', 'url', ['ItemIDs' => "$team1->ID, $team3->ID, $team2->ID"], ['action_gridFieldAlterAction?StateID=' . $stateID => true, $this->form->getSecurityToken()->getName() => $this->form->getSecurityToken()->getValue()]); $session = Controller::curr()->getRequest()->getSession(); $session->set($this->form->getSecurityToken()->getName(), $this->form->getSecurityToken()->getValue()); - $session->set($stateID, array('grid' => '', 'actionName' => 'saveGridRowSort', 'args' => array('GridFieldSortableRows' => array('sortableToggle' => true)))); + $session->set($stateID, ['grid' => '', 'actionName' => 'saveGridRowSort', 'args' => ['GridFieldSortableRows' => ['sortableToggle' => true]]]); $request->setSession($session); - $this->gridField->gridFieldAlterAction(array('StateID' => $stateID), $this->form, $request); + $this->gridField->gridFieldAlterAction(['StateID' => $stateID], $this->form, $request); $this->assertEquals($team2->ID, $this->list->last()->ID, 'User should be able to sort records with ADMIN permission.'); } @@ -88,7 +88,7 @@ class GridFieldSortableRowsTest extends SapphireTest //Force versioned to reset Versioned::reset(); - $list = GridFieldAction_SortOrder_VTeam::get(); + $list = VTeam::get(); $this->gridField->setList($list); /** @var GridFieldSortableRows $sortableGrid */ @@ -100,67 +100,22 @@ class GridFieldSortableRowsTest extends SapphireTest $item->publish('Stage', 'Live'); } - $team1 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_VTeam', 'team1'); - $team2 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_VTeam', 'team2'); - $team3 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_VTeam', 'team3'); + $team1 = $this->objFromFixture(VTeam::class, 'team1'); + $team2 = $this->objFromFixture(VTeam::class, 'team2'); + $team3 = $this->objFromFixture(VTeam::class, 'team3'); $this->logInWithPermission('ADMIN'); $stateID = 'testGridStateActionField'; - $request = new HTTPRequest('POST', 'url', array('ItemIDs' => "$team1->ID, $team3->ID, $team2->ID"), array('action_gridFieldAlterAction?StateID=' . $stateID => true, $this->form->getSecurityToken()->getName() => $this->form->getSecurityToken()->getValue())); + $request = new HTTPRequest('POST', 'url', ['ItemIDs' => "$team1->ID, $team3->ID, $team2->ID"], ['action_gridFieldAlterAction?StateID=' . $stateID => true, $this->form->getSecurityToken()->getName() => $this->form->getSecurityToken()->getValue()]); $session = Controller::curr()->getRequest()->getSession(); $session->set($this->form->getSecurityToken()->getName(), $this->form->getSecurityToken()->getValue()); - $session->set($stateID, array('grid' => '', 'actionName' => 'saveGridRowSort', 'args' => array('GridFieldSortableRows' => array('sortableToggle' => true)))); + $session->set($stateID, ['grid' => '', 'actionName' => 'saveGridRowSort', 'args' => ['GridFieldSortableRows' => ['sortableToggle' => true]]]); $request->setSession($session); - $this->gridField->gridFieldAlterAction(array('StateID' => $stateID), $this->form, $request); + $this->gridField->gridFieldAlterAction(['StateID' => $stateID], $this->form, $request); $this->assertEquals($team2->ID, $list->last()->ID, 'Sort should have happened on Versioned stage "Stage"'); - $list = Versioned::get_by_stage(GridFieldAction_SortOrder_VTeam::class, 'Live'); + $list = Versioned::get_by_stage(VTeam::class, 'Live'); $this->assertEquals($team2->ID, $list->last()->ID, 'Sort should have happened on Versioned stage "Live"'); } } - -/** - * Class GridFieldAction_SortOrder_Team - * - * @package SortableGridField\Tests - * @property string Name - * @property string City - * @property int SortOrder - */ -class GridFieldAction_SortOrder_Team extends DataObject implements TestOnly -{ - private static $table_name = 'GridFieldAction_SortOrder_Team'; - - private static $db = array( - 'Name' => DBVarchar::class, - 'City' => DBVarchar::class, - 'SortOrder' => 'Int' - ); - - private static $default_sort = 'SortOrder'; -} - -/** - * Class GridFieldAction_SortOrder_VTeam - * - * @package SortableGridField\Tests - * @property string Name - * @property string City - * @property int SortOrder - */ -class GridFieldAction_SortOrder_VTeam extends DataObject implements TestOnly -{ - private static $table_name = 'GridFieldAction_SortOrder_VTeam'; - - private static $db = array( - 'Name' => DBVarchar::class, - 'City' => DBVarchar::class, - 'SortOrder' => DBInt::class - ); - private static $default_sort = 'SortOrder'; - - private static $extensions = array( - "SilverStripe\\Versioned\\Versioned('Stage', 'Live')" - ); -} diff --git a/tests/PHPUnit/Forms/GridFieldSortableRowsTest.yml b/tests/PHPUnit/Forms/GridFieldSortableRowsTest.yml index f719e47..0f976c2 100644 --- a/tests/PHPUnit/Forms/GridFieldSortableRowsTest.yml +++ b/tests/PHPUnit/Forms/GridFieldSortableRowsTest.yml @@ -1,4 +1,4 @@ -UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_Team: +UndefinedOffset\SortableGridField\Tests\Forms\RowsTest\Team: team1: Name: Team 1 City: Cologne @@ -12,7 +12,7 @@ UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_Team: City: Auckland SortOrder: 3 -UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_VTeam: +UndefinedOffset\SortableGridField\Tests\Forms\RowsTest\VTeam: team1: Name: Team 1 City: Cologne diff --git a/tests/PHPUnit/Forms/RowsPageTest/Team.php b/tests/PHPUnit/Forms/RowsPageTest/Team.php new file mode 100644 index 0000000..efda5a8 --- /dev/null +++ b/tests/PHPUnit/Forms/RowsPageTest/Team.php @@ -0,0 +1,25 @@ + 'Varchar', + 'City' => 'Varchar', + 'SortOrder' => 'Int', + ]; + + private static $default_sort = 'SortOrder'; +} diff --git a/tests/PHPUnit/Forms/RowsPageTest/VTeam.php b/tests/PHPUnit/Forms/RowsPageTest/VTeam.php new file mode 100644 index 0000000..989c6bc --- /dev/null +++ b/tests/PHPUnit/Forms/RowsPageTest/VTeam.php @@ -0,0 +1,30 @@ + 'Varchar', + 'City' => 'Varchar', + 'SortOrder' => 'Int', + ]; + + private static $default_sort = 'SortOrder'; + + private static $extensions = [ + Versioned::class, + ]; +} diff --git a/tests/PHPUnit/Forms/RowsTest/Team.php b/tests/PHPUnit/Forms/RowsTest/Team.php new file mode 100644 index 0000000..3a0a95a --- /dev/null +++ b/tests/PHPUnit/Forms/RowsTest/Team.php @@ -0,0 +1,25 @@ + 'Varchar', + 'City' => 'Varchar', + 'SortOrder' => 'Int', + ]; + + private static $default_sort = 'SortOrder'; +} diff --git a/tests/PHPUnit/Forms/RowsTest/VTeam.php b/tests/PHPUnit/Forms/RowsTest/VTeam.php new file mode 100644 index 0000000..0b8d5e2 --- /dev/null +++ b/tests/PHPUnit/Forms/RowsTest/VTeam.php @@ -0,0 +1,29 @@ + 'Varchar', + 'City' => 'Varchar', + 'SortOrder' => 'Int', + ]; + private static $default_sort = 'SortOrder'; + + private static $extensions = [ + Versioned::class, + ]; +}