ManagerTest.php 17 KB

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