2012-09-06 17:27:23 +02:00
|
|
|
<?php
|
2022-02-16 21:15:33 +01:00
|
|
|
namespace UndefinedOffset\SortableGridField\Tests\Forms;
|
2017-05-27 06:45:28 +02:00
|
|
|
|
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\Control\HTTPRequest;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Forms\Form;
|
|
|
|
use SilverStripe\Forms\GridField\GridField;
|
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig;
|
|
|
|
use SilverStripe\ORM\DB;
|
2020-05-29 15:44:04 +02:00
|
|
|
use SilverStripe\Security\Security;
|
2017-05-27 06:45:28 +02:00
|
|
|
use SilverStripe\Versioned\Versioned;
|
|
|
|
use UndefinedOffset\SortableGridField\Forms\GridFieldSortableRows;
|
2022-02-16 21:15:33 +01:00
|
|
|
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;
|
2017-05-27 06:45:28 +02:00
|
|
|
|
2022-02-16 21:23:08 +01:00
|
|
|
class AutoSortTest extends SapphireTest
|
2017-05-27 06:45:28 +02:00
|
|
|
{
|
|
|
|
/** @var string */
|
2022-02-16 21:23:08 +01:00
|
|
|
public static $fixture_file = 'AutoSortTest.yml';
|
2017-05-27 06:45:28 +02:00
|
|
|
|
|
|
|
/** @var array */
|
2022-02-16 21:15:33 +01:00
|
|
|
protected static $extra_dataobjects = [
|
|
|
|
Player::class,
|
|
|
|
VPlayer::class,
|
|
|
|
TestParent::class,
|
|
|
|
BaseObject::class,
|
|
|
|
ChildObject::class
|
|
|
|
];
|
2017-05-27 06:45:28 +02:00
|
|
|
|
|
|
|
public function testAutoSort()
|
|
|
|
{
|
2020-05-29 15:44:04 +02:00
|
|
|
if (Security::getCurrentUser()) {
|
|
|
|
$this->logOut();
|
2017-05-27 06:45:28 +02:00
|
|
|
}
|
|
|
|
|
2022-02-16 21:15:33 +01:00
|
|
|
$list = Player::get();
|
2017-05-27 06:45:28 +02:00
|
|
|
$config = GridFieldConfig::create()->addComponent(new GridFieldSortableRows('SortOrder'));
|
|
|
|
$gridField = new GridField('testfield', 'testfield', $list, $config);
|
2022-02-16 21:15:33 +01:00
|
|
|
$form = new Form(new DummyController(), 'mockform', new FieldList([$gridField]), new FieldList());
|
2017-05-27 06:45:28 +02:00
|
|
|
|
|
|
|
$stateID = 'testGridStateActionField';
|
2022-02-16 21:15:33 +01:00
|
|
|
$request = new HTTPRequest('POST', 'url', [], ['action_gridFieldAlterAction?StateID=' . $stateID => true, $form->getSecurityToken()->getName() => $form->getSecurityToken()->getValue()]);
|
2020-05-29 15:44:04 +02:00
|
|
|
$session = Controller::curr()->getRequest()->getSession();
|
|
|
|
$session->set($form->getSecurityToken()->getName(), $form->getSecurityToken()->getValue());
|
2022-02-16 21:15:33 +01:00
|
|
|
$session->set($stateID, ['grid' => '', 'actionName' => 'sortableRowsToggle', 'args' => ['GridFieldSortableRows' => ['sortableToggle' => true]]]);
|
2020-05-29 15:44:04 +02:00
|
|
|
$request->setSession($session);
|
2022-02-16 21:15:33 +01:00
|
|
|
|
|
|
|
$gridField->gridFieldAlterAction(['StateID' => $stateID], $form, $request);
|
2017-05-27 06:45:28 +02:00
|
|
|
|
|
|
|
//Insure sort ran
|
|
|
|
$this->assertEquals(3, $list->last()->SortOrder, 'Auto sort should have run');
|
|
|
|
|
|
|
|
|
|
|
|
//Check for duplicates (there shouldn't be any)
|
|
|
|
$count = $list->Count();
|
|
|
|
$indexes = count(array_unique($list->column('SortOrder')));
|
|
|
|
$this->assertEquals(0, $count - $indexes, 'Duplicate indexes detected');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAppendToTopAutoSort()
|
|
|
|
{
|
2020-05-29 15:44:04 +02:00
|
|
|
if (Security::getCurrentUser()) {
|
|
|
|
$this->logOut();
|
2017-05-27 06:45:28 +02:00
|
|
|
}
|
|
|
|
|
2022-02-16 21:15:33 +01:00
|
|
|
$list = Player::get();
|
2017-05-27 06:45:28 +02:00
|
|
|
$config = GridFieldConfig::create()->addComponent(new GridFieldSortableRows('SortOrder'));
|
|
|
|
$gridField = new GridField('testfield', 'testfield', $list, $config);
|
2022-02-16 21:15:33 +01:00
|
|
|
$form = new Form(new DummyController(), 'mockform', new FieldList([$gridField]), new FieldList());
|
2017-05-27 06:45:28 +02:00
|
|
|
|
|
|
|
/** @var GridFieldSortableRows $sortableRows */
|
|
|
|
$sortableRows = $gridField->getConfig()->getComponentByType(GridFieldSortableRows::class);
|
|
|
|
$sortableRows->setAppendToTop(true);
|
|
|
|
|
|
|
|
$this->assertEquals(0, $list->last()->SortOrder, 'Auto sort should not have run');
|
|
|
|
|
|
|
|
$stateID = 'testGridStateActionField';
|
2022-02-16 21:15:33 +01:00
|
|
|
$request = new HTTPRequest('POST', 'url', [], ['action_gridFieldAlterAction?StateID=' . $stateID => true, $form->getSecurityToken()->getName() => $form->getSecurityToken()->getValue()]);
|
2020-05-29 15:44:04 +02:00
|
|
|
$session = Controller::curr()->getRequest()->getSession();
|
|
|
|
$session->set($form->getSecurityToken()->getName(), $form->getSecurityToken()->getValue());
|
2022-02-16 21:15:33 +01:00
|
|
|
$session->set($stateID, ['grid' => '', 'actionName' => 'sortableRowsToggle', 'args' => ['GridFieldSortableRows' => ['sortableToggle' => true]]]);
|
2020-05-29 15:44:04 +02:00
|
|
|
$request->setSession($session);
|
2022-02-16 21:15:33 +01:00
|
|
|
$gridField->gridFieldAlterAction(['StateID' => $stateID], $form, $request);
|
2017-05-27 06:45:28 +02:00
|
|
|
|
|
|
|
//Insure sort ran
|
|
|
|
$this->assertEquals(3, $list->last()->SortOrder, 'Auto sort should have run');
|
|
|
|
|
|
|
|
|
|
|
|
//Check for duplicates (there shouldn't be any)
|
|
|
|
$count = $list->Count();
|
|
|
|
$indexes = count(array_unique($list->column('SortOrder')));
|
|
|
|
$this->assertEquals(0, $count - $indexes, 'Duplicate indexes detected');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAutoSortVersioned()
|
|
|
|
{
|
2020-05-29 15:44:04 +02:00
|
|
|
if (Security::getCurrentUser()) {
|
|
|
|
$this->logOut();
|
2017-05-27 06:45:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//Force versioned to reset
|
|
|
|
Versioned::reset();
|
|
|
|
|
2022-02-16 21:15:33 +01:00
|
|
|
$list = VPlayer::get();
|
2017-05-27 06:45:28 +02:00
|
|
|
|
|
|
|
//Publish all records
|
|
|
|
foreach ($list as $item) {
|
2023-05-08 15:27:50 +02:00
|
|
|
$item->publishSingle();
|
2017-05-27 06:45:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$config = GridFieldConfig::create()->addComponent(new GridFieldSortableRows('SortOrder', true, 'Live'));
|
|
|
|
$gridField = new GridField('testfield', 'testfield', $list, $config);
|
2022-02-16 21:15:33 +01:00
|
|
|
$form = new Form(new DummyController(), 'mockform', new FieldList([$gridField]), new FieldList());
|
2017-05-27 06:45:28 +02:00
|
|
|
|
|
|
|
$stateID = 'testGridStateActionField';
|
2022-02-16 21:15:33 +01:00
|
|
|
$request = new HTTPRequest('POST', 'url', [], ['action_gridFieldAlterAction?StateID=' . $stateID => true, $form->getSecurityToken()->getName() => $form->getSecurityToken()->getValue()]);
|
2020-05-29 15:44:04 +02:00
|
|
|
$session = Controller::curr()->getRequest()->getSession();
|
|
|
|
$session->set($form->getSecurityToken()->getName(), $form->getSecurityToken()->getValue());
|
2022-02-16 21:15:33 +01:00
|
|
|
$session->set($stateID, ['grid' => '', 'actionName' => 'sortableRowsToggle', 'args' => ['GridFieldSortableRows' => ['sortableToggle' => true]]]);
|
2020-05-29 15:44:04 +02:00
|
|
|
$request->setSession($session);
|
2022-02-16 21:15:33 +01:00
|
|
|
$gridField->gridFieldAlterAction(['StateID' => $stateID], $form, $request);
|
2017-05-27 06:45:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
//Insure sort ran
|
|
|
|
$this->assertEquals(3, $list->last()->SortOrder, 'Auto sort should have run on Versioned stage "Stage"');
|
|
|
|
|
|
|
|
|
|
|
|
//Check for duplicates (there shouldn't be any)
|
|
|
|
$count = $list->Count();
|
|
|
|
$indexes = count(array_unique($list->column('SortOrder')));
|
|
|
|
$this->assertEquals(0, $count - $indexes, 'Duplicate indexes detected on Versioned stage "Stage"');
|
|
|
|
|
|
|
|
|
|
|
|
//Force versioned over to Live stage
|
|
|
|
Versioned::set_reading_mode('Live');
|
|
|
|
|
|
|
|
//Get live instance
|
2022-02-16 21:15:33 +01:00
|
|
|
$obj = Versioned::get_one_by_stage(VPlayer::class, 'Live', '"ID"=' . $list->last()->ID);
|
2017-05-27 06:45:28 +02:00
|
|
|
|
|
|
|
//Insure sort ran
|
|
|
|
$this->assertEquals(3, $obj->SortOrder, 'Auto sort should have run on Versioned stage "Live"');
|
|
|
|
|
|
|
|
|
|
|
|
//Check for duplicates (there shouldn't be any)
|
2022-02-16 21:15:33 +01:00
|
|
|
$list = Versioned::get_by_stage(VPlayer::class, 'Live');
|
2017-05-27 06:45:28 +02:00
|
|
|
$count = $list->Count();
|
|
|
|
$indexes = count(array_unique($list->column('SortOrder')));
|
|
|
|
$this->assertEquals(0, $count - $indexes, 'Duplicate indexes detected on Versioned stage "Live"');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAppendToTopAutoSortVersioned()
|
|
|
|
{
|
2020-05-29 15:44:04 +02:00
|
|
|
if (Security::getCurrentUser()) {
|
|
|
|
$this->logOut();
|
2017-05-27 06:45:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//Force versioned to reset
|
|
|
|
Versioned::reset();
|
|
|
|
|
2022-02-16 21:15:33 +01:00
|
|
|
$list = VPlayer::get();
|
2017-05-27 06:45:28 +02:00
|
|
|
|
|
|
|
//Publish all records
|
|
|
|
foreach ($list as $item) {
|
2023-05-08 15:27:50 +02:00
|
|
|
$item->publishSingle();
|
2017-05-27 06:45:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$config = GridFieldConfig::create()->addComponent(new GridFieldSortableRows('SortOrder', true, 'Live'));
|
|
|
|
$gridField = new GridField('testfield', 'testfield', $list, $config);
|
2022-02-16 21:15:33 +01:00
|
|
|
$form = new Form(new DummyController(), 'mockform', new FieldList([$gridField]), new FieldList());
|
2017-05-27 06:45:28 +02:00
|
|
|
|
|
|
|
/** @var GridFieldSortableRows $sortableRows */
|
|
|
|
$sortableRows = $gridField->getConfig()->getComponentByType(GridFieldSortableRows::class);
|
|
|
|
$sortableRows->setAppendToTop(true);
|
|
|
|
|
|
|
|
$this->assertEquals(0, $list->last()->SortOrder, 'Auto sort should not have run on Versioned stage "Stage"');
|
|
|
|
|
|
|
|
$stateID = 'testGridStateActionField';
|
2022-02-16 21:15:33 +01:00
|
|
|
$request = new HTTPRequest('POST', 'url', [], ['action_gridFieldAlterAction?StateID=' . $stateID => true, $form->getSecurityToken()->getName() => $form->getSecurityToken()->getValue()]);
|
2020-05-29 15:44:04 +02:00
|
|
|
$session = Controller::curr()->getRequest()->getSession();
|
|
|
|
$session->set($form->getSecurityToken()->getName(), $form->getSecurityToken()->getValue());
|
2022-02-16 21:15:33 +01:00
|
|
|
$session->set($stateID, ['grid' => '', 'actionName' => 'sortableRowsToggle', 'args' => ['GridFieldSortableRows' => ['sortableToggle' => true]]]);
|
2020-05-29 15:44:04 +02:00
|
|
|
$request->setSession($session);
|
2022-02-16 21:15:33 +01:00
|
|
|
$gridField->gridFieldAlterAction(['StateID' => $stateID], $form, $request);
|
2017-05-27 06:45:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
//Insure sort ran
|
|
|
|
$this->assertEquals(3, $list->last()->SortOrder, 'Auto sort should have run on Versioned stage "Stage"');
|
|
|
|
|
|
|
|
|
|
|
|
//Check for duplicates (there shouldn't be any)
|
|
|
|
$count = $list->Count();
|
|
|
|
$indexes = count(array_unique($list->column('SortOrder')));
|
|
|
|
$this->assertEquals(0, $count - $indexes, 'Duplicate indexes detected on Versioned stage "Stage"');
|
|
|
|
|
|
|
|
|
|
|
|
//Force versioned over to Live stage
|
|
|
|
Versioned::set_reading_mode('Live');
|
|
|
|
|
|
|
|
//Insure sort ran
|
|
|
|
$this->assertEquals(3, $list->last()->SortOrder, 'Auto sort should have run on Versioned stage "Live"');
|
|
|
|
|
|
|
|
|
|
|
|
//Check for duplicates (there shouldn't be any)
|
|
|
|
$count = $list->Count();
|
|
|
|
$indexes = count(array_unique($list->column('SortOrder')));
|
|
|
|
$this->assertEquals(0, $count - $indexes, 'Duplicate indexes detected on Versioned stage "Live"');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAppendToTopAutoSortChild()
|
|
|
|
{
|
2020-05-29 15:44:04 +02:00
|
|
|
if (Security::getCurrentUser()) {
|
|
|
|
$this->logOut();
|
2017-05-27 06:45:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//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')) . '\'');
|
|
|
|
|
2022-02-16 21:15:33 +01:00
|
|
|
/** @var TestParent $parent */
|
|
|
|
$parent = TestParent::get()->first();
|
2017-05-27 06:45:28 +02:00
|
|
|
|
|
|
|
/** @var DataList $list */
|
|
|
|
$list = $parent->TestRelation();
|
|
|
|
|
|
|
|
$config = GridFieldConfig::create()->addComponent(new GridFieldSortableRows('SortOrder'));
|
|
|
|
$gridField = new GridField('testfield', 'testfield', $list, $config);
|
2022-02-16 21:15:33 +01:00
|
|
|
$form = new Form(new DummyController(), 'mockform', new FieldList([$gridField]), new FieldList());
|
2017-05-27 06:45:28 +02:00
|
|
|
$form->loadDataFrom($parent);
|
|
|
|
|
|
|
|
/** @var GridFieldSortableRows $sortableRows */
|
|
|
|
$sortableRows = $gridField->getConfig()->getComponentByType(GridFieldSortableRows::class);
|
|
|
|
$sortableRows->setAppendToTop(true);
|
|
|
|
|
|
|
|
$this->assertEquals(0, $list->last()->SortOrder, 'Auto sort should not have run');
|
|
|
|
|
|
|
|
$stateID = 'testGridStateActionField';
|
2022-02-16 21:15:33 +01:00
|
|
|
$request = new HTTPRequest('POST', 'url', [], ['action_gridFieldAlterAction?StateID=' . $stateID => true, $form->getSecurityToken()->getName() => $form->getSecurityToken()->getValue()]);
|
2020-05-29 15:44:04 +02:00
|
|
|
$session = Controller::curr()->getRequest()->getSession();
|
|
|
|
$session->set($form->getSecurityToken()->getName(), $form->getSecurityToken()->getValue());
|
2022-02-16 21:15:33 +01:00
|
|
|
$session->set($stateID, ['grid' => '', 'actionName' => 'sortableRowsToggle', 'args' => ['GridFieldSortableRows' => ['sortableToggle' => true]]]);
|
2020-05-29 15:44:04 +02:00
|
|
|
$request->setSession($session);
|
2022-02-16 21:15:33 +01:00
|
|
|
$gridField->gridFieldAlterAction(['StateID' => $stateID], $form, $request);
|
2017-05-27 06:45:28 +02:00
|
|
|
|
|
|
|
//Insure sort ran
|
|
|
|
$this->assertEquals(3, $list->last()->SortOrder, 'Auto sort should have run');
|
|
|
|
|
|
|
|
|
|
|
|
//Check for duplicates (there shouldn't be any)
|
|
|
|
$count = $list->Count();
|
|
|
|
$indexes = count(array_unique($list->column('SortOrder')));
|
|
|
|
$this->assertEquals(0, $count - $indexes, 'Duplicate indexes detected');
|
|
|
|
|
|
|
|
|
|
|
|
//Make sure the last edited is today for all records
|
|
|
|
$this->assertEquals(3, $list->filter('LastEdited:GreaterThan', date('Y-m-d 00:00:00'))->count());
|
|
|
|
}
|
2012-09-06 17:27:23 +02:00
|
|
|
}
|