1
0

ProviderTest.php 6.2 KB

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