From e23452363b8d068d76ca8c0f12d777824b072a4e Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Thu, 26 Jan 2017 15:02:58 -0500 Subject: [PATCH] Handle auto-hyphenated ISBN during item conflict Always keep the hyphenated version, and don't consider it a visible conflict --- chrome/content/zotero/xpcom/sync/syncLocal.js | 37 ++++++++- test/tests/syncLocalTest.js | 82 +++++++++++++++++++ 2 files changed, 116 insertions(+), 3 deletions(-) diff --git a/chrome/content/zotero/xpcom/sync/syncLocal.js b/chrome/content/zotero/xpcom/sync/syncLocal.js index fa59466075..f37a10b4e5 100644 --- a/chrome/content/zotero/xpcom/sync/syncLocal.js +++ b/chrome/content/zotero/xpcom/sync/syncLocal.js @@ -838,7 +838,9 @@ Zotero.Sync.Data.Local = { jsonObject, { skipData: true, - notifierQueue + notifierQueue, + // Save as unsynced + saveAsChanged: !!result.localChanged } ); results.push(saveResults); @@ -884,6 +886,11 @@ Zotero.Sync.Data.Local = { jsonDataLocal[x] = jsonData[x]; }) jsonObject.data = jsonDataLocal; + + // Save as unsynced + if (results.localChanged) { + saveOptions.saveAsChanged = true; + } } } // Object doesn't exist locally @@ -1160,7 +1167,7 @@ Zotero.Sync.Data.Local = { let saveOptions = {}; Object.assign(saveOptions, options); - // Tell _saveObjectFromJSON to save as unsynced + // Tell _saveObjectFromJSON() to save as unsynced saveOptions.saveAsChanged = true; saveOptions.notifierQueue = notifierQueue; @@ -1533,7 +1540,31 @@ Zotero.Sync.Data.Local = { conflicts.push([c1, c2]); } - return { changes, conflicts }; + var localChanged = false; + + // Massage some old data + conflicts = conflicts.filter((x) => { + // If one side has auto-hyphenated ISBN, use that + if (x[0].field == 'ISBN' && x[0].op == 'add' && x[1].op == 'add') { + let hyphenatedA = Zotero.Utilities.Internal.hyphenateISBN(x[0].value); + let hyphenatedB = Zotero.Utilities.Internal.hyphenateISBN(x[1].value); + if (hyphenatedA && hyphenatedB) { + // Use remote + if (hyphenatedA == x[1].value) { + changes.push(x[1]); + return false; + } + // Use local + else if (x[0].value == hyphenatedB) { + localChanged = true; + return false; + } + } + return true; + } + }); + + return { changes, conflicts, localChanged }; }, diff --git a/test/tests/syncLocalTest.js b/test/tests/syncLocalTest.js index 598ca747f2..961bc88be0 100644 --- a/test/tests/syncLocalTest.js +++ b/test/tests/syncLocalTest.js @@ -439,6 +439,35 @@ describe("Zotero.Sync.Data.Local", function() { assert.equal(obj.getField('place'), changedPlace); }) + it("should save item with overriding local conflict as unsynced", function* () { + var libraryID = Zotero.Libraries.userLibraryID; + + var isbn = '978-0-335-22006-9'; + var type = 'item'; + let obj = createUnsavedDataObject(type, { version: 5 }); + obj.setField('ISBN', isbn); + yield obj.saveTx(); + let data = obj.toJSON(); + + data.key = obj.key; + data.version = 10; + data.ISBN = '9780335220069'; + let json = { + key: obj.key, + version: 10, + data + }; + var results = yield Zotero.Sync.Data.Local.processObjectsFromJSON( + type, libraryID, [json], { stopOnError: true } + ); + assert.isTrue(results[0].processed); + assert.isUndefined(results[0].changes); + assert.isUndefined(results[0].conflicts); + assert.equal(obj.version, 10); + assert.equal(obj.getField('ISBN'), isbn); + assert.isFalse(obj.synced); + }); + it("should delete older versions in sync cache after processing", function* () { var libraryID = Zotero.Libraries.userLibraryID; @@ -1835,5 +1864,58 @@ describe("Zotero.Sync.Data.Local", function() { ] ); }) + + it("should automatically use local hyphenated ISBN value if only difference", function () { + var json1 = { + key: "AAAAAAAA", + version: 1234, + itemType: "book", + ISBN: "978-0-335-22006-9" + }; + var json2 = { + key: "AAAAAAAA", + version: 1235, + itemType: "book", + ISBN: "9780335220069" + }; + var ignoreFields = ['dateAdded', 'dateModified']; + var result = Zotero.Sync.Data.Local._reconcileChangesWithoutCache( + 'item', json1, json2, ignoreFields + ); + assert.lengthOf(result.changes, 0); + assert.lengthOf(result.conflicts, 0); + assert.isTrue(result.localChanged); + }); + + it("should automatically use remote hyphenated ISBN value if only difference", function () { + var json1 = { + key: "AAAAAAAA", + version: 1234, + itemType: "book", + ISBN: "9780335220069" + }; + var json2 = { + key: "AAAAAAAA", + version: 1235, + itemType: "book", + ISBN: "978-0-335-22006-9" + }; + var ignoreFields = ['dateAdded', 'dateModified']; + var result = Zotero.Sync.Data.Local._reconcileChangesWithoutCache( + 'item', json1, json2, ignoreFields + ); + assert.sameDeepMembers( + result.changes, + [ + { + field: "ISBN", + op: "add", + value: "978-0-335-22006-9" + } + ] + ); + assert.lengthOf(result.conflicts, 0); + assert.isFalse(result.localChanged); + }); }) })