diff options
author | Pytal <24800714+Pytal@users.noreply.github.com> | 2022-03-17 19:31:44 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-17 19:31:44 +0300 |
commit | 2a75c303b5ce01f809866a0afb326663b9f9d125 (patch) | |
tree | 2239f1537c728ad7455cf8e66353be5c72e44c81 | |
parent | ec4be7f2be89dbd5fe99f7f3ea3f879739ffbea6 (diff) | |
parent | a99d33c72d663252d540a7b4b02ef57577c53f3d (diff) |
Merge pull request #31487 from nextcloud/enh/account-json-serialization
-rw-r--r-- | lib/private/Accounts/Account.php | 11 | ||||
-rw-r--r-- | tests/lib/Accounts/AccountTest.php | 16 |
2 files changed, 24 insertions, 3 deletions
diff --git a/lib/private/Accounts/Account.php b/lib/private/Accounts/Account.php index 540d15cd4b9..7d36af561ce 100644 --- a/lib/private/Accounts/Account.php +++ b/lib/private/Accounts/Account.php @@ -104,9 +104,16 @@ class Account implements IAccount { return $result; } - /** @return IAccountPropertyCollection[]|IAccountProperty[] */ + /** @return array<string, IAccountProperty|array<int, IAccountProperty>> */ public function jsonSerialize(): array { - return $this->properties; + $properties = $this->properties; + foreach ($properties as $propertyName => $propertyObject) { + if ($propertyObject instanceof IAccountPropertyCollection) { + // Override collection serialization to discard duplicate name + $properties[$propertyName] = $propertyObject->jsonSerialize()[$propertyName]; + } + } + return $properties; } public function getUser(): IUser { diff --git a/tests/lib/Accounts/AccountTest.php b/tests/lib/Accounts/AccountTest.php index 0e0c42804e4..064a9bf2732 100644 --- a/tests/lib/Accounts/AccountTest.php +++ b/tests/lib/Accounts/AccountTest.php @@ -120,12 +120,26 @@ class AccountTest extends TestCase { $user = $this->createMock(IUser::class); $properties = [ IAccountManager::PROPERTY_WEBSITE => new AccountProperty(IAccountManager::PROPERTY_WEBSITE, 'https://example.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED, ''), - IAccountManager::PROPERTY_EMAIL => new AccountProperty(IAccountManager::PROPERTY_EMAIL, 'user@example.com', IAccountManager::SCOPE_LOCAL, IAccountManager::VERIFIED, '') + IAccountManager::PROPERTY_EMAIL => new AccountProperty(IAccountManager::PROPERTY_EMAIL, 'user@example.com', IAccountManager::SCOPE_LOCAL, IAccountManager::VERIFIED, ''), + IAccountManager::COLLECTION_EMAIL => [ + new AccountProperty(IAccountManager::COLLECTION_EMAIL, 'apple@orange.com', IAccountManager::SCOPE_LOCAL, IAccountManager::NOT_VERIFIED, ''), + new AccountProperty(IAccountManager::COLLECTION_EMAIL, 'banana@orange.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::VERIFICATION_IN_PROGRESS, ''), + new AccountProperty(IAccountManager::COLLECTION_EMAIL, 'kiwi@watermelon.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::VERIFIED, ''), + ], ]; + $account = new Account($user); $account->setProperty(IAccountManager::PROPERTY_WEBSITE, 'https://example.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED); $account->setProperty(IAccountManager::PROPERTY_EMAIL, 'user@example.com', IAccountManager::SCOPE_LOCAL, IAccountManager::VERIFIED); + $col = new AccountPropertyCollection(IAccountManager::COLLECTION_EMAIL); + $col->setProperties([ + new AccountProperty($col->getName(), 'apple@orange.com', IAccountManager::SCOPE_LOCAL, IAccountManager::NOT_VERIFIED, ''), + new AccountProperty($col->getName(), 'banana@orange.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::VERIFICATION_IN_PROGRESS, ''), + new AccountProperty($col->getName(), 'kiwi@watermelon.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::VERIFIED, ''), + ]); + $account->setPropertyCollection($col); + $this->assertEquals($properties, $account->jsonSerialize()); } } |