ProviderTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Files\Tests\Activity;
  25. use OCA\Files\Activity\Provider;
  26. use OCP\Activity\IEvent;
  27. use OCP\Activity\IEventMerger;
  28. use OCP\Activity\IManager;
  29. use OCP\Contacts\IManager as IContactsManager;
  30. use OCP\Federation\ICloudId;
  31. use OCP\Federation\ICloudIdManager;
  32. use OCP\Files\IRootFolder;
  33. use OCP\IURLGenerator;
  34. use OCP\IUser;
  35. use OCP\IUserManager;
  36. use OCP\L10N\IFactory;
  37. use PHPUnit\Framework\MockObject\MockObject;
  38. use Test\TestCase;
  39. /**
  40. * Class ProviderTest
  41. *
  42. * @package OCA\Files\Tests\Activity
  43. */
  44. class ProviderTest extends TestCase {
  45. /** @var IFactory|MockObject */
  46. protected $l10nFactory;
  47. /** @var IURLGenerator|MockObject */
  48. protected $url;
  49. /** @var IManager|MockObject */
  50. protected $activityManager;
  51. /** @var IUserManager|MockObject */
  52. protected $userManager;
  53. /** @var IRootFolder|MockObject */
  54. protected $rootFolder;
  55. /** @var ICloudIdManager|MockObject */
  56. protected $cloudIdManager;
  57. /** @var IContactsManager|MockObject */
  58. protected $contactsManager;
  59. /** @var IEventMerger|MockObject */
  60. protected $eventMerger;
  61. protected function setUp(): void {
  62. parent::setUp();
  63. $this->l10nFactory = $this->createMock(IFactory::class);
  64. $this->url = $this->createMock(IURLGenerator::class);
  65. $this->activityManager = $this->createMock(IManager::class);
  66. $this->userManager = $this->createMock(IUserManager::class);
  67. $this->rootFolder = $this->createMock(IRootFolder::class);
  68. $this->cloudIdManager = $this->createMock(ICloudIdManager::class);
  69. $this->contactsManager = $this->createMock(IContactsManager::class);
  70. $this->eventMerger = $this->createMock(IEventMerger::class);
  71. }
  72. /**
  73. * @param string[] $methods
  74. * @return Provider|MockObject
  75. */
  76. protected function getProvider(array $methods = []) {
  77. if (!empty($methods)) {
  78. return $this->getMockBuilder(Provider::class)
  79. ->setConstructorArgs([
  80. $this->l10nFactory,
  81. $this->url,
  82. $this->activityManager,
  83. $this->userManager,
  84. $this->rootFolder,
  85. $this->cloudIdManager,
  86. $this->contactsManager,
  87. $this->eventMerger,
  88. ])
  89. ->setMethods($methods)
  90. ->getMock();
  91. }
  92. return new Provider(
  93. $this->l10nFactory,
  94. $this->url,
  95. $this->activityManager,
  96. $this->userManager,
  97. $this->rootFolder,
  98. $this->cloudIdManager,
  99. $this->contactsManager,
  100. $this->eventMerger
  101. );
  102. }
  103. public function dataGetFile() {
  104. return [
  105. [[42 => '/FortyTwo.txt'], null, '42', 'FortyTwo.txt', 'FortyTwo.txt'],
  106. [['23' => '/Twenty/Three.txt'], null, '23', 'Three.txt', 'Twenty/Three.txt'],
  107. ['/Foo/Bar.txt', 128, 128, 'Bar.txt', 'Foo/Bar.txt'], // Legacy from ownCloud 8.2 and before
  108. ];
  109. }
  110. /**
  111. * @dataProvider dataGetFile
  112. * @param mixed $parameter
  113. * @param mixed $eventId
  114. * @param int $id
  115. * @param string $name
  116. * @param string $path
  117. */
  118. public function testGetFile($parameter, $eventId, $id, $name, $path) {
  119. $provider = $this->getProvider();
  120. if ($eventId !== null) {
  121. $event = $this->createMock(IEvent::class);
  122. $event->expects($this->once())
  123. ->method('getObjectId')
  124. ->willReturn($eventId);
  125. } else {
  126. $event = null;
  127. }
  128. $this->url->expects($this->once())
  129. ->method('linkToRouteAbsolute')
  130. ->with('files.viewcontroller.showFile', ['fileid' => $id])
  131. ->willReturn('link-' . $id);
  132. $result = self::invokePrivate($provider, 'getFile', [$parameter, $event]);
  133. $this->assertSame('file', $result['type']);
  134. $this->assertSame($id, $result['id']);
  135. $this->assertSame($name, $result['name']);
  136. $this->assertSame($path, $result['path']);
  137. $this->assertSame('link-' . $id, $result['link']);
  138. }
  139. public function testGetFileThrows() {
  140. $this->expectException(\InvalidArgumentException::class);
  141. $provider = $this->getProvider();
  142. self::invokePrivate($provider, 'getFile', ['/Foo/Bar.txt', null]);
  143. }
  144. public function dataGetUser() {
  145. return [
  146. ['test', 'Test user', null, ['type' => 'user', 'id' => 'test', 'name' => 'Test user']],
  147. ['test@http://localhost', null, ['user' => 'test', 'displayId' => 'test@localhost', 'remote' => 'localhost', 'name' => null], ['type' => 'user', 'id' => 'test', 'name' => 'test@localhost', 'server' => 'localhost']],
  148. ['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']],
  149. ['test', null, null, ['type' => 'user', 'id' => 'test', 'name' => 'test']],
  150. ];
  151. }
  152. /**
  153. * @dataProvider dataGetUser
  154. * @param string $uid
  155. * @param string|null $userDisplayName
  156. * @param array|null $cloudIdData
  157. * @param array $expected
  158. */
  159. public function testGetUser(string $uid, ?string $userDisplayName, ?array $cloudIdData, array $expected): void {
  160. $provider = $this->getProvider();
  161. if ($userDisplayName !== null) {
  162. $user = $this->createMock(IUser::class);
  163. $user->expects($this->once())
  164. ->method('getUID')
  165. ->willReturn($uid);
  166. $user->expects($this->once())
  167. ->method('getDisplayName')
  168. ->willReturn($userDisplayName);
  169. $this->userManager->expects($this->once())
  170. ->method('get')
  171. ->with($uid)
  172. ->willReturn($user);
  173. }
  174. if ($cloudIdData !== null) {
  175. $this->cloudIdManager->expects($this->once())
  176. ->method('isValidCloudId')
  177. ->willReturn(true);
  178. $cloudId = $this->createMock(ICloudId::class);
  179. $cloudId->expects($this->once())
  180. ->method('getUser')
  181. ->willReturn($cloudIdData['user']);
  182. $cloudId->expects($this->once())
  183. ->method('getDisplayId')
  184. ->willReturn($cloudIdData['displayId']);
  185. $cloudId->expects($this->once())
  186. ->method('getRemote')
  187. ->willReturn($cloudIdData['remote']);
  188. $this->cloudIdManager->expects($this->once())
  189. ->method('resolveCloudId')
  190. ->with($uid)
  191. ->willReturn($cloudId);
  192. if ($cloudIdData['name'] !== null) {
  193. $this->contactsManager->expects($this->once())
  194. ->method('search')
  195. ->with($cloudIdData['displayId'], ['CLOUD'])
  196. ->willReturn([
  197. [
  198. 'CLOUD' => $cloudIdData['displayId'],
  199. 'FN' => $cloudIdData['name'],
  200. ]
  201. ]);
  202. } else {
  203. $this->contactsManager->expects($this->once())
  204. ->method('search')
  205. ->with($cloudIdData['displayId'], ['CLOUD'])
  206. ->willReturn([]);
  207. }
  208. }
  209. $result = self::invokePrivate($provider, 'getUser', [$uid]);
  210. $this->assertEquals($expected, $result);
  211. }
  212. }