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; |
||||||
0 ignored issues
–
show
|
|||||||
18 | use SilverStripe\ORM\ArrayList; |
||||||
19 | use SilverStripe\View\ArrayData; |
||||||
20 | use SilverStripe\Forms\GridField\GridFieldDataColumns; |
||||||
21 | use SilverStripe\Forms\GridField\GridFieldFilterHeader; |
||||||
22 | use SilverStripe\Forms\FieldList; |
||||||
23 | use SilverStripe\Forms\HiddenField; |
||||||
24 | use SilverStripe\Forms\TextField; |
||||||
25 | use SilverStripe\Forms\GridField\GridFieldViewButton; |
||||||
26 | use SilverStripe\Dev\Deprecation; |
||||||
27 | |||||||
28 | /** |
||||||
29 | * Tests for Cms Actions module |
||||||
30 | */ |
||||||
31 | class CmsActionsTest extends SapphireTest |
||||||
32 | { |
||||||
33 | /** |
||||||
34 | * Defines the fixture file to use for this test class |
||||||
35 | * @var string |
||||||
36 | */ |
||||||
37 | protected static $fixture_file = 'CmsActionsTest.yml'; |
||||||
38 | protected static $fixture_file_simple = 'CmsActionsSimpleTest.yml'; |
||||||
39 | |||||||
40 | protected static $extra_dataobjects = array( |
||||||
41 | Test_CmsActionsModel::class, |
||||||
42 | ); |
||||||
43 | |||||||
44 | public static function get_fixture_file() |
||||||
45 | { |
||||||
46 | if (class_exists(SiteTree::class)) { |
||||||
47 | return self::$fixture_file; |
||||||
48 | } |
||||||
49 | return self::$fixture_file_simple; |
||||||
50 | } |
||||||
51 | |||||||
52 | public static function getExtraDataObjects() |
||||||
53 | { |
||||||
54 | $arr = parent::getExtraDataObjects(); |
||||||
55 | if (class_exists(SiteTree::class)) { |
||||||
56 | $arr[] = Test_ActionsPage::class; |
||||||
57 | } |
||||||
58 | return $arr; |
||||||
59 | } |
||||||
60 | |||||||
61 | |||||||
62 | public function setUp(): void |
||||||
63 | { |
||||||
64 | parent::setUp(); |
||||||
65 | $controller = Controller::curr(); |
||||||
66 | $controller->config()->set('url_segment', 'test_controller'); |
||||||
67 | } |
||||||
68 | |||||||
69 | public function tearDown(): void |
||||||
70 | { |
||||||
71 | parent::tearDown(); |
||||||
72 | } |
||||||
73 | |||||||
74 | /** |
||||||
75 | * @return Test_ActionsPage |
||||||
76 | */ |
||||||
77 | public function getTestPage() |
||||||
78 | { |
||||||
79 | return $this->objFromFixture(Test_ActionsPage::class, 'demo'); |
||||||
80 | } |
||||||
81 | |||||||
82 | /** |
||||||
83 | * @return Test_CmsActionsModel |
||||||
84 | */ |
||||||
85 | public function getTestModel() |
||||||
86 | { |
||||||
87 | return $this->objFromFixture(Test_CmsActionsModel::class, 'demo'); |
||||||
88 | } |
||||||
89 | |||||||
90 | /** |
||||||
91 | * @return Member |
||||||
92 | */ |
||||||
93 | public function getAdminMember() |
||||||
94 | { |
||||||
95 | return $this->objFromFixture(Member::class, 'admin'); |
||||||
96 | } |
||||||
97 | |||||||
98 | /** |
||||||
99 | * @return Form |
||||||
100 | */ |
||||||
101 | public function getMemberForm() |
||||||
102 | { |
||||||
103 | $controller = Controller::curr(); |
||||||
104 | $form = new Form($controller); |
||||||
0 ignored issues
–
show
|
|||||||
105 | |||||||
106 | $record = $this->getAdminMember(); |
||||||
107 | |||||||
108 | $list = Member::get(); |
||||||
109 | $gridField = new GridField('testGridfield', null, $list); |
||||||
110 | $detailForm = new GridFieldDetailForm('testDetailForm'); |
||||||
111 | $GridFieldDetailForm = new GridFieldDetailForm_ItemRequest($gridField, $detailForm, $record, $controller, 'testPopup'); |
||||||
112 | $form = $GridFieldDetailForm->ItemEditForm(); |
||||||
113 | $form->loadDataFrom($record); |
||||||
0 ignored issues
–
show
The method
loadDataFrom() does not exist on SilverStripe\Control\HTTPResponse .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
114 | |||||||
115 | return $form; |
||||||
0 ignored issues
–
show
|
|||||||
116 | } |
||||||
117 | |||||||
118 | /** |
||||||
119 | * @param Controller $controller |
||||||
120 | * @param DataObject $record |
||||||
0 ignored issues
–
show
The type
LeKoala\CmsActions\Test\DataObject was not found. Maybe you did not declare it correctly or list all dependencies?
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. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||||||
121 | * @return Form |
||||||
122 | */ |
||||||
123 | public function getTestForm($controller = null, $record = null) |
||||||
124 | { |
||||||
125 | if (!$controller) { |
||||||
126 | $controller = Controller::curr(); |
||||||
127 | } |
||||||
128 | if (!$record) { |
||||||
129 | $record = $this->getTestModel(); |
||||||
130 | } |
||||||
131 | $list = Test_CmsActionsModel::get(); |
||||||
132 | $gridField = new GridField('testGridfield', null, $list); |
||||||
133 | $detailForm = new GridFieldDetailForm('testDetailForm'); |
||||||
134 | if ($record->hasExtension(Versioned::class)) { |
||||||
135 | $GridFieldDetailForm = new VersionedGridFieldItemRequest($gridField, $detailForm, $record, $controller, 'testPopup'); |
||||||
136 | } else { |
||||||
137 | $GridFieldDetailForm = new GridFieldDetailForm_ItemRequest($gridField, $detailForm, $record, $controller, 'testPopup'); |
||||||
138 | } |
||||||
139 | $form = $GridFieldDetailForm->ItemEditForm(); |
||||||
140 | $form->loadDataFrom($record); |
||||||
141 | |||||||
142 | return $form; |
||||||
0 ignored issues
–
show
|
|||||||
143 | } |
||||||
144 | |||||||
145 | /** |
||||||
146 | * @param Controller $controller |
||||||
147 | * @param DataObject $record |
||||||
148 | * @return Form |
||||||
149 | */ |
||||||
150 | public function getViewableForm($controller = null, $record = null) |
||||||
151 | { |
||||||
152 | $r1 = ArrayData::create([ |
||||||
153 | 'ID' => 1, |
||||||
154 | 'FieldName' => 'This is an item', |
||||||
155 | ]); |
||||||
156 | $r2 = ArrayData::create([ |
||||||
157 | 'ID' => 2, |
||||||
158 | 'FieldName' => 'This is a different item', |
||||||
159 | ]); |
||||||
160 | |||||||
161 | if (!$controller) { |
||||||
162 | $controller = Controller::curr(); |
||||||
163 | } |
||||||
164 | if (!$record) { |
||||||
165 | $record = $r1; |
||||||
166 | } |
||||||
167 | |||||||
168 | $list = ArrayList::create([ |
||||||
169 | $r1, |
||||||
170 | $r2, |
||||||
171 | ]); |
||||||
172 | |||||||
173 | $gridField = GridField::create('MyData', 'My data', $list); |
||||||
174 | $gridField->setForm(new Form($controller, "TestForm")); |
||||||
175 | $gridField->getConfig()->removeComponentsByType(GridFieldFilterHeader::class); |
||||||
176 | $columns = $gridField->getConfig()->getComponentByType(GridFieldDataColumns::class); |
||||||
177 | $columns->setDisplayFields([ |
||||||
178 | 'FieldName' => 'Column Header Label', |
||||||
179 | ]); |
||||||
180 | $detailForm = GridFieldDetailForm::create(); |
||||||
181 | $detailForm->setFields(FieldList::create([ |
||||||
182 | HiddenField::create('ID'), |
||||||
183 | TextField::create('FieldName', 'View Field Label'), |
||||||
184 | ])); |
||||||
185 | $gridField->getConfig()->addComponents([ |
||||||
186 | GridFieldViewButton::create(), |
||||||
187 | $detailForm, |
||||||
188 | ]); |
||||||
189 | $GridFieldDetailForm = new GridFieldDetailForm_ItemRequest($gridField, $detailForm, $record, $controller, 'testPopup'); |
||||||
190 | $form = $GridFieldDetailForm->ItemEditForm(); |
||||||
191 | $form->loadDataFrom($record); |
||||||
192 | |||||||
193 | return $form; |
||||||
0 ignored issues
–
show
|
|||||||
194 | } |
||||||
195 | |||||||
196 | public function testCustomDeleteTitle() |
||||||
197 | { |
||||||
198 | $form = $this->getTestForm(); |
||||||
199 | |||||||
200 | /** @var Test_CmsActionsModel $record */ |
||||||
201 | $record = $form->getRecord(); |
||||||
202 | |||||||
203 | $delete = $form->Actions()->fieldByName("action_doDelete"); |
||||||
204 | $this->assertEquals($delete->Title(), $record->getDeleteButtonTitle()); |
||||||
205 | } |
||||||
206 | |||||||
207 | public function testHasSaveAndClose() |
||||||
208 | { |
||||||
209 | $form = $this->getTestForm(); |
||||||
210 | |||||||
211 | $doSaveAndClose = $form->Actions()->fieldByName("action_doSaveAndClose"); |
||||||
212 | // It can be nested in MajorActions, then we need to use dot notation |
||||||
213 | if (!$doSaveAndClose) { |
||||||
214 | $doSaveAndClose = $form->Actions()->fieldByName("MajorActions.action_doSaveAndClose"); |
||||||
215 | } |
||||||
216 | $this->assertNotEmpty($doSaveAndClose); |
||||||
217 | } |
||||||
218 | |||||||
219 | public function testHasDefaultTitle() |
||||||
220 | { |
||||||
221 | $customLink = new CustomLink('doTest'); |
||||||
222 | $this->assertEquals('Do test', $customLink->getTitle()); |
||||||
223 | } |
||||||
224 | |||||||
225 | public function testConfirmationMessage() |
||||||
226 | { |
||||||
227 | $customLink = new CustomLink('doTest'); |
||||||
228 | $customLink->setConfirmation(true); |
||||||
229 | $this->assertStringContainsString('sure', $customLink->getConfirmation()); |
||||||
230 | } |
||||||
231 | |||||||
232 | public function testGridFieldAction() |
||||||
233 | { |
||||||
234 | $form = $this->getTestForm(); |
||||||
0 ignored issues
–
show
|
|||||||
235 | $action = new Test_GridFieldAction; |
||||||
236 | |||||||
237 | $record = $this->getTestModel(); |
||||||
238 | $list = Test_CmsActionsModel::get(); |
||||||
239 | $gridField = new GridField('testGridfield', null, $list); |
||||||
240 | $actionName = 'test'; |
||||||
241 | $arguments = ['ID' => $record->ID]; |
||||||
242 | $data = []; |
||||||
243 | |||||||
244 | $result = $action->doHandle($gridField, $actionName, $arguments, $data); |
||||||
0 ignored issues
–
show
Are you sure the assignment to
$result is correct as $action->doHandle($gridF...ame, $arguments, $data) targeting LeKoala\CmsActions\Test\...FieldAction::doHandle() seems to always return null.
This check looks for function or method calls that always return null and whose return value is assigned to a variable. class A
{
function getObject()
{
return null;
}
}
$a = new A();
$object = $a->getObject();
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() |
|||||||
245 | |||||||
246 | $this->assertEquals($actionName, $action->performedActionName); |
||||||
247 | $this->assertEquals($arguments, $action->performedArguments); |
||||||
248 | $this->assertEquals($data, $action->performedData); |
||||||
249 | } |
||||||
250 | |||||||
251 | public function testLeftAndMain() |
||||||
252 | { |
||||||
253 | if (!class_exists(SiteTree::class)) { |
||||||
254 | $this->assertTrue(true); // make phpunit happy |
||||||
255 | return; |
||||||
256 | } |
||||||
257 | $page = $this->getTestPage(); |
||||||
258 | $leftAndMain = LeftAndMain::create(); |
||||||
259 | $form = $this->getTestForm($leftAndMain, $page); |
||||||
260 | |||||||
261 | // otherwise getRecord complains |
||||||
262 | $leftAndMain->record = $page; |
||||||
263 | $result = $leftAndMain->doCustomAction( |
||||||
0 ignored issues
–
show
|
|||||||
264 | [ |
||||||
265 | 'action_doCustomAction' => [ |
||||||
266 | 'testAction' => 1 |
||||||
267 | ], |
||||||
268 | 'ID' => $page->ID, |
||||||
269 | 'ClassName' => $page->ClassName |
||||||
270 | ], |
||||||
271 | $form |
||||||
272 | ); |
||||||
273 | |||||||
274 | $this->assertEquals($page->testAction(), $form->getMessage()); |
||||||
275 | |||||||
276 | $list = []; |
||||||
277 | $simpleList = []; |
||||||
278 | foreach ($form->Actions() as $action) { |
||||||
279 | if ($action instanceof CompositeField) { |
||||||
280 | $arr = []; |
||||||
281 | foreach ($action->getChildren() as $subAction) { |
||||||
282 | $arr[] = $subAction->getName() . ' (' . get_class($subAction) . ')'; |
||||||
283 | $simpleList[] = $subAction->getName(); |
||||||
284 | } |
||||||
285 | $list[] = $arr; |
||||||
286 | } else { |
||||||
287 | $list[] = $action->getName() . ' (' . get_class($action) . ')'; |
||||||
288 | $simpleList[] = $action->getName(); |
||||||
289 | } |
||||||
290 | } |
||||||
291 | $filteredSimpleList = array_unique($simpleList); |
||||||
292 | // We should not have duplicated actions |
||||||
293 | $this->assertEquals($filteredSimpleList, $simpleList); |
||||||
294 | } |
||||||
295 | |||||||
296 | public function testGetModelLink() |
||||||
297 | { |
||||||
298 | $action = new CustomLink("testAction", "test"); |
||||||
299 | |||||||
300 | $controller = Controller::curr(); |
||||||
301 | |||||||
302 | // SS5 trailing slashes |
||||||
303 | // @link https://docs.silverstripe.org/en/5/changelogs/5.0.0/#trailing-slash |
||||||
304 | $add_trailing_slash = $controller::config()->add_trailing_slash; |
||||||
305 | |||||||
306 | // Without an url, we link on the current controller |
||||||
307 | $link = $action->getModelLink("testAction"); |
||||||
308 | if ($add_trailing_slash === null) { |
||||||
309 | $this->assertEquals('test_controller/testAction/?CustomLink=testAction', $link); |
||||||
310 | } elseif ($add_trailing_slash === false) { |
||||||
311 | $this->assertEquals('test_controller/testAction?CustomLink=testAction', $link); |
||||||
312 | } elseif ($add_trailing_slash === true) { |
||||||
313 | $this->assertEquals('test_controller/testAction/?CustomLink=testAction', $link); |
||||||
314 | } |
||||||
315 | |||||||
316 | |||||||
317 | // in settings |
||||||
318 | $controller->getRequest()->setUrl('admin/settings/EditForm/field/MyModel/item/1/edit'); |
||||||
319 | $link = $action->getModelLink("testAction"); |
||||||
320 | $this->assertEquals('admin/settings/EditForm/field/MyModel/item/1/doCustomLink?CustomLink=testAction', $link); |
||||||
321 | |||||||
322 | // in model admin |
||||||
323 | $controller->getRequest()->setUrl('admin/model_admin/MyModel/EditForm/field/MyModel/item/0/edit'); |
||||||
324 | $link = $action->getModelLink("testAction"); |
||||||
325 | $this->assertEquals('admin/model_admin/MyModel/EditForm/field/MyModel/item/0/doCustomLink?CustomLink=testAction', $link); |
||||||
326 | |||||||
327 | // in model admin with just an id |
||||||
328 | $controller->getRequest()->setUrl('admin/model_admin/MyModel/EditForm/field/MyModel/item/0/'); |
||||||
329 | $link = $action->getModelLink("testAction"); |
||||||
330 | $this->assertEquals('admin/model_admin/MyModel/EditForm/field/MyModel/item/0/doCustomLink?CustomLink=testAction', $link); |
||||||
331 | |||||||
332 | // in nested grid |
||||||
333 | $controller->getRequest()->setUrl('admin/model_admin/MyModel/EditForm/field/MyModel/item/0/ItemEditForm/field/OtherModel/item/0/edit'); |
||||||
334 | $link = $action->getModelLink("testAction"); |
||||||
335 | $this->assertEquals('admin/model_admin/MyModel/EditForm/field/MyModel/item/0/ItemEditForm/field/OtherModel/item/0/doCustomLink?CustomLink=testAction', $link); |
||||||
336 | |||||||
337 | $controller->getRequest()->setUrl(''); |
||||||
338 | } |
||||||
339 | |||||||
340 | public function testViewable() |
||||||
341 | { |
||||||
342 | $version = LeftAndMain::create()->CMSVersionNumber(); |
||||||
343 | if (version_compare($version, '5.4') === -1) { |
||||||
344 | $this->markTestSkipped("Only works on 5.4"); |
||||||
345 | return; |
||||||
346 | } |
||||||
347 | $form = $this->getViewableForm(); |
||||||
348 | |||||||
349 | $doSaveAndClose = $form->Actions()->fieldByName("action_doSaveAndClose"); |
||||||
350 | $this->assertNull($doSaveAndClose); // not available for ViewableData |
||||||
351 | } |
||||||
352 | } |
||||||
353 |
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