CampaignAdminTest::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\FunctionalTest;
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 FunctionalTest
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 readCampaignDataProvider()
139
    {
140
        return [
141
            'valid campaign' => ['change1', 'CMS_ACCESS_CampaignAdmin', 200],
142
            'non existent campaign' => ['', 'CMS_ACCESS_CampaignAdmin', 404],
143
            'inferred campaign' => ['change3', 'CMS_ACCESS_CampaignAdmin', 404],
144
            'not enough permission' => ['change1', 'VIEW_SITE', 403],
145
        ];
146
    }
147
148
    /**
149
     * @dataProvider readCampaignDataProvider
150
     */
151
    public function testReadCampaign(
152
        string $changesetName,
153
        string $permission,
154
        int $expectedResponseCode
155
    ) {
156
        $this->logOut();
157
        $this->logInWithPermission($permission);
158
        $changeSetID = $changesetName ? $this->idFromFixture(ChangeSet::class, $changesetName) : 12345;
159
        $response = $this->get("/admin/campaigns/set/$changeSetID/show", null, ['Accept' => 'application/json']);
160
        $this->assertEquals($expectedResponseCode, $response->getStatusCode());
161
    }
162
}
163