commit a804efce670a9f7fcb0cadb31255943d847b98bd
parent 8f2356f7c325a1a75aeb7842ff05a7abc01124b6
Author: Dan Stillman <dstillman@zotero.org>
Date: Wed, 27 May 2015 22:18:40 -0400
Fix getAttachments()/getNotes() with no child items after save
Diffstat:
2 files changed, 93 insertions(+), 0 deletions(-)
diff --git 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
@@ -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';