ManagerTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. <?php
  2. /**
  3. * Copyright (c) 2014 Thomas Müller <deepdiver@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. *
  8. */
  9. namespace Test\Activity;
  10. use OCP\IConfig;
  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() {
  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. );
  39. $this->assertSame([], $this->invokePrivate($this->activityManager, 'getConsumers'));
  40. $this->assertSame([], $this->invokePrivate($this->activityManager, 'getExtensions'));
  41. $this->activityManager->registerConsumer(function() {
  42. return new NoOpConsumer();
  43. });
  44. $this->activityManager->registerExtension(function() {
  45. return new NoOpExtension();
  46. });
  47. $this->activityManager->registerExtension(function() {
  48. return new SimpleExtension();
  49. });
  50. $this->assertNotEmpty($this->invokePrivate($this->activityManager, 'getConsumers'));
  51. $this->assertNotEmpty($this->invokePrivate($this->activityManager, 'getConsumers'));
  52. $this->assertNotEmpty($this->invokePrivate($this->activityManager, 'getExtensions'));
  53. $this->assertNotEmpty($this->invokePrivate($this->activityManager, 'getExtensions'));
  54. }
  55. public function testGetConsumers() {
  56. $consumers = $this->invokePrivate($this->activityManager, 'getConsumers');
  57. $this->assertNotEmpty($consumers);
  58. }
  59. /**
  60. * @expectedException \InvalidArgumentException
  61. */
  62. public function testGetConsumersInvalidConsumer() {
  63. $this->activityManager->registerConsumer(function() {
  64. return new \stdClass();
  65. });
  66. $this->invokePrivate($this->activityManager, 'getConsumers');
  67. }
  68. public function testGetExtensions() {
  69. $extensions = $this->invokePrivate($this->activityManager, 'getExtensions');
  70. $this->assertNotEmpty($extensions);
  71. }
  72. /**
  73. * @expectedException \InvalidArgumentException
  74. */
  75. public function testGetExtensionsInvalidExtension() {
  76. $this->activityManager->registerExtension(function() {
  77. return new \stdClass();
  78. });
  79. $this->invokePrivate($this->activityManager, 'getExtensions');
  80. }
  81. public function testNotificationTypes() {
  82. $result = $this->activityManager->getNotificationTypes('en');
  83. $this->assertTrue(is_array($result));
  84. $this->assertEquals(2, sizeof($result));
  85. }
  86. public function testDefaultTypes() {
  87. $result = $this->activityManager->getDefaultTypes('stream');
  88. $this->assertTrue(is_array($result));
  89. $this->assertEquals(1, sizeof($result));
  90. $result = $this->activityManager->getDefaultTypes('email');
  91. $this->assertTrue(is_array($result));
  92. $this->assertEquals(0, sizeof($result));
  93. }
  94. public function testTypeIcon() {
  95. $result = $this->activityManager->getTypeIcon('NT1');
  96. $this->assertEquals('icon-nt-one', $result);
  97. $result = $this->activityManager->getTypeIcon('NT2');
  98. $this->assertEquals('', $result);
  99. }
  100. public function testTranslate() {
  101. $result = $this->activityManager->translate('APP0', '', array(), false, false, 'en');
  102. $this->assertEquals('Stupid translation', $result);
  103. $result = $this->activityManager->translate('APP1', '', array(), false, false, 'en');
  104. $this->assertFalse($result);
  105. }
  106. public function testGetSpecialParameterList() {
  107. $result = $this->activityManager->getSpecialParameterList('APP0', '');
  108. $this->assertEquals(array(0 => 'file', 1 => 'username'), $result);
  109. $result = $this->activityManager->getSpecialParameterList('APP1', '');
  110. $this->assertFalse($result);
  111. }
  112. public function testGroupParameter() {
  113. $result = $this->activityManager->getGroupParameter(array());
  114. $this->assertEquals(5, $result);
  115. }
  116. public function testNavigation() {
  117. $result = $this->activityManager->getNavigation();
  118. $this->assertEquals(4, sizeof($result['apps']));
  119. $this->assertEquals(2, sizeof($result['top']));
  120. }
  121. public function testIsFilterValid() {
  122. $result = $this->activityManager->isFilterValid('fv01');
  123. $this->assertTrue($result);
  124. $result = $this->activityManager->isFilterValid('InvalidFilter');
  125. $this->assertFalse($result);
  126. }
  127. public function testFilterNotificationTypes() {
  128. $result = $this->activityManager->filterNotificationTypes(array('NT0', 'NT1', 'NT2', 'NT3'), 'fv01');
  129. $this->assertTrue(is_array($result));
  130. $this->assertEquals(3, sizeof($result));
  131. $result = $this->activityManager->filterNotificationTypes(array('NT0', 'NT1', 'NT2', 'NT3'), 'InvalidFilter');
  132. $this->assertTrue(is_array($result));
  133. $this->assertEquals(4, sizeof($result));
  134. }
  135. public function testQueryForFilter() {
  136. // Register twice, to test the created sql part
  137. $this->activityManager->registerExtension(function() {
  138. return new SimpleExtension();
  139. });
  140. $result = $this->activityManager->getQueryForFilter('fv01');
  141. $this->assertEquals(
  142. array(
  143. ' and ((`app` = ? and `message` like ?) or (`app` = ? and `message` like ?))',
  144. array('mail', 'ownCloud%', 'mail', 'ownCloud%')
  145. ), $result
  146. );
  147. $result = $this->activityManager->getQueryForFilter('InvalidFilter');
  148. $this->assertEquals(array(null, null), $result);
  149. }
  150. public function getUserFromTokenThrowInvalidTokenData() {
  151. return [
  152. [null, []],
  153. ['', []],
  154. ['12345678901234567890123456789', []],
  155. ['1234567890123456789012345678901', []],
  156. ['123456789012345678901234567890', []],
  157. ['123456789012345678901234567890', ['user1', 'user2']],
  158. ];
  159. }
  160. /**
  161. * @expectedException \UnexpectedValueException
  162. * @dataProvider getUserFromTokenThrowInvalidTokenData
  163. *
  164. * @param string $token
  165. * @param array $users
  166. */
  167. public function testGetUserFromTokenThrowInvalidToken($token, $users) {
  168. $this->mockRSSToken($token, $token, $users);
  169. self::invokePrivate($this->activityManager, 'getUserFromToken');
  170. }
  171. public function getUserFromTokenData() {
  172. return [
  173. [null, '123456789012345678901234567890', 'user1'],
  174. ['user2', null, 'user2'],
  175. ['user2', '123456789012345678901234567890', 'user2'],
  176. ];
  177. }
  178. /**
  179. * @dataProvider getUserFromTokenData
  180. *
  181. * @param string $userLoggedIn
  182. * @param string $token
  183. * @param string $expected
  184. */
  185. public function testGetUserFromToken($userLoggedIn, $token, $expected) {
  186. if ($userLoggedIn !== null) {
  187. $this->mockUserSession($userLoggedIn);
  188. }
  189. $this->mockRSSToken($token, '123456789012345678901234567890', ['user1']);
  190. $this->assertEquals($expected, $this->activityManager->getCurrentUserId());
  191. }
  192. protected function mockRSSToken($requestToken, $userToken, $users) {
  193. if ($requestToken !== null) {
  194. $this->request->expects($this->any())
  195. ->method('getParam')
  196. ->with('token', '')
  197. ->willReturn($requestToken);
  198. }
  199. $this->config->expects($this->any())
  200. ->method('getUsersForUserValue')
  201. ->with('activity', 'rsstoken', $userToken)
  202. ->willReturn($users);
  203. }
  204. protected function mockUserSession($user) {
  205. $mockUser = $this->getMockBuilder(IUser::class)
  206. ->disableOriginalConstructor()
  207. ->getMock();
  208. $mockUser->expects($this->any())
  209. ->method('getUID')
  210. ->willReturn($user);
  211. $this->session->expects($this->any())
  212. ->method('isLoggedIn')
  213. ->willReturn(true);
  214. $this->session->expects($this->any())
  215. ->method('getUser')
  216. ->willReturn($mockUser);
  217. }
  218. /**
  219. * @expectedException \BadMethodCallException
  220. */
  221. public function testPublishExceptionNoApp() {
  222. $event = $this->activityManager->generateEvent();
  223. $this->activityManager->publish($event);
  224. }
  225. /**
  226. * @expectedException \BadMethodCallException
  227. */
  228. public function testPublishExceptionNoType() {
  229. $event = $this->activityManager->generateEvent();
  230. $event->setApp('test');
  231. $this->activityManager->publish($event);
  232. }
  233. /**
  234. * @expectedException \BadMethodCallException
  235. */
  236. public function testPublishExceptionNoAffectedUser() {
  237. $event = $this->activityManager->generateEvent();
  238. $event->setApp('test')
  239. ->setType('test_type');
  240. $this->activityManager->publish($event);
  241. }
  242. /**
  243. * @expectedException \BadMethodCallException
  244. */
  245. public function testPublishExceptionNoSubject() {
  246. $event = $this->activityManager->generateEvent();
  247. $event->setApp('test')
  248. ->setType('test_type')
  249. ->setAffectedUser('test_affected');
  250. $this->activityManager->publish($event);
  251. }
  252. public function dataPublish() {
  253. return [
  254. [null, ''],
  255. ['test_author', 'test_author'],
  256. ];
  257. }
  258. /**
  259. * @dataProvider dataPublish
  260. * @param string|null $author
  261. * @param string $expected
  262. */
  263. public function testPublish($author, $expected) {
  264. if ($author !== null) {
  265. $authorObject = $this->getMockBuilder(IUser::class)
  266. ->disableOriginalConstructor()
  267. ->getMock();
  268. $authorObject->expects($this->once())
  269. ->method('getUID')
  270. ->willReturn($author);
  271. $this->session->expects($this->atLeastOnce())
  272. ->method('getUser')
  273. ->willReturn($authorObject);
  274. }
  275. $event = $this->activityManager->generateEvent();
  276. $event->setApp('test')
  277. ->setType('test_type')
  278. ->setSubject('test_subject', [])
  279. ->setAffectedUser('test_affected')
  280. ->setObject('file', 123);
  281. $consumer = $this->getMockBuilder('OCP\Activity\IConsumer')
  282. ->disableOriginalConstructor()
  283. ->getMock();
  284. $consumer->expects($this->once())
  285. ->method('receive')
  286. ->with($event)
  287. ->willReturnCallback(function(\OCP\Activity\IEvent $event) use ($expected) {
  288. $this->assertLessThanOrEqual(time() + 2, $event->getTimestamp(), 'Timestamp not set correctly');
  289. $this->assertGreaterThanOrEqual(time() - 2, $event->getTimestamp(), 'Timestamp not set correctly');
  290. $this->assertSame($expected, $event->getAuthor(), 'Author name not set correctly');
  291. });
  292. $this->activityManager->registerConsumer(function () use ($consumer) {
  293. return $consumer;
  294. });
  295. $this->activityManager->publish($event);
  296. }
  297. public function testPublishAllManually() {
  298. $event = $this->activityManager->generateEvent();
  299. $event->setApp('test_app')
  300. ->setType('test_type')
  301. ->setAffectedUser('test_affected')
  302. ->setAuthor('test_author')
  303. ->setTimestamp(1337)
  304. ->setSubject('test_subject', ['test_subject_param'])
  305. ->setMessage('test_message', ['test_message_param'])
  306. ->setObject('test_object_type', 42, 'test_object_name')
  307. ->setLink('test_link')
  308. ;
  309. $consumer = $this->getMockBuilder('OCP\Activity\IConsumer')
  310. ->disableOriginalConstructor()
  311. ->getMock();
  312. $consumer->expects($this->once())
  313. ->method('receive')
  314. ->willReturnCallback(function(\OCP\Activity\IEvent $event) {
  315. $this->assertSame('test_app', $event->getApp(), 'App not set correctly');
  316. $this->assertSame('test_type', $event->getType(), 'Type not set correctly');
  317. $this->assertSame('test_affected', $event->getAffectedUser(), 'Affected user not set correctly');
  318. $this->assertSame('test_author', $event->getAuthor(), 'Author not set correctly');
  319. $this->assertSame(1337, $event->getTimestamp(), 'Timestamp not set correctly');
  320. $this->assertSame('test_subject', $event->getSubject(), 'Subject not set correctly');
  321. $this->assertSame(['test_subject_param'], $event->getSubjectParameters(), 'Subject parameter not set correctly');
  322. $this->assertSame('test_message', $event->getMessage(), 'Message not set correctly');
  323. $this->assertSame(['test_message_param'], $event->getMessageParameters(), 'Message parameter not set correctly');
  324. $this->assertSame('test_object_type', $event->getObjectType(), 'Object type not set correctly');
  325. $this->assertSame(42, $event->getObjectId(), 'Object ID not set correctly');
  326. $this->assertSame('test_object_name', $event->getObjectName(), 'Object name not set correctly');
  327. $this->assertSame('test_link', $event->getLink(), 'Link not set correctly');
  328. });
  329. $this->activityManager->registerConsumer(function () use ($consumer) {
  330. return $consumer;
  331. });
  332. $this->activityManager->publish($event);
  333. }
  334. }
  335. class SimpleExtension implements \OCP\Activity\IExtension {
  336. public function getNotificationTypes($languageCode) {
  337. return array('NT1', 'NT2');
  338. }
  339. public function getDefaultTypes($method) {
  340. if ($method === 'stream') {
  341. return array('DT0');
  342. }
  343. return array();
  344. }
  345. public function getTypeIcon($type) {
  346. if ($type === 'NT1') {
  347. return 'icon-nt-one';
  348. }
  349. return '';
  350. }
  351. public function translate($app, $text, $params, $stripPath, $highlightParams, $languageCode) {
  352. if ($app === 'APP0') {
  353. return "Stupid translation";
  354. }
  355. return false;
  356. }
  357. public function getSpecialParameterList($app, $text) {
  358. if ($app === 'APP0') {
  359. return array(0 => 'file', 1 => 'username');
  360. }
  361. return false;
  362. }
  363. public function getGroupParameter($activity) {
  364. return 5;
  365. }
  366. public function getNavigation() {
  367. return array(
  368. 'apps' => array('nav1', 'nav2', 'nav3', 'nav4'),
  369. 'top' => array('top1', 'top2')
  370. );
  371. }
  372. public function isFilterValid($filterValue) {
  373. if ($filterValue === 'fv01') {
  374. return true;
  375. }
  376. return false;
  377. }
  378. public function filterNotificationTypes($types, $filter) {
  379. if ($filter === 'fv01') {
  380. unset($types[0]);
  381. }
  382. return $types;
  383. }
  384. public function getQueryForFilter($filter) {
  385. if ($filter === 'fv01') {
  386. return array('`app` = ? and `message` like ?', array('mail', 'ownCloud%'));
  387. }
  388. return false;
  389. }
  390. }
  391. class NoOpExtension implements \OCP\Activity\IExtension {
  392. public function getNotificationTypes($languageCode) {
  393. return false;
  394. }
  395. public function getDefaultTypes($method) {
  396. return false;
  397. }
  398. public function getTypeIcon($type) {
  399. return false;
  400. }
  401. public function translate($app, $text, $params, $stripPath, $highlightParams, $languageCode) {
  402. return false;
  403. }
  404. public function getSpecialParameterList($app, $text) {
  405. return false;
  406. }
  407. public function getGroupParameter($activity) {
  408. return false;
  409. }
  410. public function getNavigation() {
  411. return false;
  412. }
  413. public function isFilterValid($filterValue) {
  414. return false;
  415. }
  416. public function filterNotificationTypes($types, $filter) {
  417. return false;
  418. }
  419. public function getQueryForFilter($filter) {
  420. return false;
  421. }
  422. }
  423. class NoOpConsumer implements \OCP\Activity\IConsumer {
  424. public function receive(\OCP\Activity\IEvent $event) {
  425. }
  426. }