Passed
Push — master ( 421266...5e2898 )
by Thomas
02:58
created

CmsActionsTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 2
b 0
f 1
nc 1
nop 0
dl 0
loc 5
rs 10
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
Bug introduced by
The type SilverStripe\CMS\Model\SiteTree 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. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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);
0 ignored issues
show
Unused Code introduced by
The assignment to $form is dead and can be removed.
Loading history...
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);
0 ignored issues
show
Bug introduced by
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 ignore-call  annotation

104
        $form->/** @scrutinizer ignore-call */ 
105
               loadDataFrom($record);

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.

Loading history...
105
106
        return $form;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $form returns the type SilverStripe\Control\HTTPResponse which is incompatible with the documented return type SilverStripe\Forms\Form.
Loading history...
107
    }
108
109
    /**
110
     * @param Controller $controller
111
     * @param DataObject $record
0 ignored issues
show
Bug introduced by
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. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $form returns the type SilverStripe\Control\HTTPResponse which is incompatible with the documented return type SilverStripe\Forms\Form.
Loading history...
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");
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $delete is correct as $form->Actions()->fieldByName('action_doDelete') targeting SilverStripe\Forms\FieldList::fieldByName() 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 getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
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");
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $doSaveAndClose is correct as $form->Actions()->fieldB...action_doSaveAndClose') targeting SilverStripe\Forms\FieldList::fieldByName() 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 getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
152
        // It can be nested in MajorActions, then we need to use dot notation
153
        if (!$doSaveAndClose) {
0 ignored issues
show
introduced by
$doSaveAndClose is of type null, thus it always evaluated to false.
Loading history...
154
            $doSaveAndClose = $form->Actions()->fieldByName("MajorActions.action_doSaveAndClose");
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $doSaveAndClose is correct as $form->Actions()->fieldB...action_doSaveAndClose') targeting SilverStripe\Forms\FieldList::fieldByName() 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 getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
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();
0 ignored issues
show
Unused Code introduced by
The assignment to $form is dead and can be removed.
Loading history...
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);
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
Bug introduced by
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 getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
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(
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
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