Split unit test classes into separate files

This commit is contained in:
UndefinedOffset 2022-02-16 16:15:33 -04:00
parent 5f37c8a0b4
commit 8ed1621f8b
No known key found for this signature in database
GPG Key ID: 59C4EE2B6468B796
18 changed files with 373 additions and 336 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@
/.project /.project
/.settings /.settings
/.settings/* /.settings/*
.phpunit.result.cache

View File

@ -1,5 +1,4 @@
<?php <?php
namespace UndefinedOffset\SortableGridField\Forms; namespace UndefinedOffset\SortableGridField\Forms;
use SilverStripe\Admin\ModelAdmin; use SilverStripe\Admin\ModelAdmin;
@ -30,8 +29,6 @@ use SilverStripe\View\Requirements;
/** /**
* This component provides a checkbox which when checked enables drag-and-drop re-ordering of elements displayed in a {@link GridField} * This component provides a checkbox which when checked enables drag-and-drop re-ordering of elements displayed in a {@link GridField}
*
* @package forms
*/ */
class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionProvider, GridField_DataManipulator class GridFieldSortableRows implements GridField_HTMLProvider, GridField_ActionProvider, GridField_DataManipulator
{ {

View File

@ -0,0 +1,19 @@
<?php
namespace UndefinedOffset\SortableGridField\Tests\Forms\AutoSortTest;
use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\DataObject;
/**
* Class \UndefinedOffset\SortableGridField\Tests\Forms\AutoSortTest\BaseObject
*
* @property string Name
*/
class BaseObject extends DataObject implements TestOnly
{
private static $table_name = 'GridFieldAction_SortOrder_BaseObject';
private static $db = [
'Name' => 'Varchar',
];
}

View File

@ -0,0 +1,27 @@
<?php
namespace UndefinedOffset\SortableGridField\Tests\Forms\AutoSortTest;
use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\FieldType\DBInt;
/**
* Class \UndefinedOffset\SortableGridField\Tests\Forms\AutoSortTest\ChildObject
*
* @package SortableGridField\Tests
* @property int SortOrder
* @method TestParent Parent
*/
class ChildObject extends BaseObject implements TestOnly
{
private static $table_name = 'GridFieldAction_SortOrder_ChildObject';
private static $db = [
'SortOrder' => DBInt::class,
];
private static $has_one = [
'Parent' => TestParent::class,
];
private static $default_sort = 'SortOrder';
}

View File

@ -0,0 +1,12 @@
<?php
namespace UndefinedOffset\SortableGridField\Tests\Forms\AutoSortTest;
use SilverStripe\Control\Controller;
/**
* Class \UndefinedOffset\SortableGridField\Tests\Forms\AutoSortTest\DummyController
*/
class DummyController extends Controller
{
private static $url_segment = 'sortable-grid-field';
}

View File

@ -0,0 +1,23 @@
<?php
namespace UndefinedOffset\SortableGridField\Tests\Forms\AutoSortTest;
use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\DataObject;
/**
* Class \UndefinedOffset\SortableGridField\Tests\Forms\AutoSortTest\Player
*
* @property string Name
* @property int SortOrder
*/
class Player extends DataObject implements TestOnly
{
private static $table_name = 'GridFieldAction_SortOrder_Player';
private static $db = [
'Name' => 'Varchar',
'SortOrder' => 'Int',
];
private static $default_sort = 'SortOrder';
}

View File

@ -0,0 +1,25 @@
<?php
namespace UndefinedOffset\SortableGridField\Tests\Forms\AutoSortTest;
use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\DataObject;
/**
* Class \UndefinedOffset\SortableGridField\Tests\Forms\AutoSortTest\TestParent
*
* @package SortableGridField\Tests
* @property string Name
* @method ChildObject TestRelation
*/
class TestParent extends DataObject implements TestOnly
{
private static $table_name = 'GridFieldAction_SortOrder_TestParent';
private static $db = [
'Name' => 'Varchar',
];
private static $has_many = [
'TestRelation' => ChildObject::class,
];
}

View File

@ -0,0 +1,28 @@
<?php
namespace UndefinedOffset\SortableGridField\Tests\Forms\AutoSortTest;
use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\DataObject;
use SilverStripe\Versioned\Versioned;
/**
* Class \UndefinedOffset\SortableGridField\Tests\Forms\AutoSortTest\VPlayer
*
* @property string Name
* @property int SortOrder
*/
class VPlayer extends DataObject implements TestOnly
{
private static $table_name = 'GridFieldAction_SortOrder_VPlayer';
private static $db = [
'Name' => 'Varchar',
'SortOrder' => 'Int',
];
private static $default_sort = 'SortOrder';
private static $extensions = [
Versioned::class,
];
}

View File

@ -1,27 +1,26 @@
<?php <?php
namespace UndefinedOffset\SortableGridField\Tests\Forms;
namespace UndefinedOffset\SortableGridField\Tests;
use SilverStripe\Control\Controller; use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\HTTPRequest;
use SilverStripe\Dev\SapphireTest; use SilverStripe\Dev\SapphireTest;
use SilverStripe\Dev\TestOnly;
use SilverStripe\Forms\FieldList; use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form; use SilverStripe\Forms\Form;
use SilverStripe\Forms\GridField\GridField; use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldConfig; use SilverStripe\Forms\GridField\GridFieldConfig;
use SilverStripe\ORM\DB; use SilverStripe\ORM\DB;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\FieldType\DBInt;
use SilverStripe\ORM\FieldType\DBVarchar;
use SilverStripe\Security\Security; use SilverStripe\Security\Security;
use SilverStripe\Versioned\Versioned; use SilverStripe\Versioned\Versioned;
use UndefinedOffset\SortableGridField\Forms\GridFieldSortableRows; use UndefinedOffset\SortableGridField\Forms\GridFieldSortableRows;
use UndefinedOffset\SortableGridField\Tests\Forms\AutoSortTest\BaseObject;
use UndefinedOffset\SortableGridField\Tests\Forms\AutoSortTest\ChildObject;
use UndefinedOffset\SortableGridField\Tests\Forms\AutoSortTest\DummyController;
use UndefinedOffset\SortableGridField\Tests\Forms\AutoSortTest\Player;
use UndefinedOffset\SortableGridField\Tests\Forms\AutoSortTest\TestParent;
use UndefinedOffset\SortableGridField\Tests\Forms\AutoSortTest\VPlayer;
/** /**
* Class GridFieldSortableRowsAutoSortTest * Class \UndefinedOffset\SortableGridField\Tests\GridFieldSortableRowsAutoSortTest
*
* @package SortableGridField\Tests
*/ */
class GridFieldSortableRowsAutoSortTest extends SapphireTest class GridFieldSortableRowsAutoSortTest extends SapphireTest
{ {
@ -29,13 +28,13 @@ class GridFieldSortableRowsAutoSortTest extends SapphireTest
public static $fixture_file = 'GridFieldSortableRowsAutoSortTest.yml'; public static $fixture_file = 'GridFieldSortableRowsAutoSortTest.yml';
/** @var array */ /** @var array */
protected static $extra_dataobjects = array( protected static $extra_dataobjects = [
GridFieldAction_SortOrder_Player::class, Player::class,
GridFieldAction_SortOrder_VPlayer::class, VPlayer::class,
GridFieldAction_SortOrder_TestParent::class, TestParent::class,
GridFieldAction_SortOrder_BaseObject::class, BaseObject::class,
GridFieldAction_SortOrder_ChildObject::class ChildObject::class
); ];
public function testAutoSort() public function testAutoSort()
{ {
@ -43,19 +42,19 @@ class GridFieldSortableRowsAutoSortTest extends SapphireTest
$this->logOut(); $this->logOut();
} }
$list = GridFieldAction_SortOrder_Player::get(); $list = Player::get();
$config = GridFieldConfig::create()->addComponent(new GridFieldSortableRows('SortOrder')); $config = GridFieldConfig::create()->addComponent(new GridFieldSortableRows('SortOrder'));
$gridField = new GridField('testfield', 'testfield', $list, $config); $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'; $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 = Controller::curr()->getRequest()->getSession();
$session->set($form->getSecurityToken()->getName(), $form->getSecurityToken()->getValue()); $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); $request->setSession($session);
$gridField->gridFieldAlterAction(array('StateID' => $stateID), $form, $request); $gridField->gridFieldAlterAction(['StateID' => $stateID], $form, $request);
//Insure sort ran //Insure sort ran
$this->assertEquals(3, $list->last()->SortOrder, 'Auto sort should have run'); $this->assertEquals(3, $list->last()->SortOrder, 'Auto sort should have run');
@ -73,10 +72,10 @@ class GridFieldSortableRowsAutoSortTest extends SapphireTest
$this->logOut(); $this->logOut();
} }
$list = GridFieldAction_SortOrder_Player::get(); $list = Player::get();
$config = GridFieldConfig::create()->addComponent(new GridFieldSortableRows('SortOrder')); $config = GridFieldConfig::create()->addComponent(new GridFieldSortableRows('SortOrder'));
$gridField = new GridField('testfield', 'testfield', $list, $config); $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 */ /** @var GridFieldSortableRows $sortableRows */
$sortableRows = $gridField->getConfig()->getComponentByType(GridFieldSortableRows::class); $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'); $this->assertEquals(0, $list->last()->SortOrder, 'Auto sort should not have run');
$stateID = 'testGridStateActionField'; $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 = Controller::curr()->getRequest()->getSession();
$session->set($form->getSecurityToken()->getName(), $form->getSecurityToken()->getValue()); $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); $request->setSession($session);
$gridField->gridFieldAlterAction(array('StateID' => $stateID), $form, $request); $gridField->gridFieldAlterAction(['StateID' => $stateID], $form, $request);
//Insure sort ran //Insure sort ran
$this->assertEquals(3, $list->last()->SortOrder, 'Auto sort should have run'); $this->assertEquals(3, $list->last()->SortOrder, 'Auto sort should have run');
@ -111,7 +110,7 @@ class GridFieldSortableRowsAutoSortTest extends SapphireTest
//Force versioned to reset //Force versioned to reset
Versioned::reset(); Versioned::reset();
$list = GridFieldAction_SortOrder_VPlayer::get(); $list = VPlayer::get();
//Publish all records //Publish all records
foreach ($list as $item) { foreach ($list as $item) {
@ -121,15 +120,15 @@ class GridFieldSortableRowsAutoSortTest extends SapphireTest
$config = GridFieldConfig::create()->addComponent(new GridFieldSortableRows('SortOrder', true, 'Live')); $config = GridFieldConfig::create()->addComponent(new GridFieldSortableRows('SortOrder', true, 'Live'));
$gridField = new GridField('testfield', 'testfield', $list, $config); $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'; $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 = Controller::curr()->getRequest()->getSession();
$session->set($form->getSecurityToken()->getName(), $form->getSecurityToken()->getValue()); $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); $request->setSession($session);
$gridField->gridFieldAlterAction(array('StateID' => $stateID), $form, $request); $gridField->gridFieldAlterAction(['StateID' => $stateID], $form, $request);
//Insure sort ran //Insure sort ran
@ -146,14 +145,14 @@ class GridFieldSortableRowsAutoSortTest extends SapphireTest
Versioned::set_reading_mode('Live'); Versioned::set_reading_mode('Live');
//Get live instance //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 //Insure sort ran
$this->assertEquals(3, $obj->SortOrder, 'Auto sort should have run on Versioned stage "Live"'); $this->assertEquals(3, $obj->SortOrder, 'Auto sort should have run on Versioned stage "Live"');
//Check for duplicates (there shouldn't be any) //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(); $count = $list->Count();
$indexes = count(array_unique($list->column('SortOrder'))); $indexes = count(array_unique($list->column('SortOrder')));
$this->assertEquals(0, $count - $indexes, 'Duplicate indexes detected on Versioned stage "Live"'); $this->assertEquals(0, $count - $indexes, 'Duplicate indexes detected on Versioned stage "Live"');
@ -168,7 +167,7 @@ class GridFieldSortableRowsAutoSortTest extends SapphireTest
//Force versioned to reset //Force versioned to reset
Versioned::reset(); Versioned::reset();
$list = GridFieldAction_SortOrder_VPlayer::get(); $list = VPlayer::get();
//Publish all records //Publish all records
foreach ($list as $item) { foreach ($list as $item) {
@ -178,7 +177,7 @@ class GridFieldSortableRowsAutoSortTest extends SapphireTest
$config = GridFieldConfig::create()->addComponent(new GridFieldSortableRows('SortOrder', true, 'Live')); $config = GridFieldConfig::create()->addComponent(new GridFieldSortableRows('SortOrder', true, 'Live'));
$gridField = new GridField('testfield', 'testfield', $list, $config); $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 */ /** @var GridFieldSortableRows $sortableRows */
$sortableRows = $gridField->getConfig()->getComponentByType(GridFieldSortableRows::class); $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"'); $this->assertEquals(0, $list->last()->SortOrder, 'Auto sort should not have run on Versioned stage "Stage"');
$stateID = 'testGridStateActionField'; $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 = Controller::curr()->getRequest()->getSession();
$session->set($form->getSecurityToken()->getName(), $form->getSecurityToken()->getValue()); $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); $request->setSession($session);
$gridField->gridFieldAlterAction(array('StateID' => $stateID), $form, $request); $gridField->gridFieldAlterAction(['StateID' => $stateID], $form, $request);
//Insure sort ran //Insure sort ran
@ -227,15 +226,15 @@ class GridFieldSortableRowsAutoSortTest extends SapphireTest
//Push the edit date into the past, we're checking this later //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')) . '\''); DB::query('UPDATE "GridFieldAction_SortOrder_BaseObject" SET "LastEdited"=\'' . date('Y-m-d 00:00:00', strtotime('yesterday')) . '\'');
/** @var GridFieldAction_SortOrder_TestParent $parent */ /** @var TestParent $parent */
$parent = GridFieldAction_SortOrder_TestParent::get()->first(); $parent = TestParent::get()->first();
/** @var DataList $list */ /** @var DataList $list */
$list = $parent->TestRelation(); $list = $parent->TestRelation();
$config = GridFieldConfig::create()->addComponent(new GridFieldSortableRows('SortOrder')); $config = GridFieldConfig::create()->addComponent(new GridFieldSortableRows('SortOrder'));
$gridField = new GridField('testfield', 'testfield', $list, $config); $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); $form->loadDataFrom($parent);
/** @var GridFieldSortableRows $sortableRows */ /** @var GridFieldSortableRows $sortableRows */
@ -245,12 +244,12 @@ class GridFieldSortableRowsAutoSortTest extends SapphireTest
$this->assertEquals(0, $list->last()->SortOrder, 'Auto sort should not have run'); $this->assertEquals(0, $list->last()->SortOrder, 'Auto sort should not have run');
$stateID = 'testGridStateActionField'; $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 = Controller::curr()->getRequest()->getSession();
$session->set($form->getSecurityToken()->getName(), $form->getSecurityToken()->getValue()); $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); $request->setSession($session);
$gridField->gridFieldAlterAction(array('StateID' => $stateID), $form, $request); $gridField->gridFieldAlterAction(['StateID' => $stateID], $form, $request);
//Insure sort ran //Insure sort ran
$this->assertEquals(3, $list->last()->SortOrder, 'Auto sort should have run'); $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()); $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';
}

View File

@ -1,4 +1,4 @@
UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_Player: UndefinedOffset\SortableGridField\Tests\Forms\AutoSortTest\Player:
player1: player1:
Name: Player 1 Name: Player 1
SortOrder: 0 SortOrder: 0
@ -9,7 +9,7 @@ UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_Player:
Name: Player 3 Name: Player 3
SortOrder: 0 SortOrder: 0
UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_VPlayer: UndefinedOffset\SortableGridField\Tests\Forms\AutoSortTest\VPlayer:
player1: player1:
Name: Player 1 Name: Player 1
SortOrder: 0 SortOrder: 0
@ -20,20 +20,20 @@ UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_VPlayer:
Name: Player 3 Name: Player 3
SortOrder: 0 SortOrder: 0
UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_TestParent: UndefinedOffset\SortableGridField\Tests\Forms\AutoSortTest\TestParent:
testparent: testparent:
Name: Test Name: Test
UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_ChildObject: UndefinedOffset\SortableGridField\Tests\Forms\AutoSortTest\ChildObject:
testitem1: testitem1:
Name: Test 1 Name: Test 1
SortOrder: 0 SortOrder: 0
Parent: =>UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_TestParent.testparent Parent: =>UndefinedOffset\SortableGridField\Tests\Forms\AutoSortTest\TestParent.testparent
testitem2: testitem2:
Name: Test 2 Name: Test 2
SortOrder: 0 SortOrder: 0
Parent: =>UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_TestParent.testparent Parent: =>UndefinedOffset\SortableGridField\Tests\Forms\AutoSortTest\TestParent.testparent
testitem3: testitem3:
Name: Test 3 Name: Test 3
SortOrder: 0 SortOrder: 0
Parent: =>UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_TestParent.testparent Parent: =>UndefinedOffset\SortableGridField\Tests\Forms\AutoSortTest\TestParent.testparent

View File

@ -1,25 +1,21 @@
<?php <?php
namespace UndefinedOffset\SortableGridField\Tests; namespace UndefinedOffset\SortableGridField\Tests;
use SilverStripe\Control\Controller; use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\HTTPRequest;
use SilverStripe\Dev\SapphireTest; use SilverStripe\Dev\SapphireTest;
use SilverStripe\Dev\TestOnly;
use SilverStripe\Forms\FieldList; use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form; use SilverStripe\Forms\Form;
use SilverStripe\Forms\GridField\GridField; use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldConfig_Base; use SilverStripe\Forms\GridField\GridFieldConfig_Base;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\FieldType\DBInt;
use SilverStripe\ORM\FieldType\DBVarchar;
use SilverStripe\Versioned\Versioned; use SilverStripe\Versioned\Versioned;
use UndefinedOffset\SortableGridField\Forms\GridFieldSortableRows; use UndefinedOffset\SortableGridField\Forms\GridFieldSortableRows;
use UndefinedOffset\SortableGridField\Tests\Forms\AutoSortTest\DummyController;
use UndefinedOffset\SortableGridField\Tests\Forms\RowsPageTest\Team;
use UndefinedOffset\SortableGridField\Tests\Forms\RowsPageTest\VTeam;
/** /**
* Class GridFieldSortableRowsPageTest * Class \UndefinedOffset\SortableGridField\Tests\GridFieldSortableRowsPageTest
*
* @package SortableGridField\Tests
*/ */
class GridFieldSortableRowsPageTest extends SapphireTest class GridFieldSortableRowsPageTest extends SapphireTest
{ {
@ -36,39 +32,40 @@ class GridFieldSortableRowsPageTest extends SapphireTest
public static $fixture_file = 'GridFieldSortableRowsPageTest.yml'; public static $fixture_file = 'GridFieldSortableRowsPageTest.yml';
/** @var array */ /** @var array */
protected static $extra_dataobjects = array( protected static $extra_dataobjects = [
GridFieldAction_PageSortOrder_Team::class, Team::class,
GridFieldAction_PageSortOrder_VTeam::class VTeam::class
); ];
public function setUp() protected function setUp(): void
{ {
parent::setUp(); parent::setUp();
$this->list = GridFieldAction_PageSortOrder_Team::get();
$this->list = Team::get();
$config = GridFieldConfig_Base::create(5)->addComponent(new GridFieldSortableRows('SortOrder')); $config = GridFieldConfig_Base::create(5)->addComponent(new GridFieldSortableRows('SortOrder'));
$this->gridField = new GridField('testfield', 'testfield', $this->list, $config); $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() public function testSortToNextPage()
{ {
$this->gridField->State->GridFieldPaginator->currentPage = 1; $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'); $this->logInWithPermission('ADMIN');
$stateID = 'testGridStateActionField'; $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 = Controller::curr()->getRequest()->getSession();
$session->set($this->form->getSecurityToken()->getName(), $this->form->getSecurityToken()->getValue()); $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); $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'); $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'); $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; $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'); $this->logInWithPermission('ADMIN');
$stateID = 'testGridStateActionField'; $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 = Controller::curr()->getRequest()->getSession();
$session->set($this->form->getSecurityToken()->getName(), $this->form->getSecurityToken()->getValue()); $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); $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'); $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'); $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 //Force versioned to reset
Versioned::reset(); Versioned::reset();
$list = GridFieldAction_PageSortOrder_VTeam::get(); $list = VTeam::get();
$this->gridField->setList($list); $this->gridField->setList($list);
/** @var GridFieldSortableRows $sortableGrid */ /** @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'); $this->logInWithPermission('ADMIN');
$stateID = 'testGridStateActionField'; $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 = Controller::curr()->getRequest()->getSession();
$session->set($this->form->getSecurityToken()->getName(), $this->form->getSecurityToken()->getValue()); $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); $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"'); $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"'); $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); $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"'); $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 //Force versioned to reset
Versioned::reset(); Versioned::reset();
$list = GridFieldAction_PageSortOrder_VTeam::get(); $list = VTeam::get();
$this->gridField->setList($list); $this->gridField->setList($list);
/** @var GridFieldSortableRows $sortableGrid */ /** @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'); $this->logInWithPermission('ADMIN');
$stateID = 'testGridStateActionField'; $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 = Controller::curr()->getRequest()->getSession();
$session->set($this->form->getSecurityToken()->getName(), $this->form->getSecurityToken()->getValue()); $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); $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"'); $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"'); $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); $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"'); $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"'); $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')"
);
}

View File

@ -1,4 +1,4 @@
UndefinedOffset\SortableGridField\Tests\GridFieldAction_PageSortOrder_Team: UndefinedOffset\SortableGridField\Tests\Forms\RowsPageTest\Team:
team1: team1:
Name: Team 1 Name: Team 1
City: Cologne City: Cologne
@ -36,7 +36,7 @@ UndefinedOffset\SortableGridField\Tests\GridFieldAction_PageSortOrder_Team:
City: Auckland City: Auckland
SortOrder: 9 SortOrder: 9
UndefinedOffset\SortableGridField\Tests\GridFieldAction_PageSortOrder_VTeam: UndefinedOffset\SortableGridField\Tests\Forms\RowsPageTest\VTeam:
team1: team1:
Name: Team 1 Name: Team 1
City: Cologne City: Cologne

View File

@ -1,22 +1,20 @@
<?php <?php
namespace UndefinedOffset\SortableGridField\Tests; namespace UndefinedOffset\SortableGridField\Tests;
use SilverStripe\Control\Controller; use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\HTTPRequest;
use SilverStripe\Dev\SapphireTest; use SilverStripe\Dev\SapphireTest;
use SilverStripe\Dev\TestOnly;
use SilverStripe\Forms\FieldList; use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form; use SilverStripe\Forms\Form;
use SilverStripe\Forms\GridField\GridField; use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldConfig; use SilverStripe\Forms\GridField\GridFieldConfig;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\ValidationException; use SilverStripe\ORM\ValidationException;
use SilverStripe\ORM\FieldType\DBInt;
use SilverStripe\ORM\FieldType\DBVarchar;
use SilverStripe\Security\Member; use SilverStripe\Security\Member;
use SilverStripe\Versioned\Versioned; use SilverStripe\Versioned\Versioned;
use UndefinedOffset\SortableGridField\Forms\GridFieldSortableRows; use UndefinedOffset\SortableGridField\Forms\GridFieldSortableRows;
use UndefinedOffset\SortableGridField\Tests\Forms\AutoSortTest\DummyController;
use UndefinedOffset\SortableGridField\Tests\Forms\RowsTest\Team;
use UndefinedOffset\SortableGridField\Tests\Forms\RowsTest\VTeam;
class GridFieldSortableRowsTest extends SapphireTest class GridFieldSortableRowsTest extends SapphireTest
{ {
@ -33,18 +31,19 @@ class GridFieldSortableRowsTest extends SapphireTest
public static $fixture_file = 'GridFieldSortableRowsTest.yml'; public static $fixture_file = 'GridFieldSortableRowsTest.yml';
/** @var array */ /** @var array */
protected static $extra_dataobjects = array( protected static $extra_dataobjects = [
GridFieldAction_SortOrder_Team::class, Team::class,
GridFieldAction_SortOrder_VTeam::class VTeam::class
); ];
public function setUp() protected function setUp(): void
{ {
parent::setUp(); parent::setUp();
$this->list = GridFieldAction_SortOrder_Team::get();
$this->list = Team::get();
$config = GridFieldConfig::create()->addComponent(new GridFieldSortableRows('SortOrder')); $config = GridFieldConfig::create()->addComponent(new GridFieldSortableRows('SortOrder'));
$this->gridField = new GridField('testfield', 'testfield', $this->list, $config); $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() public function testSortActionWithoutCorrectPermission()
@ -52,34 +51,35 @@ class GridFieldSortableRowsTest extends SapphireTest
if (Member::currentUser()) { if (Member::currentUser()) {
Member::currentUser()->logOut(); Member::currentUser()->logOut();
} }
$this->setExpectedException(ValidationException::class);
$team1 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_Team', 'team1'); $this->expectException(ValidationException::class);
$team2 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_Team', 'team2'); $team1 = $this->objFromFixture(Team::class, 'team1');
$team3 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_Team', 'team3'); $team2 = $this->objFromFixture(Team::class, 'team2');
$team3 = $this->objFromFixture(Team::class, 'team3');
$stateID = 'testGridStateActionField'; $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 = Controller::curr()->getRequest()->getSession();
$session->set($this->form->getSecurityToken()->getName(), $this->form->getSecurityToken()->getValue()); $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); $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.'); $this->assertEquals($team3->ID, $this->list->last()->ID, 'User should\'t be able to sort records without correct permissions.');
} }
public function testSortActionWithAdminPermission() public function testSortActionWithAdminPermission()
{ {
$team1 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_Team', 'team1'); $team1 = $this->objFromFixture(Team::class, 'team1');
$team2 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_Team', 'team2'); $team2 = $this->objFromFixture(Team::class, 'team2');
$team3 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_Team', 'team3'); $team3 = $this->objFromFixture(Team::class, 'team3');
$this->logInWithPermission('ADMIN'); $this->logInWithPermission('ADMIN');
$stateID = 'testGridStateActionField'; $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 = Controller::curr()->getRequest()->getSession();
$session->set($this->form->getSecurityToken()->getName(), $this->form->getSecurityToken()->getValue()); $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); $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.'); $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 //Force versioned to reset
Versioned::reset(); Versioned::reset();
$list = GridFieldAction_SortOrder_VTeam::get(); $list = VTeam::get();
$this->gridField->setList($list); $this->gridField->setList($list);
/** @var GridFieldSortableRows $sortableGrid */ /** @var GridFieldSortableRows $sortableGrid */
@ -100,67 +100,22 @@ class GridFieldSortableRowsTest extends SapphireTest
$item->publish('Stage', 'Live'); $item->publish('Stage', 'Live');
} }
$team1 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_VTeam', 'team1'); $team1 = $this->objFromFixture(VTeam::class, 'team1');
$team2 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_VTeam', 'team2'); $team2 = $this->objFromFixture(VTeam::class, 'team2');
$team3 = $this->objFromFixture('UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_VTeam', 'team3'); $team3 = $this->objFromFixture(VTeam::class, 'team3');
$this->logInWithPermission('ADMIN'); $this->logInWithPermission('ADMIN');
$stateID = 'testGridStateActionField'; $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 = Controller::curr()->getRequest()->getSession();
$session->set($this->form->getSecurityToken()->getName(), $this->form->getSecurityToken()->getValue()); $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); $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"'); $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"'); $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')"
);
}

View File

@ -1,4 +1,4 @@
UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_Team: UndefinedOffset\SortableGridField\Tests\Forms\RowsTest\Team:
team1: team1:
Name: Team 1 Name: Team 1
City: Cologne City: Cologne
@ -12,7 +12,7 @@ UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_Team:
City: Auckland City: Auckland
SortOrder: 3 SortOrder: 3
UndefinedOffset\SortableGridField\Tests\GridFieldAction_SortOrder_VTeam: UndefinedOffset\SortableGridField\Tests\Forms\RowsTest\VTeam:
team1: team1:
Name: Team 1 Name: Team 1
City: Cologne City: Cologne

View File

@ -0,0 +1,25 @@
<?php
namespace UndefinedOffset\SortableGridField\Tests\Forms\RowsPageTest;
use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\DataObject;
/**
* Class \UndefinedOffset\SortableGridField\Tests\Forms\RowsPageTest\Team
*
* @property string Name
* @property string City
* @property int SortOrder
*/
class Team extends DataObject implements TestOnly
{
private static $table_name = 'GridFieldAction_PageSortOrder_Team';
private static $db = [
'Name' => 'Varchar',
'City' => 'Varchar',
'SortOrder' => 'Int',
];
private static $default_sort = 'SortOrder';
}

View File

@ -0,0 +1,30 @@
<?php
namespace UndefinedOffset\SortableGridField\Tests\Forms\RowsPageTest;
use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\DataObject;
use SilverStripe\Versioned\Versioned;
/**
* Class \UndefinedOffset\SortableGridField\Tests\Forms\RowsPageTest\VTeam
*
* @property string Name
* @property string City
* @property int SortOrder
*/
class VTeam extends DataObject implements TestOnly
{
private static $table_name = 'GridFieldAction_PageSortOrder_VTeam';
private static $db = [
'Name' => 'Varchar',
'City' => 'Varchar',
'SortOrder' => 'Int',
];
private static $default_sort = 'SortOrder';
private static $extensions = [
Versioned::class,
];
}

View File

@ -0,0 +1,25 @@
<?php
namespace UndefinedOffset\SortableGridField\Tests\Forms\RowsTest;
use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\DataObject;
/**
* Class \UndefinedOffset\SortableGridField\Tests\Forms\RowsTest\Team
*
* @property string Name
* @property string City
* @property int SortOrder
*/
class Team extends DataObject implements TestOnly
{
private static $table_name = 'GridFieldAction_SortOrder_Team';
private static $db = [
'Name' => 'Varchar',
'City' => 'Varchar',
'SortOrder' => 'Int',
];
private static $default_sort = 'SortOrder';
}

View File

@ -0,0 +1,29 @@
<?php
namespace UndefinedOffset\SortableGridField\Tests\Forms\RowsTest;
use SilverStripe\Dev\TestOnly;
use SilverStripe\ORM\DataObject;
use SilverStripe\Versioned\Versioned;
/**
* Class \UndefinedOffset\SortableGridField\Tests\Forms\RowsTest\VTeam
*
* @property string Name
* @property string City
* @property int SortOrder
*/
class VTeam extends DataObject implements TestOnly
{
private static $table_name = 'GridFieldAction_SortOrder_VTeam';
private static $db = [
'Name' => 'Varchar',
'City' => 'Varchar',
'SortOrder' => 'Int',
];
private static $default_sort = 'SortOrder';
private static $extensions = [
Versioned::class,
];
}