AccountPropertyTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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\AccountProperty;
  8. use OCP\Accounts\IAccountManager;
  9. use Test\TestCase;
  10. /**
  11. * Class AccountPropertyTest
  12. *
  13. * @package Test\Accounts
  14. */
  15. class AccountPropertyTest extends TestCase {
  16. public function testConstructor() {
  17. $accountProperty = new AccountProperty(
  18. IAccountManager::PROPERTY_WEBSITE,
  19. 'https://example.com',
  20. IAccountManager::SCOPE_PUBLISHED,
  21. IAccountManager::VERIFIED,
  22. ''
  23. );
  24. $this->assertEquals(IAccountManager::PROPERTY_WEBSITE, $accountProperty->getName());
  25. $this->assertEquals('https://example.com', $accountProperty->getValue());
  26. $this->assertEquals(IAccountManager::SCOPE_PUBLISHED, $accountProperty->getScope());
  27. $this->assertEquals(IAccountManager::VERIFIED, $accountProperty->getVerified());
  28. }
  29. public function testSetValue() {
  30. $accountProperty = new AccountProperty(
  31. IAccountManager::PROPERTY_WEBSITE,
  32. 'https://example.com',
  33. IAccountManager::SCOPE_PUBLISHED,
  34. IAccountManager::VERIFIED,
  35. ''
  36. );
  37. $actualReturn = $accountProperty->setValue('https://example.org');
  38. $this->assertEquals('https://example.org', $accountProperty->getValue());
  39. $this->assertEquals('https://example.org', $actualReturn->getValue());
  40. }
  41. public function testSetScope() {
  42. $accountProperty = new AccountProperty(
  43. IAccountManager::PROPERTY_WEBSITE,
  44. 'https://example.com',
  45. IAccountManager::SCOPE_PUBLISHED,
  46. IAccountManager::VERIFIED,
  47. ''
  48. );
  49. $actualReturn = $accountProperty->setScope(IAccountManager::SCOPE_LOCAL);
  50. $this->assertEquals(IAccountManager::SCOPE_LOCAL, $accountProperty->getScope());
  51. $this->assertEquals(IAccountManager::SCOPE_LOCAL, $actualReturn->getScope());
  52. }
  53. public function scopesProvider() {
  54. return [
  55. // current values
  56. [IAccountManager::SCOPE_PRIVATE, IAccountManager::SCOPE_PRIVATE],
  57. [IAccountManager::SCOPE_LOCAL, IAccountManager::SCOPE_LOCAL],
  58. [IAccountManager::SCOPE_FEDERATED, IAccountManager::SCOPE_FEDERATED],
  59. [IAccountManager::SCOPE_PUBLISHED, IAccountManager::SCOPE_PUBLISHED],
  60. // legacy values
  61. [IAccountManager::VISIBILITY_PRIVATE, IAccountManager::SCOPE_LOCAL],
  62. [IAccountManager::VISIBILITY_CONTACTS_ONLY, IAccountManager::SCOPE_FEDERATED],
  63. [IAccountManager::VISIBILITY_PUBLIC, IAccountManager::SCOPE_PUBLISHED],
  64. ['', IAccountManager::SCOPE_LOCAL],
  65. // invalid values
  66. ['unknown', null],
  67. ['v2-unknown', null],
  68. ];
  69. }
  70. /**
  71. * @dataProvider scopesProvider
  72. */
  73. public function testSetScopeMapping(string $storedScope, ?string $returnedScope) {
  74. if ($returnedScope === null) {
  75. $this->expectException(\InvalidArgumentException::class);
  76. }
  77. $accountProperty = new AccountProperty(
  78. IAccountManager::PROPERTY_WEBSITE,
  79. 'https://example.com',
  80. $storedScope,
  81. IAccountManager::VERIFIED,
  82. ''
  83. );
  84. $this->assertEquals($returnedScope, $accountProperty->getScope());
  85. }
  86. public function testSetVerified() {
  87. $accountProperty = new AccountProperty(
  88. IAccountManager::PROPERTY_WEBSITE,
  89. 'https://example.com',
  90. IAccountManager::SCOPE_PUBLISHED,
  91. IAccountManager::VERIFIED,
  92. ''
  93. );
  94. $actualReturn = $accountProperty->setVerified(IAccountManager::NOT_VERIFIED);
  95. $this->assertEquals(IAccountManager::NOT_VERIFIED, $accountProperty->getVerified());
  96. $this->assertEquals(IAccountManager::NOT_VERIFIED, $actualReturn->getVerified());
  97. }
  98. public function testSetVerificationData() {
  99. $accountProperty = new AccountProperty(
  100. IAccountManager::PROPERTY_WEBSITE,
  101. 'https://example.com',
  102. IAccountManager::SCOPE_PUBLISHED,
  103. IAccountManager::VERIFIED,
  104. ''
  105. );
  106. $token = uniqid();
  107. $actualReturn = $accountProperty->setVerificationData($token);
  108. $this->assertEquals($token, $accountProperty->getVerificationData());
  109. $this->assertEquals($token, $actualReturn->getVerificationData());
  110. }
  111. public function testJsonSerialize() {
  112. $accountProperty = new AccountProperty(
  113. IAccountManager::PROPERTY_WEBSITE,
  114. 'https://example.com',
  115. IAccountManager::SCOPE_PUBLISHED,
  116. IAccountManager::VERIFIED,
  117. '60a7a633b74af',
  118. );
  119. $this->assertEquals([
  120. 'name' => IAccountManager::PROPERTY_WEBSITE,
  121. 'value' => 'https://example.com',
  122. 'scope' => IAccountManager::SCOPE_PUBLISHED,
  123. 'verified' => IAccountManager::VERIFIED,
  124. 'verificationData' => '60a7a633b74af'
  125. ], $accountProperty->jsonSerialize());
  126. }
  127. }