From 9233d32834d07f8bb55d8efb3436d70e66014cb5 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 30 Mar 2015 17:21:06 +0200 Subject: Move tag related code into a helper so we can test the query without a view --- apps/files/tests/activitytest.php | 297 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 297 insertions(+) create mode 100644 apps/files/tests/activitytest.php (limited to 'apps/files/tests') diff --git a/apps/files/tests/activitytest.php b/apps/files/tests/activitytest.php new file mode 100644 index 00000000000..24240a03771 --- /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); + } +} -- cgit v1.2.3 From efcc2e87ab860c50f7904ac8e98914589a9c097a Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 31 Mar 2015 17:35:04 +0200 Subject: Adjust by/self filter aswell and fix tests --- apps/files/lib/activity.php | 2 +- apps/files/tests/activitytest.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'apps/files/tests') diff --git a/apps/files/lib/activity.php b/apps/files/lib/activity.php index 007f9a1dda4..fff49ea4ea5 100644 --- a/apps/files/lib/activity.php +++ b/apps/files/lib/activity.php @@ -318,7 +318,7 @@ class Activity implements IExtension { } // Display actions from favorites only - if ($filter === self::FILTER_FAVORITES || $filter === 'all' && $this->userSettingFavoritesOnly($user)) { + if ($filter === self::FILTER_FAVORITES || in_array($filter, ['all', 'by', 'self']) && $this->userSettingFavoritesOnly($user)) { try { $favorites = $this->helper->getFavoriteFilePaths($user); } catch (\RuntimeException $e) { diff --git a/apps/files/tests/activitytest.php b/apps/files/tests/activitytest.php index 24240a03771..b8766f95224 100644 --- a/apps/files/tests/activitytest.php +++ b/apps/files/tests/activitytest.php @@ -234,7 +234,7 @@ class ActivityTest extends TestCase { 'items' => [], 'folders' => [], ], - ' CASE WHEN `app` = ? THEN (`type` <> ? OR `type` <> ?) ELSE `app` <> ? END ', + ' CASE WHEN `app` = ? THEN ((`type` <> ? AND `type` <> ?)) ELSE `app` <> ? END ', ['files', Activity::TYPE_SHARE_CREATED, Activity::TYPE_SHARE_CHANGED, 'files'] ], [ @@ -242,7 +242,7 @@ class ActivityTest extends TestCase { 'items' => ['file.txt', 'folder'], 'folders' => ['folder'], ], - ' CASE WHEN `app` = ? THEN (`type` <> ? OR `type` <> ? OR `file` = ? OR `file` = ? OR `file` LIKE ?) ELSE `app` <> ? END ', + ' CASE WHEN `app` = ? THEN ((`type` <> ? AND `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'] ], ]; -- cgit v1.2.3 From 730efe25a4c386b2d2e4c1d1b0d16a71be7b9f28 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 1 Apr 2015 12:13:49 +0200 Subject: Make scrutinizer happy --- apps/files/lib/activityhelper.php | 2 +- apps/files/tests/activitytest.php | 2 +- lib/private/allconfig.php | 2 +- lib/private/server.php | 2 +- lib/public/iconfig.php | 2 +- lib/public/iservercontainer.php | 2 +- tests/lib/activitymanager.php | 27 ++++++++++----------------- 7 files changed, 16 insertions(+), 23 deletions(-) (limited to 'apps/files/tests') diff --git a/apps/files/lib/activityhelper.php b/apps/files/lib/activityhelper.php index e05ae6c9831..f9ff722b1c2 100644 --- a/apps/files/lib/activityhelper.php +++ b/apps/files/lib/activityhelper.php @@ -60,7 +60,7 @@ class ActivityHelper { $folders = $items = []; foreach ($favorites as $favorite) { $nodes = $rootFolder->getById($favorite); - if ($nodes) { + if (!empty($nodes)) { /** @var \OCP\Files\Node $node */ $node = array_shift($nodes); $path = substr($node->getPath(), strlen($user . '/files/')); diff --git a/apps/files/tests/activitytest.php b/apps/files/tests/activitytest.php index b8766f95224..1f8d6330e51 100644 --- a/apps/files/tests/activitytest.php +++ b/apps/files/tests/activitytest.php @@ -95,7 +95,7 @@ class ActivityTest extends TestCase { public function testTranslate() { $this->assertFalse( - $this->activityExtension->translate('files_sharing', '', '', array(), false, false, 'en'), + $this->activityExtension->translate('files_sharing', '', [], false, false, 'en'), 'Asserting that no translations are set for files_sharing' ); } diff --git a/lib/private/allconfig.php b/lib/private/allconfig.php index df75a332a13..63cc92601bb 100644 --- a/lib/private/allconfig.php +++ b/lib/private/allconfig.php @@ -253,7 +253,7 @@ class AllConfig implements \OCP\IConfig { * @param string $userId the userId of the user that we want to store the value under * @param string $appName the appName that we stored the value under * @param string $key the key under which the value is being stored - * @param string $default the default value to be returned if the value isn't set + * @param mixed $default the default value to be returned if the value isn't set * @return string */ public function getUserValue($userId, $appName, $key, $default = '') { diff --git a/lib/private/server.php b/lib/private/server.php index 6b212ee94a4..02d649585d6 100644 --- a/lib/private/server.php +++ b/lib/private/server.php @@ -425,7 +425,7 @@ class Server extends SimpleContainer implements IServerContainer { * currently being processed is returned from this method. * In case the current execution was not initiated by a web request null is returned * - * @return \OCP\IRequest|null + * @return \OCP\IRequest */ function getRequest() { return $this->query('Request'); diff --git a/lib/public/iconfig.php b/lib/public/iconfig.php index c63ba1a90a6..f28a114a2bb 100644 --- a/lib/public/iconfig.php +++ b/lib/public/iconfig.php @@ -133,7 +133,7 @@ interface IConfig { * @param string $userId the userId of the user that we want to store the value under * @param string $appName the appName that we stored the value under * @param string $key the key under which the value is being stored - * @param string $default the default value to be returned if the value isn't set + * @param mixed $default the default value to be returned if the value isn't set * @return string */ public function getUserValue($userId, $appName, $key, $default = ''); diff --git a/lib/public/iservercontainer.php b/lib/public/iservercontainer.php index d7df884adf8..f11a5c0133f 100644 --- a/lib/public/iservercontainer.php +++ b/lib/public/iservercontainer.php @@ -59,7 +59,7 @@ interface IServerContainer { * is returned from this method. * In case the current execution was not initiated by a web request null is returned * - * @return \OCP\IRequest|null + * @return \OCP\IRequest */ function getRequest(); diff --git a/tests/lib/activitymanager.php b/tests/lib/activitymanager.php index 51ab4a7a947..d3263fa2ede 100644 --- a/tests/lib/activitymanager.php +++ b/tests/lib/activitymanager.php @@ -155,17 +155,7 @@ class Test_ActivityManager extends \Test\TestCase { * @param array $users */ public function testGetUserFromTokenThrowInvalidToken($token, $users) { - if ($token !== null) { - $this->request->expects($this->any()) - ->method('getParam') - ->with('token', '') - ->willReturn($token); - } - $this->config->expects($this->any()) - ->method('getUsersForUserValue') - ->with('activity', 'rsstoken', $token) - ->willReturn($users); - + $this->mockRSSToken($token, $token, $users); \Test_Helper::invokePrivate($this->activityManager, 'getUserFromToken'); } @@ -188,20 +178,23 @@ class Test_ActivityManager extends \Test\TestCase { if ($userLoggedIn !== null) { $this->mockUserSession($userLoggedIn); } + $this->mockRSSToken($token, '123456789012345678901234567890', ['user1']); + + $this->assertEquals($expected, $this->activityManager->getCurrentUserId()); + } - if ($token !== null) { + protected function mockRSSToken($requestToken, $userToken, $users) { + if ($requestToken !== null) { $this->request->expects($this->any()) ->method('getParam') ->with('token', '') - ->willReturn($token); + ->willReturn($requestToken); } $this->config->expects($this->any()) ->method('getUsersForUserValue') - ->with('activity', 'rsstoken', '123456789012345678901234567890') - ->willReturn(['user1']); - - $this->assertEquals($expected, $this->activityManager->getCurrentUserId()); + ->with('activity', 'rsstoken', $userToken) + ->willReturn($users); } protected function mockUserSession($user) { -- cgit v1.2.3