1
0

ManagerTest.php 8.8 KB

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