Passed
Pull Request — 1 (#228)
by
unknown
12:31
created

CampaignAdminTest::testReadCampaignResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 66
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 38
c 2
b 1
f 0
dl 0
loc 66
rs 9.312
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
        // Create new ChangeSet object
148
        $changeset = ChangeSet::create();
149
        $changeset->ID = '123';
150
        $changeset->Name = 'changeset 123';
151
        $changeset->State = 'open';
152
        $changeset->IsInferred = false;
153
        
154
        $changeset->write();
155
156
        // ChangeSet should be accessable for Admin
157
        $this->assertTrue($changeset->canView());
158
159
        $expectedStatus = [
160
            '200' => [
161
                'ID' => '123',
162
                'Name' => 'show'
163
            ],
164
            '400' => [
165
                'ID' => '123',
166
                'Name' => ''
167
            ],
168
            '404' => [
169
                'ID' => '12345',
170
                'Name' => 'show'
171
            ]
172
        ];
173
174
        // Create new CampaignAdmin object
175
        $campaignAdmin = CampaignAdmin::create();
176
177
        $request = new HTTPRequest('GET', '/admin/campaigns/set/');
178
        $request->addHeader('Accept', 'application/json');
179
180
        foreach ($expectedStatus as $key => $val) {
181
            $request->setRouteParams($val);
182
            $response = $campaignAdmin->readCampaign($request);
183
            $status = $response->getStatusCode();
184
185
            // Method should return correct status
186
            $this->assertInstanceOf(HTTPResponse::class, $response);
187
            $this->assertEquals($key, $status);
188
        }
189
190
        // Login as user without admin permissions
191
        $user = $this->objFromFixture(Member::class, 'mock_user');
192
        $this->logInAs($user);
193
194
        // ChangeSet should not be accessable for Mock User
195
        $this->assertFalse($changeset->canView());
196
197
        $request->setRouteParams([ 'ID' => '123', 'Name' => 'show']);
198
        $response = $campaignAdmin->readCampaign($request);
199
        $status = $response->getStatusCode();
200
201
        // Method should return correct status if access forbidden
202
        $this->assertInstanceOf(HTTPResponse::class, $response);
203
        $this->assertEquals('403', $status);
204
    }
205
}
206