ProviderTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. $this->userManager->expects($this->once())
  163. ->method('getDisplayName')
  164. ->with($uid)
  165. ->willReturn($userDisplayName);
  166. }
  167. if ($cloudIdData !== null) {
  168. $this->cloudIdManager->expects($this->once())
  169. ->method('isValidCloudId')
  170. ->willReturn(true);
  171. $cloudId = $this->createMock(ICloudId::class);
  172. $cloudId->expects($this->once())
  173. ->method('getUser')
  174. ->willReturn($cloudIdData['user']);
  175. $cloudId->expects($this->once())
  176. ->method('getDisplayId')
  177. ->willReturn($cloudIdData['displayId']);
  178. $cloudId->expects($this->once())
  179. ->method('getRemote')
  180. ->willReturn($cloudIdData['remote']);
  181. $this->cloudIdManager->expects($this->once())
  182. ->method('resolveCloudId')
  183. ->with($uid)
  184. ->willReturn($cloudId);
  185. if ($cloudIdData['name'] !== null) {
  186. $this->contactsManager->expects($this->once())
  187. ->method('search')
  188. ->with($cloudIdData['displayId'], ['CLOUD'])
  189. ->willReturn([
  190. [
  191. 'CLOUD' => $cloudIdData['displayId'],
  192. 'FN' => $cloudIdData['name'],
  193. ]
  194. ]);
  195. } else {
  196. $this->contactsManager->expects($this->once())
  197. ->method('search')
  198. ->with($cloudIdData['displayId'], ['CLOUD'])
  199. ->willReturn([]);
  200. }
  201. }
  202. $result = self::invokePrivate($provider, 'getUser', [$uid]);
  203. $this->assertEquals($expected, $result);
  204. }
  205. }