ManagerTest.php 9.2 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\IRequest;
  25. use OCP\IUser;
  26. use OCP\IUserSession;
  27. use OCP\RichObjectStrings\IValidator;
  28. use Test\TestCase;
  29. class ManagerTest extends TestCase {
  30. /** @var \OC\Activity\Manager */
  31. private $activityManager;
  32. /** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */
  33. protected $request;
  34. /** @var IUserSession|\PHPUnit_Framework_MockObject_MockObject */
  35. protected $session;
  36. /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
  37. protected $config;
  38. /** @var IValidator|\PHPUnit_Framework_MockObject_MockObject */
  39. protected $validator;
  40. protected function setUp() {
  41. parent::setUp();
  42. $this->request = $this->createMock(IRequest::class);
  43. $this->session = $this->createMock(IUserSession::class);
  44. $this->config = $this->createMock(IConfig::class);
  45. $this->validator = $this->createMock(IValidator::class);
  46. $this->activityManager = new \OC\Activity\Manager(
  47. $this->request,
  48. $this->session,
  49. $this->config,
  50. $this->validator
  51. );
  52. $this->assertSame([], self::invokePrivate($this->activityManager, 'getConsumers'));
  53. $this->activityManager->registerConsumer(function() {
  54. return new NoOpConsumer();
  55. });
  56. $this->assertNotEmpty(self::invokePrivate($this->activityManager, 'getConsumers'));
  57. $this->assertNotEmpty(self::invokePrivate($this->activityManager, 'getConsumers'));
  58. }
  59. public function testGetConsumers() {
  60. $consumers = self::invokePrivate($this->activityManager, 'getConsumers');
  61. $this->assertNotEmpty($consumers);
  62. }
  63. /**
  64. * @expectedException \InvalidArgumentException
  65. */
  66. public function testGetConsumersInvalidConsumer() {
  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. * @expectedException \UnexpectedValueException
  84. * @dataProvider getUserFromTokenThrowInvalidTokenData
  85. *
  86. * @param string $token
  87. * @param array $users
  88. */
  89. public function testGetUserFromTokenThrowInvalidToken($token, $users) {
  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. /**
  141. * @expectedException \BadMethodCallException
  142. */
  143. public function testPublishExceptionNoApp() {
  144. $event = $this->activityManager->generateEvent();
  145. $this->activityManager->publish($event);
  146. }
  147. /**
  148. * @expectedException \BadMethodCallException
  149. */
  150. public function testPublishExceptionNoType() {
  151. $event = $this->activityManager->generateEvent();
  152. $event->setApp('test');
  153. $this->activityManager->publish($event);
  154. }
  155. /**
  156. * @expectedException \BadMethodCallException
  157. */
  158. public function testPublishExceptionNoAffectedUser() {
  159. $event = $this->activityManager->generateEvent();
  160. $event->setApp('test')
  161. ->setType('test_type');
  162. $this->activityManager->publish($event);
  163. }
  164. /**
  165. * @expectedException \BadMethodCallException
  166. */
  167. public function testPublishExceptionNoSubject() {
  168. $event = $this->activityManager->generateEvent();
  169. $event->setApp('test')
  170. ->setType('test_type')
  171. ->setAffectedUser('test_affected');
  172. $this->activityManager->publish($event);
  173. }
  174. public function dataPublish() {
  175. return [
  176. [null, ''],
  177. ['test_author', 'test_author'],
  178. ];
  179. }
  180. /**
  181. * @dataProvider dataPublish
  182. * @param string|null $author
  183. * @param string $expected
  184. */
  185. public function testPublish($author, $expected) {
  186. if ($author !== null) {
  187. $authorObject = $this->getMockBuilder(IUser::class)
  188. ->disableOriginalConstructor()
  189. ->getMock();
  190. $authorObject->expects($this->once())
  191. ->method('getUID')
  192. ->willReturn($author);
  193. $this->session->expects($this->atLeastOnce())
  194. ->method('getUser')
  195. ->willReturn($authorObject);
  196. }
  197. $event = $this->activityManager->generateEvent();
  198. $event->setApp('test')
  199. ->setType('test_type')
  200. ->setSubject('test_subject', [])
  201. ->setAffectedUser('test_affected')
  202. ->setObject('file', 123);
  203. $consumer = $this->getMockBuilder('OCP\Activity\IConsumer')
  204. ->disableOriginalConstructor()
  205. ->getMock();
  206. $consumer->expects($this->once())
  207. ->method('receive')
  208. ->with($event)
  209. ->willReturnCallback(function(\OCP\Activity\IEvent $event) use ($expected) {
  210. $this->assertLessThanOrEqual(time() + 2, $event->getTimestamp(), 'Timestamp not set correctly');
  211. $this->assertGreaterThanOrEqual(time() - 2, $event->getTimestamp(), 'Timestamp not set correctly');
  212. $this->assertSame($expected, $event->getAuthor(), 'Author name not set correctly');
  213. });
  214. $this->activityManager->registerConsumer(function () use ($consumer) {
  215. return $consumer;
  216. });
  217. $this->activityManager->publish($event);
  218. }
  219. public function testPublishAllManually() {
  220. $event = $this->activityManager->generateEvent();
  221. $event->setApp('test_app')
  222. ->setType('test_type')
  223. ->setAffectedUser('test_affected')
  224. ->setAuthor('test_author')
  225. ->setTimestamp(1337)
  226. ->setSubject('test_subject', ['test_subject_param'])
  227. ->setMessage('test_message', ['test_message_param'])
  228. ->setObject('test_object_type', 42, 'test_object_name')
  229. ->setLink('test_link')
  230. ;
  231. $consumer = $this->getMockBuilder('OCP\Activity\IConsumer')
  232. ->disableOriginalConstructor()
  233. ->getMock();
  234. $consumer->expects($this->once())
  235. ->method('receive')
  236. ->willReturnCallback(function(\OCP\Activity\IEvent $event) {
  237. $this->assertSame('test_app', $event->getApp(), 'App not set correctly');
  238. $this->assertSame('test_type', $event->getType(), 'Type not set correctly');
  239. $this->assertSame('test_affected', $event->getAffectedUser(), 'Affected user not set correctly');
  240. $this->assertSame('test_author', $event->getAuthor(), 'Author not set correctly');
  241. $this->assertSame(1337, $event->getTimestamp(), 'Timestamp not set correctly');
  242. $this->assertSame('test_subject', $event->getSubject(), 'Subject not set correctly');
  243. $this->assertSame(['test_subject_param'], $event->getSubjectParameters(), 'Subject parameter not set correctly');
  244. $this->assertSame('test_message', $event->getMessage(), 'Message not set correctly');
  245. $this->assertSame(['test_message_param'], $event->getMessageParameters(), 'Message parameter not set correctly');
  246. $this->assertSame('test_object_type', $event->getObjectType(), 'Object type not set correctly');
  247. $this->assertSame(42, $event->getObjectId(), 'Object ID not set correctly');
  248. $this->assertSame('test_object_name', $event->getObjectName(), 'Object name not set correctly');
  249. $this->assertSame('test_link', $event->getLink(), 'Link not set correctly');
  250. });
  251. $this->activityManager->registerConsumer(function () use ($consumer) {
  252. return $consumer;
  253. });
  254. $this->activityManager->publish($event);
  255. }
  256. }
  257. class NoOpConsumer implements \OCP\Activity\IConsumer {
  258. public function receive(\OCP\Activity\IEvent $event) {
  259. }
  260. }