Skip to content
Extraits de code Groupes Projets
Valider 6775356f rédigé par mattab's avatar mattab
Parcourir les fichiers

New method getAllUsersPreferences and test

parent 6156633b
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -32,6 +32,8 @@ use Piwik\Tracker\Cache;
*/
class API extends \Piwik\Plugin\API
{
const OPTION_NAME_PREFERENCE_SEPARATOR = '_';
/**
* @var Model
*/
......@@ -107,9 +109,34 @@ class API extends \Piwik\Plugin\API
return $this->getDefaultUserPreference($preferenceName, $userLogin);
}
/**
* Returns an array of Preferences
* @param $preferenceNames array of preference names
* @return array
* @ignore
*/
public function getAllUsersPreferences(array $preferenceNames)
{
Piwik::checkUserHasSuperUserAccess();
$userPreferences = array();
foreach($preferenceNames as $preferenceName) {
$optionNameMatchAllUsers = $this->getPreferenceId('%', $preferenceName);
$preferences = Option::getLike($optionNameMatchAllUsers);
foreach($preferences as $optionName => $optionValue) {
$optionName = explode(self::OPTION_NAME_PREFERENCE_SEPARATOR, $optionName);
$userName = $optionName[0];
$preference = $optionName[1];
$userPreferences[$userName][$preference] = $optionValue;
}
}
return $userPreferences;
}
private function getPreferenceId($login, $preference)
{
return $login . '_' . $preference;
return $login . self::OPTION_NAME_PREFERENCE_SEPARATOR . $preference;
}
private function getDefaultUserPreference($preferenceName, $login)
......
......@@ -69,4 +69,43 @@ class APITest extends IntegrationTestCase
$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_getAllUsersPreferences_shouldGetMultiplePreferences()
{
$user2 = 'userLogin2';
$user3 = 'userLogin3';
$this->api->addUser($user2, 'password', 'userlogin2@password.de');
$this->api->setUserPreference($user2, 'myPreferenceName', 'valueForUser2');
$this->api->setUserPreference($user2, 'Random_NOT_REQUESTED', 'Random_NOT_REQUESTED');
$this->api->addUser($user3, 'password', 'userlogin3@password.de');
$this->api->setUserPreference($user3, 'myPreferenceName', 'valueForUser3');
$this->api->setUserPreference($user3, 'otherPreferenceHere', 'otherPreferenceVALUE');
$this->api->setUserPreference($user3, 'Random_NOT_REQUESTED', 'Random_NOT_REQUESTED');
$expected = array(
$user2 => array(
'myPreferenceName' => 'valueForUser2'
),
$user3 => array(
'myPreferenceName' => 'valueForUser3',
'otherPreferenceHere' => 'otherPreferenceVALUE',
),
);
$result = $this->api->getAllUsersPreferences(array('myPreferenceName', 'otherPreferenceHere', 'randomDoesNotExist'));
$this->assertSame($expected, $result);
}
}
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter