activitytest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <?php
  2. /**
  3. * @author Joas Schilling <nickvergessen@owncloud.com>
  4. * @author Morris Jobke <hey@morrisjobke.de>
  5. *
  6. * @copyright Copyright (c) 2016, ownCloud, Inc.
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  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, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OCA\Files\Tests;
  23. use OCA\Files\Activity;
  24. use Test\TestCase;
  25. /**
  26. * Class ActivityTest
  27. *
  28. * @group DB
  29. * @package OCA\Files\Tests
  30. */
  31. class ActivityTest extends TestCase {
  32. /** @var \OC\ActivityManager */
  33. private $activityManager;
  34. /** @var \OCP\IRequest|\PHPUnit_Framework_MockObject_MockObject */
  35. protected $request;
  36. /** @var \OCP\IUserSession|\PHPUnit_Framework_MockObject_MockObject */
  37. protected $session;
  38. /** @var \OCP\IConfig|\PHPUnit_Framework_MockObject_MockObject */
  39. protected $config;
  40. /** @var \OCA\Files\ActivityHelper|\PHPUnit_Framework_MockObject_MockObject */
  41. protected $activityHelper;
  42. /** @var \OCP\L10N\IFactory|\PHPUnit_Framework_MockObject_MockObject */
  43. protected $l10nFactory;
  44. /** @var \OCA\Files\Activity */
  45. protected $activityExtension;
  46. protected function setUp() {
  47. parent::setUp();
  48. $this->request = $this->getMockBuilder('OCP\IRequest')
  49. ->disableOriginalConstructor()
  50. ->getMock();
  51. $this->session = $this->getMockBuilder('OCP\IUserSession')
  52. ->disableOriginalConstructor()
  53. ->getMock();
  54. $this->config = $this->getMockBuilder('OCP\IConfig')
  55. ->disableOriginalConstructor()
  56. ->getMock();
  57. $this->activityHelper = $this->getMockBuilder('OCA\Files\ActivityHelper')
  58. ->disableOriginalConstructor()
  59. ->getMock();
  60. $this->activityManager = new \OC\ActivityManager(
  61. $this->request,
  62. $this->session,
  63. $this->config
  64. );
  65. $this->l10nFactory = $this->getMockBuilder('OCP\L10N\IFactory')
  66. ->disableOriginalConstructor()
  67. ->getMock();
  68. $deL10n = $this->getMockBuilder('OC_L10N')
  69. ->disableOriginalConstructor()
  70. ->getMock();
  71. $deL10n->expects($this->any())
  72. ->method('t')
  73. ->willReturnCallback(function ($argument) {
  74. return 'translate(' . $argument . ')';
  75. });
  76. $this->l10nFactory->expects($this->any())
  77. ->method('get')
  78. ->willReturnMap([
  79. ['files', null, new \OC_L10N('files', 'en')],
  80. ['files', 'en', new \OC_L10N('files', 'en')],
  81. ['files', 'de', $deL10n],
  82. ]);
  83. $this->activityExtension = $activityExtension = new Activity(
  84. $this->l10nFactory,
  85. $this->getMockBuilder('OCP\IURLGenerator')->disableOriginalConstructor()->getMock(),
  86. $this->activityManager,
  87. $this->activityHelper,
  88. \OC::$server->getDatabaseConnection(),
  89. $this->config
  90. );
  91. $this->activityManager->registerExtension(function() use ($activityExtension) {
  92. return $activityExtension;
  93. });
  94. }
  95. public function testNotificationTypes() {
  96. $result = $this->activityExtension->getNotificationTypes('en');
  97. $this->assertTrue(is_array($result), 'Asserting getNotificationTypes() returns an array');
  98. $this->assertCount(5, $result);
  99. $this->assertArrayHasKey(Activity::TYPE_SHARE_CREATED, $result);
  100. $this->assertArrayHasKey(Activity::TYPE_SHARE_CHANGED, $result);
  101. $this->assertArrayHasKey(Activity::TYPE_FAVORITES, $result);
  102. $this->assertArrayHasKey(Activity::TYPE_SHARE_DELETED, $result);
  103. $this->assertArrayHasKey(Activity::TYPE_SHARE_RESTORED, $result);
  104. }
  105. public function testDefaultTypes() {
  106. $result = $this->activityExtension->getDefaultTypes('stream');
  107. $this->assertTrue(is_array($result), 'Asserting getDefaultTypes(stream) returns an array');
  108. $this->assertCount(4, $result);
  109. $result = array_flip($result);
  110. $this->assertArrayHasKey(Activity::TYPE_SHARE_CREATED, $result);
  111. $this->assertArrayHasKey(Activity::TYPE_SHARE_CHANGED, $result);
  112. $this->assertArrayNotHasKey(Activity::TYPE_FAVORITES, $result);
  113. $this->assertArrayHasKey(Activity::TYPE_SHARE_DELETED, $result);
  114. $this->assertArrayHasKey(Activity::TYPE_SHARE_RESTORED, $result);
  115. $result = $this->activityExtension->getDefaultTypes('email');
  116. $this->assertFalse($result, 'Asserting getDefaultTypes(email) returns false');
  117. }
  118. public function testTranslate() {
  119. $this->assertFalse(
  120. $this->activityExtension->translate('files_sharing', '', [], false, false, 'en'),
  121. 'Asserting that no translations are set for files_sharing'
  122. );
  123. // Test english
  124. $this->assertNotFalse(
  125. $this->activityExtension->translate('files', 'deleted_self', ['file'], false, false, 'en'),
  126. 'Asserting that translations are set for files.deleted_self'
  127. );
  128. $this->assertStringStartsWith(
  129. 'You deleted ',
  130. $this->activityExtension->translate('files', 'deleted_self', ['file'], false, false, 'en')
  131. );
  132. // Test translation
  133. $this->assertNotFalse(
  134. $this->activityExtension->translate('files', 'deleted_self', ['file'], false, false, 'de'),
  135. 'Asserting that translations are set for files.deleted_self'
  136. );
  137. $this->assertStringStartsWith(
  138. 'translate(You deleted ',
  139. $this->activityExtension->translate('files', 'deleted_self', ['file'], false, false, 'de')
  140. );
  141. }
  142. public function testGetSpecialParameterList() {
  143. $this->assertFalse(
  144. $this->activityExtension->getSpecialParameterList('files_sharing', ''),
  145. 'Asserting that no special parameters are set for files_sharing'
  146. );
  147. }
  148. public function typeIconData() {
  149. return [
  150. [Activity::TYPE_SHARE_CHANGED, 'icon-change'],
  151. [Activity::TYPE_SHARE_CREATED, 'icon-add-color'],
  152. [Activity::TYPE_SHARE_DELETED, 'icon-delete-color'],
  153. [Activity::TYPE_SHARE_RESTORED, false],
  154. [Activity::TYPE_FAVORITES, false],
  155. ['unknown type', false],
  156. ];
  157. }
  158. /**
  159. * @dataProvider typeIconData
  160. *
  161. * @param string $type
  162. * @param mixed $expected
  163. */
  164. public function testTypeIcon($type, $expected) {
  165. $this->assertSame($expected, $this->activityExtension->getTypeIcon($type));
  166. }
  167. public function testGroupParameter() {
  168. $this->assertFalse(
  169. $this->activityExtension->getGroupParameter(['app' => 'files_sharing']),
  170. 'Asserting that no group parameters are set for files_sharing'
  171. );
  172. }
  173. public function testNavigation() {
  174. $result = $this->activityExtension->getNavigation();
  175. $this->assertCount(1, $result['top']);
  176. $this->assertArrayHasKey(Activity::FILTER_FAVORITES, $result['top']);
  177. $this->assertCount(1, $result['apps']);
  178. $this->assertArrayHasKey(Activity::FILTER_FILES, $result['apps']);
  179. }
  180. public function testIsFilterValid() {
  181. $this->assertTrue($this->activityExtension->isFilterValid(Activity::FILTER_FAVORITES));
  182. $this->assertTrue($this->activityExtension->isFilterValid(Activity::FILTER_FILES));
  183. $this->assertFalse($this->activityExtension->isFilterValid('unknown filter'));
  184. }
  185. public function filterNotificationTypesData() {
  186. return [
  187. [
  188. Activity::FILTER_FILES,
  189. [
  190. 'NT0',
  191. Activity::TYPE_SHARE_CREATED,
  192. Activity::TYPE_SHARE_CHANGED,
  193. Activity::TYPE_SHARE_DELETED,
  194. Activity::TYPE_SHARE_RESTORED,
  195. Activity::TYPE_FAVORITES,
  196. ], [
  197. Activity::TYPE_SHARE_CREATED,
  198. Activity::TYPE_SHARE_CHANGED,
  199. Activity::TYPE_SHARE_DELETED,
  200. Activity::TYPE_SHARE_RESTORED,
  201. ],
  202. ],
  203. [
  204. Activity::FILTER_FILES,
  205. [
  206. 'NT0',
  207. Activity::TYPE_SHARE_CREATED,
  208. Activity::TYPE_FAVORITES,
  209. ],
  210. [
  211. Activity::TYPE_SHARE_CREATED,
  212. ],
  213. ],
  214. [
  215. Activity::FILTER_FAVORITES,
  216. [
  217. 'NT0',
  218. Activity::TYPE_SHARE_CREATED,
  219. Activity::TYPE_SHARE_CHANGED,
  220. Activity::TYPE_SHARE_DELETED,
  221. Activity::TYPE_SHARE_RESTORED,
  222. Activity::TYPE_FAVORITES,
  223. ], [
  224. Activity::TYPE_SHARE_CREATED,
  225. Activity::TYPE_SHARE_CHANGED,
  226. Activity::TYPE_SHARE_DELETED,
  227. Activity::TYPE_SHARE_RESTORED,
  228. ],
  229. ],
  230. [
  231. 'unknown filter',
  232. [
  233. 'NT0',
  234. Activity::TYPE_SHARE_CREATED,
  235. Activity::TYPE_SHARE_CHANGED,
  236. Activity::TYPE_SHARE_DELETED,
  237. Activity::TYPE_SHARE_RESTORED,
  238. Activity::TYPE_FAVORITES,
  239. ],
  240. false,
  241. ],
  242. ];
  243. }
  244. /**
  245. * @dataProvider filterNotificationTypesData
  246. *
  247. * @param string $filter
  248. * @param array $types
  249. * @param mixed $expected
  250. */
  251. public function testFilterNotificationTypes($filter, $types, $expected) {
  252. $result = $this->activityExtension->filterNotificationTypes($types, $filter);
  253. $this->assertEquals($expected, $result);
  254. }
  255. public function queryForFilterData() {
  256. return [
  257. [
  258. new \RuntimeException(),
  259. '`app` = ?',
  260. ['files']
  261. ],
  262. [
  263. [
  264. 'items' => [],
  265. 'folders' => [],
  266. ],
  267. ' CASE WHEN `app` <> ? THEN 1 WHEN `app` = ? AND ((`type` <> ? AND `type` <> ?)) THEN 1 ELSE 0 END = 1 ',
  268. ['files', 'files', Activity::TYPE_SHARE_CREATED, Activity::TYPE_SHARE_CHANGED]
  269. ],
  270. [
  271. [
  272. 'items' => ['file.txt', 'folder'],
  273. 'folders' => ['folder'],
  274. ],
  275. ' CASE WHEN `app` <> ? THEN 1 WHEN `app` = ? AND ((`type` <> ? AND `type` <> ?) OR `file` = ? OR `file` = ? OR `file` LIKE ?) THEN 1 ELSE 0 END = 1 ',
  276. ['files', 'files', Activity::TYPE_SHARE_CREATED, Activity::TYPE_SHARE_CHANGED, 'file.txt', 'folder', 'folder/%']
  277. ],
  278. ];
  279. }
  280. /**
  281. * @dataProvider queryForFilterData
  282. *
  283. * @param mixed $will
  284. * @param string $query
  285. * @param array $parameters
  286. */
  287. public function testQueryForFilter($will, $query, $parameters) {
  288. $this->mockUserSession('test');
  289. $this->config->expects($this->any())
  290. ->method('getUserValue')
  291. ->willReturnMap([
  292. ['test', 'activity', 'notify_stream_' . Activity::TYPE_FAVORITES, false, true],
  293. ]);
  294. if (is_array($will)) {
  295. $this->activityHelper->expects($this->any())
  296. ->method('getFavoriteFilePaths')
  297. ->with('test')
  298. ->willReturn($will);
  299. } else {
  300. $this->activityHelper->expects($this->any())
  301. ->method('getFavoriteFilePaths')
  302. ->with('test')
  303. ->willThrowException($will);
  304. }
  305. $result = $this->activityExtension->getQueryForFilter('all');
  306. $this->assertEquals([$query, $parameters], $result);
  307. $this->executeQueryForFilter($result);
  308. }
  309. public function executeQueryForFilter(array $result) {
  310. list($resultQuery, $resultParameters) = $result;
  311. $resultQuery = str_replace('`file`', '`user`', $resultQuery);
  312. $resultQuery = str_replace('`type`', '`key`', $resultQuery);
  313. $connection = \OC::$server->getDatabaseConnection();
  314. // Test the query on the privatedata table, because the activity table
  315. // does not exist in core
  316. $result = $connection->executeQuery('SELECT * FROM `*PREFIX*privatedata` WHERE ' . $resultQuery, $resultParameters);
  317. $rows = $result->fetchAll();
  318. $result->closeCursor();
  319. }
  320. protected function mockUserSession($user) {
  321. $mockUser = $this->getMockBuilder('\OCP\IUser')
  322. ->disableOriginalConstructor()
  323. ->getMock();
  324. $mockUser->expects($this->any())
  325. ->method('getUID')
  326. ->willReturn($user);
  327. $this->session->expects($this->any())
  328. ->method('isLoggedIn')
  329. ->willReturn(true);
  330. $this->session->expects($this->any())
  331. ->method('getUser')
  332. ->willReturn($mockUser);
  333. }
  334. }