ProviderTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Files\Tests\Activity;
  7. use OCA\Files\Activity\Provider;
  8. use OCP\Activity\IEvent;
  9. use OCP\Activity\IEventMerger;
  10. use OCP\Activity\IManager;
  11. use OCP\Contacts\IManager as IContactsManager;
  12. use OCP\Federation\ICloudId;
  13. use OCP\Federation\ICloudIdManager;
  14. use OCP\Files\IRootFolder;
  15. use OCP\IURLGenerator;
  16. use OCP\IUserManager;
  17. use OCP\L10N\IFactory;
  18. use PHPUnit\Framework\MockObject\MockObject;
  19. use Test\TestCase;
  20. /**
  21. * Class ProviderTest
  22. *
  23. * @package OCA\Files\Tests\Activity
  24. */
  25. class ProviderTest extends TestCase {
  26. /** @var IFactory|MockObject */
  27. protected $l10nFactory;
  28. /** @var IURLGenerator|MockObject */
  29. protected $url;
  30. /** @var IManager|MockObject */
  31. protected $activityManager;
  32. /** @var IUserManager|MockObject */
  33. protected $userManager;
  34. /** @var IRootFolder|MockObject */
  35. protected $rootFolder;
  36. /** @var ICloudIdManager|MockObject */
  37. protected $cloudIdManager;
  38. /** @var IContactsManager|MockObject */
  39. protected $contactsManager;
  40. /** @var IEventMerger|MockObject */
  41. protected $eventMerger;
  42. protected function setUp(): void {
  43. parent::setUp();
  44. $this->l10nFactory = $this->createMock(IFactory::class);
  45. $this->url = $this->createMock(IURLGenerator::class);
  46. $this->activityManager = $this->createMock(IManager::class);
  47. $this->userManager = $this->createMock(IUserManager::class);
  48. $this->rootFolder = $this->createMock(IRootFolder::class);
  49. $this->cloudIdManager = $this->createMock(ICloudIdManager::class);
  50. $this->contactsManager = $this->createMock(IContactsManager::class);
  51. $this->eventMerger = $this->createMock(IEventMerger::class);
  52. }
  53. /**
  54. * @param string[] $methods
  55. * @return Provider|MockObject
  56. */
  57. protected function getProvider(array $methods = []) {
  58. if (!empty($methods)) {
  59. return $this->getMockBuilder(Provider::class)
  60. ->setConstructorArgs([
  61. $this->l10nFactory,
  62. $this->url,
  63. $this->activityManager,
  64. $this->userManager,
  65. $this->rootFolder,
  66. $this->cloudIdManager,
  67. $this->contactsManager,
  68. $this->eventMerger,
  69. ])
  70. ->setMethods($methods)
  71. ->getMock();
  72. }
  73. return new Provider(
  74. $this->l10nFactory,
  75. $this->url,
  76. $this->activityManager,
  77. $this->userManager,
  78. $this->rootFolder,
  79. $this->cloudIdManager,
  80. $this->contactsManager,
  81. $this->eventMerger
  82. );
  83. }
  84. public function dataGetFile() {
  85. return [
  86. [[42 => '/FortyTwo.txt'], null, '42', 'FortyTwo.txt', 'FortyTwo.txt'],
  87. [['23' => '/Twenty/Three.txt'], null, '23', 'Three.txt', 'Twenty/Three.txt'],
  88. ['/Foo/Bar.txt', 128, 128, 'Bar.txt', 'Foo/Bar.txt'], // Legacy from ownCloud 8.2 and before
  89. ];
  90. }
  91. /**
  92. * @dataProvider dataGetFile
  93. * @param mixed $parameter
  94. * @param mixed $eventId
  95. * @param int $id
  96. * @param string $name
  97. * @param string $path
  98. */
  99. public function testGetFile($parameter, $eventId, $id, $name, $path) {
  100. $provider = $this->getProvider();
  101. if ($eventId !== null) {
  102. $event = $this->createMock(IEvent::class);
  103. $event->expects($this->once())
  104. ->method('getObjectId')
  105. ->willReturn($eventId);
  106. } else {
  107. $event = null;
  108. }
  109. $this->url->expects($this->once())
  110. ->method('linkToRouteAbsolute')
  111. ->with('files.viewcontroller.showFile', ['fileid' => $id])
  112. ->willReturn('link-' . $id);
  113. $result = self::invokePrivate($provider, 'getFile', [$parameter, $event]);
  114. $this->assertSame('file', $result['type']);
  115. $this->assertSame($id, $result['id']);
  116. $this->assertSame($name, $result['name']);
  117. $this->assertSame($path, $result['path']);
  118. $this->assertSame('link-' . $id, $result['link']);
  119. }
  120. public function testGetFileThrows() {
  121. $this->expectException(\InvalidArgumentException::class);
  122. $provider = $this->getProvider();
  123. self::invokePrivate($provider, 'getFile', ['/Foo/Bar.txt', null]);
  124. }
  125. public function dataGetUser() {
  126. return [
  127. ['test', 'Test user', null, ['type' => 'user', 'id' => 'test', 'name' => 'Test user']],
  128. ['test@http://localhost', null, ['user' => 'test', 'displayId' => 'test@localhost', 'remote' => 'localhost', 'name' => null], ['type' => 'user', 'id' => 'test', 'name' => 'test@localhost', 'server' => 'localhost']],
  129. ['test@http://localhost', null, ['user' => 'test', 'displayId' => 'test@localhost', 'remote' => 'localhost', 'name' => 'Remote user'], ['type' => 'user', 'id' => 'test', 'name' => 'Remote user (test@localhost)', 'server' => 'localhost']],
  130. ['test', null, null, ['type' => 'user', 'id' => 'test', 'name' => 'test']],
  131. ];
  132. }
  133. /**
  134. * @dataProvider dataGetUser
  135. * @param string $uid
  136. * @param string|null $userDisplayName
  137. * @param array|null $cloudIdData
  138. * @param array $expected
  139. */
  140. public function testGetUser(string $uid, ?string $userDisplayName, ?array $cloudIdData, array $expected): void {
  141. $provider = $this->getProvider();
  142. if ($userDisplayName !== null) {
  143. $this->userManager->expects($this->once())
  144. ->method('getDisplayName')
  145. ->with($uid)
  146. ->willReturn($userDisplayName);
  147. }
  148. if ($cloudIdData !== null) {
  149. $this->cloudIdManager->expects($this->once())
  150. ->method('isValidCloudId')
  151. ->willReturn(true);
  152. $cloudId = $this->createMock(ICloudId::class);
  153. $cloudId->expects($this->once())
  154. ->method('getUser')
  155. ->willReturn($cloudIdData['user']);
  156. $cloudId->expects($this->once())
  157. ->method('getDisplayId')
  158. ->willReturn($cloudIdData['displayId']);
  159. $cloudId->expects($this->once())
  160. ->method('getRemote')
  161. ->willReturn($cloudIdData['remote']);
  162. $this->cloudIdManager->expects($this->once())
  163. ->method('resolveCloudId')
  164. ->with($uid)
  165. ->willReturn($cloudId);
  166. if ($cloudIdData['name'] !== null) {
  167. $this->contactsManager->expects($this->once())
  168. ->method('search')
  169. ->with($cloudIdData['displayId'], ['CLOUD'])
  170. ->willReturn([
  171. [
  172. 'CLOUD' => $cloudIdData['displayId'],
  173. 'FN' => $cloudIdData['name'],
  174. ]
  175. ]);
  176. } else {
  177. $this->contactsManager->expects($this->once())
  178. ->method('search')
  179. ->with($cloudIdData['displayId'], ['CLOUD'])
  180. ->willReturn([]);
  181. }
  182. }
  183. $result = self::invokePrivate($provider, 'getUser', [$uid]);
  184. $this->assertEquals($expected, $result);
  185. }
  186. }