ProviderTest.php 6.9 KB

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