Newer
Older
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
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Tests\Unit\CronArchive;
use Piwik\Date;
use Piwik\CronArchive\SegmentArchivingRequestUrlProvider;
/**
* @group Core
*/
class SegmentArchivingRequestUrlProviderTest extends \PHPUnit_Framework_TestCase
{
const BASE_URL = 'http://base/';
const TOKEN_AUTH = 'tokenauth';
const TEST_NOW = '2015-03-01';
private $mockSegmentEntries;
public function setUp()
{
$this->mockSegmentEntries = array(
array(
'ts_created' => '2014-01-01',
'definition' => 'browserName==FF',
'enable_only_idsite' => 1
),
array(
'ts_created' => '2014-01-01',
'definition' => 'countryCode==us',
'enable_only_idsite' => 1
),
array(
'ts_created' => '2012-01-01',
'definition' => 'countryCode==us',
'enable_only_idsite' => 1
),
array(
'ts_created' => '2014-01-01',
'definition' => 'countryCode==ca',
'enable_only_idsite' => 2
),
array(
'ts_created' => '2012-01-01',
'definition' => 'countryCode==ca',
'enable_only_idsite' => 2
),
array(
'ts_created' => '2011-01-01',
'definition' => 'countryCode==ca',
'enable_only_idsite' => 0
)
);
}
/**
* @dataProvider getUrlToArchiveSegmentTestData
*/
public function test_getUrlToArchiveSegment_CorrectlyModifiesDateInOutputUrl($processNewSegmentsFrom, $idSite, $date, $period, $segment, $expected)
{
$urlProvider = $this->createUrlProviderToTest($processNewSegmentsFrom);
$actual = $urlProvider->getUrlToArchiveSegment($idSite, $period, $date, $segment);
$this->assertEquals($expected, $actual);
}
public function getUrlToArchiveSegmentTestData()
{
$dateRange = '2010-02-01,' . self::TEST_NOW;
return array(
array( // test beginning_of_time does not modify date
'beginning_of_time',
1,
$dateRange,
'week',
'browserName==FF',
"http://base/?module=API&method=API.get&idSite=1&period=week&date=$dateRange&format=php&token_auth=tokenauth&segment=" . urlencode('browserName==FF')
),
array( // test garbage string does not modify date
'salkdfjsdfl',
1,
$dateRange,
'week',
'browserName==FF',
"http://base/?module=API&method=API.get&idSite=1&period=week&date=$dateRange&format=php&token_auth=tokenauth&segment=" . urlencode('browserName==FF')
),
array( // test creation_time uses creation time of segment
'creation_time',
1,
$dateRange,
'week',
'browserName==FF',
'http://base/?module=API&method=API.get&idSite=1&period=week&date=2014-01-01,2015-03-01&format=php&token_auth=tokenauth&segment=' . urlencode('browserName==FF')
),
array( // test creation_time uses earliest time of segment if multiple match (multiple for site)
'creation_time',
1,
$dateRange,
'week',
'countryCode==us',
'http://base/?module=API&method=API.get&idSite=1&period=week&date=2012-01-01,2015-03-01&format=php&token_auth=tokenauth&segment=' . urlencode('countryCode==us')
),
array( // test creation_time uses earliest time of segment if multiple match (multiple for site + one for all)
'creation_time',
2,
$dateRange,
'week',
'countryCode==ca',
'http://base/?module=API&method=API.get&idSite=2&period=week&date=2011-01-01,2015-03-01&format=php&token_auth=tokenauth&segment=' . urlencode('countryCode==ca')
),
array( // test 'now' is used if no site matches (testing w/o any segments)
'creation_time',
1,
$dateRange,
'week',
'pageTitle==abc',
'http://base/?module=API&method=API.get&idSite=1&period=week&date=2015-03-01,2015-03-01&format=php&token_auth=tokenauth&segment=' . urlencode('pageTitle==abc')
),
array( // test 'now' is used if no site matches (testing w/ segment for another site)
'creation_time',
3,
$dateRange,
'week',
'countryCode==us',
'http://base/?module=API&method=API.get&idSite=3&period=week&date=2015-03-01,2015-03-01&format=php&token_auth=tokenauth&segment=' . urlencode('countryCode==us')
),
array( // test lastN rewinds created date by N days
'last10',
1,
$dateRange,
'week',
'countryCode==us',
'http://base/?module=API&method=API.get&idSite=1&period=week&date=2011-12-22,2015-03-01&format=php&token_auth=tokenauth&segment=' . urlencode('countryCode==us')
),
array( // test lastN rewinds now by N days (testing w/ no found segment)
'last10',
3,
$dateRange,
'week',
'countryCode==us',
'http://base/?module=API&method=API.get&idSite=3&period=week&date=2015-02-19,2015-03-01&format=php&token_auth=tokenauth&segment=' . urlencode('countryCode==us')
),
);
}
private function createUrlProviderToTest($processNewSegmentsFrom)
{
$mockSegmentEditorModel = $this->getMock('Piwik\Plugins\SegmentEditor\Model', array('getAllSegmentsAndIgnoreVisibility'));
$mockSegmentEditorModel->expects($this->any())->method('getAllSegmentsAndIgnoreVisibility')->will($this->returnValue($this->mockSegmentEntries));
return new SegmentArchivingRequestUrlProvider(self::BASE_URL, self::TOKEN_AUTH, $processNewSegmentsFrom,
$mockSegmentEditorModel, null, Date::factory(self::TEST_NOW));
}
}