From a804efce670a9f7fcb0cadb31255943d847b98bd Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Wed, 27 May 2015 22:18:40 -0400 Subject: [PATCH] Fix getAttachments()/getNotes() with no child items after save --- chrome/content/zotero/xpcom/data/item.js | 8 +++ test/tests/itemTest.js | 85 ++++++++++++++++++++++++ 2 files changed, 93 insertions(+) diff --git a/chrome/content/zotero/xpcom/data/item.js b/chrome/content/zotero/xpcom/data/item.js index 2a0ae84ed..49b243f61 100644 --- a/chrome/content/zotero/xpcom/data/item.js +++ b/chrome/content/zotero/xpcom/data/item.js @@ -1970,6 +1970,10 @@ Zotero.Item.prototype.getNotes = function(includeTrashed) { this._requireData('childItems'); + if (!this._notes) { + return []; + } + var sortChronologically = Zotero.Prefs.get('sortNotesChronologically'); var cacheKey = (sortChronologically ? "chronological" : "alphabetical") + 'With' + (includeTrashed ? '' : 'out') + 'Trashed'; @@ -3112,6 +3116,10 @@ Zotero.Item.prototype.getAttachments = function(includeTrashed) { this._requireData('childItems'); + if (!this._attachments) { + return []; + } + var cacheKey = (Zotero.Prefs.get('sortAttachmentsChronologically') ? 'chronological' : 'alphabetical') + 'With' + (includeTrashed ? '' : 'out') + 'Trashed'; diff --git a/test/tests/itemTest.js b/test/tests/itemTest.js index 14d3f8181..c77ef9da4 100644 --- a/test/tests/itemTest.js +++ b/test/tests/itemTest.js @@ -311,6 +311,91 @@ describe("Zotero.Item", function () { }) }) + describe("#getAttachments()", function () { + it("#should return child attachments", function* () { + var item = yield createDataObject('item'); + var attachment = new Zotero.Item("attachment"); + attachment.parentID = item.id; + attachment.attachmentLinkMode = Zotero.Attachments.LINK_MODE_IMPORTED_FILE; + yield attachment.saveTx(); + + var attachments = item.getAttachments(); + assert.lengthOf(attachments, 1); + assert.equal(attachments[0], attachment.id); + }) + + it("#should ignore trashed child attachments by default", function* () { + var item = yield createDataObject('item'); + var attachment = new Zotero.Item("attachment"); + attachment.parentID = item.id; + attachment.attachmentLinkMode = Zotero.Attachments.LINK_MODE_IMPORTED_FILE; + attachment.deleted = true; + yield attachment.saveTx(); + + var attachments = item.getAttachments(); + assert.lengthOf(attachments, 0); + }) + + it("#should include trashed child attachments if includeTrashed=true", function* () { + var item = yield createDataObject('item'); + var attachment = new Zotero.Item("attachment"); + attachment.parentID = item.id; + attachment.attachmentLinkMode = Zotero.Attachments.LINK_MODE_IMPORTED_FILE; + attachment.deleted = true; + yield attachment.saveTx(); + + var attachments = item.getAttachments(true); + assert.lengthOf(attachments, 1); + assert.equal(attachments[0], attachment.id); + }) + + it("#should return an empty array for an item with no attachments", function* () { + var item = yield createDataObject('item'); + assert.lengthOf(item.getAttachments(), 0); + }) + }) + + describe("#getNotes()", function () { + it("#should return child notes", function* () { + var item = yield createDataObject('item'); + var note = new Zotero.Item("note"); + note.parentID = item.id; + yield note.saveTx(); + + var notes = item.getNotes(); + assert.lengthOf(notes, 1); + assert.equal(notes[0], note.id); + }) + + it("#should ignore trashed child notes by default", function* () { + var item = yield createDataObject('item'); + var note = new Zotero.Item("note"); + note.parentID = item.id; + note.deleted = true; + yield note.saveTx(); + + var notes = item.getNotes(); + assert.lengthOf(notes, 0); + }) + + it("#should include trashed child notes if includeTrashed=true", function* () { + var item = yield createDataObject('item'); + var note = new Zotero.Item("note"); + note.parentID = item.id; + note.deleted = true; + yield note.saveTx(); + + var notes = item.getNotes(true); + assert.lengthOf(notes, 1); + assert.equal(notes[0], note.id); + }) + + it("#should return an empty array for an item with no notes", function* () { + var item = yield createDataObject('item'); + assert.lengthOf(item.getNotes(), 0); + }) + }) + describe("#attachmentCharset", function () { it("should get and set a value", function* () { var charset = 'utf-8';