1
0

AccountTest.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Test\Accounts;
  7. use OC\Accounts\Account;
  8. use OC\Accounts\AccountProperty;
  9. use OC\Accounts\AccountPropertyCollection;
  10. use OCP\Accounts\IAccountManager;
  11. use OCP\IUser;
  12. use Test\TestCase;
  13. /**
  14. * Class AccountTest
  15. *
  16. * @package Test\Accounts
  17. */
  18. class AccountTest extends TestCase {
  19. public function testConstructor(): void {
  20. $user = $this->createMock(IUser::class);
  21. $account = new Account($user);
  22. $this->assertEquals($user, $account->getUser());
  23. }
  24. public function testSetProperty(): void {
  25. $user = $this->createMock(IUser::class);
  26. $property = new AccountProperty(IAccountManager::PROPERTY_WEBSITE, 'https://example.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED, '');
  27. $account = new Account($user);
  28. $account->setProperty(IAccountManager::PROPERTY_WEBSITE, 'https://example.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED);
  29. $this->assertEquals($property, $account->getProperty(IAccountManager::PROPERTY_WEBSITE));
  30. }
  31. public function testGetAndGetAllProperties(): void {
  32. $user = $this->createMock(IUser::class);
  33. $properties = [
  34. IAccountManager::PROPERTY_WEBSITE => new AccountProperty(IAccountManager::PROPERTY_WEBSITE, 'https://example.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED, ''),
  35. IAccountManager::PROPERTY_EMAIL => new AccountProperty(IAccountManager::PROPERTY_EMAIL, 'user@example.com', IAccountManager::SCOPE_LOCAL, IAccountManager::VERIFIED, '')
  36. ];
  37. $account = new Account($user);
  38. $account->setProperty(IAccountManager::PROPERTY_WEBSITE, 'https://example.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED);
  39. $account->setProperty(IAccountManager::PROPERTY_EMAIL, 'user@example.com', IAccountManager::SCOPE_LOCAL, IAccountManager::VERIFIED);
  40. $col = new AccountPropertyCollection(IAccountManager::COLLECTION_EMAIL);
  41. $additionalProperty = new AccountProperty($col->getName(), 'second@example.org', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED, '');
  42. $col->addProperty($additionalProperty);
  43. $account->setPropertyCollection($col);
  44. $this->assertEquals($properties, $account->getProperties());
  45. $properties[] = $additionalProperty;
  46. $this->assertEquals(array_values($properties), \iterator_to_array($account->getAllProperties()));
  47. }
  48. public function testSetAllPropertiesFromJson(): void {
  49. $user = $this->createMock(IUser::class);
  50. $properties = [
  51. IAccountManager::PROPERTY_DISPLAYNAME => new AccountProperty(IAccountManager::PROPERTY_DISPLAYNAME, 'Steve', IAccountManager::SCOPE_FEDERATED, IAccountManager::NOT_VERIFIED, ''),
  52. IAccountManager::PROPERTY_ADDRESS => new AccountProperty(IAccountManager::PROPERTY_ADDRESS, '123 Acorn Avenue', IAccountManager::SCOPE_FEDERATED, IAccountManager::NOT_VERIFIED, ''),
  53. IAccountManager::PROPERTY_WEBSITE => new AccountProperty(IAccountManager::PROPERTY_WEBSITE, 'https://www.example.org', IAccountManager::SCOPE_FEDERATED, IAccountManager::VERIFIED, ''),
  54. IAccountManager::PROPERTY_EMAIL => new AccountProperty(IAccountManager::PROPERTY_EMAIL, 'steve@earth.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::VERIFICATION_IN_PROGRESS, ''),
  55. IAccountManager::PROPERTY_AVATAR => new AccountProperty(IAccountManager::PROPERTY_AVATAR, '', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED, ''),
  56. IAccountManager::PROPERTY_PHONE => new AccountProperty(IAccountManager::PROPERTY_PHONE, '+358407991028', IAccountManager::SCOPE_LOCAL, IAccountManager::NOT_VERIFIED, ''),
  57. IAccountManager::PROPERTY_TWITTER => new AccountProperty(IAccountManager::PROPERTY_TWITTER, 'therealsteve', IAccountManager::SCOPE_PRIVATE, IAccountManager::NOT_VERIFIED, ''),
  58. IAccountManager::PROPERTY_ORGANISATION => new AccountProperty(IAccountManager::PROPERTY_ORGANISATION, 'Steve Incorporated', IAccountManager::SCOPE_FEDERATED, IAccountManager::NOT_VERIFIED, ''),
  59. IAccountManager::PROPERTY_ROLE => new AccountProperty(IAccountManager::PROPERTY_ROLE, 'Founder', IAccountManager::SCOPE_FEDERATED, IAccountManager::NOT_VERIFIED, ''),
  60. IAccountManager::PROPERTY_HEADLINE => new AccountProperty(IAccountManager::PROPERTY_HEADLINE, 'I am Steve', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED, ''),
  61. IAccountManager::PROPERTY_BIOGRAPHY => new AccountProperty(IAccountManager::PROPERTY_BIOGRAPHY, 'Steve is the best', IAccountManager::SCOPE_LOCAL, IAccountManager::NOT_VERIFIED, ''),
  62. IAccountManager::PROPERTY_PROFILE_ENABLED => new AccountProperty(IAccountManager::PROPERTY_PROFILE_ENABLED, '1', IAccountManager::SCOPE_FEDERATED, IAccountManager::NOT_VERIFIED, ''),
  63. IAccountManager::COLLECTION_EMAIL => [
  64. new AccountProperty(IAccountManager::COLLECTION_EMAIL, 'steve@mars.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED, ''),
  65. new AccountProperty(IAccountManager::COLLECTION_EMAIL, 'steve@neptune.com', IAccountManager::SCOPE_FEDERATED, IAccountManager::NOT_VERIFIED, ''),
  66. ],
  67. ];
  68. $account = new Account($user);
  69. $account->setAllPropertiesFromJson(json_decode(json_encode($properties), true));
  70. $this->assertEquals($properties, $account->jsonSerialize());
  71. }
  72. public function testGetFilteredProperties(): void {
  73. $user = $this->createMock(IUser::class);
  74. $properties = [
  75. IAccountManager::PROPERTY_WEBSITE => new AccountProperty(IAccountManager::PROPERTY_WEBSITE, 'https://example.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED, ''),
  76. IAccountManager::PROPERTY_EMAIL => new AccountProperty(IAccountManager::PROPERTY_EMAIL, 'user@example.com', IAccountManager::SCOPE_LOCAL, IAccountManager::VERIFIED, ''),
  77. IAccountManager::PROPERTY_PHONE => new AccountProperty(IAccountManager::PROPERTY_PHONE, '123456', IAccountManager::SCOPE_PUBLISHED, IAccountManager::VERIFIED, ''),
  78. ];
  79. $account = new Account($user);
  80. $account->setProperty(IAccountManager::PROPERTY_WEBSITE, 'https://example.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED);
  81. $account->setProperty(IAccountManager::PROPERTY_EMAIL, 'user@example.com', IAccountManager::SCOPE_LOCAL, IAccountManager::VERIFIED);
  82. $account->setProperty(IAccountManager::PROPERTY_PHONE, '123456', IAccountManager::SCOPE_PUBLISHED, IAccountManager::VERIFIED);
  83. $col = new AccountPropertyCollection(IAccountManager::COLLECTION_EMAIL);
  84. $additionalProperty1 = new AccountProperty($col->getName(), 'second@example.org', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED, '');
  85. $additionalProperty2 = new AccountProperty($col->getName(), 'third@example.org', IAccountManager::SCOPE_PUBLISHED, IAccountManager::VERIFIED, '');
  86. $col->addProperty($additionalProperty1);
  87. $col->addProperty($additionalProperty2);
  88. $account->setPropertyCollection($col);
  89. $this->assertEquals(
  90. [
  91. IAccountManager::PROPERTY_WEBSITE => $properties[IAccountManager::PROPERTY_WEBSITE],
  92. IAccountManager::PROPERTY_PHONE => $properties[IAccountManager::PROPERTY_PHONE],
  93. IAccountManager::COLLECTION_EMAIL . '#0' => $additionalProperty1,
  94. IAccountManager::COLLECTION_EMAIL . '#1' => $additionalProperty2,
  95. ],
  96. $account->getFilteredProperties(IAccountManager::SCOPE_PUBLISHED)
  97. );
  98. $this->assertEquals(
  99. [
  100. IAccountManager::PROPERTY_EMAIL => $properties[IAccountManager::PROPERTY_EMAIL],
  101. IAccountManager::PROPERTY_PHONE => $properties[IAccountManager::PROPERTY_PHONE],
  102. IAccountManager::COLLECTION_EMAIL . '#0' => $additionalProperty2,
  103. ],
  104. $account->getFilteredProperties(null, IAccountManager::VERIFIED)
  105. );
  106. $this->assertEquals(
  107. [
  108. IAccountManager::PROPERTY_PHONE => $properties[IAccountManager::PROPERTY_PHONE],
  109. IAccountManager::COLLECTION_EMAIL . '#0' => $additionalProperty2,
  110. ],
  111. $account->getFilteredProperties(IAccountManager::SCOPE_PUBLISHED, IAccountManager::VERIFIED),
  112. );
  113. }
  114. public function testJsonSerialize(): void {
  115. $user = $this->createMock(IUser::class);
  116. $properties = [
  117. IAccountManager::PROPERTY_WEBSITE => new AccountProperty(IAccountManager::PROPERTY_WEBSITE, 'https://example.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED, ''),
  118. IAccountManager::PROPERTY_EMAIL => new AccountProperty(IAccountManager::PROPERTY_EMAIL, 'user@example.com', IAccountManager::SCOPE_LOCAL, IAccountManager::VERIFIED, ''),
  119. IAccountManager::COLLECTION_EMAIL => [
  120. new AccountProperty(IAccountManager::COLLECTION_EMAIL, 'apple@orange.com', IAccountManager::SCOPE_LOCAL, IAccountManager::NOT_VERIFIED, ''),
  121. new AccountProperty(IAccountManager::COLLECTION_EMAIL, 'banana@orange.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::VERIFICATION_IN_PROGRESS, ''),
  122. new AccountProperty(IAccountManager::COLLECTION_EMAIL, 'kiwi@watermelon.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::VERIFIED, ''),
  123. ],
  124. ];
  125. $account = new Account($user);
  126. $account->setProperty(IAccountManager::PROPERTY_WEBSITE, 'https://example.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED);
  127. $account->setProperty(IAccountManager::PROPERTY_EMAIL, 'user@example.com', IAccountManager::SCOPE_LOCAL, IAccountManager::VERIFIED);
  128. $col = new AccountPropertyCollection(IAccountManager::COLLECTION_EMAIL);
  129. $col->setProperties([
  130. new AccountProperty($col->getName(), 'apple@orange.com', IAccountManager::SCOPE_LOCAL, IAccountManager::NOT_VERIFIED, ''),
  131. new AccountProperty($col->getName(), 'banana@orange.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::VERIFICATION_IN_PROGRESS, ''),
  132. new AccountProperty($col->getName(), 'kiwi@watermelon.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::VERIFIED, ''),
  133. ]);
  134. $account->setPropertyCollection($col);
  135. $this->assertEquals($properties, $account->jsonSerialize());
  136. }
  137. }