Don't allow items to be set as their own parents, and correct existing

This commit is contained in:
Dan Stillman 2020-10-14 19:29:02 -04:00
parent 6b626ba992
commit 828ec4010e
4 changed files with 32 additions and 1 deletions

View file

@ -1387,6 +1387,10 @@ Zotero.Item.prototype._saveData = Zotero.Promise.coroutine(function* (env) {
: null; : null;
if (this._changed.parentKey) { if (this._changed.parentKey) {
if (isNew) { if (isNew) {
if (parentItemKey == this.key) {
throw new Error("Item cannot be set as parent of itself");
}
if (!parentItemID) { if (!parentItemID) {
// TODO: clear caches? // TODO: clear caches?
let msg = "Parent item " + this.libraryID + "/" + parentItemKey + " not found"; let msg = "Parent item " + this.libraryID + "/" + parentItemKey + " not found";
@ -1412,6 +1416,10 @@ Zotero.Item.prototype._saveData = Zotero.Promise.coroutine(function* (env) {
} }
else { else {
if (parentItemKey) { if (parentItemKey) {
if (parentItemKey == this.key) {
throw new Error("Item cannot be set as parent of itself");
}
if (!parentItemID) { if (!parentItemID) {
// TODO: clear caches // TODO: clear caches
let msg = "Parent item " + this.libraryID + "/" + parentItemKey + " not found"; let msg = "Parent item " + this.libraryID + "/" + parentItemKey + " not found";

View file

@ -3069,6 +3069,11 @@ Zotero.Schema = new function(){
yield Zotero.DB.queryAsync("CREATE TABLE dbDebug1 (\n a INTEGER PRIMARY KEY\n)"); yield Zotero.DB.queryAsync("CREATE TABLE dbDebug1 (\n a INTEGER PRIMARY KEY\n)");
} }
else if (i == 110) {
yield Zotero.DB.queryAsync("UPDATE itemAttachments SET parentItemID=NULL WHERE itemID=parentItemID");
yield Zotero.DB.queryAsync("UPDATE itemNotes SET parentItemID=NULL WHERE itemID=parentItemID");
}
// If breaking compatibility or doing anything dangerous, clear minorUpdateFrom // If breaking compatibility or doing anything dangerous, clear minorUpdateFrom
} }

View file

@ -1,4 +1,4 @@
-- 109 -- 110
-- Copyright (c) 2009 Center for History and New Media -- Copyright (c) 2009 Center for History and New Media
-- George Mason University, Fairfax, Virginia, USA -- George Mason University, Fairfax, Virginia, USA

View file

@ -407,6 +407,14 @@ describe("Zotero.Item", function () {
assert.ok(item.parentID); assert.ok(item.parentID);
assert.equal(item.parentID, parentItemID); assert.equal(item.parentID, parentItemID);
}); });
it("should not be settable to item itself", async function () {
var item = await createDataObject('item', { itemType: 'note' });
item.parentID = item.id;
var e = await getPromiseError(item.saveTx());
assert.ok(e);
assert.equal(e.message, "Item cannot be set as parent of itself");
});
}); });
describe("#parentKey", function () { describe("#parentKey", function () {
@ -485,6 +493,16 @@ describe("Zotero.Item", function () {
assert.isFalse(noteItem.isTopLevelItem()); assert.isFalse(noteItem.isTopLevelItem());
}) })
it("should not be settable to item itself", async function () {
var item = new Zotero.Item('note');
item.libraryID = Zotero.Libraries.userLibraryID;
item.key = Zotero.DataObjectUtilities.generateKey();
item.parentKey = item.key;
var e = await getPromiseError(item.saveTx());
assert.ok(e);
assert.equal(e.message, "Item cannot be set as parent of itself");
});
}); });
describe("#getCreators()", function () { describe("#getCreators()", function () {