ManagerTest.php 17 KB

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