2012-03-08 01:58:53 +01:00
|
|
|
<?php
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DataList;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-06-23 01:37:22 +02:00
|
|
|
use SilverStripe\Security\Member;
|
|
|
|
use SilverStripe\Security\SecurityToken;
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2012-03-08 01:58:53 +01:00
|
|
|
class GridFieldDeleteActionTest extends SapphireTest {
|
|
|
|
|
|
|
|
/** @var ArrayList */
|
|
|
|
protected $list;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-08 01:58:53 +01:00
|
|
|
/** @var GridField */
|
|
|
|
protected $gridField;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-08 01:58:53 +01:00
|
|
|
/** @var Form */
|
|
|
|
protected $form;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-08 01:58:53 +01:00
|
|
|
/** @var string */
|
2013-03-21 19:48:54 +01:00
|
|
|
protected static $fixture_file = 'GridFieldActionTest.yml';
|
2012-03-08 01:58:53 +01:00
|
|
|
|
|
|
|
/** @var array */
|
|
|
|
protected $extraDataObjects = array('GridFieldAction_Delete_Team', 'GridFieldAction_Edit_Team');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-08 01:58:53 +01:00
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
$this->list = new DataList('GridFieldAction_Delete_Team');
|
|
|
|
$config = GridFieldConfig::create()->addComponent(new GridFieldDeleteAction());
|
|
|
|
$this->gridField = new GridField('testfield', 'testfield', $this->list, $config);
|
|
|
|
$this->form = new Form(new Controller(), 'mockform', new FieldList(array($this->gridField)), new FieldList());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-08 01:58:53 +01:00
|
|
|
public function testDontShowDeleteButtons() {
|
|
|
|
if(Member::currentUser()) { Member::currentUser()->logOut(); }
|
|
|
|
$content = new CSSContentParser($this->gridField->FieldHolder());
|
|
|
|
// Check that there are content
|
|
|
|
$this->assertEquals(4, count($content->getBySelector('.ss-gridfield-item')));
|
|
|
|
// Make sure that there are no delete buttons
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals(0, count($content->getBySelector('.gridfield-button-delete')),
|
|
|
|
'Delete buttons should not show when not logged in.');
|
2012-03-08 01:58:53 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-08 01:58:53 +01:00
|
|
|
public function testShowDeleteButtonsWithAdminPermission() {
|
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
$content = new CSSContentParser($this->gridField->FieldHolder());
|
|
|
|
$deleteButtons = $content->getBySelector('.gridfield-button-delete');
|
|
|
|
$this->assertEquals(3, count($deleteButtons), 'Delete buttons should show when logged in.');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-02-17 03:09:21 +01:00
|
|
|
public function testActionsRequireCSRF() {
|
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
$this->setExpectedException(
|
|
|
|
'SS_HTTPResponse_Exception',
|
|
|
|
_t("Form.CSRF_FAILED_MESSAGE",
|
|
|
|
"There seems to have been a technical problem. Please click the back button, ".
|
|
|
|
"refresh your browser, and try again."
|
|
|
|
),
|
|
|
|
400
|
|
|
|
);
|
|
|
|
$stateID = 'testGridStateActionField';
|
|
|
|
$request = new SS_HTTPRequest(
|
|
|
|
'POST',
|
|
|
|
'url',
|
|
|
|
array(),
|
|
|
|
array(
|
|
|
|
'action_gridFieldAlterAction?StateID='.$stateID,
|
|
|
|
'SecurityID' => null,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->gridField->gridFieldAlterAction(array('StateID'=>$stateID), $this->form, $request);
|
|
|
|
}
|
|
|
|
|
2012-03-08 01:58:53 +01:00
|
|
|
public function testDeleteActionWithoutCorrectPermission() {
|
|
|
|
if(Member::currentUser()) { Member::currentUser()->logOut(); }
|
2016-06-15 06:03:16 +02:00
|
|
|
$this->setExpectedException('SilverStripe\\ORM\\ValidationException');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-08 01:58:53 +01:00
|
|
|
$stateID = 'testGridStateActionField';
|
2016-02-17 03:09:21 +01:00
|
|
|
Session::set(
|
|
|
|
$stateID,
|
|
|
|
array(
|
|
|
|
'grid' => '',
|
|
|
|
'actionName' => 'deleterecord',
|
|
|
|
'args' => array(
|
|
|
|
'RecordID' => $this->idFromFixture('GridFieldAction_Delete_Team', 'team1')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$token = SecurityToken::inst();
|
|
|
|
$request = new SS_HTTPRequest(
|
|
|
|
'POST',
|
|
|
|
'url',
|
|
|
|
array(),
|
|
|
|
array(
|
|
|
|
'action_gridFieldAlterAction?StateID='.$stateID => true,
|
|
|
|
$token->getName() => $token->getValue(),
|
|
|
|
)
|
|
|
|
);
|
2012-03-08 01:58:53 +01:00
|
|
|
$this->gridField->gridFieldAlterAction(array('StateID'=>$stateID), $this->form, $request);
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertEquals(3, $this->list->count(),
|
|
|
|
'User should\'t be able to delete records without correct permissions.');
|
2012-03-08 01:58:53 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-08 01:58:53 +01:00
|
|
|
public function testDeleteActionWithAdminPermission() {
|
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
$stateID = 'testGridStateActionField';
|
2016-02-17 03:09:21 +01:00
|
|
|
Session::set(
|
|
|
|
$stateID,
|
|
|
|
array(
|
|
|
|
'grid'=>'',
|
|
|
|
'actionName'=>'deleterecord',
|
|
|
|
'args' => array(
|
|
|
|
'RecordID' => $this->idFromFixture('GridFieldAction_Delete_Team', 'team1')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$token = SecurityToken::inst();
|
|
|
|
$request = new SS_HTTPRequest(
|
|
|
|
'POST',
|
|
|
|
'url',
|
|
|
|
array(),
|
|
|
|
array(
|
|
|
|
'action_gridFieldAlterAction?StateID='.$stateID=>true,
|
|
|
|
$token->getName() => $token->getValue(),
|
|
|
|
)
|
|
|
|
);
|
2012-03-08 01:58:53 +01:00
|
|
|
$this->gridField->gridFieldAlterAction(array('StateID'=>$stateID), $this->form, $request);
|
|
|
|
$this->assertEquals(2, $this->list->count(), 'User should be able to delete records with ADMIN permission.');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-09 01:55:49 +01:00
|
|
|
public function testDeleteActionRemoveRelation() {
|
|
|
|
$this->logInWithPermission('ADMIN');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-09 01:55:49 +01:00
|
|
|
$config = GridFieldConfig::create()->addComponent(new GridFieldDeleteAction(true));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-09 01:55:49 +01:00
|
|
|
$gridField = new GridField('testfield', 'testfield', $this->list, $config);
|
|
|
|
$form = new Form(new Controller(), 'mockform', new FieldList(array($this->gridField)), new FieldList());
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-09 01:55:49 +01:00
|
|
|
$stateID = 'testGridStateActionField';
|
2016-02-17 03:09:21 +01:00
|
|
|
Session::set(
|
|
|
|
$stateID,
|
|
|
|
array(
|
|
|
|
'grid'=>'',
|
|
|
|
'actionName'=>'deleterecord',
|
|
|
|
'args' => array(
|
|
|
|
'RecordID' => $this->idFromFixture('GridFieldAction_Delete_Team', 'team1')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$token = SecurityToken::inst();
|
|
|
|
$request = new SS_HTTPRequest(
|
|
|
|
'POST',
|
|
|
|
'url',
|
|
|
|
array(),
|
|
|
|
array(
|
|
|
|
'action_gridFieldAlterAction?StateID='.$stateID=>true,
|
|
|
|
$token->getName() => $token->getValue(),
|
|
|
|
)
|
|
|
|
);
|
2012-03-09 01:55:49 +01:00
|
|
|
$this->gridField->gridFieldAlterAction(array('StateID'=>$stateID), $this->form, $request);
|
|
|
|
$this->assertEquals(2, $this->list->count(), 'User should be able to delete records with ADMIN permission.');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-09 01:55:49 +01:00
|
|
|
}
|
2012-03-08 01:58:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class GridFieldAction_Delete_Team extends DataObject implements TestOnly {
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2012-03-08 01:58:53 +01:00
|
|
|
'Name' => 'Varchar',
|
|
|
|
'City' => 'Varchar'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-08 01:58:53 +01:00
|
|
|
public function canView($member = null) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-08 01:58:53 +01:00
|
|
|
public function canDelete($member = null) {
|
|
|
|
return parent::canDelete($member);
|
|
|
|
}
|
2012-03-24 04:38:57 +01:00
|
|
|
}
|