From 9233d32834d07f8bb55d8efb3436d70e66014cb5 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 30 Mar 2015 17:21:06 +0200 Subject: [PATCH] Move tag related code into a helper so we can test the query without a view --- apps/files/appinfo/app.php | 4 +- apps/files/lib/activity.php | 63 +++---- apps/files/lib/activityhelper.php | 84 +++++++++ apps/files/tests/activitytest.php | 297 ++++++++++++++++++++++++++++++ 4 files changed, 414 insertions(+), 34 deletions(-) create mode 100644 apps/files/lib/activityhelper.php create mode 100644 apps/files/tests/activitytest.php diff --git a/apps/files/appinfo/app.php b/apps/files/appinfo/app.php index 578c1a52e8..c483ad31ec 100644 --- a/apps/files/appinfo/app.php +++ b/apps/files/appinfo/app.php @@ -61,7 +61,9 @@ $templateManager->registerTemplate('application/vnd.oasis.opendocument.spreadshe \OC::$server->query('L10NFactory'), \OC::$server->getURLGenerator(), \OC::$server->getActivityManager(), - \OC::$server->getTagManager(), + new \OCA\Files\ActivityHelper( + \OC::$server->getTagManager() + ), \OC::$server->getConfig() ); }); diff --git a/apps/files/lib/activity.php b/apps/files/lib/activity.php index 559342d979..7302bfb33b 100644 --- a/apps/files/lib/activity.php +++ b/apps/files/lib/activity.php @@ -27,7 +27,6 @@ use OCP\Activity\IExtension; use OCP\Activity\IManager; use OCP\IConfig; use OCP\IL10N; -use OCP\ITagManager; use OCP\IURLGenerator; class Activity implements IExtension { @@ -55,22 +54,22 @@ class Activity implements IExtension { /** @var \OCP\IConfig */ protected $config; - /** @var \OCP\ITagManager */ - protected $tagManager; + /** @var \OCA\Files\ActivityHelper */ + protected $helper; /** * @param Factory $languageFactory * @param IURLGenerator $URLGenerator * @param IManager $activityManager - * @param ITagManager $tagManager + * @param ActivityHelper $helper * @param IConfig $config */ - public function __construct(Factory $languageFactory, IURLGenerator $URLGenerator, IManager $activityManager, ITagManager $tagManager, IConfig $config) { + public function __construct(Factory $languageFactory, IURLGenerator $URLGenerator, IManager $activityManager, ActivityHelper $helper, IConfig $config) { $this->languageFactory = $languageFactory; $this->URLGenerator = $URLGenerator; $this->l = $this->getL10N(); $this->activityManager = $activityManager; - $this->tagManager = $tagManager; + $this->helper = $helper; $this->config = $config; } @@ -94,7 +93,7 @@ class Activity implements IExtension { return [ self::TYPE_SHARE_CREATED => (string) $l->t('A new file or folder has been created'), self::TYPE_SHARE_CHANGED => (string) $l->t('A file or folder has been changed'), - self::TYPE_FAVORITES => (string) $l->t('Limit notifications about creation and changes to your favorite files'), + self::TYPE_FAVORITES => (string) $l->t('Limit notifications about creation and changes to your favorite files (Stream only)'), self::TYPE_SHARE_DELETED => (string) $l->t('A file or folder has been deleted'), self::TYPE_SHARE_RESTORED => (string) $l->t('A file or folder has been restored'), ]; @@ -250,19 +249,20 @@ class Activity implements IExtension { */ public function getNavigation() { return [ - 'apps' => [ + 'top' => [ self::FILTER_FAVORITES => [ 'id' => self::FILTER_FAVORITES, 'name' => (string) $this->l->t('Favorites'), 'url' => $this->URLGenerator->linkToRoute('activity.Activities.showList', ['filter' => self::FILTER_FAVORITES]), ], + ], + 'apps' => [ self::FILTER_FILES => [ 'id' => self::FILTER_FILES, 'name' => (string) $this->l->t('Files'), 'url' => $this->URLGenerator->linkToRoute('activity.Activities.showList', ['filter' => self::FILTER_FILES]), ], ], - 'top' => [], ]; } @@ -319,41 +319,38 @@ class Activity implements IExtension { // Display actions from favorites only if ($filter === self::FILTER_FAVORITES || $filter === 'all' && $this->userSettingFavoritesOnly($user)) { - $tags = $this->tagManager->load('files', [], false, $user); - $favorites = $tags->getFavorites(); - - if (isset($favorites[50])) { + try { + $favorites = $this->helper->getFavoriteFilePaths($user); + } catch (\RuntimeException $e) { // Too many favorites, can not put them into one query anymore... return ['`app` = ?', ['files']]; } - // Can not DI because the user is not known on instantiation - $rootFolder = \OC::$server->getUserFolder($user); + /* + * Display activities only, when they are not `type` create/change + * or `file` is a favorite or in a favorite folder + */ $parameters = $fileQueryList = []; - foreach ($favorites as $favorite) { - $nodes = $rootFolder->getById($favorite); - if ($nodes) { - /** @var \OCP\Files\Node $node */ - $node = array_shift($nodes); + $parameters[] = 'files'; - $fileQueryList[] = '`file` = ?'; - $parameters[] = substr($node->getPath(), strlen($user . '/files/')); + $fileQueryList[] = '`type` <> ?'; + $parameters[] = self::TYPE_SHARE_CREATED; + $fileQueryList[] = '`type` <> ?'; + $parameters[] = self::TYPE_SHARE_CHANGED; - if ($node instanceof \OCP\Files\Folder) { - // Also look for subfolders and files - $fileQueryList[] = '`file` LIKE ?'; - $parameters[] = substr($node->getPath(), strlen($user . '/files/')) . '/%'; - } - } + foreach ($favorites['items'] as $favorite) { + $fileQueryList[] = '`file` = ?'; + $parameters[] = $favorite; + } + foreach ($favorites['folders'] as $favorite) { + $fileQueryList[] = '`file` LIKE ?'; + $parameters[] = $favorite . '/%'; } - if (empty($fileQueryList)) { - // No favorites... - return ['`app` = ?', ['files']]; - } + $parameters[] = 'files'; return [ - ' CASE WHEN `app` = \'files\' THEN (' . implode(' OR ', $fileQueryList) . ') ELSE `app` <> \'files\' END ', + ' CASE WHEN `app` = ? THEN (' . implode(' OR ', $fileQueryList) . ') ELSE `app` <> ? END ', $parameters, ]; } diff --git a/apps/files/lib/activityhelper.php b/apps/files/lib/activityhelper.php new file mode 100644 index 0000000000..e05ae6c983 --- /dev/null +++ b/apps/files/lib/activityhelper.php @@ -0,0 +1,84 @@ + + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see + * + */ + +namespace OCA\Files; + +use OCP\Files\Folder; +use OCP\ITagManager; + +class ActivityHelper { + /** If a user has a lot of favorites the query might get too slow and long */ + const FAVORITE_LIMIT = 50; + + /** @var \OCP\ITagManager */ + protected $tagManager; + + /** + * @param ITagManager $tagManager + */ + public function __construct(ITagManager $tagManager) { + $this->tagManager = $tagManager; + } + + /** + * Returns an array with the favorites + * + * @param string $user + * @return array + * @throws \RuntimeException when too many or no favorites where found + */ + public function getFavoriteFilePaths($user) { + $tags = $this->tagManager->load('files', [], false, $user); + $favorites = $tags->getFavorites(); + + if (empty($favorites)) { + throw new \RuntimeException('No favorites', 1); + } else if (isset($favorites[self::FAVORITE_LIMIT])) { + throw new \RuntimeException('Too many favorites', 2); + } + + // Can not DI because the user is not known on instantiation + $rootFolder = \OC::$server->getUserFolder($user); + $folders = $items = []; + foreach ($favorites as $favorite) { + $nodes = $rootFolder->getById($favorite); + if ($nodes) { + /** @var \OCP\Files\Node $node */ + $node = array_shift($nodes); + $path = substr($node->getPath(), strlen($user . '/files/')); + + $items[] = $path; + if ($node instanceof Folder) { + $folders[] = $path; + } + } + } + + if (empty($items)) { + throw new \RuntimeException('No favorites', 1); + } + + return [ + 'items' => $items, + 'folders' => $folders, + ]; + } +} diff --git a/apps/files/tests/activitytest.php b/apps/files/tests/activitytest.php new file mode 100644 index 0000000000..24240a0377 --- /dev/null +++ b/apps/files/tests/activitytest.php @@ -0,0 +1,297 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + * +*/ + +namespace OCA\Files\Tests; + +use OCA\Files\Activity; +use Test\TestCase; + +class ActivityTest extends TestCase { + + /** @var \OC\ActivityManager */ + private $activityManager; + + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $request; + + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $session; + + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $config; + + /** @var \PHPUnit_Framework_MockObject_MockObject */ + protected $activityHelper; + + /** @var \OCA\Files\Activity */ + protected $activityExtension; + + protected function setUp() { + parent::setUp(); + + $this->request = $this->getMockBuilder('OCP\IRequest') + ->disableOriginalConstructor() + ->getMock(); + $this->session = $this->getMockBuilder('OCP\IUserSession') + ->disableOriginalConstructor() + ->getMock(); + $this->config = $this->getMockBuilder('OCP\IConfig') + ->disableOriginalConstructor() + ->getMock(); + $this->activityHelper = $this->getMockBuilder('OCA\Files\ActivityHelper') + ->disableOriginalConstructor() + ->getMock(); + + $this->activityManager = new \OC\ActivityManager( + $this->request, + $this->session, + $this->config + ); + + $this->activityExtension = $activityExtension = new Activity( + new \OC\L10N\Factory(), + $this->getMockBuilder('OCP\IURLGenerator')->disableOriginalConstructor()->getMock(), + $this->activityManager, + $this->activityHelper, + $this->config + ); + + $this->activityManager->registerExtension(function() use ($activityExtension) { + return $activityExtension; + }); + } + + public function testNotificationTypes() { + $result = $this->activityExtension->getNotificationTypes('en'); + $this->assertTrue(is_array($result), 'Asserting getNotificationTypes() returns an array'); + $this->assertCount(5, $result); + $this->assertArrayHasKey(Activity::TYPE_SHARE_CREATED, $result); + $this->assertArrayHasKey(Activity::TYPE_SHARE_CHANGED, $result); + $this->assertArrayHasKey(Activity::TYPE_FAVORITES, $result); + $this->assertArrayHasKey(Activity::TYPE_SHARE_DELETED, $result); + $this->assertArrayHasKey(Activity::TYPE_SHARE_RESTORED, $result); + } + + public function testDefaultTypes() { + $result = $this->activityExtension->getDefaultTypes('stream'); + $this->assertTrue(is_array($result), 'Asserting getDefaultTypes(stream) returns an array'); + $this->assertCount(4, $result); + $result = array_flip($result); + $this->assertArrayHasKey(Activity::TYPE_SHARE_CREATED, $result); + $this->assertArrayHasKey(Activity::TYPE_SHARE_CHANGED, $result); + $this->assertArrayNotHasKey(Activity::TYPE_FAVORITES, $result); + $this->assertArrayHasKey(Activity::TYPE_SHARE_DELETED, $result); + $this->assertArrayHasKey(Activity::TYPE_SHARE_RESTORED, $result); + + $result = $this->activityExtension->getDefaultTypes('email'); + $this->assertFalse($result, 'Asserting getDefaultTypes(email) returns false'); + } + + public function testTranslate() { + $this->assertFalse( + $this->activityExtension->translate('files_sharing', '', '', array(), false, false, 'en'), + 'Asserting that no translations are set for files_sharing' + ); + } + + public function testGetSpecialParameterList() { + $this->assertFalse( + $this->activityExtension->getSpecialParameterList('files_sharing', ''), + 'Asserting that no special parameters are set for files_sharing' + ); + } + + public function typeIconData() { + return [ + [Activity::TYPE_SHARE_CHANGED, 'icon-change'], + [Activity::TYPE_SHARE_CREATED, 'icon-add-color'], + [Activity::TYPE_SHARE_DELETED, 'icon-delete-color'], + [Activity::TYPE_SHARE_RESTORED, false], + [Activity::TYPE_FAVORITES, false], + ['unknown type', false], + ]; + } + + /** + * @dataProvider typeIconData + * + * @param string $type + * @param mixed $expected + */ + public function testTypeIcon($type, $expected) { + $this->assertSame($expected, $this->activityExtension->getTypeIcon($type)); + } + + public function testGroupParameter() { + $this->assertFalse( + $this->activityExtension->getGroupParameter(['app' => 'files_sharing']), + 'Asserting that no group parameters are set for files_sharing' + ); + } + + public function testNavigation() { + $result = $this->activityExtension->getNavigation(); + $this->assertCount(1, $result['top']); + $this->assertArrayHasKey(Activity::FILTER_FAVORITES, $result['top']); + + $this->assertCount(1, $result['apps']); + $this->assertArrayHasKey(Activity::FILTER_FILES, $result['apps']); + } + + public function testIsFilterValid() { + $this->assertTrue($this->activityExtension->isFilterValid(Activity::FILTER_FAVORITES)); + $this->assertTrue($this->activityExtension->isFilterValid(Activity::FILTER_FILES)); + $this->assertFalse($this->activityExtension->isFilterValid('unknown filter')); + } + + public function filterNotificationTypesData() { + return [ + [ + Activity::FILTER_FILES, + [ + 'NT0', + Activity::TYPE_SHARE_CREATED, + Activity::TYPE_SHARE_CHANGED, + Activity::TYPE_SHARE_DELETED, + Activity::TYPE_SHARE_RESTORED, + Activity::TYPE_FAVORITES, + ], [ + Activity::TYPE_SHARE_CREATED, + Activity::TYPE_SHARE_CHANGED, + Activity::TYPE_SHARE_DELETED, + Activity::TYPE_SHARE_RESTORED, + ], + ], + [ + Activity::FILTER_FILES, + [ + 'NT0', + Activity::TYPE_SHARE_CREATED, + Activity::TYPE_FAVORITES, + ], + [ + Activity::TYPE_SHARE_CREATED, + ], + ], + [ + Activity::FILTER_FAVORITES, + [ + 'NT0', + Activity::TYPE_SHARE_CREATED, + Activity::TYPE_SHARE_CHANGED, + Activity::TYPE_SHARE_DELETED, + Activity::TYPE_SHARE_RESTORED, + Activity::TYPE_FAVORITES, + ], [ + Activity::TYPE_SHARE_CREATED, + Activity::TYPE_SHARE_CHANGED, + Activity::TYPE_SHARE_DELETED, + Activity::TYPE_SHARE_RESTORED, + ], + ], + [ + 'unknown filter', + [ + 'NT0', + Activity::TYPE_SHARE_CREATED, + Activity::TYPE_SHARE_CHANGED, + Activity::TYPE_SHARE_DELETED, + Activity::TYPE_SHARE_RESTORED, + Activity::TYPE_FAVORITES, + ], + false, + ], + ]; + } + + /** + * @dataProvider filterNotificationTypesData + * + * @param string $filter + * @param array $types + * @param mixed $expected + */ + public function testFilterNotificationTypes($filter, $types, $expected) { + $result = $this->activityExtension->filterNotificationTypes($types, $filter); + $this->assertEquals($expected, $result); + } + + public function queryForFilterData() { + return [ + [ + new \RuntimeException(), + '`app` = ?', + ['files'] + ], + [ + [ + 'items' => [], + 'folders' => [], + ], + ' CASE WHEN `app` = ? THEN (`type` <> ? OR `type` <> ?) ELSE `app` <> ? END ', + ['files', Activity::TYPE_SHARE_CREATED, Activity::TYPE_SHARE_CHANGED, 'files'] + ], + [ + [ + 'items' => ['file.txt', 'folder'], + 'folders' => ['folder'], + ], + ' CASE WHEN `app` = ? THEN (`type` <> ? OR `type` <> ? OR `file` = ? OR `file` = ? OR `file` LIKE ?) ELSE `app` <> ? END ', + ['files', Activity::TYPE_SHARE_CREATED, Activity::TYPE_SHARE_CHANGED, 'file.txt', 'folder', 'folder/%', 'files'] + ], + ]; + } + + /** + * @dataProvider queryForFilterData + * + * @param mixed $will + * @param string $query + * @param array $parameters + */ + public function testQueryForFilter($will, $query, $parameters) { + $this->mockUserSession('test'); + + $this->config->expects($this->any()) + ->method('getUserValue') + ->willReturnMap([ + ['test', 'activity', 'notify_stream_' . Activity::TYPE_FAVORITES, false, true], + ]); + if (is_array($will)) { + $this->activityHelper->expects($this->any()) + ->method('getFavoriteFilePaths') + ->with('test') + ->willReturn($will); + } else { + $this->activityHelper->expects($this->any()) + ->method('getFavoriteFilePaths') + ->with('test') + ->willThrowException($will); + } + + $result = $this->activityExtension->getQueryForFilter('all'); + $this->assertEquals([$query, $parameters], $result); + } + + protected function mockUserSession($user) { + $mockUser = $this->getMockBuilder('\OCP\IUser') + ->disableOriginalConstructor() + ->getMock(); + $mockUser->expects($this->any()) + ->method('getUID') + ->willReturn($user); + + $this->session->expects($this->any()) + ->method('isLoggedIn') + ->willReturn(true); + $this->session->expects($this->any()) + ->method('getUser') + ->willReturn($mockUser); + } +}