Assign merge-tracking relations properly when merging >2 items

https://forums.zotero.org/discussion/71459/incorrect-document-refresh-after-three-item-merge
This commit is contained in:
Dan Stillman 2018-04-15 17:05:23 -04:00
parent 8853f8ca47
commit 3835bc9110
2 changed files with 26 additions and 1 deletions

View file

@ -735,6 +735,8 @@ Zotero.Items = function() {
this.merge = function (item, otherItems) {
Zotero.debug("Merging items");
return Zotero.DB.executeTransaction(function* () {
var otherItemIDs = [];
var itemURI = Zotero.URI.getItemURI(item);
@ -758,7 +760,10 @@ Zotero.Items = function() {
}
// Add relations to master
item.setRelations(otherItem.getRelations());
let oldRelations = otherItem.getRelations();
for (let pred in oldRelations) {
oldRelations[pred].forEach(obj => item.addRelation(pred, obj));
}
// Remove merge-tracking relations from other item, so that there aren't two
// subjects for a given deleted object

View file

@ -171,6 +171,26 @@ describe("Zotero.Items", function () {
assert.equal(rels[0], item2URI);
})
it("should merge three items", async function () {
var item1 = await createDataObject('item');
var item2 = await createDataObject('item');
var item3 = await createDataObject('item');
var item2URI = Zotero.URI.getItemURI(item2);
var item3URI = Zotero.URI.getItemURI(item3);
await Zotero.Items.merge(item1, [item2, item3]);
assert.isFalse(item1.deleted);
assert.isTrue(item2.deleted);
assert.isTrue(item3.deleted);
// Check for merge-tracking relation
assert.isFalse(item1.hasChanged());
var rels = item1.getRelationsByPredicate(Zotero.Relations.replacedItemPredicate);
assert.lengthOf(rels, 2);
assert.sameMembers(rels, [item2URI, item3URI]);
})
it("should merge two items when servant is linked to an item absent from cache", function* () {
// two group libraries
var groupOneInfo = yield createGroup({