aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/private/Accounts/AccountManager.php243
-rw-r--r--tests/lib/Accounts/AccountManagerTest.php269
2 files changed, 251 insertions, 261 deletions
diff --git a/lib/private/Accounts/AccountManager.php b/lib/private/Accounts/AccountManager.php
index ddc4e7c2fb0..89d4ac965ae 100644
--- a/lib/private/Accounts/AccountManager.php
+++ b/lib/private/Accounts/AccountManager.php
@@ -32,6 +32,7 @@
*/
namespace OC\Accounts;
+use InvalidArgumentException;
use libphonenumber\NumberParseException;
use libphonenumber\PhoneNumber;
use libphonenumber\PhoneNumberFormat;
@@ -39,7 +40,9 @@ use libphonenumber\PhoneNumberUtil;
use OCA\Settings\BackgroundJobs\VerifyUserData;
use OCP\Accounts\IAccount;
use OCP\Accounts\IAccountManager;
+use OCP\Accounts\IAccountProperty;
use OCP\Accounts\IAccountPropertyCollection;
+use OCP\Accounts\PropertyDoesNotExistException;
use OCP\BackgroundJob\IJobList;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IConfig;
@@ -49,7 +52,9 @@ use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use function array_flip;
+use function iterator_to_array;
use function json_decode;
+use function json_encode;
use function json_last_error;
/**
@@ -99,7 +104,7 @@ class AccountManager implements IAccountManager {
/**
* @param string $input
* @return string Provided phone number in E.164 format when it was a valid number
- * @throws \InvalidArgumentException When the phone number was invalid or no default region is set and the number doesn't start with a country code
+ * @throws InvalidArgumentException When the phone number was invalid or no default region is set and the number doesn't start with a country code
*/
protected function parsePhoneNumber(string $input): string {
$defaultRegion = $this->config->getSystemValueString('default_phone_region', '');
@@ -107,7 +112,7 @@ class AccountManager implements IAccountManager {
if ($defaultRegion === '') {
// When no default region is set, only +49… numbers are valid
if (strpos($input, '+') !== 0) {
- throw new \InvalidArgumentException(self::PROPERTY_PHONE);
+ throw new InvalidArgumentException(self::PROPERTY_PHONE);
}
$defaultRegion = 'EN';
@@ -122,81 +127,100 @@ class AccountManager implements IAccountManager {
} catch (NumberParseException $e) {
}
- throw new \InvalidArgumentException(self::PROPERTY_PHONE);
+ throw new InvalidArgumentException(self::PROPERTY_PHONE);
}
/**
*
* @param string $input
* @return string
- * @throws \InvalidArgumentException When the website did not have http(s) as protocol or the host name was empty
+ * @throws InvalidArgumentException When the website did not have http(s) as protocol or the host name was empty
*/
protected function parseWebsite(string $input): string {
$parts = parse_url($input);
if (!isset($parts['scheme']) || ($parts['scheme'] !== 'https' && $parts['scheme'] !== 'http')) {
- throw new \InvalidArgumentException(self::PROPERTY_WEBSITE);
+ throw new InvalidArgumentException(self::PROPERTY_WEBSITE);
}
if (!isset($parts['host']) || $parts['host'] === '') {
- throw new \InvalidArgumentException(self::PROPERTY_WEBSITE);
+ throw new InvalidArgumentException(self::PROPERTY_WEBSITE);
}
return $input;
}
- protected function sanitizeLength(array &$propertyData, bool $throwOnData = false): void {
- if (isset($propertyData['value']) && strlen($propertyData['value']) > 2048) {
+ /**
+ * @param IAccountProperty[] $properties
+ */
+ protected function testValueLengths(array $properties, bool $throwOnData = false): void {
+ foreach ($properties as $property) {
+ if (strlen($property->getValue()) > 2048) {
+ if ($throwOnData) {
+ throw new InvalidArgumentException();
+ } else {
+ $property->setValue('');
+ }
+ }
+ }
+ }
+
+ protected function testPropertyScope(IAccountProperty $property, array $allowedScopes, bool $throwOnData): void {
+ if ($throwOnData && !in_array($property->getScope(), $allowedScopes, true)) {
+ throw new InvalidArgumentException('scope');
+ }
+
+ if (
+ $property->getScope() === self::SCOPE_PRIVATE
+ && in_array($property->getName(), [self::PROPERTY_DISPLAYNAME, self::PROPERTY_EMAIL])
+ ) {
if ($throwOnData) {
- throw new \InvalidArgumentException();
+ // v2-private is not available for these fields
+ throw new InvalidArgumentException('scope');
} else {
- $propertyData['value'] = '';
+ // default to local
+ $property->setScope(self::SCOPE_LOCAL);
}
+ } else {
+ // migrate scope values to the new format
+ // invalid scopes are mapped to a default value
+ $property->setScope(AccountProperty::mapScopeToV2($property->getScope()));
}
}
- protected function testValueLengths(array &$data, bool $throwOnData = false): void {
+ protected function sanitizePhoneNumberValue(IAccountProperty $property, bool $throwOnData = false) {
+ if ($property->getName() !== self::PROPERTY_PHONE) {
+ if ($throwOnData) {
+ throw new InvalidArgumentException(sprintf('sanitizePhoneNumberValue can only sanitize phone numbers, %s given', $property->getName()));
+ }
+ return;
+ }
+ if ($property->getValue() === '') {
+ return;
+ }
try {
- foreach ($data as $propertyName => &$propertyData) {
- if ($this->isCollection($propertyName)) {
- $this->testValueLengths($propertyData, $throwOnData);
- } else {
- $this->sanitizeLength($propertyData, $throwOnData);
- }
+ $property->setValue($this->parsePhoneNumber($property->getValue()));
+ } catch (InvalidArgumentException $e) {
+ if ($throwOnData) {
+ throw $e;
}
- } catch (\InvalidArgumentException $e) {
- throw new \InvalidArgumentException($propertyName);
+ $property->setValue('');
}
}
- protected function testPropertyScopes(array &$data, array $allowedScopes, bool $throwOnData = false, string $parentPropertyName = null): void {
- foreach ($data as $propertyNameOrIndex => &$propertyData) {
- if ($this->isCollection($propertyNameOrIndex)) {
- $this->testPropertyScopes($propertyData, $allowedScopes, $throwOnData);
- } elseif (isset($propertyData['scope'])) {
- $effectivePropertyName = $parentPropertyName ?? $propertyNameOrIndex;
-
- if ($throwOnData && !in_array($propertyData['scope'], $allowedScopes, true)) {
- throw new \InvalidArgumentException('scope');
- }
-
- if (
- $propertyData['scope'] === self::SCOPE_PRIVATE
- && ($effectivePropertyName === self::PROPERTY_DISPLAYNAME || $effectivePropertyName === self::PROPERTY_EMAIL)
- ) {
- if ($throwOnData) {
- // v2-private is not available for these fields
- throw new \InvalidArgumentException('scope');
- } else {
- // default to local
- $data[$propertyNameOrIndex]['scope'] = self::SCOPE_LOCAL;
- }
- } else {
- // migrate scope values to the new format
- // invalid scopes are mapped to a default value
- $data[$propertyNameOrIndex]['scope'] = AccountProperty::mapScopeToV2($propertyData['scope']);
- }
+ protected function sanitizeWebsite(IAccountProperty $property, bool $throwOnData = false) {
+ if ($property->getName() !== self::PROPERTY_WEBSITE) {
+ if ($throwOnData) {
+ throw new InvalidArgumentException(sprintf('sanitizeWebsite can only sanitize web domains, %s given', $property->getName()));
}
}
+ try {
+ $property->setValue($this->parseWebsite($property->getValue()));
+ } catch (InvalidArgumentException $e) {
+ if ($throwOnData) {
+ throw $e;
+ }
+ $property->setValue('');
+ }
}
/**
@@ -206,51 +230,12 @@ class AccountManager implements IAccountManager {
* @param array $data
* @param bool $throwOnData Set to true if you can inform the user about invalid data
* @return array The potentially modified data (e.g. phone numbers are converted to E.164 format)
- * @throws \InvalidArgumentException Message is the property that was invalid
+ * @throws InvalidArgumentException Message is the property that was invalid
*/
public function updateUser(IUser $user, array $data, bool $throwOnData = false): array {
$userData = $this->getUser($user);
$updated = true;
- if (isset($data[self::PROPERTY_PHONE]) && $data[self::PROPERTY_PHONE]['value'] !== '') {
- // Sanitize null value.
- $data[self::PROPERTY_PHONE]['value'] = $data[self::PROPERTY_PHONE]['value'] ?? '';
-
- try {
- $data[self::PROPERTY_PHONE]['value'] = $this->parsePhoneNumber($data[self::PROPERTY_PHONE]['value']);
- } catch (\InvalidArgumentException $e) {
- if ($throwOnData) {
- throw $e;
- }
- $data[self::PROPERTY_PHONE]['value'] = '';
- }
- }
-
- $this->testValueLengths($data);
-
- if (isset($data[self::PROPERTY_WEBSITE]) && $data[self::PROPERTY_WEBSITE]['value'] !== '') {
- try {
- $data[self::PROPERTY_WEBSITE]['value'] = $this->parseWebsite($data[self::PROPERTY_WEBSITE]['value']);
- } catch (\InvalidArgumentException $e) {
- if ($throwOnData) {
- throw $e;
- }
- $data[self::PROPERTY_WEBSITE]['value'] = '';
- }
- }
-
- $allowedScopes = [
- self::SCOPE_PRIVATE,
- self::SCOPE_LOCAL,
- self::SCOPE_FEDERATED,
- self::SCOPE_PUBLISHED,
- self::VISIBILITY_PRIVATE,
- self::VISIBILITY_CONTACTS_ONLY,
- self::VISIBILITY_PUBLIC,
- ];
-
- $this->testPropertyScopes($data, $allowedScopes, $throwOnData);
-
if (empty($userData)) {
$this->insertNewUser($user, $data);
} elseif ($userData !== $data) {
@@ -312,7 +297,7 @@ class AccountManager implements IAccountManager {
->from($this->table)
->where($query->expr()->eq('uid', $query->createParameter('uid')))
->setParameter('uid', $uid);
- $result = $query->execute();
+ $result = $query->executeQuery();
$accountData = $result->fetchAll();
$result->closeCursor();
@@ -345,7 +330,7 @@ class AccountManager implements IAccountManager {
$matches = [];
foreach ($chunks as $chunk) {
$query->setParameter('values', $chunk, IQueryBuilder::PARAM_STR_ARRAY);
- $result = $query->execute();
+ $result = $query->executeQuery();
while ($row = $result->fetch()) {
$matches[$row['uid']] = $row['value'];
@@ -404,6 +389,13 @@ class AccountManager implements IAccountManager {
if (!isset($userData[$key]['verified']) && !$this->isCollection($key)) {
$userData[$key]['verified'] = self::NOT_VERIFIED;
}
+ if ($this->isCollection($key)) {
+ foreach ($value as &$singlePropertyData) {
+ $singlePropertyData['name'] = $key;
+ }
+ } else {
+ $userData[$key]['name'] = $key;
+ }
}
if (!isset($userData[IAccountManager::COLLECTION_EMAIL])) {
$userData[IAccountManager::COLLECTION_EMAIL] = [];
@@ -467,6 +459,23 @@ class AccountManager implements IAccountManager {
return $newData;
}
+ protected function dataArrayToJson(array $accountData): string {
+ $jsonData = [];
+ foreach ($accountData as $property => $data) {
+ //$property = $data['name'];
+ unset($data['name']);
+ if ($this->isCollection($property)) {
+ if (!isset($jsonData[$property])) {
+ $jsonData[$property] = [];
+ }
+ $jsonData[$property][] = $data;
+ } else {
+ $jsonData[$property] = $data;
+ }
+ }
+ return json_encode($jsonData);
+ }
+
/**
* add new user to accounts table
*
@@ -475,7 +484,7 @@ class AccountManager implements IAccountManager {
*/
protected function insertNewUser(IUser $user, array $data): void {
$uid = $user->getUID();
- $jsonEncodedData = json_encode($data);
+ $jsonEncodedData = $this->dataArrayToJson($data);
$query = $this->connection->getQueryBuilder();
$query->insert($this->table)
->values(
@@ -484,7 +493,7 @@ class AccountManager implements IAccountManager {
'data' => $query->createNamedParameter($jsonEncodedData),
]
)
- ->execute();
+ ->executeStatement();
$this->deleteUserData($user);
$this->writeUserData($user, $data);
@@ -522,19 +531,21 @@ class AccountManager implements IAccountManager {
$this->writeUserDataProperties($query, $data);
}
- protected function writeUserDataProperties(IQueryBuilder $query, array $data, string $parentPropertyName = null): void {
+ protected function writeUserDataProperties(IQueryBuilder $query, array $data): void {
foreach ($data as $propertyName => $property) {
- if ($this->isCollection($propertyName)) {
- $this->writeUserDataProperties($query, $property, $propertyName);
+ if (isset($property['name']) && $property['name'] === self::PROPERTY_AVATAR) {
continue;
}
- if (($parentPropertyName ?? $propertyName) === self::PROPERTY_AVATAR) {
+ if ($this->isCollection($property['name'] ?? $propertyName) && !isset($property['name'])) {
+ foreach ($property as $singleProperty) {
+ $this->writeUserDataProperties($query, [$propertyName => $singleProperty]);
+ }
continue;
}
- $query->setParameter('name', $parentPropertyName ?? $propertyName)
+ $query->setParameter('name', $property['name'] ?? $propertyName)
->setParameter('value', $property['value'] ?? '');
- $query->execute();
+ $query->executeStatement();
}
}
@@ -548,40 +559,47 @@ class AccountManager implements IAccountManager {
return [
self::PROPERTY_DISPLAYNAME =>
[
+ 'name' => self::PROPERTY_DISPLAYNAME,
'value' => $user->getDisplayName(),
'scope' => self::SCOPE_FEDERATED,
'verified' => self::NOT_VERIFIED,
],
self::PROPERTY_ADDRESS =>
[
+ 'name' => self::PROPERTY_ADDRESS,
'value' => '',
'scope' => self::SCOPE_LOCAL,
'verified' => self::NOT_VERIFIED,
],
self::PROPERTY_WEBSITE =>
[
+ 'name' => self::PROPERTY_WEBSITE,
'value' => '',
'scope' => self::SCOPE_LOCAL,
'verified' => self::NOT_VERIFIED,
],
self::PROPERTY_EMAIL =>
[
+ 'name' => self::PROPERTY_EMAIL,
'value' => $user->getEMailAddress(),
'scope' => self::SCOPE_FEDERATED,
'verified' => self::NOT_VERIFIED,
],
self::PROPERTY_AVATAR =>
[
+ 'name' => self::PROPERTY_AVATAR,
'scope' => self::SCOPE_FEDERATED
],
self::PROPERTY_PHONE =>
[
+ 'name' => self::PROPERTY_PHONE,
'value' => '',
'scope' => self::SCOPE_LOCAL,
'verified' => self::NOT_VERIFIED,
],
self::PROPERTY_TWITTER =>
[
+ 'name' => self::PROPERTY_TWITTER,
'value' => '',
'scope' => self::SCOPE_LOCAL,
'verified' => self::NOT_VERIFIED,
@@ -624,14 +642,43 @@ class AccountManager implements IAccountManager {
public function updateAccount(IAccount $account): void {
$data = [];
- foreach ($account->getProperties() as $property) {
- $data[$property->getName()] = [
+ foreach ($account->getAllProperties() as $property) {
+ $data[] = [
+ 'name' => $property->getName(),
'value' => $property->getValue(),
'scope' => $property->getScope(),
'verified' => $property->getVerified(),
];
}
+ $this->testValueLengths(iterator_to_array($account->getAllProperties()), true);
+ try {
+ $property = $account->getProperty(self::PROPERTY_PHONE);
+ $this->sanitizePhoneNumberValue($property);
+ } catch (PropertyDoesNotExistException $e) {
+ // valid case, nothing to do
+ }
+
+ try {
+ $property = $account->getProperty(self::PROPERTY_WEBSITE);
+ $this->sanitizeWebsite($property);
+ } catch (PropertyDoesNotExistException $e) {
+ // valid case, nothing to do
+ }
+
+ static $allowedScopes = [
+ self::SCOPE_PRIVATE,
+ self::SCOPE_LOCAL,
+ self::SCOPE_FEDERATED,
+ self::SCOPE_PUBLISHED,
+ self::VISIBILITY_PRIVATE,
+ self::VISIBILITY_CONTACTS_ONLY,
+ self::VISIBILITY_PUBLIC,
+ ];
+ foreach ($account->getAllProperties() as $property) {
+ $this->testPropertyScope($property, $allowedScopes, true);
+ }
+
$this->updateUser($account->getUser(), $data, true);
}
}
diff --git a/tests/lib/Accounts/AccountManagerTest.php b/tests/lib/Accounts/AccountManagerTest.php
index 18f34b454f6..d9a55825122 100644
--- a/tests/lib/Accounts/AccountManagerTest.php
+++ b/tests/lib/Accounts/AccountManagerTest.php
@@ -108,23 +108,71 @@ class AccountManagerTest extends TestCase {
[
'user' => $this->makeUser('j.doe', 'Jane Doe', 'jane.doe@acme.com'),
'data' => [
- IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'Jane Doe', 'scope' => IAccountManager::SCOPE_PUBLISHED],
- IAccountManager::PROPERTY_EMAIL => ['value' => 'jane.doe@acme.com', 'scope' => IAccountManager::SCOPE_LOCAL],
- IAccountManager::PROPERTY_TWITTER => ['value' => '@sometwitter', 'scope' => IAccountManager::SCOPE_PUBLISHED],
- IAccountManager::PROPERTY_PHONE => ['value' => '+491601231212', 'scope' => IAccountManager::SCOPE_FEDERATED],
- IAccountManager::PROPERTY_ADDRESS => ['value' => 'some street', 'scope' => IAccountManager::SCOPE_LOCAL],
- IAccountManager::PROPERTY_WEBSITE => ['value' => 'https://acme.com', 'scope' => IAccountManager::SCOPE_PRIVATE],
+ IAccountManager::PROPERTY_DISPLAYNAME => [
+ 'name' => IAccountManager::PROPERTY_DISPLAYNAME,
+ 'value' => 'Jane Doe',
+ 'scope' => IAccountManager::SCOPE_PUBLISHED
+ ],
+ IAccountManager::PROPERTY_EMAIL => [
+ 'name' => IAccountManager::PROPERTY_EMAIL,
+ 'value' => 'jane.doe@acme.com',
+ 'scope' => IAccountManager::SCOPE_LOCAL
+ ],
+ IAccountManager::PROPERTY_TWITTER => [
+ 'name' => IAccountManager::PROPERTY_TWITTER,
+ 'value' => '@sometwitter',
+ 'scope' => IAccountManager::SCOPE_PUBLISHED
+ ],
+ IAccountManager::PROPERTY_PHONE => [
+ 'name' => IAccountManager::PROPERTY_PHONE,
+ 'value' => '+491601231212',
+ 'scope' => IAccountManager::SCOPE_FEDERATED
+ ],
+ IAccountManager::PROPERTY_ADDRESS => [
+ 'name' => IAccountManager::PROPERTY_ADDRESS,
+ 'value' => 'some street',
+ 'scope' => IAccountManager::SCOPE_LOCAL
+ ],
+ IAccountManager::PROPERTY_WEBSITE => [
+ 'name' => IAccountManager::PROPERTY_WEBSITE,
+ 'value' => 'https://acme.com',
+ 'scope' => IAccountManager::SCOPE_PRIVATE
+ ],
],
],
[
'user' => $this->makeUser('a.allison', 'Alice Allison', 'a.allison@example.org'),
'data' => [
- IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'Alice Allison', 'scope' => IAccountManager::SCOPE_LOCAL],
- IAccountManager::PROPERTY_EMAIL => ['value' => 'a.allison@example.org', 'scope' => IAccountManager::SCOPE_LOCAL],
- IAccountManager::PROPERTY_TWITTER => ['value' => '@a_alice', 'scope' => IAccountManager::SCOPE_FEDERATED],
- IAccountManager::PROPERTY_PHONE => ['value' => '+491602312121', 'scope' => IAccountManager::SCOPE_LOCAL],
- IAccountManager::PROPERTY_ADDRESS => ['value' => 'Dundee Road 45', 'scope' => IAccountManager::SCOPE_LOCAL],
- IAccountManager::PROPERTY_WEBSITE => ['value' => 'https://example.org', 'scope' => IAccountManager::SCOPE_LOCAL],
+ IAccountManager::PROPERTY_DISPLAYNAME => [
+ 'name' => IAccountManager::PROPERTY_DISPLAYNAME,
+ 'value' => 'Alice Allison',
+ 'scope' => IAccountManager::SCOPE_LOCAL
+ ],
+ IAccountManager::PROPERTY_EMAIL => [
+ 'name' => IAccountManager::PROPERTY_EMAIL,
+ 'value' => 'a.allison@example.org',
+ 'scope' => IAccountManager::SCOPE_LOCAL
+ ],
+ IAccountManager::PROPERTY_TWITTER => [
+ 'name' => IAccountManager::PROPERTY_TWITTER,
+ 'value' => '@a_alice',
+ 'scope' => IAccountManager::SCOPE_FEDERATED
+ ],
+ IAccountManager::PROPERTY_PHONE => [
+ 'name' => IAccountManager::PROPERTY_PHONE,
+ 'value' => '+491602312121',
+ 'scope' => IAccountManager::SCOPE_LOCAL
+ ],
+ IAccountManager::PROPERTY_ADDRESS => [
+ 'name' => IAccountManager::PROPERTY_ADDRESS,
+ 'value' => 'Dundee Road 45',
+ 'scope' => IAccountManager::SCOPE_LOCAL
+ ],
+ IAccountManager::PROPERTY_WEBSITE => [
+ 'name' => IAccountManager::PROPERTY_WEBSITE,
+ 'value' => 'https://example.org',
+ 'scope' => IAccountManager::SCOPE_LOCAL
+ ],
],
],
[
@@ -148,20 +196,52 @@ class AccountManagerTest extends TestCase {
IAccountManager::PROPERTY_ADDRESS => ['value' => 'Pinapple Street 22', 'scope' => IAccountManager::SCOPE_LOCAL],
IAccountManager::PROPERTY_WEBSITE => ['value' => 'https://emca.com', 'scope' => IAccountManager::SCOPE_FEDERATED],
IAccountManager::COLLECTION_EMAIL => [
- ['value' => 'k.cheng@emca.com', 'scope' => IAccountManager::SCOPE_LOCAL],
- ['value' => 'kai.cheng@emca.com', 'scope' => IAccountManager::SCOPE_LOCAL],
+ [
+ 'name' => IAccountManager::COLLECTION_EMAIL,
+ 'value' => 'k.cheng@emca.com',
+ 'scope' => IAccountManager::SCOPE_LOCAL
+ ],
+ [
+ 'name' => IAccountManager::COLLECTION_EMAIL,
+ 'value' => 'kai.cheng@emca.com',
+ 'scope' => IAccountManager::SCOPE_LOCAL
+ ],
],
],
],
[
'user' => $this->makeUser('goodpal@elpmaxe.org', 'Goodpal, Kim', 'goodpal@elpmaxe.org'),
'data' => [
- IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'Goodpal, Kim', 'scope' => IAccountManager::SCOPE_PUBLISHED],
- IAccountManager::PROPERTY_EMAIL => ['value' => 'goodpal@elpmaxe.org', 'scope' => IAccountManager::SCOPE_PUBLISHED],
- IAccountManager::PROPERTY_TWITTER => ['value' => '', 'scope' => IAccountManager::SCOPE_LOCAL],
- IAccountManager::PROPERTY_PHONE => ['value' => '+71602121231', 'scope' => IAccountManager::SCOPE_FEDERATED],
- IAccountManager::PROPERTY_ADDRESS => ['value' => 'Octopus Ave 17', 'scope' => IAccountManager::SCOPE_FEDERATED],
- IAccountManager::PROPERTY_WEBSITE => ['value' => 'https://elpmaxe.org', 'scope' => IAccountManager::SCOPE_PUBLISHED],
+ IAccountManager::PROPERTY_DISPLAYNAME => [
+ 'name' => IAccountManager::PROPERTY_DISPLAYNAME,
+ 'value' => 'Goodpal, Kim',
+ 'scope' => IAccountManager::SCOPE_PUBLISHED
+ ],
+ IAccountManager::PROPERTY_EMAIL => [
+ 'name' => IAccountManager::PROPERTY_EMAIL,
+ 'value' => 'goodpal@elpmaxe.org',
+ 'scope' => IAccountManager::SCOPE_PUBLISHED
+ ],
+ IAccountManager::PROPERTY_TWITTER => [
+ 'name' => IAccountManager::PROPERTY_TWITTER,
+ 'value' => '',
+ 'scope' => IAccountManager::SCOPE_LOCAL
+ ],
+ IAccountManager::PROPERTY_PHONE => [
+ 'name' => IAccountManager::PROPERTY_PHONE,
+ 'value' => '+71602121231',
+ 'scope' => IAccountManager::SCOPE_FEDERATED
+ ],
+ IAccountManager::PROPERTY_ADDRESS => [
+ 'name' => IAccountManager::PROPERTY_ADDRESS,
+ 'value' => 'Octopus Ave 17',
+ 'scope' => IAccountManager::SCOPE_FEDERATED
+ ],
+ IAccountManager::PROPERTY_WEBSITE => [
+ 'name' => IAccountManager::PROPERTY_WEBSITE,
+ 'value' => 'https://elpmaxe.org',
+ 'scope' => IAccountManager::SCOPE_PUBLISHED
+ ],
],
],
];
@@ -250,144 +330,6 @@ class AccountManagerTest extends TestCase {
];
}
- public function updateUserSetScopeProvider() {
- return [
- // regular scope switching
- [
- [
- IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'Display Name', 'scope' => IAccountManager::SCOPE_PUBLISHED],
- IAccountManager::PROPERTY_EMAIL => ['value' => 'test@example.org', 'scope' => IAccountManager::SCOPE_PUBLISHED],
- IAccountManager::PROPERTY_AVATAR => ['value' => '@sometwitter', 'scope' => IAccountManager::SCOPE_PUBLISHED],
- IAccountManager::PROPERTY_TWITTER => ['value' => '@sometwitter', 'scope' => IAccountManager::SCOPE_PUBLISHED],
- IAccountManager::PROPERTY_PHONE => ['value' => '+491601231212', 'scope' => IAccountManager::SCOPE_FEDERATED],
- IAccountManager::PROPERTY_ADDRESS => ['value' => 'some street', 'scope' => IAccountManager::SCOPE_LOCAL],
- IAccountManager::PROPERTY_WEBSITE => ['value' => 'https://example.org', 'scope' => IAccountManager::SCOPE_PRIVATE],
- ],
- [
- IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'Display Name', 'scope' => IAccountManager::SCOPE_LOCAL],
- IAccountManager::PROPERTY_EMAIL => ['value' => 'test@example.org', 'scope' => IAccountManager::SCOPE_FEDERATED],
- IAccountManager::PROPERTY_TWITTER => ['value' => '@sometwitter', 'scope' => IAccountManager::SCOPE_PRIVATE],
- IAccountManager::PROPERTY_PHONE => ['value' => '+491601231212', 'scope' => IAccountManager::SCOPE_LOCAL],
- IAccountManager::PROPERTY_ADDRESS => ['value' => 'some street', 'scope' => IAccountManager::SCOPE_FEDERATED],
- IAccountManager::PROPERTY_WEBSITE => ['value' => 'https://example.org', 'scope' => IAccountManager::SCOPE_PUBLISHED],
- ],
- [
- IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'Display Name', 'scope' => IAccountManager::SCOPE_LOCAL],
- IAccountManager::PROPERTY_EMAIL => ['value' => 'test@example.org', 'scope' => IAccountManager::SCOPE_FEDERATED],
- IAccountManager::PROPERTY_TWITTER => ['value' => '@sometwitter', 'scope' => IAccountManager::SCOPE_PRIVATE],
- IAccountManager::PROPERTY_PHONE => ['value' => '+491601231212', 'scope' => IAccountManager::SCOPE_LOCAL],
- IAccountManager::PROPERTY_ADDRESS => ['value' => 'some street', 'scope' => IAccountManager::SCOPE_FEDERATED],
- IAccountManager::PROPERTY_WEBSITE => ['value' => 'https://example.org', 'scope' => IAccountManager::SCOPE_PUBLISHED],
- ],
- ],
- // legacy scope mapping, the given visibility values get converted to scopes
- [
- [
- IAccountManager::PROPERTY_TWITTER => ['value' => '@sometwitter', 'scope' => IAccountManager::SCOPE_PUBLISHED],
- IAccountManager::PROPERTY_PHONE => ['value' => '+491601231212', 'scope' => IAccountManager::SCOPE_FEDERATED],
- IAccountManager::PROPERTY_WEBSITE => ['value' => 'https://example.org', 'scope' => IAccountManager::SCOPE_PRIVATE],
- ],
- [
- IAccountManager::PROPERTY_TWITTER => ['value' => '@sometwitter', 'scope' => IAccountManager::VISIBILITY_PUBLIC],
- IAccountManager::PROPERTY_PHONE => ['value' => '+491601231212', 'scope' => IAccountManager::VISIBILITY_CONTACTS_ONLY],
- IAccountManager::PROPERTY_WEBSITE => ['value' => 'https://example.org', 'scope' => IAccountManager::VISIBILITY_PRIVATE],
- ],
- [
- IAccountManager::PROPERTY_TWITTER => ['value' => '@sometwitter', 'scope' => IAccountManager::SCOPE_PUBLISHED],
- IAccountManager::PROPERTY_PHONE => ['value' => '+491601231212', 'scope' => IAccountManager::SCOPE_FEDERATED],
- IAccountManager::PROPERTY_WEBSITE => ['value' => 'https://example.org', 'scope' => IAccountManager::SCOPE_LOCAL],
- ],
- ],
- // invalid or unsupported scope values get converted to SCOPE_LOCAL
- [
- [
- IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'Display Name', 'scope' => IAccountManager::SCOPE_PUBLISHED],
- IAccountManager::PROPERTY_EMAIL => ['value' => 'test@example.org', 'scope' => IAccountManager::SCOPE_PUBLISHED],
- IAccountManager::PROPERTY_TWITTER => ['value' => '@sometwitter', 'scope' => IAccountManager::SCOPE_PUBLISHED],
- IAccountManager::PROPERTY_PHONE => ['value' => '+491601231212', 'scope' => IAccountManager::SCOPE_FEDERATED],
- ],
- [
- // SCOPE_PRIVATE is not allowed for display name and email
- IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'Display Name', 'scope' => IAccountManager::SCOPE_PRIVATE],
- IAccountManager::PROPERTY_EMAIL => ['value' => 'test@example.org', 'scope' => IAccountManager::SCOPE_PRIVATE],
- IAccountManager::PROPERTY_TWITTER => ['value' => '@sometwitter', 'scope' => IAccountManager::SCOPE_LOCAL],
- IAccountManager::PROPERTY_PHONE => ['value' => '+491601231212', 'scope' => IAccountManager::SCOPE_LOCAL],
- ],
- [
- IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'Display Name', 'scope' => IAccountManager::SCOPE_LOCAL],
- IAccountManager::PROPERTY_EMAIL => ['value' => 'test@example.org', 'scope' => IAccountManager::SCOPE_LOCAL],
- IAccountManager::PROPERTY_TWITTER => ['value' => '@sometwitter', 'scope' => IAccountManager::SCOPE_LOCAL],
- IAccountManager::PROPERTY_PHONE => ['value' => '+491601231212', 'scope' => IAccountManager::SCOPE_LOCAL],
- ],
- false, false,
- ],
- // illegal scope values
- [
- [
- IAccountManager::PROPERTY_PHONE => ['value' => '+491601231212', 'scope' => IAccountManager::SCOPE_FEDERATED],
- IAccountManager::PROPERTY_ADDRESS => ['value' => 'some street', 'scope' => IAccountManager::SCOPE_LOCAL],
- IAccountManager::PROPERTY_WEBSITE => ['value' => 'https://example.org', 'scope' => IAccountManager::SCOPE_PRIVATE],
- ],
- [
- IAccountManager::PROPERTY_PHONE => ['value' => '+491601231212', 'scope' => ''],
- IAccountManager::PROPERTY_ADDRESS => ['value' => 'some street', 'scope' => 'v2-invalid'],
- IAccountManager::PROPERTY_WEBSITE => ['value' => 'https://example.org', 'scope' => 'invalid'],
- ],
- [],
- true, true
- ],
- // invalid or unsupported scope values throw an exception when passing $throwOnData=true
- [
- [IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'Display Name', 'scope' => IAccountManager::SCOPE_PUBLISHED]],
- [IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'Display Name', 'scope' => IAccountManager::SCOPE_PRIVATE]],
- null,
- // throw exception
- true, true,
- ],
- [
- [IAccountManager::PROPERTY_EMAIL => ['value' => 'test@example.org', 'scope' => IAccountManager::SCOPE_PUBLISHED]],
- [IAccountManager::PROPERTY_EMAIL => ['value' => 'test@example.org', 'scope' => IAccountManager::SCOPE_PRIVATE]],
- null,
- // throw exception
- true, true,
- ],
- [
- [IAccountManager::PROPERTY_TWITTER => ['value' => '@sometwitter', 'scope' => IAccountManager::SCOPE_PUBLISHED]],
- [IAccountManager::PROPERTY_TWITTER => ['value' => '@sometwitter', 'scope' => 'invalid']],
- null,
- // throw exception
- true, true,
- ],
- ];
- }
-
- /**
- * @dataProvider updateUserSetScopeProvider
- */
- public function testUpdateUserSetScope($oldData, $newData, $savedData, $throwOnData = true, $expectedThrow = false) {
- $accountManager = $this->getInstance(['getUser', 'insertNewUser', 'updateExistingUser', 'updateVerifyStatus', 'checkEmailVerification']);
- /** @var IUser $user */
- $user = $this->createMock(IUser::class);
-
- $accountManager->expects($this->once())->method('getUser')->with($user)->willReturn($oldData);
-
- if ($expectedThrow) {
- $accountManager->expects($this->never())->method('updateExistingUser');
- $this->expectException(\InvalidArgumentException::class);
- $this->expectExceptionMessage('scope');
- } else {
- $accountManager->expects($this->once())->method('checkEmailVerification')
- ->with($oldData, $savedData, $user)->willReturn($savedData);
- $accountManager->expects($this->once())->method('updateVerifyStatus')
- ->with($oldData, $savedData)->willReturn($savedData);
- $accountManager->expects($this->once())->method('updateExistingUser')
- ->with($user, $savedData);
- $accountManager->expects($this->never())->method('insertNewUser');
- }
-
- $accountManager->updateUser($user, $newData, $throwOnData);
- }
-
/**
* @dataProvider dataTestGetUser
*
@@ -433,8 +375,8 @@ class AccountManagerTest extends TestCase {
public function testUpdateExistingUser() {
$user = $this->getMockBuilder(IUser::class)->getMock();
$user->expects($this->atLeastOnce())->method('getUID')->willReturn('uid');
- $oldData = ['key' => ['value' => 'value']];
- $newData = ['newKey' => ['value' => 'newValue']];
+ $oldData = ['key' => ['value' => 'value', 'name' => 'name']];
+ $newData = ['newKey' => ['value' => 'newValue', 'name' => 'name']];
$this->addDummyValuesToTable('uid', $oldData);
$this->invokePrivate($this->accountManager, 'updateExistingUser', [$user, $newData]);
@@ -445,13 +387,14 @@ class AccountManagerTest extends TestCase {
public function testInsertNewUser() {
$user = $this->getMockBuilder(IUser::class)->getMock();
$uid = 'uid';
- $data = ['key' => ['value' => 'value']];
+ $data = ['key' => ['value' => 'value', 'name' => 'name']];
$user->expects($this->atLeastOnce())->method('getUID')->willReturn($uid);
$this->assertNull($this->getDataFromTable($uid));
$this->invokePrivate($this->accountManager, 'insertNewUser', [$user, $data]);
$dataFromDb = $this->getDataFromTable($uid);
+ $dataFromDb['key']['name'] = 'name'; // from transformation
$this->assertEquals($data, $dataFromDb);
}
@@ -462,8 +405,8 @@ class AccountManagerTest extends TestCase {
];
$expected = [
- 'key1' => ['value' => 'value1', 'verified' => '0'],
- 'key2' => ['value' => 'value1', 'verified' => '0'],
+ 'key1' => ['value' => 'value1', 'verified' => '0', 'name' => 'key1'],
+ 'key2' => ['value' => 'value1', 'verified' => '0', 'name' => 'key2'],
'additional_mail' => []
];