diff --git a/chrome/content/zotero/xpcom/data/item.js b/chrome/content/zotero/xpcom/data/item.js index 7acfdeb93..e07635c8b 100644 --- a/chrome/content/zotero/xpcom/data/item.js +++ b/chrome/content/zotero/xpcom/data/item.js @@ -1035,6 +1035,10 @@ Zotero.Item.prototype.addRelatedItem = function (item) { return false; } + if (!this.libraryID) { + this.libraryID = Zotero.Libraries.userLibraryID; + } + if (item.libraryID != this.libraryID) { throw new Error("Cannot relate item to an item in a different library"); } diff --git a/test/tests/itemTest.js b/test/tests/itemTest.js index b55ec90e5..ad2e359de 100644 --- a/test/tests/itemTest.js +++ b/test/tests/itemTest.js @@ -1,3 +1,5 @@ +"use strict"; + describe("Zotero.Item", function () { describe("#getField()", function () { it("should return an empty string for valid unset fields on unsaved items", function () { @@ -642,7 +644,7 @@ describe("Zotero.Item", function () { // Relations and related items // describe("#addRelatedItem", function () { - it("#should add a dc:relation relation to an item", function* () { + it("should add a dc:relation relation to an item", function* () { var item1 = yield createDataObject('item'); var item2 = yield createDataObject('item'); item1.addRelatedItem(item2); @@ -653,7 +655,18 @@ describe("Zotero.Item", function () { assert.equal(rels[0], Zotero.URI.getItemURI(item2)); }) - it("#should throw an error for a relation in a different library", function* () { + it("should allow an unsaved item to be related to an item in the user library", function* () { + var item1 = yield createDataObject('item'); + var item2 = createUnsavedDataObject('item'); + item2.addRelatedItem(item1); + yield item2.saveTx(); + + var rels = item2.getRelationsByPredicate(Zotero.Relations.relatedItemPredicate); + assert.lengthOf(rels, 1); + assert.equal(rels[0], Zotero.URI.getItemURI(item1)); + }) + + it("should throw an error for a relation in a different library", function* () { var group = yield getGroup(); var item1 = yield createDataObject('item'); var item2 = yield createDataObject('item', { libraryID: group.libraryID });