AccountTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2018 Julius Härtl <jus@bitgrid.net>
  4. *
  5. * @author Julius Härtl <jus@bitgrid.net>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace Test\Accounts;
  24. use OC\Accounts\Account;
  25. use OC\Accounts\AccountProperty;
  26. use OCP\Accounts\IAccountManager;
  27. use OCP\IUser;
  28. use Test\TestCase;
  29. /**
  30. * Class AccountTest
  31. *
  32. * @package Test\Accounts
  33. */
  34. class AccountTest extends TestCase {
  35. public function testConstructor() {
  36. $user = $this->createMock(IUser::class);
  37. $account = new Account($user);
  38. $this->assertEquals($user, $account->getUser());
  39. }
  40. public function testSetProperty() {
  41. $user = $this->createMock(IUser::class);
  42. $property = new AccountProperty(IAccountManager::PROPERTY_WEBSITE, 'https://example.com', IAccountManager::VISIBILITY_PUBLIC, IAccountManager::NOT_VERIFIED);
  43. $account = new Account($user);
  44. $account->setProperty(IAccountManager::PROPERTY_WEBSITE, 'https://example.com', IAccountManager::VISIBILITY_PUBLIC, IAccountManager::NOT_VERIFIED);
  45. $this->assertEquals($property, $account->getProperty(IAccountManager::PROPERTY_WEBSITE));
  46. }
  47. public function testGetProperties() {
  48. $user = $this->createMock(IUser::class);
  49. $properties = [
  50. IAccountManager::PROPERTY_WEBSITE => new AccountProperty(IAccountManager::PROPERTY_WEBSITE, 'https://example.com', IAccountManager::VISIBILITY_PUBLIC, IAccountManager::NOT_VERIFIED),
  51. IAccountManager::PROPERTY_EMAIL => new AccountProperty(IAccountManager::PROPERTY_EMAIL, 'user@example.com', IAccountManager::VISIBILITY_PRIVATE, IAccountManager::VERIFIED)
  52. ];
  53. $account = new Account($user);
  54. $account->setProperty(IAccountManager::PROPERTY_WEBSITE, 'https://example.com', IAccountManager::VISIBILITY_PUBLIC, IAccountManager::NOT_VERIFIED);
  55. $account->setProperty(IAccountManager::PROPERTY_EMAIL, 'user@example.com', IAccountManager::VISIBILITY_PRIVATE, IAccountManager::VERIFIED);
  56. $this->assertEquals($properties, $account->getProperties());
  57. }
  58. public function testGetFilteredProperties() {
  59. $user = $this->createMock(IUser::class);
  60. $properties = [
  61. IAccountManager::PROPERTY_WEBSITE => new AccountProperty(IAccountManager::PROPERTY_WEBSITE, 'https://example.com', IAccountManager::VISIBILITY_PUBLIC, IAccountManager::NOT_VERIFIED),
  62. IAccountManager::PROPERTY_EMAIL => new AccountProperty(IAccountManager::PROPERTY_EMAIL, 'user@example.com', IAccountManager::VISIBILITY_PRIVATE, IAccountManager::VERIFIED),
  63. IAccountManager::PROPERTY_PHONE => new AccountProperty(IAccountManager::PROPERTY_PHONE, '123456', IAccountManager::VISIBILITY_PUBLIC, IAccountManager::VERIFIED),
  64. ];
  65. $account = new Account($user);
  66. $account->setProperty(IAccountManager::PROPERTY_WEBSITE, 'https://example.com', IAccountManager::VISIBILITY_PUBLIC, IAccountManager::NOT_VERIFIED);
  67. $account->setProperty(IAccountManager::PROPERTY_EMAIL, 'user@example.com', IAccountManager::VISIBILITY_PRIVATE, IAccountManager::VERIFIED);
  68. $account->setProperty(IAccountManager::PROPERTY_PHONE, '123456', IAccountManager::VISIBILITY_PUBLIC, IAccountManager::VERIFIED);
  69. $this->assertEquals(
  70. [
  71. IAccountManager::PROPERTY_WEBSITE => $properties[IAccountManager::PROPERTY_WEBSITE],
  72. IAccountManager::PROPERTY_PHONE => $properties[IAccountManager::PROPERTY_PHONE],
  73. ],
  74. $account->getFilteredProperties(IAccountManager::VISIBILITY_PUBLIC)
  75. );
  76. $this->assertEquals(
  77. [
  78. IAccountManager::PROPERTY_EMAIL => $properties[IAccountManager::PROPERTY_EMAIL],
  79. IAccountManager::PROPERTY_PHONE => $properties[IAccountManager::PROPERTY_PHONE],
  80. ],
  81. $account->getFilteredProperties(null, IAccountManager::VERIFIED)
  82. );
  83. $this->assertEquals(
  84. [IAccountManager::PROPERTY_PHONE => $properties[IAccountManager::PROPERTY_PHONE]],
  85. $account->getFilteredProperties(IAccountManager::VISIBILITY_PUBLIC, IAccountManager::VERIFIED)
  86. );
  87. }
  88. public function testJsonSerialize() {
  89. $user = $this->createMock(IUser::class);
  90. $properties = [
  91. IAccountManager::PROPERTY_WEBSITE => new AccountProperty(IAccountManager::PROPERTY_WEBSITE, 'https://example.com', IAccountManager::VISIBILITY_PUBLIC, IAccountManager::NOT_VERIFIED),
  92. IAccountManager::PROPERTY_EMAIL => new AccountProperty(IAccountManager::PROPERTY_EMAIL, 'user@example.com', IAccountManager::VISIBILITY_PRIVATE, IAccountManager::VERIFIED)
  93. ];
  94. $account = new Account($user);
  95. $account->setProperty(IAccountManager::PROPERTY_WEBSITE, 'https://example.com', IAccountManager::VISIBILITY_PUBLIC, IAccountManager::NOT_VERIFIED);
  96. $account->setProperty(IAccountManager::PROPERTY_EMAIL, 'user@example.com', IAccountManager::VISIBILITY_PRIVATE, IAccountManager::VERIFIED);
  97. $this->assertEquals($properties, $account->jsonSerialize());
  98. }
  99. }