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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
<?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\Plugins\SegmentEditor\tests\Integration;
use Piwik\Access;
use Piwik\Date;
use Piwik\Piwik;
use Piwik\Plugins\SegmentEditor\API;
use Piwik\Plugins\SegmentEditor\Model;
use Piwik\Plugins\SitesManager\API as APISitesManager;
use Piwik\Tests\Framework\Mock\FakeAccess;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
use Exception;
/**
* Class Plugins_SegmentEditorTest
*
* @group Plugins
*/
class SegmentEditorTest extends IntegrationTestCase
{
public function setUp()
{
parent::setUp();
\Piwik\Plugin\Manager::getInstance()->loadPlugin('SegmentEditor');
\Piwik\Plugin\Manager::getInstance()->installLoadedPlugins();
// setup the access layer
$pseudoMockAccess = new FakeAccess;
FakeAccess::setIdSitesView(array(1, 2));
FakeAccess::setIdSitesAdmin(array(3, 4));
//finally we set the user as a Super User by default
FakeAccess::$superUser = true;
FakeAccess::$superUserLogin = 'superusertest';
Access::setSingletonInstance($pseudoMockAccess);
APISitesManager::getInstance()->addSite('test', 'http://example.org');
}
/**
* @group Plugins
*/
public function testAddInvalidSegment_ShouldThrow()
{
try {
API::getInstance()->add('name', 'test==test2');
$this->fail("Exception not raised.");
} catch (Exception $expected) {
}
try {
API::getInstance()->add('name', 'test');
$this->fail("Exception not raised.");
} catch (Exception $expected) {
}
}
/**
* @group Plugins
*/
public function test_AddAndGet_SimpleSegment()
{
$name = 'name';
$definition = 'searches>1,visitIp!=127.0.0.1';
$idSegment = API::getInstance()->add($name, $definition);
$this->assertEquals($idSegment, 1);
$segment = API::getInstance()->get($idSegment);
unset($segment['ts_created']);
$expected = array(
'idsegment' => 1,
'name' => $name,
'definition' => $definition,
'login' => 'superUserLogin',
'enable_all_users' => '0',
'enable_only_idsite' => '0',
'auto_archive' => '0',
'ts_last_edit' => null,
'deleted' => '0',
);
$this->assertEquals($segment, $expected);
}
/**
* @group Plugins
*/
public function test_AddAndGet_AnotherSegment()
{
$name = 'name';
$definition = 'searches>1,visitIp!=127.0.0.1';
$idSegment = API::getInstance()->add($name, $definition, $idSite = 1, $autoArchive = 1, $enabledAllUsers = 1);
$this->assertEquals($idSegment, 1);
// Testing get()
$segment = API::getInstance()->get($idSegment);
$expected = array(
'idsegment' => '1',
'name' => $name,
'definition' => $definition,
'login' => 'superUserLogin',
'enable_all_users' => '1',
'enable_only_idsite' => '1',
'auto_archive' => '1',
'ts_last_edit' => null,
'deleted' => '0',
);
unset($segment['ts_created']);
$this->assertEquals($segment, $expected);
// There is a segment to process for this particular site
$model = new Model();
$segments = $model->getSegmentsToAutoArchive($idSite);
unset($segments[0]['ts_created']);
$this->assertEquals($segments, array($expected));
// There is no segment to process for a non existing site
try {
$model->getSegmentsToAutoArchive(33);
$this->fail();
} catch(Exception $e) {
// expected
}
// There is no segment to process across all sites
$segments = $model->getSegmentsToAutoArchive($idSite = false);
$this->assertEquals($segments, array());
}
/**
* @group Plugins
*/
public function test_UpdateSegment()
{
$name = 'name"';
$definition = 'searches>1,visitIp!=127.0.0.1';
$nameSegment1 = 'hello';
$idSegment1 = API::getInstance()->add($nameSegment1, 'searches==0', $idSite = 1, $autoArchive = 1, $enabledAllUsers = 1);
$idSegment2 = API::getInstance()->add($name, $definition, $idSite = 1, $autoArchive = 1, $enabledAllUsers = 1);
$updatedSegment = array(
'idsegment' => $idSegment2,
'name' => 'NEW name',
'definition' => 'searches==0',
'enable_only_idsite' => '0',
'enable_all_users' => '0',
'auto_archive' => '0',
'ts_last_edit' => Date::now()->getDatetime(),
'ts_created' => Date::now()->getDatetime(),
'login' => Piwik::getCurrentUserLogin(),
'deleted' => '0',
);
API::getInstance()->update($idSegment2,
$updatedSegment['name'],
$updatedSegment['definition'],
$updatedSegment['enable_only_idsite'],
$updatedSegment['auto_archive'],
$updatedSegment['enable_all_users']
);
$newSegment = API::getInstance()->get($idSegment2);
// avoid test failures for when ts_created/ts_last_edit are different by between 1/2 secs
$this->removeSecondsFromSegmentInfo($updatedSegment);
$this->removeSecondsFromSegmentInfo($newSegment);
$this->assertEquals($newSegment, $updatedSegment);
// Check the other segmenet was not updated
$newSegment = API::getInstance()->get($idSegment1);
$this->assertEquals($newSegment['name'], $nameSegment1);
}
/**
* @group Plugins
*/
public function test_deleteSegment()
{
$idSegment1 = API::getInstance()->add('name 1', 'searches==0', $idSite = 1, $autoArchive = 1, $enabledAllUsers = 1);
$idSegment2 = API::getInstance()->add('name 2', 'searches>1,visitIp!=127.0.0.1', $idSite = 1, $autoArchive = 1, $enabledAllUsers = 1);
$deleted = API::getInstance()->delete($idSegment2);
$this->assertTrue($deleted);
try {
API::getInstance()->get($idSegment2);
$this->fail("getting deleted segment should have failed");
} catch(Exception $e) {
// expected
}
// and this should work
API::getInstance()->get($idSegment1);
}
private function removeSecondsFromSegmentInfo(&$segmentInfo)
{
$timestampProperties = array('ts_last_edit', 'ts_created');
foreach ($timestampProperties as $propertyName) {
if (isset($segmentInfo[$propertyName])) {
$segmentInfo[$propertyName] = substr($segmentInfo[$propertyName], 0, strlen($segmentInfo[$propertyName] - 2));
}
}
}
}
|