Completed
Push — 3.2 ( d51264...ff5ed6 )
by Damian
13:29 queued 01:53
created

testDeleteActionRemoveRelation()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 33
rs 8.8571
cc 1
eloc 23
nc 1
nop 0
1
<?php
2
3
class GridFieldDeleteActionTest extends SapphireTest {
4
5
	/** @var ArrayList */
6
	protected $list;
7
8
	/** @var GridField */
9
	protected $gridField;
10
11
	/** @var Form */
12
	protected $form;
13
14
	/** @var string */
15
	protected static $fixture_file = 'GridFieldActionTest.yml';
16
17
	/** @var array */
18
	protected $extraDataObjects = array('GridFieldAction_Delete_Team', 'GridFieldAction_Edit_Team');
19
20
	public function setUp() {
21
		parent::setUp();
22
		$this->list = new DataList('GridFieldAction_Delete_Team');
0 ignored issues
show
Documentation Bug introduced by
It seems like new \DataList('GridFieldAction_Delete_Team') of type object<DataList> is incompatible with the declared type object<ArrayList> of property $list.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
23
		$config = GridFieldConfig::create()->addComponent(new GridFieldDeleteAction());
24
		$this->gridField = new GridField('testfield', 'testfield', $this->list, $config);
25
		$this->form = new Form(new Controller(), 'mockform', new FieldList(array($this->gridField)), new FieldList());
26
	}
27
28
	public function testDontShowDeleteButtons() {
29
		if(Member::currentUser()) { Member::currentUser()->logOut(); }
30
		$content = new CSSContentParser($this->gridField->FieldHolder());
31
		// Check that there are content
32
		$this->assertEquals(4, count($content->getBySelector('.ss-gridfield-item')));
33
		// Make sure that there are no delete buttons
34
		$this->assertEquals(0, count($content->getBySelector('.gridfield-button-delete')),
35
			'Delete buttons should not show when not logged in.');
36
	}
37
38
	public function testShowDeleteButtonsWithAdminPermission() {
39
		$this->logInWithPermission('ADMIN');
40
		$content = new CSSContentParser($this->gridField->FieldHolder());
41
		$deleteButtons = $content->getBySelector('.gridfield-button-delete');
42
		$this->assertEquals(3, count($deleteButtons), 'Delete buttons should show when logged in.');
43
	}
44
45
	public function testActionsRequireCSRF() {
46
		$this->logInWithPermission('ADMIN');
47
		$this->setExpectedException(
48
			'SS_HTTPResponse_Exception',
49
			_t("Form.CSRF_FAILED_MESSAGE",
50
				"There seems to have been a technical problem. Please click the back button, ".
51
				"refresh your browser, and try again."
52
			),
53
			400
54
		);
55
		$stateID = 'testGridStateActionField';
56
		$request = new SS_HTTPRequest(
57
			'POST',
58
			'url',
59
			array(),
60
			array(
61
				'action_gridFieldAlterAction?StateID='.$stateID,
62
				'SecurityID' => null,
63
			)
64
		);
65
		$this->gridField->gridFieldAlterAction(array('StateID'=>$stateID), $this->form, $request);
66
	}
67
68
	public function testDeleteActionWithoutCorrectPermission() {
69
		if(Member::currentUser()) { Member::currentUser()->logOut(); }
70
		$this->setExpectedException('ValidationException');
71
72
		$stateID = 'testGridStateActionField';
73
		Session::set(
74
			$stateID,
75
			array(
76
				'grid' => '',
77
				'actionName' => 'deleterecord',
78
				'args' => array(
79
					'RecordID' => $this->idFromFixture('GridFieldAction_Delete_Team', 'team1')
80
				)
81
			)
82
		);
83
		$token = SecurityToken::inst();
84
		$request = new SS_HTTPRequest(
85
			'POST',
86
			'url',
87
			array(),
88
			array(
89
				'action_gridFieldAlterAction?StateID='.$stateID => true,
90
				$token->getName() => $token->getValue(),
91
			)
92
		);
93
		$this->gridField->gridFieldAlterAction(array('StateID'=>$stateID), $this->form, $request);
94
		$this->assertEquals(3, $this->list->count(),
95
			'User should\'t be able to delete records without correct permissions.');
96
	}
97
98
	public function testDeleteActionWithAdminPermission() {
99
		$this->logInWithPermission('ADMIN');
100
		$stateID = 'testGridStateActionField';
101
		Session::set(
102
			$stateID,
103
			array(
104
				'grid'=>'',
105
				'actionName'=>'deleterecord',
106
				'args' => array(
107
					'RecordID' => $this->idFromFixture('GridFieldAction_Delete_Team', 'team1')
108
				)
109
			)
110
		);
111
		$token = SecurityToken::inst();
112
		$request = new SS_HTTPRequest(
113
			'POST',
114
			'url',
115
			array(),
116
			array(
117
				'action_gridFieldAlterAction?StateID='.$stateID=>true,
118
				$token->getName() => $token->getValue(),
119
			)
120
		);
121
		$this->gridField->gridFieldAlterAction(array('StateID'=>$stateID), $this->form, $request);
122
		$this->assertEquals(2, $this->list->count(), 'User should be able to delete records with ADMIN permission.');
123
	}
124
125
	public function testDeleteActionRemoveRelation() {
126
		$this->logInWithPermission('ADMIN');
127
128
		$config = GridFieldConfig::create()->addComponent(new GridFieldDeleteAction(true));
129
130
		$gridField = new GridField('testfield', 'testfield', $this->list, $config);
131
		$form = new Form(new Controller(), 'mockform', new FieldList(array($this->gridField)), new FieldList());
132
133
		$stateID = 'testGridStateActionField';
134
		Session::set(
135
			$stateID,
136
			array(
137
				'grid'=>'',
138
				'actionName'=>'deleterecord',
139
				'args' => array(
140
					'RecordID' => $this->idFromFixture('GridFieldAction_Delete_Team', 'team1')
141
				)
142
			)
143
		);
144
		$token = SecurityToken::inst();
145
		$request = new SS_HTTPRequest(
146
			'POST',
147
			'url',
148
			array(),
149
			array(
150
				'action_gridFieldAlterAction?StateID='.$stateID=>true,
151
				$token->getName() => $token->getValue(),
152
			)
153
		);
154
		$this->gridField->gridFieldAlterAction(array('StateID'=>$stateID), $this->form, $request);
155
		$this->assertEquals(2, $this->list->count(), 'User should be able to delete records with ADMIN permission.');
156
157
	}
158
}
159
160
class GridFieldAction_Delete_Team extends DataObject implements TestOnly {
161
	private static $db = array(
162
		'Name' => 'Varchar',
163
		'City' => 'Varchar'
164
	);
165
166
	public function canView($member = null) {
167
		return true;
168
	}
169
170
	public function canDelete($member = null) {
171
		return parent::canDelete($member);
172
	}
173
}
174