2012-05-15 17:33:00 +02:00
|
|
|
<?php
|
2017-05-27 06:45:28 +02:00
|
|
|
namespace UndefinedOffset\SortableGridField\Tests;
|
|
|
|
|
2023-05-08 15:27:50 +02:00
|
|
|
use SilverStripe\Core\Injector\Injector;
|
2020-05-29 15:44:04 +02:00
|
|
|
use SilverStripe\Control\Controller;
|
2017-05-27 06:45:28 +02:00
|
|
|
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\ValidationException;
|
2023-05-08 15:27:50 +02:00
|
|
|
use SilverStripe\Security\IdentityStore;
|
|
|
|
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\DummyController;
|
2022-02-16 21:23:08 +01:00
|
|
|
use UndefinedOffset\SortableGridField\Tests\Forms\OrderingTest\Team;
|
|
|
|
use UndefinedOffset\SortableGridField\Tests\Forms\OrderingTest\VTeam;
|
2017-05-27 06:45:28 +02:00
|
|
|
|
2022-02-16 21:23:08 +01:00
|
|
|
class OrderingTest extends SapphireTest
|
2017-05-27 06:45:28 +02:00
|
|
|
{
|
|
|
|
/** @var ArrayList */
|
|
|
|
protected $list;
|
|
|
|
|
|
|
|
/** @var GridField */
|
|
|
|
protected $gridField;
|
|
|
|
|
|
|
|
/** @var Form */
|
|
|
|
protected $form;
|
|
|
|
|
|
|
|
/** @var string */
|
2022-02-16 21:23:08 +01:00
|
|
|
public static $fixture_file = 'OrderingTest.yml';
|
2017-05-27 06:45:28 +02:00
|
|
|
|
|
|
|
/** @var array */
|
2022-02-16 21:15:33 +01:00
|
|
|
protected static $extra_dataobjects = [
|
|
|
|
Team::class,
|
|
|
|
VTeam::class
|
|
|
|
];
|
2017-05-27 06:45:28 +02:00
|
|
|
|
2022-02-16 21:15:33 +01:00
|
|
|
protected function setUp(): void
|
2017-05-27 06:45:28 +02:00
|
|
|
{
|
|
|
|
parent::setUp();
|
2022-02-16 21:15:33 +01:00
|
|
|
|
|
|
|
$this->list = Team::get();
|
2017-05-27 06:45:28 +02:00
|
|
|
$config = GridFieldConfig::create()->addComponent(new GridFieldSortableRows('SortOrder'));
|
|
|
|
$this->gridField = new GridField('testfield', 'testfield', $this->list, $config);
|
2022-02-16 21:15:33 +01:00
|
|
|
$this->form = new Form(new DummyController(), 'mockform', new FieldList([$this->gridField]), new FieldList());
|
2017-05-27 06:45:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSortActionWithoutCorrectPermission()
|
|
|
|
{
|
2023-05-08 15:27:50 +02:00
|
|
|
if (Security::getCurrentUser()) {
|
|
|
|
Injector::inst()->get(IdentityStore::class)->logOut(Controller::curr()->getRequest());
|
2017-05-27 06:45:28 +02:00
|
|
|
}
|
2022-02-16 21:15:33 +01:00
|
|
|
|
|
|
|
$this->expectException(ValidationException::class);
|
|
|
|
$team1 = $this->objFromFixture(Team::class, 'team1');
|
|
|
|
$team2 = $this->objFromFixture(Team::class, 'team2');
|
|
|
|
$team3 = $this->objFromFixture(Team::class, 'team3');
|
2017-05-27 06:45:28 +02:00
|
|
|
|
|
|
|
$stateID = 'testGridStateActionField';
|
2022-02-16 21:15:33 +01:00
|
|
|
$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()]);
|
2020-05-29 15:44:04 +02:00
|
|
|
$session = Controller::curr()->getRequest()->getSession();
|
|
|
|
$session->set($this->form->getSecurityToken()->getName(), $this->form->getSecurityToken()->getValue());
|
2022-02-16 21:15:33 +01:00
|
|
|
$session->set($stateID, ['grid' => '', 'actionName' => 'saveGridRowSort', 'args' => ['GridFieldSortableRows' => ['sortableToggle' => true]]]);
|
2020-05-29 15:44:04 +02:00
|
|
|
$request->setSession($session);
|
2022-02-16 21:15:33 +01:00
|
|
|
$this->gridField->gridFieldAlterAction(['StateID' => $stateID], $this->form, $request);
|
2017-05-27 06:45:28 +02:00
|
|
|
$this->assertEquals($team3->ID, $this->list->last()->ID, 'User should\'t be able to sort records without correct permissions.');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSortActionWithAdminPermission()
|
|
|
|
{
|
2022-02-16 21:15:33 +01:00
|
|
|
$team1 = $this->objFromFixture(Team::class, 'team1');
|
|
|
|
$team2 = $this->objFromFixture(Team::class, 'team2');
|
|
|
|
$team3 = $this->objFromFixture(Team::class, 'team3');
|
2017-05-27 06:45:28 +02:00
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
$stateID = 'testGridStateActionField';
|
2022-02-16 21:15:33 +01:00
|
|
|
$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()]);
|
2020-05-29 15:44:04 +02:00
|
|
|
$session = Controller::curr()->getRequest()->getSession();
|
|
|
|
$session->set($this->form->getSecurityToken()->getName(), $this->form->getSecurityToken()->getValue());
|
2022-02-16 21:15:33 +01:00
|
|
|
$session->set($stateID, ['grid' => '', 'actionName' => 'saveGridRowSort', 'args' => ['GridFieldSortableRows' => ['sortableToggle' => true]]]);
|
2020-05-29 15:44:04 +02:00
|
|
|
$request->setSession($session);
|
2022-02-16 21:15:33 +01:00
|
|
|
$this->gridField->gridFieldAlterAction(['StateID' => $stateID], $this->form, $request);
|
2017-05-27 06:45:28 +02:00
|
|
|
$this->assertEquals($team2->ID, $this->list->last()->ID, 'User should be able to sort records with ADMIN permission.');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSortActionVersioned()
|
|
|
|
{
|
|
|
|
//Force versioned to reset
|
|
|
|
Versioned::reset();
|
|
|
|
|
2022-02-16 21:15:33 +01:00
|
|
|
$list = VTeam::get();
|
2017-05-27 06:45:28 +02:00
|
|
|
$this->gridField->setList($list);
|
|
|
|
|
|
|
|
/** @var GridFieldSortableRows $sortableGrid */
|
|
|
|
$sortableGrid = $this->gridField->getConfig()->getComponentByType(GridFieldSortableRows::class);
|
|
|
|
$sortableGrid->setUpdateVersionedStage('Live');
|
|
|
|
|
|
|
|
//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
|
|
|
}
|
|
|
|
|
2022-02-16 21:15:33 +01:00
|
|
|
$team1 = $this->objFromFixture(VTeam::class, 'team1');
|
|
|
|
$team2 = $this->objFromFixture(VTeam::class, 'team2');
|
|
|
|
$team3 = $this->objFromFixture(VTeam::class, 'team3');
|
2017-05-27 06:45:28 +02:00
|
|
|
|
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
$stateID = 'testGridStateActionField';
|
2022-02-16 21:15:33 +01:00
|
|
|
$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()]);
|
2020-05-29 15:44:04 +02:00
|
|
|
$session = Controller::curr()->getRequest()->getSession();
|
|
|
|
$session->set($this->form->getSecurityToken()->getName(), $this->form->getSecurityToken()->getValue());
|
2022-02-16 21:15:33 +01:00
|
|
|
$session->set($stateID, ['grid' => '', 'actionName' => 'saveGridRowSort', 'args' => ['GridFieldSortableRows' => ['sortableToggle' => true]]]);
|
2020-05-29 15:44:04 +02:00
|
|
|
$request->setSession($session);
|
2022-02-16 21:15:33 +01:00
|
|
|
$this->gridField->gridFieldAlterAction(['StateID' => $stateID], $this->form, $request);
|
2017-05-27 06:45:28 +02:00
|
|
|
|
|
|
|
$this->assertEquals($team2->ID, $list->last()->ID, 'Sort should have happened on Versioned stage "Stage"');
|
|
|
|
|
2022-02-16 21:15:33 +01:00
|
|
|
$list = Versioned::get_by_stage(VTeam::class, 'Live');
|
2017-05-27 06:45:28 +02:00
|
|
|
$this->assertEquals($team2->ID, $list->last()->ID, 'Sort should have happened on Versioned stage "Live"');
|
|
|
|
}
|
2012-05-15 17:33:00 +02:00
|
|
|
}
|