Passed
Push — 1 ( f8e5cb...ba63b3 )
by Robbie
03:44
created

CampaignAdminTest::callProtectedMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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

90
        /** @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...
91
            [
92
                [ 'Name' => 'changeset 1' ],
93
            ],
94
            $results
95
        );
96
97
        // Test published, no inferred
98
        CampaignAdmin::config()->set('show_published', true);
99
        $results = $this->callProtectedMethod($admin, 'getListItems');
100
        $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

100
        /** @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...
101
            [
102
                [ 'Name' => 'changeset 1' ],
103
                [ 'Name' => 'changeset 2' ],
104
            ],
105
            $results
106
        );
107
108
        // Test published + inferred
109
        CampaignAdmin::config()->set('show_inferred', true);
110
        $results = $this->callProtectedMethod($admin, 'getListItems');
111
        $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

111
        /** @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...
112
            [
113
                [ 'Name' => 'changeset 1' ],
114
                [ 'Name' => 'changeset 2' ],
115
                [ 'Name' => 'changeset 3' ],
116
                [ 'Name' => 'changeset 4' ],
117
            ],
118
            $results
119
        );
120
121
        // Test inferred, no published
122
        CampaignAdmin::config()->set('show_published', false);
123
        $results = $this->callProtectedMethod($admin, 'getListItems');
124
        $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

124
        /** @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...
125
            [
126
                [ 'Name' => 'changeset 1' ],
127
                [ 'Name' => 'changeset 3' ],
128
            ],
129
            $results
130
        );
131
    }
132
}
133