Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TimetableTest.php « Scheduler « Unit « PHPUnit « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7ed937460cbcd0d471b1ec5a20b07a5be1d4c7d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Tests\Unit\Scheduler;

use Piwik\Date;
use Piwik\Plugin;
use Piwik\Scheduler\Task;
use Piwik\Scheduler\Timetable;
use Piwik\Tests\Framework\Mock\PiwikOption;
use ReflectionProperty;

/**
 * @group Scheduler
 */
class TimetableTest extends \PHPUnit_Framework_TestCase
{
    private $timetable = array(
        'CoreAdminHome.purgeOutdatedArchives' => 1355529607,
        'PrivacyManager.deleteReportData_1'   => 1322229607,
    );

    public function tearDown()
    {
        self::resetPiwikOption();
    }

    /**
     * Dataprovider for testGetTimetableFromOptionValue
     */
    public function getTimetableFromOptionValueTestCases()
    {
        return array(

            // invalid option values should return a fresh array
            array(array(), false),
            array(array(), null),
            array(array(), 1),
            array(array(), ''),
            array(array(), 'test'),

            // valid serialized array
            array(
                array(
                    'CoreAdminHome.purgeOutdatedArchives' => 1355529607,
                    'PrivacyManager.deleteReportData'     => 1355529607,
                ),
                'a:2:{s:35:"CoreAdminHome.purgeOutdatedArchives";i:1355529607;s:31:"PrivacyManager.deleteReportData";i:1355529607;}'
            ),
        );
    }

    /**
     * @dataProvider getTimetableFromOptionValueTestCases
     */
    public function testGetTimetableFromOptionValue($expectedTimetable, $option)
    {
        self::stubPiwikOption($option);

        $timetable = new Timetable();
        $this->assertEquals($expectedTimetable, $timetable->getTimetable());
    }

    public function testRescheduleTaskAndRunTomorrow()
    {
        self::stubPiwikOption(serialize([]));

        $timetable = new Timetable();
        $task = $this->getMockBuilder(Task::class)
            ->disableOriginalConstructor()
            ->getMock();
        $task->method('getName')->willReturn('taskName');

        $timetable->rescheduleTaskAndRunTomorrow($task);

        $this->assertEquals(Date::factory('tomorrow')->getTimeStamp(), $timetable->getTimetable()[$task->getName()]);
    }

    /**
     * Dataprovider for testTaskHasBeenScheduledOnce
     */
    public function taskHasBeenScheduledOnceTestCases()
    {
        return array(
            array(true, 'CoreAdminHome.purgeOutdatedArchives', $this->timetable),
            array(true, 'PrivacyManager.deleteReportData_1', $this->timetable),
            array(false, 'ScheduledReports.weeklySchedule"', $this->timetable)
        );
    }

    /**
     * @dataProvider taskHasBeenScheduledOnceTestCases
     */
    public function testTaskHasBeenScheduledOnce($expectedDecision, $taskName, $timetable)
    {
        self::stubPiwikOption(false);

        $timetableObj = new Timetable();
        $timetableObj->setTimetable($timetable);
        $this->assertEquals($expectedDecision, $timetableObj->taskHasBeenScheduledOnce($taskName));

        self::resetPiwikOption();
    }

    /**
     * Dataprovider for testGetScheduledTimeForMethod
     */
    public function getScheduledTimeForMethodTestCases()
    {
        $timetable = serialize($this->timetable);

        return array(
            array(1355529607, 'CoreAdminHome', 'purgeOutdatedArchives', null, $timetable),
            array(1322229607, 'PrivacyManager', 'deleteReportData', 1, $timetable),
            array(false, 'ScheduledReports', 'weeklySchedule', null, $timetable)
        );
    }

    /**
     * Dataprovider for testTaskShouldBeExecuted
     */
    public function taskShouldBeExecutedTestCases()
    {
        $timetable = $this->timetable;

        // set a date in the future (should not run)
        $timetable['CoreAdminHome.purgeOutdatedArchives'] = time() + 60000;

        // set now (should run)
        $timetable['PrivacyManager.deleteReportData_1'] = time();

        return array(
            array(false, 'CoreAdminHome.purgeOutdatedArchives', $timetable),
            array(true, 'PrivacyManager.deleteReportData_1', $timetable),
            array(false, 'ScheduledReports.weeklySchedule"', $timetable)
        );
    }

    /**
     * @dataProvider taskShouldBeExecutedTestCases
     */
    public function testTaskShouldBeExecuted($expectedDecision, $taskName, $timetable)
    {
        self::stubPiwikOption(serialize($timetable));

        $timetable = new Timetable();
        $this->assertEquals($expectedDecision, $timetable->shouldExecuteTask($taskName));

        self::resetPiwikOption();
    }

    private static function stubPiwikOption($timetable)
    {
        self::getReflectedPiwikOptionInstance()->setValue(new PiwikOption($timetable));
    }

    private static function resetPiwikOption()
    {
        self::getReflectedPiwikOptionInstance()->setValue(null);
    }

    private static function getReflectedPiwikOptionInstance()
    {
        $piwikOptionInstance = new ReflectionProperty('Piwik\Option', 'instance');
        $piwikOptionInstance->setAccessible(true);
        return $piwikOptionInstance;
    }
}