diff --git a/changelog/unreleased/bugfix-renaming-favorites-issue b/changelog/unreleased/bugfix-renaming-favorites-issue new file mode 100644 index 00000000000..7475bb94475 --- /dev/null +++ b/changelog/unreleased/bugfix-renaming-favorites-issue @@ -0,0 +1,7 @@ +Bugfix: Rename a file in favorites list with same name but in different folder + +We've fixed a bug, where renaming a file in the favorites file list to a file with the same name but located +in a different folder was not possible, as the message `The name "..." is already taken` appeared. + +https://github.com/owncloud/web/pull/6804 +https://github.com/owncloud/web/issues/1750 diff --git a/packages/web-app-files/src/helpers/resources.ts b/packages/web-app-files/src/helpers/resources.ts index 667d296ec4e..43b08345a1e 100644 --- a/packages/web-app-files/src/helpers/resources.ts +++ b/packages/web-app-files/src/helpers/resources.ts @@ -25,7 +25,7 @@ export function renameResource(resource, newName, newPath) { } resource.name = newName - resource.path = resourcePath + resource.path = '/' + resourcePath resource.webDavPath = '/' + newPath + newName resource.extension = extractExtensionFromFile(resource) diff --git a/packages/web-app-files/src/mixins/actions/rename.js b/packages/web-app-files/src/mixins/actions/rename.js index 7e4fe8f617d..5fcbf15b121 100644 --- a/packages/web-app-files/src/mixins/actions/rename.js +++ b/packages/web-app-files/src/mixins/actions/rename.js @@ -79,7 +79,7 @@ export default { if (!this.areFileExtensionsShown) { newName = `${newName}.${resources[0].extension}` } - this.$_rename_checkNewName(resources[0].name, newName, parentResources) + this.$_rename_checkNewName(resources[0], newName, parentResources) } const nameWithoutExtension = extractNameWithoutExtension(resources[0]) const modalTitle = @@ -123,7 +123,10 @@ export default { this.createModal(modal) }, - $_rename_checkNewName(currentName, newName, parentResources) { + $_rename_checkNewName(resource, newName, parentResources) { + const newPath = + resource.path.substring(0, resource.path.length - resource.name.length) + newName + if (!newName) { return this.setModalInputErrorMessage(this.$gettext('The name cannot be empty')) } @@ -145,7 +148,7 @@ export default { } if (!this.flatFileList) { - const exists = this.files.find((file) => file.name === newName && currentName !== newName) + const exists = this.files.find((file) => file.path === newPath && resource.name !== newName) if (exists) { const translated = this.$gettext('The name "%{name}" is already taken') @@ -158,7 +161,7 @@ export default { if (parentResources) { const exists = parentResources.find( - (file) => file.name === newName && currentName !== newName + (file) => file.path === newPath && resource.name !== newName ) if (exists) { diff --git a/packages/web-app-files/tests/unit/mixins/actions/rename.spec.js b/packages/web-app-files/tests/unit/mixins/actions/rename.spec.js index 66edcdfe6d1..dbb95acd4f6 100644 --- a/packages/web-app-files/tests/unit/mixins/actions/rename.spec.js +++ b/packages/web-app-files/tests/unit/mixins/actions/rename.spec.js @@ -47,7 +47,17 @@ describe('rename', () => { it('should not show an error with a valid name', () => { const wrapper = getWrapper() const spyErrorMessageStub = jest.spyOn(wrapper.vm, 'setModalInputErrorMessage') - wrapper.vm.$_rename_checkNewName('currentName', 'newName') + wrapper.vm.$_rename_checkNewName({ name: 'currentName', path: '/currentName' }, 'newName') + expect(spyErrorMessageStub).toHaveBeenCalledWith(null) + }) + + it('should not show an error if resource name already exists but in different folder', () => { + const wrapper = getWrapper() + const spyErrorMessageStub = jest.spyOn(wrapper.vm, 'setModalInputErrorMessage') + wrapper.vm.$_rename_checkNewName( + { name: 'currentName', path: '/favorites/currentName' }, + 'file1' + ) expect(spyErrorMessageStub).toHaveBeenCalledWith(null) }) @@ -69,14 +79,14 @@ describe('rename', () => { { currentName: 'currentName', newName: 'newname', - parentResources: [{ name: 'newname' }], + parentResources: [{ name: 'newname', path: '/newname' }], message: 'The name "%{name}" is already taken' } ])('should detect name errors and display error messages accordingly', (inputData) => { const wrapper = getWrapper() const spyGetTextStub = jest.spyOn(wrapper.vm, '$gettext') wrapper.vm.$_rename_checkNewName( - inputData.currentName, + { name: inputData.currentName, path: `/${inputData.currentName}` }, inputData.newName, inputData.parentResources ) @@ -142,7 +152,9 @@ function getWrapper(renameFilePromise) { return { href: r.name } } }, - $client: { files: { find: jest.fn(() => [{ name: 'file1' }]), list: jest.fn(() => []) } }, + $client: { + files: { find: jest.fn(() => [{ name: 'file1', path: '/file1' }]), list: jest.fn(() => []) } + }, $gettextInterpolate: jest.fn(), $gettext: jest.fn(), publicPage: () => false, @@ -154,7 +166,7 @@ function getWrapper(renameFilePromise) { namespaced: true, getters: { currentFolder: () => currentFolder, - files: () => [{ name: 'file1' }] + files: () => [{ name: 'file1', path: '/file1' }] }, actions: { renameFile: jest.fn(() => {