commit 195a7370490012e013087b6dac53d0750fb7450f
parent da627e137ab76f434112ead06919bfdd5496a0b7
Author: Dan Stillman <dstillman@zotero.org>
Date: Thu, 4 Jun 2015 19:01:18 -0400
Allow item.addRelatedItem() to work on unsaved item without libraryID
Production code should just assign a libraryID, but this is useful for
tests, and addCollection() does it.
Diffstat:
2 files changed, 19 insertions(+), 2 deletions(-)
diff --git 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
@@ -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 });