1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LeKoala\CmsActions\Test; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Forms\Form; |
6
|
|
|
use SilverStripe\Security\Member; |
7
|
|
|
use LeKoala\CmsActions\CustomLink; |
8
|
|
|
use SilverStripe\Dev\SapphireTest; |
9
|
|
|
use SilverStripe\Admin\LeftAndMain; |
10
|
|
|
use SilverStripe\Control\Controller; |
11
|
|
|
use SilverStripe\Versioned\Versioned; |
12
|
|
|
use SilverStripe\Forms\CompositeField; |
13
|
|
|
use SilverStripe\Forms\GridField\GridField; |
14
|
|
|
use SilverStripe\Forms\GridField\GridFieldDetailForm; |
15
|
|
|
use SilverStripe\Versioned\VersionedGridFieldItemRequest; |
16
|
|
|
use SilverStripe\Forms\GridField\GridFieldDetailForm_ItemRequest; |
17
|
|
|
use SilverStripe\CMS\Model\SiteTree; |
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Tests for Cms Actions module |
21
|
|
|
*/ |
22
|
|
|
class CmsActionsTest extends SapphireTest |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Defines the fixture file to use for this test class |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected static $fixture_file = 'CmsActionsTest.yml'; |
29
|
|
|
protected static $fixture_file_simple = 'CmsActionsSimpleTest.yml'; |
30
|
|
|
|
31
|
|
|
protected static $extra_dataobjects = array( |
32
|
|
|
Test_CmsActionsModel::class, |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
public static function get_fixture_file() |
36
|
|
|
{ |
37
|
|
|
if (class_exists(SiteTree::class)) { |
38
|
|
|
return self::$fixture_file; |
39
|
|
|
} |
40
|
|
|
return self::$fixture_file_simple; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public static function getExtraDataObjects() |
44
|
|
|
{ |
45
|
|
|
$arr = parent::getExtraDataObjects(); |
46
|
|
|
if (class_exists(SiteTree::class)) { |
47
|
|
|
$arr[] = Test_ActionsPage::class; |
48
|
|
|
} |
49
|
|
|
return $arr; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
public function setUp(): void |
54
|
|
|
{ |
55
|
|
|
parent::setUp(); |
56
|
|
|
$controller = Controller::curr(); |
57
|
|
|
$controller->config()->set('url_segment', 'test_controller'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function tearDown(): void |
61
|
|
|
{ |
62
|
|
|
parent::tearDown(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return Test_ActionsPage |
67
|
|
|
*/ |
68
|
|
|
public function getTestPage() |
69
|
|
|
{ |
70
|
|
|
return $this->objFromFixture(Test_ActionsPage::class, 'demo'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return Test_CmsActionsModel |
75
|
|
|
*/ |
76
|
|
|
public function getTestModel() |
77
|
|
|
{ |
78
|
|
|
return $this->objFromFixture(Test_CmsActionsModel::class, 'demo'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return Member |
83
|
|
|
*/ |
84
|
|
|
public function getAdminMember() |
85
|
|
|
{ |
86
|
|
|
return $this->objFromFixture(Member::class, 'admin'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return Form |
91
|
|
|
*/ |
92
|
|
|
public function getMemberForm() |
93
|
|
|
{ |
94
|
|
|
$controller = Controller::curr(); |
95
|
|
|
$form = new Form($controller); |
|
|
|
|
96
|
|
|
|
97
|
|
|
$record = $this->getAdminMember(); |
98
|
|
|
|
99
|
|
|
$list = Member::get(); |
100
|
|
|
$gridField = new GridField('testGridfield', null, $list); |
101
|
|
|
$detailForm = new GridFieldDetailForm('testDetailForm'); |
102
|
|
|
$GridFieldDetailForm = new GridFieldDetailForm_ItemRequest($gridField, $detailForm, $record, $controller, 'testPopup'); |
103
|
|
|
$form = $GridFieldDetailForm->ItemEditForm(); |
104
|
|
|
$form->loadDataFrom($record); |
|
|
|
|
105
|
|
|
|
106
|
|
|
return $form; |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param Controller $controller |
111
|
|
|
* @param DataObject $record |
|
|
|
|
112
|
|
|
* @return Form |
113
|
|
|
*/ |
114
|
|
|
public function getTestForm($controller = null, $record = null) |
115
|
|
|
{ |
116
|
|
|
if (!$controller) { |
117
|
|
|
$controller = Controller::curr(); |
118
|
|
|
} |
119
|
|
|
if (!$record) { |
120
|
|
|
$record = $this->getTestModel(); |
121
|
|
|
} |
122
|
|
|
$list = Test_CmsActionsModel::get(); |
123
|
|
|
$gridField = new GridField('testGridfield', null, $list); |
124
|
|
|
$detailForm = new GridFieldDetailForm('testDetailForm'); |
125
|
|
|
if ($record->hasExtension(Versioned::class)) { |
126
|
|
|
$GridFieldDetailForm = new VersionedGridFieldItemRequest($gridField, $detailForm, $record, $controller, 'testPopup'); |
127
|
|
|
} else { |
128
|
|
|
$GridFieldDetailForm = new GridFieldDetailForm_ItemRequest($gridField, $detailForm, $record, $controller, 'testPopup'); |
129
|
|
|
} |
130
|
|
|
$form = $GridFieldDetailForm->ItemEditForm(); |
131
|
|
|
$form->loadDataFrom($record); |
132
|
|
|
|
133
|
|
|
return $form; |
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function testCustomDeleteTitle() |
137
|
|
|
{ |
138
|
|
|
$form = $this->getTestForm(); |
139
|
|
|
|
140
|
|
|
/** @var Test_CmsActionsModel $record */ |
141
|
|
|
$record = $form->getRecord(); |
142
|
|
|
|
143
|
|
|
$delete = $form->Actions()->fieldByName("action_doDelete"); |
|
|
|
|
144
|
|
|
$this->assertEquals($delete->Title(), $record->getDeleteButtonTitle()); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function testHasSaveAndClose() |
148
|
|
|
{ |
149
|
|
|
$form = $this->getTestForm(); |
150
|
|
|
|
151
|
|
|
$doSaveAndClose = $form->Actions()->fieldByName("action_doSaveAndClose"); |
|
|
|
|
152
|
|
|
// It can be nested in MajorActions, then we need to use dot notation |
153
|
|
|
if (!$doSaveAndClose) { |
|
|
|
|
154
|
|
|
$doSaveAndClose = $form->Actions()->fieldByName("MajorActions.action_doSaveAndClose"); |
|
|
|
|
155
|
|
|
} |
156
|
|
|
$this->assertNotEmpty($doSaveAndClose); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function testHasDefaultTitle() |
160
|
|
|
{ |
161
|
|
|
$customLink = new CustomLink('doTest'); |
162
|
|
|
$this->assertEquals('Do test', $customLink->getTitle()); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function testConfirmationMessage() |
166
|
|
|
{ |
167
|
|
|
$customLink = new CustomLink('doTest'); |
168
|
|
|
$customLink->setConfirmation(true); |
169
|
|
|
$this->assertStringContainsString('sure', $customLink->getConfirmation()); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function testGridFieldAction() |
173
|
|
|
{ |
174
|
|
|
$form = $this->getTestForm(); |
|
|
|
|
175
|
|
|
$action = new Test_GridFieldAction; |
176
|
|
|
|
177
|
|
|
$record = $this->getTestModel(); |
178
|
|
|
$list = Test_CmsActionsModel::get(); |
179
|
|
|
$gridField = new GridField('testGridfield', null, $list); |
180
|
|
|
$actionName = 'test'; |
181
|
|
|
$arguments = ['ID' => $record->ID]; |
182
|
|
|
$data = []; |
183
|
|
|
|
184
|
|
|
$result = $action->doHandle($gridField, $actionName, $arguments, $data); |
|
|
|
|
185
|
|
|
|
186
|
|
|
$this->assertEquals($actionName, $action->performedActionName); |
187
|
|
|
$this->assertEquals($arguments, $action->performedArguments); |
188
|
|
|
$this->assertEquals($data, $action->performedData); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function testLeftAndMain() |
192
|
|
|
{ |
193
|
|
|
if (!class_exists(SiteTree::class)) { |
194
|
|
|
$this->assertTrue(true); // make phpunit happy |
195
|
|
|
return; |
196
|
|
|
} |
197
|
|
|
$page = $this->getTestPage(); |
198
|
|
|
$leftAndMain = LeftAndMain::create(); |
199
|
|
|
$form = $this->getTestForm($leftAndMain, $page); |
200
|
|
|
|
201
|
|
|
// otherwise getRecord complains |
202
|
|
|
$leftAndMain->record = $page; |
203
|
|
|
$result = $leftAndMain->doCustomAction( |
|
|
|
|
204
|
|
|
[ |
205
|
|
|
'action_doCustomAction' => [ |
206
|
|
|
'testAction' => 1 |
207
|
|
|
], |
208
|
|
|
'ID' => $page->ID, |
209
|
|
|
'ClassName' => $page->ClassName |
210
|
|
|
], |
211
|
|
|
$form |
212
|
|
|
); |
213
|
|
|
|
214
|
|
|
$this->assertEquals($page->testAction(), $form->getMessage()); |
215
|
|
|
|
216
|
|
|
$list = []; |
217
|
|
|
$simpleList = []; |
218
|
|
|
foreach ($form->Actions() as $action) { |
219
|
|
|
if ($action instanceof CompositeField) { |
220
|
|
|
$arr = []; |
221
|
|
|
foreach ($action->getChildren() as $subAction) { |
222
|
|
|
$arr[] = $subAction->getName() . ' (' . get_class($subAction) . ')'; |
223
|
|
|
$simpleList[] = $subAction->getName(); |
224
|
|
|
} |
225
|
|
|
$list[] = $arr; |
226
|
|
|
} else { |
227
|
|
|
$list[] = $action->getName() . ' (' . get_class($action) . ')'; |
228
|
|
|
$simpleList[] = $action->getName(); |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
$filteredSimpleList = array_unique($simpleList); |
232
|
|
|
// We should not have duplicated actions |
233
|
|
|
$this->assertEquals($filteredSimpleList, $simpleList); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
public function testGetModelLink() |
237
|
|
|
{ |
238
|
|
|
$action = new CustomLink("testAction", "test"); |
239
|
|
|
|
240
|
|
|
$controller = Controller::curr(); |
241
|
|
|
|
242
|
|
|
// Without an url, we link on the current controller |
243
|
|
|
$link = $action->getModelLink("testAction"); |
244
|
|
|
$this->assertEquals('test_controller/testAction/?CustomLink=testAction', $link); |
245
|
|
|
|
246
|
|
|
// in settings |
247
|
|
|
$controller->getRequest()->setUrl('admin/settings/EditForm/field/MyModel/item/1/edit'); |
248
|
|
|
$link = $action->getModelLink("testAction"); |
249
|
|
|
$this->assertEquals('admin/settings/EditForm/field/MyModel/item/1/doCustomLink?CustomLink=testAction', $link); |
250
|
|
|
|
251
|
|
|
// in model admin |
252
|
|
|
$controller->getRequest()->setUrl('admin/model_admin/MyModel/EditForm/field/MyModel/item/0/edit'); |
253
|
|
|
$link = $action->getModelLink("testAction"); |
254
|
|
|
$this->assertEquals('admin/model_admin/MyModel/EditForm/field/MyModel/item/0/doCustomLink?CustomLink=testAction', $link); |
255
|
|
|
|
256
|
|
|
// in nested grid |
257
|
|
|
$controller->getRequest()->setUrl('admin/model_admin/MyModel/EditForm/field/MyModel/item/0/ItemEditForm/field/OtherModel/item/0/edit'); |
258
|
|
|
$link = $action->getModelLink("testAction"); |
259
|
|
|
$this->assertEquals('admin/model_admin/MyModel/EditForm/field/MyModel/item/0/ItemEditForm/field/OtherModel/item/0/doCustomLink?CustomLink=testAction', $link); |
260
|
|
|
|
261
|
|
|
$controller->getRequest()->setUrl(''); |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths