CloudIdManagerTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 OCP\Contacts\IManager;
  24. use Test\TestCase;
  25. class CloudIdManagerTest extends TestCase {
  26. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  27. protected $contactsManager;
  28. /** @var CloudIdManager */
  29. private $cloudIdManager;
  30. protected function setUp(): void {
  31. parent::setUp();
  32. $this->contactsManager = $this->createMock(IManager::class);
  33. $this->cloudIdManager = new CloudIdManager($this->contactsManager);
  34. }
  35. public function cloudIdProvider() {
  36. return [
  37. ['test@example.com', 'test', 'example.com', 'test@example.com'],
  38. ['test@example.com/cloud', 'test', 'example.com/cloud', 'test@example.com/cloud'],
  39. ['test@example.com/cloud/', 'test', 'example.com/cloud', 'test@example.com/cloud'],
  40. ['test@example.com/cloud/index.php', 'test', 'example.com/cloud', 'test@example.com/cloud'],
  41. ['test@example.com@example.com', 'test@example.com', 'example.com', 'test@example.com@example.com'],
  42. ];
  43. }
  44. /**
  45. * @dataProvider cloudIdProvider
  46. *
  47. * @param string $cloudId
  48. * @param string $user
  49. * @param string $remote
  50. */
  51. public function testResolveCloudId($cloudId, $user, $remote, $cleanId) {
  52. $displayName = 'Ample Ex';
  53. $this->contactsManager->expects($this->any())
  54. ->method('search')
  55. ->with($cleanId, ['CLOUD'])
  56. ->willReturn([
  57. [
  58. 'CLOUD' => [$cleanId],
  59. 'FN' => 'Ample Ex',
  60. ]
  61. ]);
  62. $cloudId = $this->cloudIdManager->resolveCloudId($cloudId);
  63. $this->assertEquals($user, $cloudId->getUser());
  64. $this->assertEquals($remote, $cloudId->getRemote());
  65. $this->assertEquals($cleanId, $cloudId->getId());
  66. $this->assertEquals($displayName . '@' . $remote, $cloudId->getDisplayId());
  67. }
  68. public function invalidCloudIdProvider() {
  69. return [
  70. ['example.com'],
  71. ['test:foo@example.com'],
  72. ['test/foo@example.com']
  73. ];
  74. }
  75. /**
  76. * @dataProvider invalidCloudIdProvider
  77. *
  78. * @param string $cloudId
  79. *
  80. */
  81. public function testInvalidCloudId($cloudId) {
  82. $this->expectException(\InvalidArgumentException::class);
  83. $this->contactsManager->expects($this->never())
  84. ->method('search');
  85. $this->cloudIdManager->resolveCloudId($cloudId);
  86. }
  87. public function getCloudIdProvider() {
  88. return [
  89. ['test', 'example.com', 'test@example.com'],
  90. ['test@example.com', 'example.com', 'test@example.com@example.com'],
  91. ];
  92. }
  93. /**
  94. * @dataProvider getCloudIdProvider
  95. *
  96. * @param string $user
  97. * @param string $remote
  98. * @param string $id
  99. */
  100. public function testGetCloudId($user, $remote, $id) {
  101. $this->contactsManager->expects($this->any())
  102. ->method('search')
  103. ->with($id, ['CLOUD'])
  104. ->willReturn([
  105. [
  106. 'CLOUD' => [$id],
  107. 'FN' => 'Ample Ex',
  108. ]
  109. ]);
  110. $cloudId = $this->cloudIdManager->getCloudId($user, $remote);
  111. $this->assertEquals($id, $cloudId->getId());
  112. }
  113. }