fix(shareApiController): avoid duplicated expiryDate operations

`expireDate` can be set once and used anywhere needed, the current implementation,

duplicates this behavior which leads to `parseDate` receiving an a date object it

parsed and returend earlier in the createShare method.

Signed-off-by: fenn-cs <fenn25.fn@gmail.com>
This commit is contained in:
fenn-cs 2024-04-15 21:38:55 +01:00
parent abd24c89cb
commit 4292ff6df1
5 changed files with 851 additions and 29 deletions

View File

@ -110,11 +110,10 @@ class ShareAPIController extends OCSController {
IUserManager $userManager,
IRootFolder $rootFolder,
IURLGenerator $urlGenerator,
string $userId = null,
IL10N $l10n,
IConfig $config,
IAppManager $appManager,
IServerContainer $serverContainer,
ContainerInterface $serverContainer,
IUserStatusManager $userStatusManager,
IPreview $previewManager,
private IDateTimeZone $dateTimeZone,
@ -647,6 +646,16 @@ class ShareAPIController extends OCSController {
$share = $this->setShareAttributes($share, $attributes);
}
//Expire date
if ($expireDate !== '') {
try {
$expireDate = $this->parseDate($expireDate);
$share->setExpirationDate($expireDate);
} catch (\Exception $e) {
throw new OCSNotFoundException($this->l->t('Invalid date, date format must be YYYY-MM-DD'));
}
}
$share->setSharedBy($this->currentUser);
$this->checkInheritedAttributes($share);
@ -733,15 +742,6 @@ class ShareAPIController extends OCSController {
$share->setSharedWith($shareWith);
$share->setPermissions($permissions);
if ($expireDate !== '') {
try {
$expireDate = $this->parseDate($expireDate);
$share->setExpirationDate($expireDate);
} catch (\Exception $e) {
throw new OCSNotFoundException($this->l->t('Invalid date, date format must be YYYY-MM-DD'));
}
}
$share->setSharedWithDisplayName($this->getCachedFederatedDisplayName($shareWith, false));
} elseif ($shareType === IShare::TYPE_REMOTE_GROUP) {
if (!$this->shareManager->outgoingServer2ServerGroupSharesAllowed()) {
@ -754,14 +754,6 @@ class ShareAPIController extends OCSController {
$share->setSharedWith($shareWith);
$share->setPermissions($permissions);
if ($expireDate !== '') {
try {
$expireDate = $this->parseDate($expireDate);
$share->setExpirationDate($expireDate);
} catch (\Exception $e) {
throw new OCSNotFoundException($this->l->t('Invalid date, date format must be YYYY-MM-DD'));
}
}
} elseif ($shareType === IShare::TYPE_CIRCLE) {
if (!\OC::$server->getAppManager()->isEnabledForUser('circles') || !class_exists('\OCA\Circles\ShareByCircleProvider')) {
throw new OCSNotFoundException($this->l->t('You cannot share to a Circle if the app is not enabled'));
@ -797,16 +789,6 @@ class ShareAPIController extends OCSController {
throw new OCSBadRequestException($this->l->t('Unknown share type'));
}
//Expire date
if ($expireDate !== '') {
try {
$expireDate = $this->parseDate($expireDate);
$share->setExpirationDate($expireDate);
} catch (\Exception $e) {
throw new OCSNotFoundException($this->l->t('Invalid date, date format must be YYYY-MM-DD'));
}
}
$share->setShareType($shareType);
if ($note !== '') {

View File

@ -0,0 +1,70 @@
/**
* @copyright 2014 Vincent Petry <pvince81@owncloud.com>
*
* @author Vincent Petry <vincent@nextcloud.com>
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* 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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
describe('OCA.Trashbin.App tests', function() {
var App = OCA.Trashbin.App;
beforeEach(function() {
$('#testArea').append(
'<div id="app-navigation">' +
'<ul><li data-id="files"><a>Files</a></li>' +
'<li data-id="trashbin"><a>Trashbin</a></li>' +
'</div>' +
'<div id="app-content">' +
'<div id="app-content-files" class="hidden">' +
'</div>' +
'<div id="app-content-trashbin" class="hidden">' +
'</div>' +
'</div>' +
'</div>'
);
App.initialize($('#app-content-trashbin'));
});
afterEach(function() {
App._initialized = false;
App.fileList = null;
});
describe('initialization', function() {
it('creates a custom filelist instance', function() {
App.initialize();
expect(App.fileList).toBeDefined();
expect(App.fileList.$el.is('#app-content-trashbin')).toEqual(true);
});
it('registers custom file actions', function() {
var fileActions;
App.initialize();
fileActions = App.fileList.fileActions;
expect(fileActions.actions.all).toBeDefined();
expect(fileActions.actions.all.Restore).toBeDefined();
expect(fileActions.actions.all.Delete).toBeDefined();
expect(fileActions.actions.all.Rename).not.toBeDefined();
expect(fileActions.actions.all.Download).not.toBeDefined();
expect(fileActions.defaults.dir).toEqual('Open');
});
});
});

View File

@ -0,0 +1,397 @@
/**
* @copyright 2014 Vincent Petry <pvince81@owncloud.com>
*
* @author Abijeet <abijeetpatro@gmail.com>
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Jan C. Borchardt <hey@jancborchardt.net>
* @author Jan-Christoph Borchardt <hey@jancborchardt.net>
* @author John Molakvoæ <skjnldsv@protonmail.com>
* @author Robin Appelman <robin@icewind.nl>
* @author Vincent Petry <vincent@nextcloud.com>
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* 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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
describe('OCA.Trashbin.FileList tests', function () {
var testFiles, alertStub, notificationStub, fileList, client;
beforeEach(function () {
alertStub = sinon.stub(OC.dialogs, 'alert');
notificationStub = sinon.stub(OC.Notification, 'show');
client = new OC.Files.Client({
host: 'localhost',
port: 80,
root: '/remote.php/dav/trashbin/user',
useHTTPS: OC.getProtocol() === 'https'
});
// init parameters and test table elements
$('#testArea').append(
'<div id="app-content">' +
// set this but it shouldn't be used (could be the one from the
// files app)
'<input type="hidden" id="permissions" value="31"></input>' +
// dummy controls
'<div class="files-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 class="files-filestable list-container view-grid">' +
'<thead><tr><th class="hidden column-name">' +
'<input type="checkbox" id="select_all_trash" class="select-all">' +
'<span class="name">Name</span>' +
'<span class="selectedActions hidden">' +
'<a href="" class="actions-selected"><span class="icon icon-more"></span><span>Actions</span>' +
'</span>' +
'</th></tr></thead>' +
'<tbody class="files-fileList"></tbody>' +
'<tfoot></tfoot>' +
'</table>' +
'<div class="emptyfilelist emptycontent">Empty content message</div>' +
'</div>'
);
testFiles = [{
id: 1,
type: 'file',
name: 'One.txt.d11111',
displayName: 'One.txt',
mtime: 11111000,
mimetype: 'text/plain',
etag: 'abc'
}, {
id: 2,
type: 'file',
name: 'Two.jpg.d22222',
displayName: 'Two.jpg',
mtime: 22222000,
mimetype: 'image/jpeg',
etag: 'def',
}, {
id: 3,
type: 'file',
name: 'Three.pdf.d33333',
displayName: 'Three.pdf',
mtime: 33333000,
mimetype: 'application/pdf',
etag: '123',
}, {
id: 4,
type: 'dir',
mtime: 99999000,
name: 'somedir.d99999',
displayName: 'somedir',
mimetype: 'httpd/unix-directory',
etag: '456'
}];
// register file actions like the trashbin App does
var fileActions = OCA.Trashbin.App._createFileActions(fileList);
fileList = new OCA.Trashbin.FileList(
$('#app-content'), {
fileActions: fileActions,
multiSelectMenu: [{
name: 'restore',
displayName: t('files', 'Restore'),
iconClass: 'icon-history',
},
{
name: 'delete',
displayName: t('files', 'Delete'),
iconClass: 'icon-delete',
}
],
client: client
}
);
});
afterEach(function () {
testFiles = undefined;
fileList.destroy();
fileList = undefined;
notificationStub.restore();
alertStub.restore();
});
describe('Initialization', function () {
it('Sorts by mtime by default', function () {
expect(fileList._sort).toEqual('mtime');
expect(fileList._sortDirection).toEqual('desc');
});
it('Always returns read and delete permission', function () {
expect(fileList.getDirectoryPermissions()).toEqual(OC.PERMISSION_READ | OC.PERMISSION_DELETE);
});
});
describe('Breadcrumbs', function () {
beforeEach(function () {
var data = {
status: 'success',
data: {
files: testFiles,
permissions: 1
}
};
fakeServer.respondWith(/\/index\.php\/apps\/files_trashbin\/ajax\/list.php\?dir=%2Fsubdir/, [
200, {
"Content-Type": "application/json"
},
JSON.stringify(data)
]);
});
it('links the breadcrumb to the trashbin view', function () {
fileList.changeDirectory('/subdir', false, true);
fakeServer.respond();
var $crumbs = fileList.$el.find('.files-controls .crumb');
expect($crumbs.length).toEqual(3);
expect($crumbs.eq(1).find('a').text()).toEqual('Home');
expect($crumbs.eq(1).find('a').attr('href'))
.toEqual(OC.getRootPath() + '/index.php/apps/files?view=trashbin&dir=/');
expect($crumbs.eq(2).find('a').text()).toEqual('subdir');
expect($crumbs.eq(2).find('a').attr('href'))
.toEqual(OC.getRootPath() + '/index.php/apps/files?view=trashbin&dir=/subdir');
});
});
describe('Rendering rows', function () {
it('renders rows with the correct data when in root', function () {
// dir listing is false when in root
fileList.setFiles(testFiles);
var $rows = fileList.$el.find('tbody tr');
var $tr = $rows.eq(0);
expect($rows.length).toEqual(4);
expect($tr.attr('data-id')).toEqual('1');
expect($tr.attr('data-type')).toEqual('file');
expect($tr.attr('data-file')).toEqual('One.txt.d11111');
expect($tr.attr('data-size')).not.toBeDefined();
expect($tr.attr('data-etag')).toEqual('abc');
expect($tr.attr('data-permissions')).toEqual('9'); // read and delete
expect($tr.attr('data-mime')).toEqual('text/plain');
expect($tr.attr('data-mtime')).toEqual('11111000');
expect($tr.find('a.name').attr('href')).toEqual('#');
expect($tr.find('.nametext').text().trim()).toEqual('One.txt');
expect(fileList.findFileEl('One.txt.d11111')[0]).toEqual($tr[0]);
});
it('renders rows with the correct data when in root after calling setFiles with the same data set', function () {
// dir listing is false when in root
fileList.setFiles(testFiles);
fileList.setFiles(fileList.files);
var $rows = fileList.$el.find('tbody tr');
var $tr = $rows.eq(0);
expect($rows.length).toEqual(4);
expect($tr.attr('data-id')).toEqual('1');
expect($tr.attr('data-type')).toEqual('file');
expect($tr.attr('data-file')).toEqual('One.txt.d11111');
expect($tr.attr('data-size')).not.toBeDefined();
expect($tr.attr('data-etag')).toEqual('abc');
expect($tr.attr('data-permissions')).toEqual('9'); // read and delete
expect($tr.attr('data-mime')).toEqual('text/plain');
expect($tr.attr('data-mtime')).toEqual('11111000');
expect($tr.find('a.name').attr('href')).toEqual('#');
expect($tr.find('.nametext').text().trim()).toEqual('One.txt');
expect(fileList.findFileEl('One.txt.d11111')[0]).toEqual($tr[0]);
});
it('renders rows with the correct data when in subdirectory', function () {
fileList.setFiles(testFiles.map(function (file) {
file.name = file.displayName;
return file;
}));
var $rows = fileList.$el.find('tbody tr');
var $tr = $rows.eq(0);
expect($rows.length).toEqual(4);
expect($tr.attr('data-id')).toEqual('1');
expect($tr.attr('data-type')).toEqual('file');
expect($tr.attr('data-file')).toEqual('One.txt');
expect($tr.attr('data-size')).not.toBeDefined();
expect($tr.attr('data-etag')).toEqual('abc');
expect($tr.attr('data-permissions')).toEqual('9'); // read and delete
expect($tr.attr('data-mime')).toEqual('text/plain');
expect($tr.attr('data-mtime')).toEqual('11111000');
expect($tr.find('a.name').attr('href')).toEqual('#');
expect($tr.find('.nametext').text().trim()).toEqual('One.txt');
expect(fileList.findFileEl('One.txt')[0]).toEqual($tr[0]);
});
it('does not render a size column', function () {
expect(fileList.$el.find('tbody tr .filesize').length).toEqual(0);
});
});
describe('File actions', function () {
describe('Deleting single files', function () {
// TODO: checks ajax call
// TODO: checks spinner
// TODO: remove item after delete
// TODO: bring back item if delete failed
});
describe('Restoring single files', function () {
// TODO: checks ajax call
// TODO: checks spinner
// TODO: remove item after restore
// TODO: bring back item if restore failed
});
});
describe('file previews', function () {
// TODO: check that preview URL is going through files_trashbin
});
describe('loading file list', function () {
// TODO: check that ajax URL is going through files_trashbin
});
describe('breadcrumbs', function () {
// TODO: test label + URL
});
describe('elementToFile', function () {
var $tr;
beforeEach(function () {
fileList.setFiles(testFiles);
$tr = fileList.findFileEl('One.txt.d11111');
});
it('converts data attributes to file info structure', function () {
var fileInfo = fileList.elementToFile($tr);
expect(fileInfo.id).toEqual(1);
expect(fileInfo.name).toEqual('One.txt.d11111');
expect(fileInfo.displayName).toEqual('One.txt');
expect(fileInfo.mtime).toEqual(11111000);
expect(fileInfo.etag).toEqual('abc');
expect(fileInfo.permissions).toEqual(OC.PERMISSION_READ | OC.PERMISSION_DELETE);
expect(fileInfo.mimetype).toEqual('text/plain');
expect(fileInfo.type).toEqual('file');
});
});
describe('Global Actions', function () {
beforeEach(function () {
fileList.setFiles(testFiles);
fileList.findFileEl('One.txt.d11111').find('input:checkbox').click();
fileList.findFileEl('Three.pdf.d33333').find('input:checkbox').click();
fileList.findFileEl('somedir.d99999').find('input:checkbox').click();
fileList.$el.find('.actions-selected').click();
});
afterEach(function () {
fileList.$el.find('.actions-selected').click();
});
describe('Delete', function () {
it('Shows trashbin actions', function () {
// visible because a few files were selected
expect($('.selectedActions').is(':visible')).toEqual(true);
expect($('.selectedActions .item-delete').is(':visible')).toEqual(true);
expect($('.selectedActions .item-restore').is(':visible')).toEqual(true);
// check
fileList.$el.find('.select-all').click();
// stays visible
expect($('.selectedActions').is(':visible')).toEqual(true);
expect($('.selectedActions .item-delete').is(':visible')).toEqual(true);
expect($('.selectedActions .item-restore').is(':visible')).toEqual(true);
// uncheck
fileList.$el.find('.select-all').click();
// becomes hidden now
expect($('.selectedActions').is(':visible')).toEqual(false);
expect($('.selectedActions .item-delete').is(':visible')).toEqual(false);
expect($('.selectedActions .item-restore').is(':visible')).toEqual(false);
});
it('Deletes selected files when "Delete" clicked', function (done) {
var request;
var promise = fileList._onClickDeleteSelected({
preventDefault: function () {
}
});
var files = ["One.txt.d11111", "Three.pdf.d33333", "somedir.d99999"];
expect(fakeServer.requests.length).toEqual(files.length);
for (var i = 0; i < files.length; i++) {
request = fakeServer.requests[i];
expect(request.url).toEqual(OC.getRootPath() + '/remote.php/dav/trashbin/user/trash/' + files[i]);
request.respond(200);
}
return promise.then(function () {
expect(fileList.findFileEl('One.txt.d11111').length).toEqual(0);
expect(fileList.findFileEl('Three.pdf.d33333').length).toEqual(0);
expect(fileList.findFileEl('somedir.d99999').length).toEqual(0);
expect(fileList.findFileEl('Two.jpg.d22222').length).toEqual(1);
}).then(done, done);
});
it('Deletes all files when all selected when "Delete" clicked', function (done) {
var request;
$('.select-all').click();
var promise = fileList._onClickDeleteSelected({
preventDefault: function () {
}
});
expect(fakeServer.requests.length).toEqual(1);
request = fakeServer.requests[0];
expect(request.url).toEqual(OC.getRootPath() + '/remote.php/dav/trashbin/user/trash');
request.respond(200);
return promise.then(function () {
expect(fileList.isEmpty).toEqual(true);
}).then(done, done);
});
});
describe('Restore', function () {
it('Restores selected files when "Restore" clicked', function (done) {
var request;
var promise = fileList._onClickRestoreSelected({
preventDefault: function () {
}
});
var files = ["One.txt.d11111", "Three.pdf.d33333", "somedir.d99999"];
expect(fakeServer.requests.length).toEqual(files.length);
for (var i = 0; i < files.length; i++) {
request = fakeServer.requests[i];
expect(request.url).toEqual(OC.getRootPath() + '/remote.php/dav/trashbin/user/trash/' + files[i]);
expect(request.requestHeaders.Destination).toEqual(OC.getRootPath() + '/remote.php/dav/trashbin/user/restore/' + files[i]);
request.respond(200);
}
return promise.then(function() {
expect(fileList.findFileEl('One.txt.d11111').length).toEqual(0);
expect(fileList.findFileEl('Three.pdf.d33333').length).toEqual(0);
expect(fileList.findFileEl('somedir.d99999').length).toEqual(0);
expect(fileList.findFileEl('Two.jpg.d22222').length).toEqual(1);
}).then(done, done);
});
it('Restores all files when all selected when "Restore" clicked', function (done) {
var request;
$('.select-all').click();
var promise = fileList._onClickRestoreSelected({
preventDefault: function () {
}
});
var files = ["One.txt.d11111", "Two.jpg.d22222", "Three.pdf.d33333", "somedir.d99999"];
expect(fakeServer.requests.length).toEqual(files.length);
for (var i = 0; i < files.length; i++) {
request = fakeServer.requests[i];
expect(request.url).toEqual(OC.getRootPath() + '/remote.php/dav/trashbin/user/trash/' + files[i]);
expect(request.requestHeaders.Destination).toEqual(OC.getRootPath() + '/remote.php/dav/trashbin/user/restore/' + files[i]);
request.respond(200);
}
return promise.then(function() {
expect(fileList.isEmpty).toEqual(true);
}).then(done, done);
});
});
});
});

View File

@ -0,0 +1,252 @@
/**
* @copyright 2016 Vincent Petry <pvince81@owncloud.com>
*
* @author Daniel Calviño Sánchez <danxuliu@gmail.com>
* @author John Molakvoæ <skjnldsv@protonmail.com>
* @author Vincent Petry <vincent@nextcloud.com>
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* 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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
describe('OCA.SystemTags.SystemTagsInfoView tests', function() {
var isAdminStub;
var view;
var clock;
beforeEach(function() {
clock = sinon.useFakeTimers();
view = new OCA.SystemTags.SystemTagsInfoView();
$('#testArea').append(view.$el);
isAdminStub = sinon.stub(OC, 'isUserAdmin').returns(true);
});
afterEach(function() {
isAdminStub.restore();
clock.restore();
view.remove();
view = undefined;
});
describe('rendering', function() {
it('renders input field view', function() {
view.render();
expect(view.$el.find('input[name=tags]').length).toEqual(1);
});
it('fetches selected tags then renders when setting file info', function() {
var fetchStub = sinon.stub(OC.SystemTags.SystemTagsMappingCollection.prototype, 'fetch');
var setDataStub = sinon.stub(OC.SystemTags.SystemTagsInputField.prototype, 'setData');
expect(view.$el.hasClass('hidden')).toEqual(false);
view.setFileInfo({id: '123'});
expect(view.$el.find('input[name=tags]').length).toEqual(1);
expect(fetchStub.calledOnce).toEqual(true);
expect(view.selectedTagsCollection.url())
.toEqual(OC.linkToRemote('dav') + '/systemtags-relations/files/123');
view.selectedTagsCollection.add([
{id: '1', name: 'test1'},
{id: '3', name: 'test3'}
]);
fetchStub.yieldTo('success', view.selectedTagsCollection);
expect(setDataStub.calledOnce).toEqual(true);
expect(setDataStub.getCall(0).args[0]).toEqual([{
id: '1', name: 'test1', userVisible: true, userAssignable: true, canAssign: true
}, {
id: '3', name: 'test3', userVisible: true, userAssignable: true, canAssign: true
}]);
expect(view.$el.hasClass('hidden')).toEqual(false);
fetchStub.restore();
setDataStub.restore();
});
it('overrides initSelection to use the local collection', function() {
var inputViewSpy = sinon.spy(OC.SystemTags, 'SystemTagsInputField');
var element = $('<input type="hidden" val="1,3"/>');
view.remove();
view = new OCA.SystemTags.SystemTagsInfoView();
view.selectedTagsCollection.add([
{id: '1', name: 'test1'},
{id: '3', name: 'test3', userVisible: false, userAssignable: false, canAssign: false}
]);
var callback = sinon.stub();
inputViewSpy.getCall(0).args[0].initSelection(element, callback);
expect(callback.calledOnce).toEqual(true);
expect(callback.getCall(0).args[0]).toEqual([{
id: '1', name: 'test1', userVisible: true, userAssignable: true, canAssign: true
}, {
id: '3', name: 'test3', userVisible: false, userAssignable: false, canAssign: false
}]);
inputViewSpy.restore();
});
it('sets locked flag on non-assignable tags when user is not an admin', function() {
isAdminStub.returns(false);
var inputViewSpy = sinon.spy(OC.SystemTags, 'SystemTagsInputField');
var element = $('<input type="hidden" val="1,3"/>');
view.remove();
view = new OCA.SystemTags.SystemTagsInfoView();
view.selectedTagsCollection.add([
{id: '1', name: 'test1'},
{id: '3', name: 'test3', userAssignable: false, canAssign: false}
]);
var callback = sinon.stub();
inputViewSpy.getCall(0).args[0].initSelection(element, callback);
expect(callback.calledOnce).toEqual(true);
expect(callback.getCall(0).args[0]).toEqual([{
id: '1', name: 'test1', userVisible: true, userAssignable: true, canAssign: true
}, {
id: '3', name: 'test3', userVisible: true, userAssignable: false, canAssign: false, locked: true
}]);
inputViewSpy.restore();
});
it('does not set locked flag on non-assignable tags when canAssign overrides it with true', function() {
isAdminStub.returns(false);
var inputViewSpy = sinon.spy(OC.SystemTags, 'SystemTagsInputField');
var element = $('<input type="hidden" val="1,4"/>');
view.remove();
view = new OCA.SystemTags.SystemTagsInfoView();
view.selectedTagsCollection.add([
{id: '1', name: 'test1'},
{id: '4', name: 'test4', userAssignable: false, canAssign: true}
]);
var callback = sinon.stub();
inputViewSpy.getCall(0).args[0].initSelection(element, callback);
expect(callback.calledOnce).toEqual(true);
expect(callback.getCall(0).args[0]).toEqual([{
id: '1', name: 'test1', userVisible: true, userAssignable: true, canAssign: true
}, {
id: '4', name: 'test4', userVisible: true, userAssignable: false, canAssign: true
}]);
inputViewSpy.restore();
});
});
describe('events', function() {
var allTagsCollection;
beforeEach(function() {
allTagsCollection = view._inputView.collection;
allTagsCollection.add([
{id: '1', name: 'test1'},
{id: '2', name: 'test2'},
{id: '3', name: 'test3'}
]);
view.selectedTagsCollection.add([
{id: '1', name: 'test1'},
{id: '3', name: 'test3'}
]);
view.render();
});
it('renames model in selection collection on rename', function() {
allTagsCollection.get('3').set('name', 'test3_renamed');
expect(view.selectedTagsCollection.get('3').get('name')).toEqual('test3_renamed');
});
it('adds tag to selection collection when selected by input', function() {
var createStub = sinon.stub(OC.SystemTags.SystemTagsMappingCollection.prototype, 'create');
view._inputView.trigger('select', allTagsCollection.get('2'));
expect(createStub.calledOnce).toEqual(true);
expect(createStub.getCall(0).args[0]).toEqual({
id: '2',
name: 'test2',
userVisible: true,
userAssignable: true,
canAssign: true
});
createStub.restore();
});
it('removes tag from selection collection when deselected by input', function() {
var destroyStub = sinon.stub(OC.SystemTags.SystemTagModel.prototype, 'destroy');
view._inputView.trigger('deselect', '3');
expect(destroyStub.calledOnce).toEqual(true);
expect(destroyStub.calledOn(view.selectedTagsCollection.get('3'))).toEqual(true);
destroyStub.restore();
});
it('removes tag from selection whenever the tag was deleted globally', function() {
expect(view.selectedTagsCollection.get('3')).not.toBeFalsy();
allTagsCollection.remove('3');
expect(view.selectedTagsCollection.get('3')).toBeFalsy();
});
});
describe('visibility', function() {
it('reports visibility based on the "hidden" class name', function() {
view.$el.addClass('hidden');
expect(view.isVisible()).toBeFalsy();
view.$el.removeClass('hidden');
expect(view.isVisible()).toBeTruthy();
});
it('is visible after rendering', function() {
view.render();
expect(view.isVisible()).toBeTruthy();
});
it('shows and hides the element', function() {
view.show();
expect(view.isVisible()).toBeTruthy();
view.hide();
expect(view.isVisible()).toBeFalsy();
view.show();
expect(view.isVisible()).toBeTruthy();
});
});
describe('select2', function() {
var select2Stub;
beforeEach(function() {
select2Stub = sinon.stub($.fn, 'select2');
});
afterEach(function() {
select2Stub.restore();
});
it('opens dropdown', function() {
view.openDropdown();
expect(select2Stub.calledOnce).toBeTruthy();
expect(select2Stub.withArgs('open')).toBeTruthy();
});
});
});

View File

@ -0,0 +1,121 @@
<!--
- @copyright Copyright (c) 2020 Georg Ehrke <oc.list@georgehrke.com>
- @author Georg Ehrke <oc.list@georgehrke.com>
-
- @license GNU AGPL version 3 or any later version
-
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
-
- 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
- along with this program. If not, see <http://www.gnu.org/licenses/>.
-
-->
<template>
<NcDashboardWidget id="user-status_panel"
:items="items"
:loading="loading"
:empty-content-message="t('user_status', 'No recent status changes')">
<template #default="{ item }">
<NcDashboardWidgetItem :main-text="item.mainText"
:sub-text="item.subText">
<template #avatar>
<NcAvatar class="item-avatar"
:size="44"
:user="item.avatarUsername"
:display-name="item.mainText"
:show-user-status="false"
:show-user-status-compact="false" />
</template>
</NcDashboardWidgetItem>
</template>
<template #emptyContentIcon>
<div class="icon-user-status-dark" />
</template>
</NcDashboardWidget>
</template>
<script>
import { loadState } from '@nextcloud/initial-state'
import NcAvatar from '@nextcloud/vue/dist/Components/NcAvatar'
import NcDashboardWidget from '@nextcloud/vue/dist/Components/NcDashboardWidget'
import NcDashboardWidgetItem from '@nextcloud/vue/dist/Components/NcDashboardWidgetItem'
import moment from '@nextcloud/moment'
export default {
name: 'Dashboard',
components: {
NcAvatar,
NcDashboardWidget,
NcDashboardWidgetItem,
},
data() {
return {
statuses: [],
loading: true,
}
},
computed: {
items() {
return this.statuses.map((item) => {
const icon = item.icon || ''
let message = item.message || ''
if (message === '') {
if (item.status === 'away') {
message = t('user_status', 'Away')
}
if (item.status === 'dnd') {
message = t('user_status', 'Do not disturb')
}
}
const status = item.icon !== '' ? `${icon} ${message}` : message
let subText
if (item.icon === null && message === '' && item.timestamp === null) {
subText = ''
} else if (item.icon === null && message === '' && item.timestamp !== null) {
subText = moment(item.timestamp, 'X').fromNow()
} else if (item.timestamp !== null) {
subText = this.t('user_status', '{status}, {timestamp}', {
status,
timestamp: moment(item.timestamp, 'X').fromNow(),
}, null, { escape: false, sanitize: false })
} else {
subText = status
}
return {
mainText: item.displayName,
subText,
avatarUsername: item.userId,
}
})
},
},
mounted() {
try {
this.statuses = loadState('user_status', 'dashboard_data')
this.loading = false
} catch (e) {
console.error(e)
}
},
}
</script>
<style lang="scss">
.icon-user-status-dark {
width: 64px;
height: 64px;
background-size: 64px;
filter: var(--background-invert-if-dark);
}
</style>