File size: 6,346 Bytes
bf8c0ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
174
175
176
<?php

/**
 * Matomo - free/libre analytics platform
 *
 * @link    https://matomo.org
 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Tests\Integration\Period;

use Piwik\Config;
use Piwik\Date;
use Piwik\Period;
use Piwik\Period\Day;
use Piwik\Period\Month;
use Piwik\Period\Range;
use Piwik\Period\Week;
use Piwik\Period\Year;
use Piwik\Tests\Framework\TestCase\UnitTestCase;

class TestPeriod
{
    // empty
}

class TestPeriodFactory extends Period\Factory
{
    /**
     * @var Config
     */
    private $config;

    // use constructor to make sure period factories are injected
    public function __construct(Config $config)
    {
        $this->config = $config;
    }

    public function shouldHandle($strPeriod, $strDate)
    {
        return $strPeriod == 'customperiod';
    }

    public function make($strPeriod, $date, $timezone)
    {
        return new TestPeriod();
    }
}

class MockPluginManager extends \Piwik\Plugin\Manager
{
    public function findComponents($componentName, $expectedSubclass)
    {
        if ($componentName == 'PeriodFactory') {
            return [
                TestPeriodFactory::class,
            ];
        }

        return parent::findComponents($componentName, $expectedSubclass);
    }
}

/**
 * @group PeriodFactoryTest
 */
class FactoryTest extends UnitTestCase
{
    /**
     * @dataProvider getTestDataForMakePeriodFromQueryParams
     */
    public function testMakePeriodFromQueryParamsAppliesTimezoneProperly($now, $timezone, $period, $date, $expectedLabel, $expectedRange)
    {
        Date::$now = strtotime($now);

        $factory = Period\Factory::makePeriodFromQueryParams($timezone, $period, $date);
        $this->assertEquals($expectedLabel, $factory->getLabel());
        $this->assertEquals($expectedRange, $factory->getRangeString());
    }

    public function getTestDataForMakePeriodFromQueryParams()
    {
        return [
            ['2020-12-24 03:37:00', 'America/Chicago', 'day', 'now', 'day', '2020-12-23,2020-12-23'],
            ['2020-12-24 03:37:00', 'America/Chicago', 'day', 'today', 'day', '2020-12-23,2020-12-23'],
            ['2020-12-24 16:37:00', 'America/Chicago', 'day', 'today', 'day', '2020-12-24,2020-12-24'],
            ['2020-12-24 22:37:00', 'UTC+5', 'day', 'today', 'day', '2020-12-25,2020-12-25'],
            ['2020-12-24 03:37:00', 'America/Chicago', 'day', 'yesterday', 'day', '2020-12-22,2020-12-22'],
            ['2020-12-24 03:37:00', 'UTC+5', 'day', 'yesterday', 'day', '2020-12-23,2020-12-23'],
            ['2020-12-24 16:37:00', 'UTC+12', 'day', 'yesterday', 'day', '2020-12-24,2020-12-24'],
            ['2020-12-24 03:37:00', 'America/Chicago', 'day', 'yesterdaySameTime', 'day', '2020-12-22,2020-12-22'],
            ['2020-12-24 03:37:00', 'America/Chicago', 'day', 'last-week', 'day', '2020-12-16,2020-12-16'],
            ['2020-12-24 03:37:00', 'America/Chicago', 'day', 'last-month', 'day', '2020-11-23,2020-11-23'],
            ['2020-12-24 03:37:00', 'UTC', 'week', 'last-month', 'week', '2020-11-23,2020-11-29'],
            ['2020-12-23 03:37:00', 'America/Chicago', 'week', 'last-month', 'week', '2020-11-16,2020-11-22'],
            ['2020-12-24 03:37:00', 'America/Chicago', 'day', 'last-year', 'day', '2019-12-23,2019-12-23'],
            ['2020-12-24 03:37:00', 'America/Chicago', 'day', '2020-12-23', 'day', '2020-12-23,2020-12-23'],
        ];
    }

    /**
     * @dataProvider getBuildTestData
     */
    public function testBuildCreatesCorrectPeriodInstances(
        $strPeriod,
        $date,
        $timezone,
        $expectedPeriodClass,
        $expectedRangeString
    ) {
        $period = Period\Factory::build($strPeriod, $date, $timezone);
        $this->assertInstanceOf($expectedPeriodClass, $period);
        $this->assertEquals($expectedRangeString, $period->getRangeString());
    }

    public function getBuildTestData()
    {
        return [
            ['day', '2015-01-01', 'UTC', Day::class, '2015-01-01,2015-01-01'],
            ['week', '2015-01-01', 'UTC', Week::class, '2014-12-29,2015-01-04'],
            ['month', '2015-01-01', 'UTC', Month::class, '2015-01-01,2015-01-31'],
            ['year', '2015-01-01', 'UTC', Year::class, '2015-01-01,2015-12-31'],

            ['range', '2015-01-01,2015-01-10', 'UTC', Range::class, '2015-01-01,2015-01-10'],
            ['range', '2015-01-01,2015-01-10', 'Antarctica/Casey', Range::class, '2015-01-01,2015-01-10'],
            ['range', '2015-01-01,2015-01-01', 'Antarctica/Casey', Day::class, '2015-01-01,2015-01-01'],

            // multiple periods
            ['day', '2015-01-01,2015-01-10', 'UTC', Range::class, '2015-01-01,2015-01-10'],
            ['week', '2015-01-01,2015-01-10', 'UTC', Range::class, '2014-12-29,2015-01-11'],
            ['month', '2015-01-01,2015-02-10', 'UTC', Range::class, '2015-01-01,2015-02-28'],
            ['year', '2015-01-01,2016-01-10', 'UTC', Range::class, '2015-01-01,2016-12-31'],
        ];
    }

    public function testMakePeriodFromQueryParams()
    {
        $factory = Period\Factory::makePeriodFromQueryParams('UTC', 'range', '2019-01-01,2019-01-01');
        $this->assertTrue($factory instanceof Day);
        $this->assertEquals('2019-01-01', $factory->toString());
    }

    public function testBuildCreatesCustomPeriodInstances()
    {
        Config::getInstance()->General['enabled_periods_API'] .= ',customperiod';

        $period = Period\Factory::build('customperiod', '2015-01-01');
        $this->assertInstanceOf(TestPeriod::class, $period);
    }

    public function testBuildThrowsIfPeriodIsUnrecognized()
    {
        $this->expectException(\Exception::class);
        $this->expectExceptionMessage('General_ExceptionInvalidPeriod');

        Period\Factory::build('garbageperiod', '2015-01-01');
    }

    public function testBuildThrowsIfPeriodIsNotEnabledForApi()
    {
        $this->expectException(\Exception::class);
        $this->expectExceptionMessage('General_ExceptionInvalidPeriod');

        Config::getInstance()->General['enabled_periods_API'] = 'day';
        Period\Factory::build('week', '2015-01-01');
    }

    public function provideContainerConfig()
    {
        return [
            \Piwik\Plugin\Manager::class => \Piwik\DI::autowire(MockPluginManager::class),
        ];
    }
}