From 07df7d0decf99288f0a1fbae41f777a0a7eb0a31 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Thu, 25 Nov 2021 00:21:32 -0500 Subject: [PATCH] Fix standalone notes not being added to collections on RDF import This includes the `Zotero.RDF.getResourceURI()` fix in zotero/translate@85b39a5be. This position change here for `this._handleRelated()` for attachments isn't necessary, since the attachment is already saved and already has an id, but it's done for consistency with the call for notes (where the note previously didn't yet have an id). --- chrome/content/zotero/xpcom/translate | 2 +- .../xpcom/translation/translate_item.js | 6 ++- test/tests/translateTest.js | 47 ++++++++++++++++--- 3 files changed, 46 insertions(+), 9 deletions(-) diff --git a/chrome/content/zotero/xpcom/translate b/chrome/content/zotero/xpcom/translate index a9308c0e86..85b39a5be5 160000 --- a/chrome/content/zotero/xpcom/translate +++ b/chrome/content/zotero/xpcom/translate @@ -1 +1 @@ -Subproject commit a9308c0e8632846ca2dc069a1b72db0a33f99ca6 +Subproject commit 85b39a5be5bb59c5ee5e7447609273a29ea0b799 diff --git a/chrome/content/zotero/xpcom/translation/translate_item.js b/chrome/content/zotero/xpcom/translation/translate_item.js index 93728c1484..ee52e86528 100644 --- a/chrome/content/zotero/xpcom/translation/translate_item.js +++ b/chrome/content/zotero/xpcom/translation/translate_item.js @@ -552,8 +552,8 @@ Zotero.Translate.ItemSaver.prototype = { if (attachment.accessDate) newAttachment.setField("accessDate", attachment.accessDate); if (attachment.tags) newAttachment.setTags(this._cleanTags(attachment.tags)); if (attachment.note) newAttachment.setNote(attachment.note); - this._handleRelated(attachment, newAttachment); yield newAttachment.saveTx(this._saveOptions); + this._handleRelated(attachment, newAttachment); Zotero.debug("Translate: Created attachment; id is " + newAttachment.id, 4); attachmentCallback(attachment, 100); @@ -941,7 +941,6 @@ Zotero.Translate.ItemSaver.prototype = { if(typeof note == "object") { myNote.setNote(note.note); if(note.tags) myNote.setTags(this._cleanTags(note.tags)); - this._handleRelated(note, myNote); } else { myNote.setNote(note); } @@ -949,6 +948,9 @@ Zotero.Translate.ItemSaver.prototype = { myNote.setCollections(this._collections); } yield myNote.save(this._saveOptions); + if (typeof note == "object") { + this._handleRelated(note, myNote); + } return myNote; }), diff --git a/test/tests/translateTest.js b/test/tests/translateTest.js index 2918e047e1..ea83ab985a 100644 --- a/test/tests/translateTest.js +++ b/test/tests/translateTest.js @@ -1176,23 +1176,46 @@ describe("Zotero.Translate", function() { var c3 = await createDataObject('collection', { name: '3', parentID: c2.id }); var c4 = await createDataObject('collection', { name: '4', parentID: c3.id }); var c5 = await createDataObject('collection', { name: '5', parentID: c4.id }); - var item = await createDataObject('item', { collections: [c5.id] }); + // Add item, standalone note, and standalone attachment to collection + var item = await createDataObject( + 'item', + { collections: [c5.id], title: Zotero.Utilities.randomString() } + ); + var note = await createDataObject( + 'item', + { itemType: 'note', collections: [c5.id], note: Zotero.Utilities.randomString() } + ); + var attachment = await importFileAttachment('test.pdf', { + url: 'https://example.com/test.pdf', + title: Zotero.Utilities.randomString(), + collections: [c5.id] + }); var tmpDir = await getTempDirectory(); - var libraryExportFile = OS.Path.join(tmpDir, 'export-library.rdf'); - var collectionExportFile = OS.Path.join(tmpDir, 'export-collection.rdf'); + var libraryExportDir = OS.Path.join(tmpDir, 'export-library'); + var libraryExportFile = OS.Path.join(libraryExportDir, 'export-library.rdf'); + var collectionExportDir = OS.Path.join(tmpDir, 'export-collection'); + var collectionExportFile = OS.Path.join(collectionExportDir, 'export-collection.rdf'); // Export library var translation = new Zotero.Translate.Export(); - translation.setLocation(Zotero.File.pathToFile(libraryExportFile)); + translation.setLocation(Zotero.File.pathToFile(libraryExportDir)); translation.setLibraryID(Zotero.Libraries.userLibraryID); + translation.setDisplayOptions({ + exportFileData: true, + exportNotes: true + }); translation.setTranslator('14763d24-8ba0-45df-8f52-b8d1108e7ac9'); // Zotero RDF await translation.translate(); // Export top-most collection translation = new Zotero.Translate.Export(); - translation.setLocation(Zotero.File.pathToFile(collectionExportFile)); + translation.setLocation(Zotero.File.pathToFile(collectionExportDir)); translation.setCollection(c1); + translation.setDisplayOptions({ + exportFileData: true, + exportNotes: true + }); translation.setTranslator('14763d24-8ba0-45df-8f52-b8d1108e7ac9'); // Zotero RDF await translation.translate(); @@ -1223,7 +1246,19 @@ describe("Zotero.Translate", function() { while (name = collectionNames.shift()) { collections = collections[0].getChildCollections(); assert.lengthOf(collections, 1, mode); - assert.equal(collections[0].name, name, mode) + let c = collections[0]; + assert.equal(c.name, name, mode); + + // Get the collection we imported items into + if (name == c5.name) { + // Make sure items were imported and added to collection + let titles = c.getChildItems().map(x => x.getDisplayTitle()); + assert.sameMembers(titles, [item, note, attachment].map(x => x.getDisplayTitle())); + } + // Other collections should be empty + else { + assert.lengthOf(c.getChildItems(), 0); + } } }