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.
This commit is contained in:
Dan Stillman 2015-06-04 19:01:18 -04:00
parent da627e137a
commit 195a737049
2 changed files with 19 additions and 2 deletions

View file

@ -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");
}

View file

@ -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 });