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
|
<?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\UsersManager\tests;
use Piwik\Option;
use Piwik\Piwik;
use Piwik\Plugins\UsersManager\API;
use Piwik\Tests\Framework\Fixture;
use Piwik\Tests\Framework\Mock\FakeAccess;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
/**
* @group UsersManager
* @group APITest
* @group Plugins
*/
class APITest extends IntegrationTestCase
{
/**
* @var API
*/
private $api;
private $login = 'userLogin';
public function setUp()
{
parent::setUp();
$this->api = API::getInstance();
FakeAccess::$superUser = true;
Fixture::createWebsite('2014-01-01 00:00:00');
Fixture::createWebsite('2014-01-01 00:00:00');
Fixture::createWebsite('2014-01-01 00:00:00');
$this->api->addUser($this->login, 'password', 'userlogin@password.de');
}
public function test_setUserAccess_ShouldTriggerRemoveSiteAccessEvent_IfAccessToAWebsiteIsRemoved()
{
$eventTriggered = false;
$self = $this;
Piwik::addAction('UsersManager.removeSiteAccess', function ($login, $idSites) use (&$eventTriggered, $self) {
$eventTriggered = true;
$self->assertEquals($self->login, $login);
$self->assertEquals(array(1, 2), $idSites);
});
$this->api->setUserAccess($this->login, 'noaccess', array(1, 2));
$this->assertTrue($eventTriggered, 'UsersManager.removeSiteAccess event was not triggered');
}
public function test_setUserAccess_ShouldNotTriggerRemoveSiteAccessEvent_IfAccessIsAdded()
{
$eventTriggered = false;
Piwik::addAction('UsersManager.removeSiteAccess', function () use (&$eventTriggered) {
$eventTriggered = true;
});
$this->api->setUserAccess($this->login, 'admin', array(1, 2));
$this->assertFalse($eventTriggered, 'UsersManager.removeSiteAccess event was triggered but should not');
}
public function test_getAllUsersPreferences_isEmpty_whenNoPreference()
{
$preferences = $this->api->getAllUsersPreferences(array('preferenceName'));
$this->assertEmpty($preferences);
}
public function test_getAllUsersPreferences_isEmpty_whenNoPreferenceAndMultipleRequested()
{
$preferences = $this->api->getAllUsersPreferences(array('preferenceName', 'otherOne'));
$this->assertEmpty($preferences);
}
public function test_getUserPreference_ShouldReturnADefaultPreference_IfNoneIsSet()
{
$siteId = $this->api->getUserPreference($this->login, API::PREFERENCE_DEFAULT_REPORT);
$this->assertEquals('1', $siteId);
}
public function test_getUserPreference_ShouldReturnASetreference_IfNoneIsSet()
{
$this->api->setUserPreference($this->login, API::PREFERENCE_DEFAULT_REPORT, 5);
$siteId = $this->api->getUserPreference($this->login, API::PREFERENCE_DEFAULT_REPORT);
$this->assertEquals('5', $siteId);
}
public function test_initUserPreferenceWithDefault_ShouldSaveTheDefaultPreference_IfPreferenceIsNotSet()
{
// make sure there is no value saved so it will use default preference
$siteId = Option::get($this->getPreferenceId(API::PREFERENCE_DEFAULT_REPORT));
$this->assertFalse($siteId);
$this->api->initUserPreferenceWithDefault($this->login, API::PREFERENCE_DEFAULT_REPORT);
// make sure it did save the preference
$siteId = Option::get($this->getPreferenceId(API::PREFERENCE_DEFAULT_REPORT));
$this->assertEquals('1', $siteId);
}
public function test_initUserPreferenceWithDefault_ShouldNotSaveTheDefaultPreference_IfPreferenceIsAlreadySet()
{
// set value so there will already be a default
Option::set($this->getPreferenceId(API::PREFERENCE_DEFAULT_REPORT), '999');
$siteId = Option::get($this->getPreferenceId(API::PREFERENCE_DEFAULT_REPORT));
$this->assertEquals('999', $siteId);
$this->api->initUserPreferenceWithDefault($this->login, API::PREFERENCE_DEFAULT_REPORT);
// make sure it did not save the preference
$siteId = Option::get($this->getPreferenceId(API::PREFERENCE_DEFAULT_REPORT));
$this->assertEquals('999', $siteId);
}
public function test_getAllUsersPreferences_shouldGetMultiplePreferences()
{
$user2 = 'userLogin2';
$user3 = 'userLogin3';
$this->api->addUser($user2, 'password', 'userlogin2@password.de');
$this->api->setUserPreference($user2, 'myPreferenceName', 'valueForUser2');
$this->api->setUserPreference($user2, 'RandomNOTREQUESTED', 'RandomNOTREQUESTED');
$this->api->addUser($user3, 'password', 'userlogin3@password.de');
$this->api->setUserPreference($user3, 'myPreferenceName', 'valueForUser3');
$this->api->setUserPreference($user3, 'otherPreferenceHere', 'otherPreferenceVALUE');
$this->api->setUserPreference($user3, 'RandomNOTREQUESTED', 'RandomNOTREQUESTED');
$expected = array(
$user2 => array(
'myPreferenceName' => 'valueForUser2'
),
$user3 => array(
'myPreferenceName' => 'valueForUser3',
'otherPreferenceHere' => 'otherPreferenceVALUE',
),
);
$result = $this->api->getAllUsersPreferences(array('myPreferenceName', 'otherPreferenceHere', 'randomDoesNotExist'));
$this->assertSame($expected, $result);
}
public function test_getAllUsersPreferences_whenLoginContainsUnderscore()
{
$user2 = 'user_Login2';
$this->api->addUser($user2, 'password', 'userlogin2@password.de');
$this->api->setUserPreference($user2, 'myPreferenceName', 'valueForUser2');
$this->api->setUserPreference($user2, 'RandomNOTREQUESTED', 'RandomNOTREQUESTED');
$expected = array(
$user2 => array(
'myPreferenceName' => 'valueForUser2'
),
);
$result = $this->api->getAllUsersPreferences(array('myPreferenceName', 'otherPreferenceHere', 'randomDoesNotExist'));
$this->assertSame($expected, $result);
}
/**
* @expectedException \Exception
*/
public function test_setUserPreference_throws_whenPreferenceNameContainsUnderscore()
{
$user2 = 'userLogin2';
$this->api->addUser($user2, 'password', 'userlogin2@password.de');
$this->api->setUserPreference($user2, 'ohOH_myPreferenceName', 'valueForUser2');
}
public function test_getSitesAccessFromUser_forSuperUser()
{
$user2 = 'userLogin2';
$this->api->addUser($user2, 'password', 'userlogin2@password.de');
// new user doesn't have access to anything
$access = $this->api->getSitesAccessFromUser($user2);
$this->assertEmpty($access);
$this->api->setSuperUserAccess($user2, true);
// super user has admin access for every site
$access = $this->api->getSitesAccessFromUser($user2);
$expected = array(
array(
'site' => 1,
'access' => 'admin'
),
array(
'site' => 2,
'access' => 'admin'
),
array(
'site' => 3,
'access' => 'admin'
),
);
$this->assertEquals($expected, $access);
}
private function getPreferenceId($preferenceName)
{
return $this->login . '_' . $preferenceName;
}
public function provideContainerConfig()
{
return array(
'Piwik\Access' => new FakeAccess()
);
}
}
|