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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
<?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\Goals\tests;
use Piwik\Access;
use Piwik\Plugins\Goals\API;
use Piwik\Tests\Fixture;
/**
* @group Goals
* @group Plugins
* @group APITest
* @group Database
*/
class APITest extends \DatabaseTestCase
{
/**
* @var API
*/
private $api;
private $idSite = 1;
public function setUp()
{
parent::setUp();
$this->api = API::getInstance();
Fixture::createWebsite('2014-01-01 00:00:00');
Fixture::createWebsite('2014-01-01 00:00:00');
}
public function test_addGoal_shouldReturnGoalId_IfCreationIsSuccessful()
{
$idGoal = $this->createAnyGoal();
$this->assertSame(1, $idGoal);
}
public function test_addGoal_shouldSucceed_IfOnlyMinimumFieldsGiven()
{
$idGoal = $this->api->addGoal($this->idSite, 'MyName', 'url', 'http://www.test.de', 'exact');
$this->assertGoal($idGoal, 'MyName', 'url', 'http://www.test.de', 'exact', 0, 0, 0);
}
public function test_addGoal_ShouldSucceed_IfAllFieldsGiven()
{
$idGoal = $this->api->addGoal($this->idSite, 'MyName', 'url', 'http://www.test.de', 'exact', true, 50, true);
$this->assertGoal($idGoal, 'MyName', 'url', 'http://www.test.de', 'exact', 1, 50, 1);
}
/**
* @expectedException \Exception
* @expectedExceptionMessage Goals_ExceptionInvalidMatchingString
*/
public function test_addGoal_shouldThrowException_IfPatternTypeIsExactAndMatchAttributeNotEvent()
{
$this->api->addGoal($this->idSite, 'MyName', 'url', 'www.test.de', 'exact');
}
/**
* @expectedException \Exception
* @expectedExceptionMessage Goals_ExceptionInvalidMatchingString
*/
public function test_addGoal_shouldThrowException_IfPatternTypeIsExactAndMatchAttributeNotEvent2()
{
$this->api->addGoal($this->idSite, 'MyName', 'external_website', 'www.test.de', 'exact');
}
public function test_addGoal_shouldNotThrowException_IfPatternTypeIsExactAndMatchAttributeIsEvent()
{
$this->api->addGoal($this->idSite, 'MyName1', 'event_action', 'test', 'exact');
$this->api->addGoal($this->idSite, 'MyName2', 'event_name', 'test', 'exact');
$idGoal = $this->api->addGoal($this->idSite, 'MyName3', 'event_category', 'test', 'exact');
$this->assertSame('3', $idGoal);
}
/**
* @expectedException \Exception
* @expectedExceptionMessage checkUserHasAdminAccess Fake exception
*/
public function test_addGoal_shouldThrowException_IfNotEnoughPermission()
{
$this->setNonAdminUser();
$this->createAnyGoal();
}
/**
* @expectedException \Exception
* @expectedExceptionMessage checkUserHasAdminAccess Fake exception
*/
public function test_updateGoal_shouldThrowException_IfNotEnoughPermission()
{
$idGoal = $this->createAnyGoal();
$this->assertSame(1, $idGoal); // make sure goal is created and does not already fail here
$this->setNonAdminUser();
$this->api->updateGoal($this->idSite, $idGoal, 'MyName', 'url', 'www.test.de', 'exact');
}
/**
* @expectedException \Exception
* @expectedExceptionMessage Goals_ExceptionInvalidMatchingString
*/
public function test_updateGoal_shouldThrowException_IfPatternTypeIsExactAndMatchAttributeNotEvent()
{
$idGoal = $this->createAnyGoal();
$this->api->updateGoal($this->idSite, $idGoal, 'MyName', 'url', 'www.test.de', 'exact');
}
public function test_updateGoal_shouldNotThrowException_IfPatternTypeIsExactAndMatchAttributeIsEvent()
{
$idGoal = $this->createAnyGoal();
$this->api->updateGoal($this->idSite, $idGoal, 'MyName', 'event_action', 'www.test.de', 'exact');
$this->api->updateGoal($this->idSite, $idGoal, 'MyName', 'event_category', 'www.test.de', 'exact');
$this->api->updateGoal($this->idSite, $idGoal, 'MyName', 'event_name', 'www.test.de', 'exact');
$this->assertSame(1, $idGoal);
}
public function test_updateGoal_shouldUpdateAllGivenFields()
{
$idGoal = $this->createAnyGoal();
$this->api->updateGoal($this->idSite, $idGoal, 'UpdatedName', 'file', 'http://www.updatetest.de', 'contains', true, 999, true);
$this->assertGoal($idGoal, 'UpdatedName', 'file', 'http://www.updatetest.de', 'contains', 1, 999, 1);
}
public function test_updateGoal_shouldUpdateMinimalFields_ShouldLeaveOtherFieldsUntouched()
{
$idGoal = $this->createAnyGoal();
$this->api->updateGoal($this->idSite, $idGoal, 'UpdatedName', 'file', 'http://www.updatetest.de', 'contains');
$this->assertGoal($idGoal, 'UpdatedName', 'file', 'http://www.updatetest.de', 'contains', 0, 0, 0);
}
public function test_deleteGoal_shouldNotDeleteAGoal_IfGoalIdDoesNotExist()
{
$this->assertHasNoGoals();
$this->createAnyGoal();
$this->assertHasGoals();
$this->api->deleteGoal($this->idSite, 999);
$this->assertHasGoals();
}
public function test_deleteGoal_shouldNotDeleteAGoal_IfSiteDoesNotMatchGoalId()
{
$this->assertHasNoGoals();
$idGoal = $this->createAnyGoal();
$this->assertHasGoals();
$this->api->deleteGoal($idSite = 2, $idGoal);
$this->assertHasGoals();
}
public function test_deleteGoal_shouldDeleteAGoal_IfGoalAndSiteMatches()
{
$this->assertHasNoGoals();
$idGoal = $this->createAnyGoal();
$this->assertHasGoals();
$this->api->deleteGoal($this->idSite, $idGoal);
$this->assertHasNoGoals();
}
private function assertHasGoals()
{
$goals = $this->getGoals();
$this->assertNotEmpty($goals);
}
private function assertHasNoGoals()
{
$goals = $this->getGoals();
$this->assertEmpty($goals);
}
private function assertGoal($idGoal, $name, $url, $pattern, $patternType, $caseSenstive = 0, $revenue = 0, $allowMultiple = 0)
{
$expected = array($idGoal => array(
'idsite' => $this->idSite,
'idgoal' => $idGoal,
'name' => $name,
'match_attribute' => $url,
'pattern' => $pattern,
'pattern_type' => $patternType,
'case_sensitive' => $caseSenstive,
'allow_multiple' => $allowMultiple,
'revenue' => $revenue,
'deleted' => 0
));
$goals = $this->getGoals();
$this->assertEquals($expected, $goals);
}
private function getGoals()
{
return $this->api->getGoals($this->idSite);
}
private function createAnyGoal()
{
return $this->api->addGoal($this->idSite, 'MyName1', 'event_action', 'test', 'exact');
}
protected function setNonAdminUser()
{
$pseudoMockAccess = new \FakeAccess;
\FakeAccess::setSuperUserAccess(false);
\FakeAccess::$idSitesView = array(99);
\FakeAccess::$identity = 'aUser';
Access::setSingletonInstance($pseudoMockAccess);
}
}
|