ManagerTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2014 Thomas Müller <deepdiver@owncloud.com>
  4. * @copyright Copyright (c) 2019 Joas Schilling <coding@schilljs.com>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace Test\Activity;
  23. use OCP\IConfig;
  24. use OCP\IL10N;
  25. use OCP\IRequest;
  26. use OCP\IUser;
  27. use OCP\IUserSession;
  28. use OCP\RichObjectStrings\IValidator;
  29. use Test\TestCase;
  30. class ManagerTest extends TestCase {
  31. /** @var \OC\Activity\Manager */
  32. private $activityManager;
  33. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
  34. protected $request;
  35. /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
  36. protected $session;
  37. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  38. protected $config;
  39. /** @var IValidator|\PHPUnit\Framework\MockObject\MockObject */
  40. protected $validator;
  41. protected function setUp(): void {
  42. parent::setUp();
  43. $this->request = $this->createMock(IRequest::class);
  44. $this->session = $this->createMock(IUserSession::class);
  45. $this->config = $this->createMock(IConfig::class);
  46. $this->validator = $this->createMock(IValidator::class);
  47. $this->activityManager = new \OC\Activity\Manager(
  48. $this->request,
  49. $this->session,
  50. $this->config,
  51. $this->validator,
  52. $this->createMock(IL10N::class)
  53. );
  54. $this->assertSame([], self::invokePrivate($this->activityManager, 'getConsumers'));
  55. $this->activityManager->registerConsumer(function () {
  56. return new NoOpConsumer();
  57. });
  58. $this->assertNotEmpty(self::invokePrivate($this->activityManager, 'getConsumers'));
  59. $this->assertNotEmpty(self::invokePrivate($this->activityManager, 'getConsumers'));
  60. }
  61. public function testGetConsumers() {
  62. $consumers = self::invokePrivate($this->activityManager, 'getConsumers');
  63. $this->assertNotEmpty($consumers);
  64. }
  65. public function testGetConsumersInvalidConsumer() {
  66. $this->expectException(\InvalidArgumentException::class);
  67. $this->activityManager->registerConsumer(function () {
  68. return new \stdClass();
  69. });
  70. self::invokePrivate($this->activityManager, 'getConsumers');
  71. }
  72. public function getUserFromTokenThrowInvalidTokenData() {
  73. return [
  74. [null, []],
  75. ['', []],
  76. ['12345678901234567890123456789', []],
  77. ['1234567890123456789012345678901', []],
  78. ['123456789012345678901234567890', []],
  79. ['123456789012345678901234567890', ['user1', 'user2']],
  80. ];
  81. }
  82. /**
  83. * @dataProvider getUserFromTokenThrowInvalidTokenData
  84. *
  85. * @param string $token
  86. * @param array $users
  87. */
  88. public function testGetUserFromTokenThrowInvalidToken($token, $users) {
  89. $this->expectException(\UnexpectedValueException::class);
  90. $this->mockRSSToken($token, $token, $users);
  91. self::invokePrivate($this->activityManager, 'getUserFromToken');
  92. }
  93. public function getUserFromTokenData() {
  94. return [
  95. [null, '123456789012345678901234567890', 'user1'],
  96. ['user2', null, 'user2'],
  97. ['user2', '123456789012345678901234567890', 'user2'],
  98. ];
  99. }
  100. /**
  101. * @dataProvider getUserFromTokenData
  102. *
  103. * @param string $userLoggedIn
  104. * @param string $token
  105. * @param string $expected
  106. */
  107. public function testGetUserFromToken($userLoggedIn, $token, $expected) {
  108. if ($userLoggedIn !== null) {
  109. $this->mockUserSession($userLoggedIn);
  110. }
  111. $this->mockRSSToken($token, '123456789012345678901234567890', ['user1']);
  112. $this->assertEquals($expected, $this->activityManager->getCurrentUserId());
  113. }
  114. protected function mockRSSToken($requestToken, $userToken, $users) {
  115. if ($requestToken !== null) {
  116. $this->request->expects($this->any())
  117. ->method('getParam')
  118. ->with('token', '')
  119. ->willReturn($requestToken);
  120. }
  121. $this->config->expects($this->any())
  122. ->method('getUsersForUserValue')
  123. ->with('activity', 'rsstoken', $userToken)
  124. ->willReturn($users);
  125. }
  126. protected function mockUserSession($user) {
  127. $mockUser = $this->getMockBuilder(IUser::class)
  128. ->disableOriginalConstructor()
  129. ->getMock();
  130. $mockUser->expects($this->any())
  131. ->method('getUID')
  132. ->willReturn($user);
  133. $this->session->expects($this->any())
  134. ->method('isLoggedIn')
  135. ->willReturn(true);
  136. $this->session->expects($this->any())
  137. ->method('getUser')
  138. ->willReturn($mockUser);
  139. }
  140. public function testPublishExceptionNoApp() {
  141. $this->expectException(\BadMethodCallException::class);
  142. $event = $this->activityManager->generateEvent();
  143. $this->activityManager->publish($event);
  144. }
  145. public function testPublishExceptionNoType() {
  146. $this->expectException(\BadMethodCallException::class);
  147. $event = $this->activityManager->generateEvent();
  148. $event->setApp('test');
  149. $this->activityManager->publish($event);
  150. }
  151. public function testPublishExceptionNoAffectedUser() {
  152. $this->expectException(\BadMethodCallException::class);
  153. $event = $this->activityManager->generateEvent();
  154. $event->setApp('test')
  155. ->setType('test_type');
  156. $this->activityManager->publish($event);
  157. }
  158. public function testPublishExceptionNoSubject() {
  159. $this->expectException(\BadMethodCallException::class);
  160. $event = $this->activityManager->generateEvent();
  161. $event->setApp('test')
  162. ->setType('test_type')
  163. ->setAffectedUser('test_affected');
  164. $this->activityManager->publish($event);
  165. }
  166. public function dataPublish() {
  167. return [
  168. [null, ''],
  169. ['test_author', 'test_author'],
  170. ];
  171. }
  172. /**
  173. * @dataProvider dataPublish
  174. * @param string|null $author
  175. * @param string $expected
  176. */
  177. public function testPublish($author, $expected) {
  178. if ($author !== null) {
  179. $authorObject = $this->getMockBuilder(IUser::class)
  180. ->disableOriginalConstructor()
  181. ->getMock();
  182. $authorObject->expects($this->once())
  183. ->method('getUID')
  184. ->willReturn($author);
  185. $this->session->expects($this->atLeastOnce())
  186. ->method('getUser')
  187. ->willReturn($authorObject);
  188. }
  189. $event = $this->activityManager->generateEvent();
  190. $event->setApp('test')
  191. ->setType('test_type')
  192. ->setSubject('test_subject', [])
  193. ->setAffectedUser('test_affected')
  194. ->setObject('file', 123);
  195. $consumer = $this->getMockBuilder('OCP\Activity\IConsumer')
  196. ->disableOriginalConstructor()
  197. ->getMock();
  198. $consumer->expects($this->once())
  199. ->method('receive')
  200. ->with($event)
  201. ->willReturnCallback(function (\OCP\Activity\IEvent $event) use ($expected) {
  202. $this->assertLessThanOrEqual(time() + 2, $event->getTimestamp(), 'Timestamp not set correctly');
  203. $this->assertGreaterThanOrEqual(time() - 2, $event->getTimestamp(), 'Timestamp not set correctly');
  204. $this->assertSame($expected, $event->getAuthor(), 'Author name not set correctly');
  205. });
  206. $this->activityManager->registerConsumer(function () use ($consumer) {
  207. return $consumer;
  208. });
  209. $this->activityManager->publish($event);
  210. }
  211. public function testPublishAllManually() {
  212. $event = $this->activityManager->generateEvent();
  213. $event->setApp('test_app')
  214. ->setType('test_type')
  215. ->setAffectedUser('test_affected')
  216. ->setAuthor('test_author')
  217. ->setTimestamp(1337)
  218. ->setSubject('test_subject', ['test_subject_param'])
  219. ->setMessage('test_message', ['test_message_param'])
  220. ->setObject('test_object_type', 42, 'test_object_name')
  221. ->setLink('test_link')
  222. ;
  223. $consumer = $this->getMockBuilder('OCP\Activity\IConsumer')
  224. ->disableOriginalConstructor()
  225. ->getMock();
  226. $consumer->expects($this->once())
  227. ->method('receive')
  228. ->willReturnCallback(function (\OCP\Activity\IEvent $event) {
  229. $this->assertSame('test_app', $event->getApp(), 'App not set correctly');
  230. $this->assertSame('test_type', $event->getType(), 'Type not set correctly');
  231. $this->assertSame('test_affected', $event->getAffectedUser(), 'Affected user not set correctly');
  232. $this->assertSame('test_author', $event->getAuthor(), 'Author not set correctly');
  233. $this->assertSame(1337, $event->getTimestamp(), 'Timestamp not set correctly');
  234. $this->assertSame('test_subject', $event->getSubject(), 'Subject not set correctly');
  235. $this->assertSame(['test_subject_param'], $event->getSubjectParameters(), 'Subject parameter not set correctly');
  236. $this->assertSame('test_message', $event->getMessage(), 'Message not set correctly');
  237. $this->assertSame(['test_message_param'], $event->getMessageParameters(), 'Message parameter not set correctly');
  238. $this->assertSame('test_object_type', $event->getObjectType(), 'Object type not set correctly');
  239. $this->assertSame(42, $event->getObjectId(), 'Object ID not set correctly');
  240. $this->assertSame('test_object_name', $event->getObjectName(), 'Object name not set correctly');
  241. $this->assertSame('test_link', $event->getLink(), 'Link not set correctly');
  242. });
  243. $this->activityManager->registerConsumer(function () use ($consumer) {
  244. return $consumer;
  245. });
  246. $this->activityManager->publish($event);
  247. }
  248. }
  249. class NoOpConsumer implements \OCP\Activity\IConsumer {
  250. public function receive(\OCP\Activity\IEvent $event) {
  251. }
  252. }