File: AddSegmentByLabelInUTCTest.php

package info (click to toggle)
matomo 5.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 95,068 kB
  • sloc: php: 289,425; xml: 127,249; javascript: 112,130; python: 202; sh: 178; makefile: 20; sql: 10
file content (113 lines) | stat: -rw-r--r-- 3,423 bytes parent folder | download
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
<?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\Plugins\VisitTime\tests\Unit;

use Piwik\DataTable\Row;
use Piwik\DataTable;

/**
 * @group VisitTime
 * @group AddSegmentByLabelInUTCTest
 * @group Plugins
 */
class AddSegmentByLabelInUTCTest extends \PHPUnit\Framework\TestCase
{
    private $filter = 'Piwik\Plugins\VisitTime\DataTable\Filter\AddSegmentByLabelInUTC';

    /**
     * @var DataTable
     */
    private $table;

    public function setUp(): void
    {
        $this->table = new DataTable();
        $this->addRow(array('label' => '0'));
        $this->addRow(array('label' => '1'));
        $this->addRow(array('label' => '2'));
        $this->addRow(array('label' => '12'));
        $this->addRow(array('label' => '13'));
        $this->addRow(array('label' => '14'));
        $this->addRow(array('label' => '20'));
    }

    private function addRow($columns)
    {
        $this->table->addRow($this->buildRow($columns));
    }

    private function buildRow($columns)
    {
        return new Row(array(Row::COLUMNS => $columns));
    }

    public function testFilterShouldNotChangeHoursIfTimezoneIsUTCAlready()
    {
        $this->table->filter($this->filter, array('UTC', 'day', 'today'));

        $this->assertSegmentValues(array('0', '1', '2', '12', '13', '14', '20'));
    }

    public function testFilterShouldConvertHoursFromTimezoneIntoUTCMinus1()
    {
        $this->table->filter($this->filter, array('UTC-1', 'day', 'today'));
        $this->assertSegmentValues(array('1', '2', '3', '13', '14', '15', '21'));
    }

    public function testFilterShouldConvertHoursFromTimezoneIntoUTCPlus1()
    {
        $this->table->filter($this->filter, array('UTC+1', 'day', 'today'));
        $this->assertSegmentValuesInUTCplus1();
    }

    public function testFilterShouldHandleRangePeriodPlus1()
    {
        $this->table->filter($this->filter, array('UTC+1', 'range', '2015-02-02,2015-02-14'));
        $this->assertSegmentValuesInUTCplus1();
    }

    public function testFilterShouldHandleRangePeriodWithLast7Plus1()
    {
        $this->table->filter($this->filter, array('UTC+1', 'range', 'last7'));
        $this->assertSegmentValuesInUTCplus1();
    }

    public function testFilterShouldHandleDayPeriodWithRangePlus1()
    {
        $this->table->filter($this->filter, array('UTC+1', 'day', '2015-02-02,2015-02-14'));
        $this->assertSegmentValuesInUTCplus1();
    }

    public function testFilterShouldHandleWeekWithLast7Plus1()
    {
        $this->table->filter($this->filter, array('UTC+1', 'day', 'last7'));
        $this->assertSegmentValuesInUTCplus1();
    }

    public function testFilterShouldIgnoreSummaryRow()
    {
        $row = $this->buildRow(array('label' => 'other'));
        $this->table->addSummaryRow($row);
        $this->table->filter($this->filter, array('UTC', 'day', 'today'));

        $this->assertFalse($row->getMetadata('segmentValue'));
    }

    private function assertSegmentValuesInUTCplus1()
    {
        $this->assertSegmentValues(array('23', '0', '1', '11', '12', '13', '19'));
    }

    private function assertSegmentValues($expectedSegmentValues)
    {
        $segmentValues = $this->table->getRowsMetadata('segmentValue');
        $this->assertSame($expectedSegmentValues, $segmentValues);
    }
}