Fix faulty siblings-of-type check in setAutoAttachmentTitle() (#5045)

This commit is contained in:
Abe Jellinek 2025-02-12 22:29:49 -05:00 • committed by Dan Stillman
parent 184671f919
commit 264f55545a
3 changed files with 43 additions and 7 deletions

View file

@ -2556,12 +2556,15 @@ Zotero.Item.prototype.numNonHTMLFileAttachments = function () {
};
Zotero.Item.prototype.numFileAttachmentsWithContentType = function (contentType) {
Zotero.Item.prototype.getFileAttachmentsWithContentType = function (contentType) {
this._requireData('childItems');
return this.getAttachments()
.map(itemID => Zotero.Items.get(itemID))
.filter(item => item.isFileAttachment() && item.attachmentContentType == contentType)
.length;
return Zotero.Items.get(this.getAttachments())
.filter(item => item.isFileAttachment() && item.attachmentContentType == contentType);
};
Zotero.Item.prototype.numFileAttachmentsWithContentType = function (contentType) {
return this.getFileAttachmentsWithContentType(contentType).length;
};
@ -3974,7 +3977,8 @@ Zotero.Item.prototype.setAutoAttachmentTitle = function ({ ignoreAutoRenamePrefs
// If this is the only attachment of its type on the parent item and the
// file is being renamed, give it a default title ("PDF", "Webpage", etc.)
let isFirstOfType = this.parentItemID
&& this.parentItem.numFileAttachmentsWithContentType(this.attachmentContentType) <= 1;
&& this.parentItem.getFileAttachmentsWithContentType(this.attachmentContentType)
.every(item => item === this);
let isBeingRenamed = ignoreAutoRenamePrefs || Zotero.Attachments.shouldAutoRenameAttachment(this);
if (isFirstOfType && isBeingRenamed) {
let defaultTitle = this._getDefaultTitleForAttachmentContentType();

View file

@ -4516,7 +4516,7 @@ var ZoteroPane = new function()
/**
* @param {Boolean} [link]
* @param {Number} [parentItemID]
* @param {nsIFile[]} [files] Used instead of showing a file picker - for tests
* @param {String[]} [files] Used instead of showing a file picker - for tests
* @returns {Promise<Zotero.Item[] | null>}
*/
this.addAttachmentFromDialog = async function (link, parentItemID, files = null) {

View file

@ -1692,6 +1692,38 @@ describe("ZoteroPane", function() {
assert.equal(parentItem.getAttachments().length, 4);
assert.equal(epubAttachment.getField('title'), Zotero.getString('file-type-ebook'));
});
describe("Linked file renaming", function () {
before(() => {
Zotero.Prefs.set('autoRenameFiles.linked', true);
});
after(() => {
Zotero.Prefs.clear('autoRenameFiles.linked');
});
it("should only rename and change the title of the first PDF attachment", async function () {
let testFile = getTestDataDirectory();
testFile.append('test.pdf');
let tempDir = await getTempDirectory();
let copy1 = PathUtils.join(tempDir, 'copy1.pdf');
let copy2 = PathUtils.join(tempDir, 'copy2.pdf');
await IOUtils.copy(testFile.path, copy1);
await IOUtils.copy(testFile.path, copy2);
let parentItem = await createDataObject('item', { title: 'Foo' });
let [attachment1] = await zp.addAttachmentFromDialog(false, parentItem.id, [copy1]);
assert.equal(attachment1.getField('title'), Zotero.getString('file-type-pdf'));
assert.equal(attachment1.attachmentFilename, 'Foo.pdf');
let [attachment2] = await zp.addAttachmentFromDialog(false, parentItem.id, [copy2]);
assert.equal(attachment2.getField('title'), 'copy2');
assert.equal(attachment2.attachmentFilename, 'copy2.pdf');
});
});
});
describe("#createParentItemsFromSelected()", function () {