Passed
Push — master ( 9447cf...76f63b )
by Thomas
06:46
created

CmsActionsTest::testConfirmationMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
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 SilverStripe\Dev\SapphireTest;
8
use SilverStripe\Control\Controller;
9
use SilverStripe\Core\Config\Config;
10
use LeKoala\CmsActions\ActionsGridFieldItemRequest;
11
use LeKoala\CmsActions\CustomLink;
12
use SilverStripe\Admin\LeftAndMain;
13
use SilverStripe\Forms\GridField\GridField;
14
use SilverStripe\Forms\GridField\GridFieldDetailForm;
15
use SilverStripe\Forms\GridField\GridFieldDetailForm_ItemRequest;
16
17
/**
18
 * Tests for Cms Actions module
19
 */
20
class CmsActionsTest extends SapphireTest
21
{
22
    /**
23
     * Defines the fixture file to use for this test class
24
     * @var string
25
     */
26
    protected static $fixture_file = 'CmsActionsTest.yml';
27
28
    protected static $extra_dataobjects = array(
29
        Test_CmsActionsModel::class,
30
        Test_ActionsPage::class,
31
    );
32
33
    public function setUp()
34
    {
35
        parent::setUp();
36
        $controller = Controller::curr();
37
        $controller->config()->set('url_segment', 'test_controller');
38
    }
39
40
    public function tearDown()
41
    {
42
        parent::tearDown();
43
    }
44
45
    /**
46
     * @return Test_ActionsPage
47
     */
48
    public function getTestPage()
49
    {
50
        return $this->objFromFixture(Test_ActionsPage::class, 'demo');
51
    }
52
53
    /**
54
     * @return Test_CmsActionsModel
55
     */
56
    public function getTestModel()
57
    {
58
        return $this->objFromFixture(Test_CmsActionsModel::class, 'demo');
59
    }
60
61
    /**
62
     * @return Member
63
     */
64
    public function getAdminMember()
65
    {
66
        return $this->objFromFixture(Member::class, 'admin');
67
    }
68
69
70
    /**
71
     * @return Form
72
     */
73
    public function getMemberForm()
74
    {
75
        $controller = Controller::curr();
76
        $form = new Form($controller);
0 ignored issues
show
Unused Code introduced by
The assignment to $form is dead and can be removed.
Loading history...
77
78
        $record = $this->getAdminMember();
79
80
        $list = Member::get();
81
        $gridField = new GridField('testGridfield', null, $list);
82
        $detailForm = new GridFieldDetailForm('testDetailForm');
83
        $GridFieldDetailForm = new GridFieldDetailForm_ItemRequest($gridField, $detailForm, $record, $controller, 'testPopup');
84
        $form = $GridFieldDetailForm->ItemEditForm();
85
        $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

85
        $form->/** @scrutinizer ignore-call */ 
86
               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...
86
87
        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...
88
    }
89
90
    public function getTestForm()
91
    {
92
        $controller = Controller::curr();
93
94
        $record = $this->getTestModel();
95
96
        $list = Test_CmsActionsModel::get();
97
        $gridField = new GridField('testGridfield', null, $list);
98
        $detailForm = new GridFieldDetailForm('testDetailForm');
99
        $GridFieldDetailForm = new GridFieldDetailForm_ItemRequest($gridField, $detailForm, $record, $controller, 'testPopup');
100
        $form = $GridFieldDetailForm->ItemEditForm();
101
        $form->loadDataFrom($record);
102
103
        return $form;
104
    }
105
106
    public function testCustomDeleteTitle()
107
    {
108
        $form = $this->getTestForm();
109
        $record = $form->getRecord();
0 ignored issues
show
Bug introduced by
The method getRecord() 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

109
        /** @scrutinizer ignore-call */ 
110
        $record = $form->getRecord();

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...
110
111
        $delete = $form->Actions()->fieldByName("action_doDelete");
0 ignored issues
show
Bug introduced by
The method Actions() 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

111
        $delete = $form->/** @scrutinizer ignore-call */ Actions()->fieldByName("action_doDelete");

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...
112
        $this->assertEquals($delete->Title(), $record->getDeleteButtonTitle());
113
    }
114
115
    public function testHasSaveAndClose()
116
    {
117
        $form = $this->getTestForm();
118
119
        $doSaveAndClose = $form->Actions()->fieldByName("action_doSaveAndClose");
120
        // It can be nested in MajorActions, then we need to use dot notation
121
        if (!$doSaveAndClose) {
122
            $doSaveAndClose = $form->Actions()->fieldByName("MajorActions.action_doSaveAndClose");
123
        }
124
        $this->assertNotEmpty($doSaveAndClose);
125
    }
126
127
    public function testHasDefaultTitle()
128
    {
129
        $customLink = new CustomLink('doTest');
130
        $this->assertEquals('Do test', $customLink->getTitle());
131
    }
132
133
    public function testConfirmationMessage()
134
    {
135
        $customLink = new CustomLink('doTest');
136
        $customLink->setConfirmation(true);
137
        $this->assertContains('sure', $customLink->getConfirmation());
138
    }
139
140
    public function testGridFieldAction()
141
    {
142
        $form = $this->getTestForm();
0 ignored issues
show
Unused Code introduced by
The assignment to $form is dead and can be removed.
Loading history...
143
        $action = new Test_GridFieldAction;
144
145
        $record = $this->getTestModel();
146
        $list = Test_CmsActionsModel::get();
147
        $gridField = new GridField('testGridfield', null, $list);
148
        $actionName = 'test';
149
        $arguments = ['ID' => $record->ID];
150
        $data = [];
151
152
        $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...
153
154
        $this->assertEquals($actionName, $action->performedActionName);
155
        $this->assertEquals($arguments, $action->performedArguments);
156
        $this->assertEquals($data, $action->performedData);
157
    }
158
159
    public function testLeftAndMain()
160
    {
161
        $leftAndMain = LeftAndMain::create();
162
        $form = $this->getTestForm();
163
164
        $page = $this->getTestPage();
165
        // otherwise getRecord complains
166
        $leftAndMain->record = $page;
167
        $result = $leftAndMain->doCustomAction(['action_doCustomAction' => [
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
168
            'testAction' => 1
169
        ], 'ID' => $page->ID, 'ClassName' => $page->ClassName], $form);
170
171
        $this->assertEquals($page->testAction(), $form->getMessage());
0 ignored issues
show
Bug introduced by
The method getMessage() 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

171
        $this->assertEquals($page->testAction(), $form->/** @scrutinizer ignore-call */ getMessage());

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...
172
    }
173
}
174