Passed
Pull Request — 1 (#228)
by
unknown
03:39
created

CampaignAdminTest::testReadCampaignResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 51
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
c 1
b 0
f 0
dl 0
loc 51
rs 9.424
cc 2
nc 2
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SilverStripe\CampaignAdmin\Tests;
4
5
use ReflectionClass;
6
use SilverStripe\CampaignAdmin\CampaignAdmin;
7
use SilverStripe\Control\HTTPRequest;
8
use SilverStripe\Control\HTTPResponse;
9
use SilverStripe\Dev\SapphireTest;
10
use SilverStripe\ORM\DataList;
11
use SilverStripe\ORM\FieldType\DBDatetime;
12
use SilverStripe\Security\Group;
13
use SilverStripe\Security\Member;
14
use SilverStripe\Security\Permission;
15
use SilverStripe\Versioned\ChangeSet;
16
17
class CampaignAdminTest extends SapphireTest
18
{
19
    protected $extraDataObjects = [
20
        CampaignAdminTest\InvalidChangeSet::class,
21
    ];
22
23
    protected static $fixture_file = 'CampaignAdminTest.yml';
24
25
    protected function setUp(): void
26
    {
27
        parent::setUp();
28
        DBDatetime::set_mock_now('2011-09-24 11:11:00');
29
        CampaignAdmin::config()->set('sync_expires', 300);
30
        CampaignAdmin::config()->set('show_published', false);
31
        CampaignAdmin::config()->set('show_inferred', false);
32
        $this->logInWithPermission('ADMIN');
33
    }
34
35
    /**
36
     * Call a protected method on an object via reflection
37
     *
38
     * @param object $object The object to call the method on
39
     * @param string $method The name of the method
40
     * @param array $args The arguments to pass to the method
41
     * @return mixed
42
     */
43
    protected function callProtectedMethod($object, $method, $args = [])
44
    {
45
        $class = new ReflectionClass(get_class($object));
46
        $methodObj = $class->getMethod($method);
47
        $methodObj->setAccessible(true);
48
        return $methodObj->invokeArgs($object, $args);
49
    }
50
51
    public function testInvalidDataHandling()
52
    {
53
        $changeset = new CampaignAdminTest\InvalidChangeSet();
54
        $admin = new CampaignAdmin();
55
56
        $result = $this->callProtectedMethod($admin, 'getChangeSetResource', [$changeset, true]);
57
        $this->assertEquals('Corrupt database! bad data', $result['Details']);
58
    }
59
60
    /**
61
     * Test sync
62
     */
63
    public function testSync()
64
    {
65
        $admin = CampaignAdmin::create();
66
67
        /** @var ChangeSet $changeset */
68
        $changesetID = $this->idFromFixture(ChangeSet::class, 'change1');
69
        $admin->readCampaigns();
70
71
        // Check initial sync date
72
        $lastSynced = ChangeSet::get()->byID($changesetID)->LastSynced;
73
        $this->assertEquals('2011-09-24 11:11:00', $lastSynced);
74
75
        // After 10 seconds, sync should not be modified when viewing campaigns
76
        DBDatetime::set_mock_now('2011-09-24 11:11:10');
77
        $admin->readCampaigns();
78
        $lastSynced = ChangeSet::get()->byID($changesetID)->LastSynced;
79
        $this->assertEquals('2011-09-24 11:11:00', $lastSynced);
80
81
        // After 7 minutes sync will trigger a refresh
82
        DBDatetime::set_mock_now('2011-09-24 11:18:00');
83
        $admin->readCampaigns();
84
        $lastSynced = ChangeSet::get()->byID($changesetID)->LastSynced;
85
        $this->assertEquals('2011-09-24 11:18:00', $lastSynced);
86
    }
87
88
    public function testFilters()
89
    {
90
        $admin = CampaignAdmin::create();
91
92
        // Test limited items
93
        /** @var DataList $results */
94
        $results = $this->callProtectedMethod($admin, 'getListItems');
95
        $this->assertDOSEquals(
0 ignored issues
show
Deprecated Code introduced by
The function SilverStripe\Dev\SapphireTest::assertDOSEquals() has been deprecated: 4.0.0:5.0.0 Use assertListEquals() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

95
        /** @scrutinizer ignore-deprecated */ $this->assertDOSEquals(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
96
            [
97
                [ 'Name' => 'changeset 1' ],
98
            ],
99
            $results
100
        );
101
102
        // Test published, no inferred
103
        CampaignAdmin::config()->set('show_published', true);
104
        $results = $this->callProtectedMethod($admin, 'getListItems');
105
        $this->assertDOSEquals(
0 ignored issues
show
Deprecated Code introduced by
The function SilverStripe\Dev\SapphireTest::assertDOSEquals() has been deprecated: 4.0.0:5.0.0 Use assertListEquals() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

105
        /** @scrutinizer ignore-deprecated */ $this->assertDOSEquals(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
106
            [
107
                [ 'Name' => 'changeset 1' ],
108
                [ 'Name' => 'changeset 2' ],
109
            ],
110
            $results
111
        );
112
113
        // Test published + inferred
114
        CampaignAdmin::config()->set('show_inferred', true);
115
        $results = $this->callProtectedMethod($admin, 'getListItems');
116
        $this->assertDOSEquals(
0 ignored issues
show
Deprecated Code introduced by
The function SilverStripe\Dev\SapphireTest::assertDOSEquals() has been deprecated: 4.0.0:5.0.0 Use assertListEquals() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

116
        /** @scrutinizer ignore-deprecated */ $this->assertDOSEquals(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
117
            [
118
                [ 'Name' => 'changeset 1' ],
119
                [ 'Name' => 'changeset 2' ],
120
                [ 'Name' => 'changeset 3' ],
121
                [ 'Name' => 'changeset 4' ],
122
            ],
123
            $results
124
        );
125
126
        // Test inferred, no published
127
        CampaignAdmin::config()->set('show_published', false);
128
        $results = $this->callProtectedMethod($admin, 'getListItems');
129
        $this->assertDOSEquals(
0 ignored issues
show
Deprecated Code introduced by
The function SilverStripe\Dev\SapphireTest::assertDOSEquals() has been deprecated: 4.0.0:5.0.0 Use assertListEquals() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

129
        /** @scrutinizer ignore-deprecated */ $this->assertDOSEquals(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
130
            [
131
                [ 'Name' => 'changeset 1' ],
132
                [ 'Name' => 'changeset 3' ],
133
            ],
134
            $results
135
        );
136
    }
137
138
    public function testReadCampaignResponse()
139
    {
140
        // Login as admin user
141
        $group = $this->objFromFixture(Group::class, 'admins');
142
        Permission::grant($group->ID, 'ADMIN');
143
144
        $admin = $this->objFromFixture(Member::class, 'admin_user');
145
        $this->logInAs($admin);
146
147
        /** @var ChangeSet $changeset */
148
        $changeset = $this->objFromFixture(ChangeSet::class, 'change1');
0 ignored issues
show
Unused Code introduced by
The assignment to $changeset is dead and can be removed.
Loading history...
149
150
        $expectedStatus = [
151
            '200' => [
152
                'ID' => '1',
153
                'Name' => 'show'
154
            ],
155
            '400' => [
156
                'ID' => '1',
157
                'Name' => ''
158
            ],
159
            '404' => [
160
                'ID' => '12345',
161
                'Name' => 'show'
162
            ]
163
        ];
164
165
        $campaignAdmin = CampaignAdmin::create();
166
167
        $request = new HTTPRequest('GET', '/admin/campaigns/set/');
168
        $request->addHeader('Accept', 'application/json');
169
170
        foreach ($expectedStatus as $key => $val) {
171
            $request->setRouteParams($val);
172
            $response = $campaignAdmin->readCampaign($request);
173
            $status = $response->getStatusCode();
174
175
            $this->assertInstanceOf(HTTPResponse::class, $response);
176
            $this->assertEquals($key, $status);
177
        }
178
179
        // Login as user without admin permissions
180
        $user = $this->objFromFixture(Member::class, 'mock_user');
181
        $this->logInAs($user);
182
183
        $request->setRouteParams([ 'ID' => '1', 'Name' => 'show']);
184
        $response = $campaignAdmin->readCampaign($request);
185
        $status = $response->getStatusCode();
186
187
        $this->assertInstanceOf(HTTPResponse::class, $response);
188
        $this->assertEquals('403', $status);
189
    }
190
}
191