1
0

AccountPropertyTest.php 5.1 KB

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