CloudIdManagerTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace Test\Federation;
  22. use OC\Federation\CloudIdManager;
  23. use OC\Memcache\ArrayCache;
  24. use OCP\Contacts\IManager;
  25. use OCP\EventDispatcher\IEventDispatcher;
  26. use OCP\ICacheFactory;
  27. use OCP\IURLGenerator;
  28. use OCP\IUserManager;
  29. use Test\TestCase;
  30. class CloudIdManagerTest extends TestCase {
  31. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  32. protected $contactsManager;
  33. /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
  34. private $urlGenerator;
  35. /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  36. private $userManager;
  37. /** @var CloudIdManager */
  38. private $cloudIdManager;
  39. /** @var ICacheFactory|\PHPUnit\Framework\MockObject\MockObject */
  40. private $cacheFactory;
  41. protected function setUp(): void {
  42. parent::setUp();
  43. $this->contactsManager = $this->createMock(IManager::class);
  44. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  45. $this->userManager = $this->createMock(IUserManager::class);
  46. $this->cacheFactory = $this->createMock(ICacheFactory::class);
  47. $this->cacheFactory->method('createLocal')
  48. ->willReturn(new ArrayCache(''));
  49. $this->cloudIdManager = new CloudIdManager(
  50. $this->contactsManager,
  51. $this->urlGenerator,
  52. $this->userManager,
  53. $this->cacheFactory,
  54. $this->createMock(IEventDispatcher::class)
  55. );
  56. }
  57. public function cloudIdProvider() {
  58. return [
  59. ['test@example.com', 'test', 'example.com', 'test@example.com'],
  60. ['test@example.com/cloud', 'test', 'example.com/cloud', 'test@example.com/cloud'],
  61. ['test@example.com/cloud/', 'test', 'example.com/cloud', 'test@example.com/cloud'],
  62. ['test@example.com/cloud/index.php', 'test', 'example.com/cloud', 'test@example.com/cloud'],
  63. ['test@example.com@example.com', 'test@example.com', 'example.com', 'test@example.com@example.com'],
  64. ];
  65. }
  66. /**
  67. * @dataProvider cloudIdProvider
  68. *
  69. * @param string $cloudId
  70. * @param string $user
  71. * @param string $remote
  72. */
  73. public function testResolveCloudId($cloudId, $user, $remote, $cleanId) {
  74. $displayName = 'Ample Ex';
  75. $this->contactsManager->expects($this->any())
  76. ->method('search')
  77. ->with($cleanId, ['CLOUD'])
  78. ->willReturn([
  79. [
  80. 'CLOUD' => [$cleanId],
  81. 'FN' => 'Ample Ex',
  82. ]
  83. ]);
  84. $cloudId = $this->cloudIdManager->resolveCloudId($cloudId);
  85. $this->assertEquals($user, $cloudId->getUser());
  86. $this->assertEquals($remote, $cloudId->getRemote());
  87. $this->assertEquals($cleanId, $cloudId->getId());
  88. $this->assertEquals($displayName . '@' . $remote, $cloudId->getDisplayId());
  89. }
  90. public function invalidCloudIdProvider() {
  91. return [
  92. ['example.com'],
  93. ['test:foo@example.com'],
  94. ['test/foo@example.com']
  95. ];
  96. }
  97. /**
  98. * @dataProvider invalidCloudIdProvider
  99. *
  100. * @param string $cloudId
  101. *
  102. */
  103. public function testInvalidCloudId($cloudId) {
  104. $this->expectException(\InvalidArgumentException::class);
  105. $this->contactsManager->expects($this->never())
  106. ->method('search');
  107. $this->cloudIdManager->resolveCloudId($cloudId);
  108. }
  109. public function getCloudIdProvider() {
  110. return [
  111. ['test', 'example.com', 'test@example.com'],
  112. ['test@example.com', 'example.com', 'test@example.com@example.com'],
  113. ['test@example.com', null, 'test@example.com@example.com'],
  114. ];
  115. }
  116. /**
  117. * @dataProvider getCloudIdProvider
  118. *
  119. * @param string $user
  120. * @param string $remote
  121. * @param string $id
  122. */
  123. public function testGetCloudId($user, $remote, $id) {
  124. if ($remote !== null) {
  125. $this->contactsManager->expects($this->any())
  126. ->method('search')
  127. ->with($id, ['CLOUD'])
  128. ->willReturn([
  129. [
  130. 'CLOUD' => [$id],
  131. 'FN' => 'Ample Ex',
  132. ]
  133. ]);
  134. } else {
  135. $this->urlGenerator->expects(self::once())
  136. ->method('getAbsoluteUrl')
  137. ->willReturn('https://example.com');
  138. }
  139. $cloudId = $this->cloudIdManager->getCloudId($user, $remote);
  140. $this->assertEquals($id, $cloudId->getId());
  141. }
  142. }