From aa1fc01b310b2d4880e841f0eea45c23b42e1c7d Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Tue, 2 May 2017 20:15:54 -0400 Subject: [PATCH] Fix syncing of related item removal --- .../zotero/xpcom/data/dataObjectUtilities.js | 11 ++++-- test/tests/itemTest.js | 34 +++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/chrome/content/zotero/xpcom/data/dataObjectUtilities.js b/chrome/content/zotero/xpcom/data/dataObjectUtilities.js index 2fe5c7a384..8e5813b073 100644 --- a/chrome/content/zotero/xpcom/data/dataObjectUtilities.js +++ b/chrome/content/zotero/xpcom/data/dataObjectUtilities.js @@ -274,8 +274,13 @@ Zotero.DataObjectUtilities = { var pred2 = Object.keys(data2); pred2.sort(); if (!Zotero.Utilities.arrayEquals(pred1, pred2)) return true; - for (let i in pred1) { - if (!Zotero.Utilities.arrayEquals(pred1[i], pred2[i])) { + for (let pred in pred1) { + let vals1 = typeof data1[pred] == 'string' ? [data1[pred]] : data1[pred]; + let vals2 = (!data2[pred] || data2[pred] === '') + ? [] + : typeof data2[pred] == 'string' ? [data2[pred]] : data2[pred]; + + if (!Zotero.Utilities.arrayEquals(vals1, vals2)) { return true; } } @@ -561,7 +566,7 @@ Zotero.DataObjectUtilities = { } } for (let pred in data2) { - // Property in first object have already been handled + // Property in first object has already been handled if (data1[pred]) continue; let vals = typeof data2[pred] == 'string' ? [data2[pred]] : data2[pred]; diff --git a/test/tests/itemTest.js b/test/tests/itemTest.js index c915ac9a6e..643ee82879 100644 --- a/test/tests/itemTest.js +++ b/test/tests/itemTest.js @@ -1316,6 +1316,40 @@ describe("Zotero.Item", function () { assert.isFalse(json.parentItem); }); + it("should include relations if related item was removed", function* () { + var item1 = yield createDataObject('item'); + var item2 = yield createDataObject('item'); + var item3 = yield createDataObject('item'); + var item4 = yield createDataObject('item'); + + var relateItems = Zotero.Promise.coroutine(function* (i1, i2) { + yield Zotero.DB.executeTransaction(function* () { + i1.addRelatedItem(i2); + yield i1.save({ + skipDateModifiedUpdate: true + }); + i2.addRelatedItem(i1); + yield i2.save({ + skipDateModifiedUpdate: true + }); + }); + }); + + yield relateItems(item1, item2); + yield relateItems(item1, item3); + yield relateItems(item1, item4); + + var patchBase = item1.toJSON(); + + item1.removeRelatedItem(item2); + yield item1.saveTx(); + item2.removeRelatedItem(item1); + yield item2.saveTx(); + + var json = item1.toJSON({ patchBase }); + assert.sameMembers(json.relations['dc:relation'], item1.getRelations()['dc:relation']); + }); + it("shouldn't clear storage properties from original in .skipStorageProperties mode", function* () { var item = new Zotero.Item('attachment'); item.attachmentLinkMode = 'imported_file';