ManagerTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Activity;
  8. use OCP\Activity\Exceptions\IncompleteActivityException;
  9. use OCP\IConfig;
  10. use OCP\IL10N;
  11. use OCP\IRequest;
  12. use OCP\IUser;
  13. use OCP\IUserSession;
  14. use OCP\RichObjectStrings\IValidator;
  15. use Test\TestCase;
  16. class ManagerTest extends TestCase {
  17. /** @var \OC\Activity\Manager */
  18. private $activityManager;
  19. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
  20. protected $request;
  21. /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
  22. protected $session;
  23. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  24. protected $config;
  25. /** @var IValidator|\PHPUnit\Framework\MockObject\MockObject */
  26. protected $validator;
  27. protected function setUp(): void {
  28. parent::setUp();
  29. $this->request = $this->createMock(IRequest::class);
  30. $this->session = $this->createMock(IUserSession::class);
  31. $this->config = $this->createMock(IConfig::class);
  32. $this->validator = $this->createMock(IValidator::class);
  33. $this->activityManager = new \OC\Activity\Manager(
  34. $this->request,
  35. $this->session,
  36. $this->config,
  37. $this->validator,
  38. $this->createMock(IL10N::class)
  39. );
  40. $this->assertSame([], self::invokePrivate($this->activityManager, 'getConsumers'));
  41. $this->activityManager->registerConsumer(function () {
  42. return new NoOpConsumer();
  43. });
  44. $this->assertNotEmpty(self::invokePrivate($this->activityManager, 'getConsumers'));
  45. $this->assertNotEmpty(self::invokePrivate($this->activityManager, 'getConsumers'));
  46. }
  47. public function testGetConsumers(): void {
  48. $consumers = self::invokePrivate($this->activityManager, 'getConsumers');
  49. $this->assertNotEmpty($consumers);
  50. }
  51. public function testGetConsumersInvalidConsumer(): void {
  52. $this->expectException(\InvalidArgumentException::class);
  53. $this->activityManager->registerConsumer(function () {
  54. return new \stdClass();
  55. });
  56. self::invokePrivate($this->activityManager, 'getConsumers');
  57. }
  58. public function getUserFromTokenThrowInvalidTokenData() {
  59. return [
  60. [null, []],
  61. ['', []],
  62. ['12345678901234567890123456789', []],
  63. ['1234567890123456789012345678901', []],
  64. ['123456789012345678901234567890', []],
  65. ['123456789012345678901234567890', ['user1', 'user2']],
  66. ];
  67. }
  68. /**
  69. * @dataProvider getUserFromTokenThrowInvalidTokenData
  70. *
  71. * @param string $token
  72. * @param array $users
  73. */
  74. public function testGetUserFromTokenThrowInvalidToken($token, $users): void {
  75. $this->expectException(\UnexpectedValueException::class);
  76. $this->mockRSSToken($token, $token, $users);
  77. self::invokePrivate($this->activityManager, 'getUserFromToken');
  78. }
  79. public function getUserFromTokenData() {
  80. return [
  81. [null, '123456789012345678901234567890', 'user1'],
  82. ['user2', null, 'user2'],
  83. ['user2', '123456789012345678901234567890', 'user2'],
  84. ];
  85. }
  86. /**
  87. * @dataProvider getUserFromTokenData
  88. *
  89. * @param string $userLoggedIn
  90. * @param string $token
  91. * @param string $expected
  92. */
  93. public function testGetUserFromToken($userLoggedIn, $token, $expected): void {
  94. if ($userLoggedIn !== null) {
  95. $this->mockUserSession($userLoggedIn);
  96. }
  97. $this->mockRSSToken($token, '123456789012345678901234567890', ['user1']);
  98. $this->assertEquals($expected, $this->activityManager->getCurrentUserId());
  99. }
  100. protected function mockRSSToken($requestToken, $userToken, $users) {
  101. if ($requestToken !== null) {
  102. $this->request->expects($this->any())
  103. ->method('getParam')
  104. ->with('token', '')
  105. ->willReturn($requestToken);
  106. }
  107. $this->config->expects($this->any())
  108. ->method('getUsersForUserValue')
  109. ->with('activity', 'rsstoken', $userToken)
  110. ->willReturn($users);
  111. }
  112. protected function mockUserSession($user) {
  113. $mockUser = $this->getMockBuilder(IUser::class)
  114. ->disableOriginalConstructor()
  115. ->getMock();
  116. $mockUser->expects($this->any())
  117. ->method('getUID')
  118. ->willReturn($user);
  119. $this->session->expects($this->any())
  120. ->method('isLoggedIn')
  121. ->willReturn(true);
  122. $this->session->expects($this->any())
  123. ->method('getUser')
  124. ->willReturn($mockUser);
  125. }
  126. public function testPublishExceptionNoApp(): void {
  127. $this->expectException(IncompleteActivityException::class);
  128. $event = $this->activityManager->generateEvent();
  129. $this->activityManager->publish($event);
  130. }
  131. public function testPublishExceptionNoType(): void {
  132. $this->expectException(IncompleteActivityException::class);
  133. $event = $this->activityManager->generateEvent();
  134. $event->setApp('test');
  135. $this->activityManager->publish($event);
  136. }
  137. public function testPublishExceptionNoAffectedUser(): void {
  138. $this->expectException(IncompleteActivityException::class);
  139. $event = $this->activityManager->generateEvent();
  140. $event->setApp('test')
  141. ->setType('test_type');
  142. $this->activityManager->publish($event);
  143. }
  144. public function testPublishExceptionNoSubject(): void {
  145. $this->expectException(IncompleteActivityException::class);
  146. $event = $this->activityManager->generateEvent();
  147. $event->setApp('test')
  148. ->setType('test_type')
  149. ->setAffectedUser('test_affected');
  150. $this->activityManager->publish($event);
  151. }
  152. public function dataPublish() {
  153. return [
  154. [null, ''],
  155. ['test_author', 'test_author'],
  156. ];
  157. }
  158. /**
  159. * @dataProvider dataPublish
  160. * @param string|null $author
  161. * @param string $expected
  162. */
  163. public function testPublish($author, $expected): void {
  164. if ($author !== null) {
  165. $authorObject = $this->getMockBuilder(IUser::class)
  166. ->disableOriginalConstructor()
  167. ->getMock();
  168. $authorObject->expects($this->once())
  169. ->method('getUID')
  170. ->willReturn($author);
  171. $this->session->expects($this->atLeastOnce())
  172. ->method('getUser')
  173. ->willReturn($authorObject);
  174. }
  175. $event = $this->activityManager->generateEvent();
  176. $event->setApp('test')
  177. ->setType('test_type')
  178. ->setSubject('test_subject', [])
  179. ->setAffectedUser('test_affected')
  180. ->setObject('file', 123);
  181. $consumer = $this->getMockBuilder('OCP\Activity\IConsumer')
  182. ->disableOriginalConstructor()
  183. ->getMock();
  184. $consumer->expects($this->once())
  185. ->method('receive')
  186. ->with($event)
  187. ->willReturnCallback(function (\OCP\Activity\IEvent $event) use ($expected) {
  188. $this->assertLessThanOrEqual(time() + 2, $event->getTimestamp(), 'Timestamp not set correctly');
  189. $this->assertGreaterThanOrEqual(time() - 2, $event->getTimestamp(), 'Timestamp not set correctly');
  190. $this->assertSame($expected, $event->getAuthor(), 'Author name not set correctly');
  191. });
  192. $this->activityManager->registerConsumer(function () use ($consumer) {
  193. return $consumer;
  194. });
  195. $this->activityManager->publish($event);
  196. }
  197. public function testPublishAllManually(): void {
  198. $event = $this->activityManager->generateEvent();
  199. $event->setApp('test_app')
  200. ->setType('test_type')
  201. ->setAffectedUser('test_affected')
  202. ->setAuthor('test_author')
  203. ->setTimestamp(1337)
  204. ->setSubject('test_subject', ['test_subject_param'])
  205. ->setMessage('test_message', ['test_message_param'])
  206. ->setObject('test_object_type', 42, 'test_object_name')
  207. ->setLink('test_link')
  208. ;
  209. $consumer = $this->getMockBuilder('OCP\Activity\IConsumer')
  210. ->disableOriginalConstructor()
  211. ->getMock();
  212. $consumer->expects($this->once())
  213. ->method('receive')
  214. ->willReturnCallback(function (\OCP\Activity\IEvent $event) {
  215. $this->assertSame('test_app', $event->getApp(), 'App not set correctly');
  216. $this->assertSame('test_type', $event->getType(), 'Type not set correctly');
  217. $this->assertSame('test_affected', $event->getAffectedUser(), 'Affected user not set correctly');
  218. $this->assertSame('test_author', $event->getAuthor(), 'Author not set correctly');
  219. $this->assertSame(1337, $event->getTimestamp(), 'Timestamp not set correctly');
  220. $this->assertSame('test_subject', $event->getSubject(), 'Subject not set correctly');
  221. $this->assertSame(['test_subject_param'], $event->getSubjectParameters(), 'Subject parameter not set correctly');
  222. $this->assertSame('test_message', $event->getMessage(), 'Message not set correctly');
  223. $this->assertSame(['test_message_param'], $event->getMessageParameters(), 'Message parameter not set correctly');
  224. $this->assertSame('test_object_type', $event->getObjectType(), 'Object type not set correctly');
  225. $this->assertSame(42, $event->getObjectId(), 'Object ID not set correctly');
  226. $this->assertSame('test_object_name', $event->getObjectName(), 'Object name not set correctly');
  227. $this->assertSame('test_link', $event->getLink(), 'Link not set correctly');
  228. });
  229. $this->activityManager->registerConsumer(function () use ($consumer) {
  230. return $consumer;
  231. });
  232. $this->activityManager->publish($event);
  233. }
  234. }
  235. class NoOpConsumer implements \OCP\Activity\IConsumer {
  236. public function receive(\OCP\Activity\IEvent $event) {
  237. }
  238. }