activitymanager.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. class Test_ActivityManager extends \Test\TestCase {
  10. /** @var \OC\ActivityManager */
  11. private $activityManager;
  12. /** @var \PHPUnit_Framework_MockObject_MockObject */
  13. protected $request;
  14. /** @var \PHPUnit_Framework_MockObject_MockObject */
  15. protected $session;
  16. /** @var \PHPUnit_Framework_MockObject_MockObject */
  17. protected $config;
  18. protected function setUp() {
  19. parent::setUp();
  20. $this->request = $this->getMockBuilder('OCP\IRequest')
  21. ->disableOriginalConstructor()
  22. ->getMock();
  23. $this->session = $this->getMockBuilder('OCP\IUserSession')
  24. ->disableOriginalConstructor()
  25. ->getMock();
  26. $this->config = $this->getMockBuilder('OCP\IConfig')
  27. ->disableOriginalConstructor()
  28. ->getMock();
  29. $this->activityManager = new \OC\ActivityManager(
  30. $this->request,
  31. $this->session,
  32. $this->config
  33. );
  34. $this->activityManager->registerExtension(function() {
  35. return new NoOpExtension();
  36. });
  37. $this->activityManager->registerExtension(function() {
  38. return new SimpleExtension();
  39. });
  40. }
  41. public function testNotificationTypes() {
  42. $result = $this->activityManager->getNotificationTypes('en');
  43. $this->assertTrue(is_array($result));
  44. $this->assertEquals(2, sizeof($result));
  45. }
  46. public function testDefaultTypes() {
  47. $result = $this->activityManager->getDefaultTypes('stream');
  48. $this->assertTrue(is_array($result));
  49. $this->assertEquals(1, sizeof($result));
  50. $result = $this->activityManager->getDefaultTypes('email');
  51. $this->assertTrue(is_array($result));
  52. $this->assertEquals(0, sizeof($result));
  53. }
  54. public function testTypeIcon() {
  55. $result = $this->activityManager->getTypeIcon('NT1');
  56. $this->assertEquals('icon-nt-one', $result);
  57. $result = $this->activityManager->getTypeIcon('NT2');
  58. $this->assertEquals('', $result);
  59. }
  60. public function testTranslate() {
  61. $result = $this->activityManager->translate('APP0', '', '', array(), false, false, 'en');
  62. $this->assertEquals('Stupid translation', $result);
  63. $result = $this->activityManager->translate('APP1', '', '', array(), false, false, 'en');
  64. $this->assertFalse($result);
  65. }
  66. public function testGetSpecialParameterList() {
  67. $result = $this->activityManager->getSpecialParameterList('APP0', '');
  68. $this->assertEquals(array(0 => 'file', 1 => 'username'), $result);
  69. $result = $this->activityManager->getSpecialParameterList('APP1', '');
  70. $this->assertFalse($result);
  71. }
  72. public function testGroupParameter() {
  73. $result = $this->activityManager->getGroupParameter(array());
  74. $this->assertEquals(5, $result);
  75. }
  76. public function testNavigation() {
  77. $result = $this->activityManager->getNavigation();
  78. $this->assertEquals(4, sizeof($result['apps']));
  79. $this->assertEquals(2, sizeof($result['top']));
  80. }
  81. public function testIsFilterValid() {
  82. $result = $this->activityManager->isFilterValid('fv01');
  83. $this->assertTrue($result);
  84. $result = $this->activityManager->isFilterValid('InvalidFilter');
  85. $this->assertFalse($result);
  86. }
  87. public function testFilterNotificationTypes() {
  88. $result = $this->activityManager->filterNotificationTypes(array('NT0', 'NT1', 'NT2', 'NT3'), 'fv01');
  89. $this->assertTrue(is_array($result));
  90. $this->assertEquals(3, sizeof($result));
  91. $result = $this->activityManager->filterNotificationTypes(array('NT0', 'NT1', 'NT2', 'NT3'), 'InvalidFilter');
  92. $this->assertTrue(is_array($result));
  93. $this->assertEquals(4, sizeof($result));
  94. }
  95. public function testQueryForFilter() {
  96. // Register twice, to test the created sql part
  97. $this->activityManager->registerExtension(function() {
  98. return new SimpleExtension();
  99. });
  100. $result = $this->activityManager->getQueryForFilter('fv01');
  101. $this->assertEquals(
  102. array(
  103. ' and ((`app` = ? and `message` like ?) or (`app` = ? and `message` like ?))',
  104. array('mail', 'ownCloud%', 'mail', 'ownCloud%')
  105. ), $result
  106. );
  107. $result = $this->activityManager->getQueryForFilter('InvalidFilter');
  108. $this->assertEquals(array(null, null), $result);
  109. }
  110. public function getUserFromTokenThrowInvalidTokenData() {
  111. return [
  112. [null, []],
  113. ['', []],
  114. ['12345678901234567890123456789', []],
  115. ['1234567890123456789012345678901', []],
  116. ['123456789012345678901234567890', []],
  117. ['123456789012345678901234567890', ['user1', 'user2']],
  118. ];
  119. }
  120. /**
  121. * @expectedException \UnexpectedValueException
  122. * @dataProvider getUserFromTokenThrowInvalidTokenData
  123. *
  124. * @param string $token
  125. * @param array $users
  126. */
  127. public function testGetUserFromTokenThrowInvalidToken($token, $users) {
  128. $this->mockRSSToken($token, $token, $users);
  129. \Test_Helper::invokePrivate($this->activityManager, 'getUserFromToken');
  130. }
  131. public function getUserFromTokenData() {
  132. return [
  133. [null, '123456789012345678901234567890', 'user1'],
  134. ['user2', null, 'user2'],
  135. ['user2', '123456789012345678901234567890', 'user2'],
  136. ];
  137. }
  138. /**
  139. * @dataProvider getUserFromTokenData
  140. *
  141. * @param string $userLoggedIn
  142. * @param string $token
  143. * @param string $expected
  144. */
  145. public function testGetUserFromToken($userLoggedIn, $token, $expected) {
  146. if ($userLoggedIn !== null) {
  147. $this->mockUserSession($userLoggedIn);
  148. }
  149. $this->mockRSSToken($token, '123456789012345678901234567890', ['user1']);
  150. $this->assertEquals($expected, $this->activityManager->getCurrentUserId());
  151. }
  152. protected function mockRSSToken($requestToken, $userToken, $users) {
  153. if ($requestToken !== null) {
  154. $this->request->expects($this->any())
  155. ->method('getParam')
  156. ->with('token', '')
  157. ->willReturn($requestToken);
  158. }
  159. $this->config->expects($this->any())
  160. ->method('getUsersForUserValue')
  161. ->with('activity', 'rsstoken', $userToken)
  162. ->willReturn($users);
  163. }
  164. protected function mockUserSession($user) {
  165. $mockUser = $this->getMockBuilder('\OCP\IUser')
  166. ->disableOriginalConstructor()
  167. ->getMock();
  168. $mockUser->expects($this->any())
  169. ->method('getUID')
  170. ->willReturn($user);
  171. $this->session->expects($this->any())
  172. ->method('isLoggedIn')
  173. ->willReturn(true);
  174. $this->session->expects($this->any())
  175. ->method('getUser')
  176. ->willReturn($mockUser);
  177. }
  178. }
  179. class SimpleExtension implements \OCP\Activity\IExtension {
  180. public function getNotificationTypes($languageCode) {
  181. return array('NT1', 'NT2');
  182. }
  183. public function getDefaultTypes($method) {
  184. if ($method === 'stream') {
  185. return array('DT0');
  186. }
  187. return array();
  188. }
  189. public function getTypeIcon($type) {
  190. if ($type === 'NT1') {
  191. return 'icon-nt-one';
  192. }
  193. return '';
  194. }
  195. public function translate($app, $text, $params, $stripPath, $highlightParams, $languageCode) {
  196. if ($app === 'APP0') {
  197. return "Stupid translation";
  198. }
  199. return false;
  200. }
  201. public function getSpecialParameterList($app, $text) {
  202. if ($app === 'APP0') {
  203. return array(0 => 'file', 1 => 'username');
  204. }
  205. return false;
  206. }
  207. public function getGroupParameter($activity) {
  208. return 5;
  209. }
  210. public function getNavigation() {
  211. return array(
  212. 'apps' => array('nav1', 'nav2', 'nav3', 'nav4'),
  213. 'top' => array('top1', 'top2')
  214. );
  215. }
  216. public function isFilterValid($filterValue) {
  217. if ($filterValue === 'fv01') {
  218. return true;
  219. }
  220. return false;
  221. }
  222. public function filterNotificationTypes($types, $filter) {
  223. if ($filter === 'fv01') {
  224. unset($types[0]);
  225. }
  226. return $types;
  227. }
  228. public function getQueryForFilter($filter) {
  229. if ($filter === 'fv01') {
  230. return array('`app` = ? and `message` like ?', array('mail', 'ownCloud%'));
  231. }
  232. return false;
  233. }
  234. }
  235. class NoOpExtension implements \OCP\Activity\IExtension {
  236. public function getNotificationTypes($languageCode) {
  237. return false;
  238. }
  239. public function getDefaultTypes($method) {
  240. return false;
  241. }
  242. public function getTypeIcon($type) {
  243. return false;
  244. }
  245. public function translate($app, $text, $params, $stripPath, $highlightParams, $languageCode) {
  246. return false;
  247. }
  248. public function getSpecialParameterList($app, $text) {
  249. return false;
  250. }
  251. public function getGroupParameter($activity) {
  252. return false;
  253. }
  254. public function getNavigation() {
  255. return false;
  256. }
  257. public function isFilterValid($filterValue) {
  258. return false;
  259. }
  260. public function filterNotificationTypes($types, $filter) {
  261. return false;
  262. }
  263. public function getQueryForFilter($filter) {
  264. return false;
  265. }
  266. }