]> source.dussan.org Git - nextcloud-server.git/commitdiff
Discard duplicate collection name from account serialization 31487/head
authorChristopher Ng <chrng8@gmail.com>
Tue, 8 Mar 2022 00:38:38 +0000 (00:38 +0000)
committerChristopher Ng <chrng8@gmail.com>
Thu, 17 Mar 2022 04:27:39 +0000 (04:27 +0000)
Signed-off-by: Christopher Ng <chrng8@gmail.com>
lib/private/Accounts/Account.php
tests/lib/Accounts/AccountTest.php

index 540d15cd4b9e9ba0ba563b90ba4027947bfe5868..7d36af561ce81e4be99fdebe47fe82e0f27bbd0d 100644 (file)
@@ -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 {
index 0e0c42804e43403e68bf4dc899565b4094b92c87..064a9bf2732a69c0c427cd29509c872bc1b730aa 100644 (file)
@@ -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());
        }
 }