diff options
Diffstat (limited to 'apps/files/tests')
-rw-r--r-- | apps/files/tests/js/favoritesfilelistspec.js | 109 | ||||
-rw-r--r-- | apps/files/tests/js/favoritespluginspec.js | 130 | ||||
-rw-r--r-- | apps/files/tests/js/tagspluginspec.js | 84 | ||||
-rw-r--r-- | apps/files/tests/service/tagservice.php | 121 |
4 files changed, 444 insertions, 0 deletions
diff --git a/apps/files/tests/js/favoritesfilelistspec.js b/apps/files/tests/js/favoritesfilelistspec.js new file mode 100644 index 00000000000..608ddaca18b --- /dev/null +++ b/apps/files/tests/js/favoritesfilelistspec.js @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2014 Vincent Petry <pvince81@owncloud.com> + * + * This file is licensed under the Affero General Public License version 3 + * or later. + * + * See the COPYING-README file. + * + */ + +describe('OCA.Files.FavoritesFileList tests', function() { + var fileList; + + beforeEach(function() { + // init parameters and test table elements + $('#testArea').append( + '<div id="app-content-container">' + + // init horrible parameters + '<input type="hidden" id="dir" value="/"></input>' + + '<input type="hidden" id="permissions" value="31"></input>' + + // dummy controls + '<div id="controls">' + + ' <div class="actions creatable"></div>' + + ' <div class="notCreatable"></div>' + + '</div>' + + // dummy table + // TODO: at some point this will be rendered by the fileList class itself! + '<table id="filestable">' + + '<thead><tr>' + + '<th id="headerName" class="hidden column-name">' + + '<a class="name columntitle" data-sort="name"><span>Name</span><span class="sort-indicator"></span></a>' + + '</th>' + + '<th class="hidden column-mtime">' + + '<a class="columntitle" data-sort="mtime"><span class="sort-indicator"></span></a>' + + '</th>' + + '</tr></thead>' + + '<tbody id="fileList"></tbody>' + + '<tfoot></tfoot>' + + '</table>' + + '<div id="emptycontent">Empty content message</div>' + + '</div>' + ); + }); + afterEach(function() { + fileList.destroy(); + fileList = undefined; + }); + + describe('loading file list', function() { + var response; + + beforeEach(function() { + fileList = new OCA.Files.FavoritesFileList( + $('#app-content-container') + ); + OCA.Files.FavoritesPlugin.attach(fileList); + + fileList.reload(); + + /* jshint camelcase: false */ + response = { + files: [{ + id: 7, + name: 'test.txt', + path: '/somedir', + size: 123, + mtime: 11111000, + tags: [OC.TAG_FAVORITE], + permissions: OC.PERMISSION_ALL, + mimetype: 'text/plain' + }] + }; + }); + it('render files', function() { + var request; + + expect(fakeServer.requests.length).toEqual(1); + request = fakeServer.requests[0]; + expect(request.url).toEqual( + OC.generateUrl('apps/files/api/v1/tags/{tagName}/files', {tagName: OC.TAG_FAVORITE}) + ); + + fakeServer.requests[0].respond( + 200, + { 'Content-Type': 'application/json' }, + JSON.stringify(response) + ); + + var $rows = fileList.$el.find('tbody tr'); + var $tr = $rows.eq(0); + expect($rows.length).toEqual(1); + expect($tr.attr('data-id')).toEqual('7'); + expect($tr.attr('data-type')).toEqual('file'); + expect($tr.attr('data-file')).toEqual('test.txt'); + expect($tr.attr('data-path')).toEqual('/somedir'); + expect($tr.attr('data-size')).toEqual('123'); + expect(parseInt($tr.attr('data-permissions'), 10)) + .toEqual(OC.PERMISSION_ALL); + expect($tr.attr('data-mime')).toEqual('text/plain'); + expect($tr.attr('data-mtime')).toEqual('11111000'); + expect($tr.find('a.name').attr('href')).toEqual( + OC.webroot + + '/index.php/apps/files/ajax/download.php' + + '?dir=%2Fsomedir&files=test.txt' + ); + expect($tr.find('.nametext').text().trim()).toEqual('test.txt'); + }); + }); +}); diff --git a/apps/files/tests/js/favoritespluginspec.js b/apps/files/tests/js/favoritespluginspec.js new file mode 100644 index 00000000000..90b40ede74b --- /dev/null +++ b/apps/files/tests/js/favoritespluginspec.js @@ -0,0 +1,130 @@ +/* + * Copyright (c) 2014 Vincent Petry <pvince81@owncloud.com> + * + * This file is licensed under the Affero General Public License version 3 + * or later. + * + * See the COPYING-README file. + * + */ + +describe('OCA.Files.FavoritesPlugin tests', function() { + var Plugin = OCA.Files.FavoritesPlugin; + var fileList; + + beforeEach(function() { + $('#testArea').append( + '<div id="app-navigation">' + + '<ul><li data-id="files"><a>Files</a></li>' + + '<li data-id="sharingin"><a></a></li>' + + '<li data-id="sharingout"><a></a></li>' + + '</ul></div>' + + '<div id="app-content">' + + '<div id="app-content-files" class="hidden">' + + '</div>' + + '<div id="app-content-favorites" class="hidden">' + + '</div>' + + '</div>' + + '</div>' + ); + OC.Plugins.attach('OCA.Files.App', Plugin); + fileList = Plugin.showFileList($('#app-content-favorites')); + }); + afterEach(function() { + OC.Plugins.detach('OCA.Files.App', Plugin); + }); + + describe('initialization', function() { + it('inits favorites list on show', function() { + expect(fileList).toBeDefined(); + }); + }); + describe('file actions', function() { + var oldLegacyFileActions; + + beforeEach(function() { + oldLegacyFileActions = window.FileActions; + window.FileActions = new OCA.Files.FileActions(); + }); + + afterEach(function() { + window.FileActions = oldLegacyFileActions; + }); + it('provides default file actions', function() { + var fileActions = fileList.fileActions; + + expect(fileActions.actions.all).toBeDefined(); + expect(fileActions.actions.all.Delete).toBeDefined(); + expect(fileActions.actions.all.Rename).toBeDefined(); + expect(fileActions.actions.all.Download).toBeDefined(); + + expect(fileActions.defaults.dir).toEqual('Open'); + }); + it('provides custom file actions', function() { + var actionStub = sinon.stub(); + // regular file action + OCA.Files.fileActions.register( + 'all', + 'RegularTest', + OC.PERMISSION_READ, + OC.imagePath('core', 'actions/shared'), + actionStub + ); + + Plugin.favoritesFileList = null; + fileList = Plugin.showFileList($('#app-content-favorites')); + + expect(fileList.fileActions.actions.all.RegularTest).toBeDefined(); + }); + it('does not provide legacy file actions', function() { + var actionStub = sinon.stub(); + // legacy file action + window.FileActions.register( + 'all', + 'LegacyTest', + OC.PERMISSION_READ, + OC.imagePath('core', 'actions/shared'), + actionStub + ); + + Plugin.favoritesFileList = null; + fileList = Plugin.showFileList($('#app-content-favorites')); + + expect(fileList.fileActions.actions.all.LegacyTest).not.toBeDefined(); + }); + it('redirects to files app when opening a directory', function() { + var oldList = OCA.Files.App.fileList; + // dummy new list to make sure it exists + OCA.Files.App.fileList = new OCA.Files.FileList($('<table><thead></thead><tbody></tbody></table>')); + + var setActiveViewStub = sinon.stub(OCA.Files.App, 'setActiveView'); + // create dummy table so we can click the dom + var $table = '<table><thead></thead><tbody id="fileList"></tbody></table>'; + $('#app-content-favorites').append($table); + + Plugin.favoritesFileList = null; + fileList = Plugin.showFileList($('#app-content-favorites')); + + fileList.setFiles([{ + name: 'testdir', + type: 'dir', + path: '/somewhere/inside/subdir', + counterParts: ['user2'], + shareOwner: 'user2' + }]); + + fileList.findFileEl('testdir').find('td a.name').click(); + + expect(OCA.Files.App.fileList.getCurrentDirectory()).toEqual('/somewhere/inside/subdir/testdir'); + + expect(setActiveViewStub.calledOnce).toEqual(true); + expect(setActiveViewStub.calledWith('files')).toEqual(true); + + setActiveViewStub.restore(); + + // restore old list + OCA.Files.App.fileList = oldList; + }); + }); +}); + diff --git a/apps/files/tests/js/tagspluginspec.js b/apps/files/tests/js/tagspluginspec.js new file mode 100644 index 00000000000..424017a9dc6 --- /dev/null +++ b/apps/files/tests/js/tagspluginspec.js @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2014 Vincent Petry <pvince81@owncloud.com> + * + * This file is licensed under the Affero General Public License version 3 + * or later. + * + * See the COPYING-README file. + * + */ + +describe('OCA.Files.TagsPlugin tests', function() { + var fileList; + var testFiles; + + beforeEach(function() { + var $content = $('<div id="content"></div>'); + $('#testArea').append($content); + // dummy file list + var $div = $( + '<div>' + + '<table id="filestable">' + + '<thead></thead>' + + '<tbody id="fileList"></tbody>' + + '</table>' + + '</div>'); + $('#content').append($div); + + fileList = new OCA.Files.FileList($div); + OCA.Files.TagsPlugin.attach(fileList); + + testFiles = [{ + id: 1, + type: 'file', + name: 'One.txt', + path: '/subdir', + mimetype: 'text/plain', + size: 12, + permissions: OC.PERMISSION_ALL, + etag: 'abc', + shareOwner: 'User One', + isShareMountPoint: false, + tags: ['tag1', 'tag2'] + }]; + }); + afterEach(function() { + fileList.destroy(); + fileList = null; + }); + + describe('Favorites icon', function() { + it('renders favorite icon and extra data', function() { + var $action, $tr; + fileList.setFiles(testFiles); + $tr = fileList.$el.find('tbody tr:first'); + $action = $tr.find('.action-favorite'); + expect($action.length).toEqual(1); + expect($action.hasClass('permanent')).toEqual(false); + + expect($tr.attr('data-tags').split('|')).toEqual(['tag1', 'tag2']); + expect($tr.attr('data-favorite')).not.toBeDefined(); + }); + it('renders permanent favorite icon and extra data', function() { + var $action, $tr; + testFiles[0].tags.push(OC.TAG_FAVORITE); + fileList.setFiles(testFiles); + $tr = fileList.$el.find('tbody tr:first'); + $action = $tr.find('.action-favorite'); + expect($action.length).toEqual(1); + expect($action.hasClass('permanent')).toEqual(true); + + expect($tr.attr('data-tags').split('|')).toEqual(['tag1', 'tag2', OC.TAG_FAVORITE]); + expect($tr.attr('data-favorite')).toEqual('true'); + }); + }); + describe('Applying tags', function() { + it('sends request to server and updates icon', function() { + // TODO + fileList.setFiles(testFiles); + }); + it('sends all tags to server when applyFileTags() is called ', function() { + // TODO + }); + }); +}); diff --git a/apps/files/tests/service/tagservice.php b/apps/files/tests/service/tagservice.php new file mode 100644 index 00000000000..158dd77e858 --- /dev/null +++ b/apps/files/tests/service/tagservice.php @@ -0,0 +1,121 @@ +<?php + +/** + * ownCloud + * + * @author Vincent Petry + * @copyright 2014 Vincent Petry <pvince81@owncloud.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE + * License as published by the Free Software Foundation; either + * version 3 of the License, or any later version. + * + * This library 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 along with this library. If not, see <http://www.gnu.org/licenses/>. + * + */ +namespace OCA\Files; + +use \OCA\Files\Service\TagService; + +class TagServiceTest extends \Test\TestCase { + + /** + * @var string + */ + private $user; + + /** + * @var \OCP\Files\Folder + */ + private $root; + + /** + * @var \OCA\Files\Service\TagService + */ + private $tagService; + + /** + * @var \OCP\ITags + */ + private $tagger; + + protected function setUp() { + parent::setUp(); + $this->user = $this->getUniqueId('user'); + \OC_User::createUser($this->user, 'test'); + \OC_User::setUserId($this->user); + \OC_Util::setupFS($this->user); + /** + * @var \OCP\IUser + */ + $user = new \OC\User\User($this->user, null); + /** + * @var \OCP\IUserSession + */ + $userSession = $this->getMock('\OCP\IUserSession'); + $userSession->expects($this->any()) + ->method('getUser') + ->withAnyParameters() + ->will($this->returnValue($user)); + + $this->root = \OC::$server->getUserFolder(); + + $this->tagger = \OC::$server->getTagManager()->load('files'); + $this->tagService = new TagService( + $userSession, + $this->tagger, + $this->root + ); + } + + protected function tearDown() { + \OC_User::setUserId(''); + \OC_User::deleteUser($this->user); + } + + public function testUpdateFileTags() { + $tag1 = 'tag1'; + $tag2 = 'tag2'; + + $subdir = $this->root->newFolder('subdir'); + $testFile = $subdir->newFile('test.txt'); + $testFile->putContent('test contents'); + + $fileId = $testFile->getId(); + + // set tags + $this->tagService->updateFileTags('subdir/test.txt', array($tag1, $tag2)); + + $this->assertEquals(array($fileId), $this->tagger->getIdsForTag($tag1)); + $this->assertEquals(array($fileId), $this->tagger->getIdsForTag($tag2)); + + // remove tag + $result = $this->tagService->updateFileTags('subdir/test.txt', array($tag2)); + $this->assertEquals(array(), $this->tagger->getIdsForTag($tag1)); + $this->assertEquals(array($fileId), $this->tagger->getIdsForTag($tag2)); + + // clear tags + $result = $this->tagService->updateFileTags('subdir/test.txt', array()); + $this->assertEquals(array(), $this->tagger->getIdsForTag($tag1)); + $this->assertEquals(array(), $this->tagger->getIdsForTag($tag2)); + + // non-existing file + $caught = false; + try { + $this->tagService->updateFileTags('subdir/unexist.txt', array($tag1)); + } catch (\OCP\Files\NotFoundException $e) { + $caught = true; + } + $this->assertTrue($caught); + + $subdir->delete(); + } +} + |