Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Take path into account while renaming a file #6804

Merged
merged 6 commits into from
Apr 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions changelog/unreleased/bugfix-renaming-favorites-issue
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion packages/web-app-files/src/helpers/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
11 changes: 7 additions & 4 deletions packages/web-app-files/src/mixins/actions/rename.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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'))
}
Expand All @@ -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')
Expand All @@ -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) {
Expand Down
22 changes: 17 additions & 5 deletions packages/web-app-files/tests/unit/mixins/actions/rename.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

Expand All @@ -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
)
Expand Down Expand Up @@ -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,
Expand All @@ -154,7 +166,7 @@ function getWrapper(renameFilePromise) {
namespaced: true,
getters: {
currentFolder: () => currentFolder,
files: () => [{ name: 'file1' }]
files: () => [{ name: 'file1', path: '/file1' }]
},
actions: {
renameFile: jest.fn(() => {
Expand Down