diff --git a/chrome/content/zotero/xpcom/server_connector.js b/chrome/content/zotero/xpcom/server_connector.js index d7587397f..b5b726304 100644 --- a/chrome/content/zotero/xpcom/server_connector.js +++ b/chrome/content/zotero/xpcom/server_connector.js @@ -458,9 +458,10 @@ Zotero.Server.Connector.SaveSnapshot.prototype = { } } else { - Zotero.HTTP.processDocuments(["zotero://connector/"+encodeURIComponent(data["url"])], - function(doc) { - delete Zotero.Server.Connector.Data[data["url"]]; + Zotero.HTTP.processDocuments( + ["zotero://connector/" + encodeURIComponent(data.url)], + Zotero.Promise.coroutine(function* (doc) { + delete Zotero.Server.Connector.Data[data.url]; try { // create new webpage item @@ -469,13 +470,14 @@ Zotero.Server.Connector.SaveSnapshot.prototype = { item.setField("title", doc.title); item.setField("url", data.url); item.setField("accessDate", "CURRENT_TIMESTAMP"); - var itemID = item.save(); - if(collection) collection.addItem(itemID); + if (collection) { + item.setCollections([collection.id]); + } + var itemID = yield item.saveTx(); // save snapshot if (filesEditable && !data.skipSnapshot) { - // TODO: async - Zotero.Attachments.importFromDocument({ + yield Zotero.Attachments.importFromDocument({ document: doc, parentItemID: itemID }); @@ -483,11 +485,14 @@ Zotero.Server.Connector.SaveSnapshot.prototype = { sendResponseCallback(201); } catch(e) { + Zotero.debug("ERROR"); + Zotero.debug(e); sendResponseCallback(500); throw e; } - }, - null, null, false, cookieSandbox); + }), + null, null, false, cookieSandbox + ); } } } diff --git a/test/tests/server_connectorTest.js b/test/tests/server_connectorTest.js index e9e025116..754b0b8ef 100644 --- a/test/tests/server_connectorTest.js +++ b/test/tests/server_connectorTest.js @@ -30,7 +30,7 @@ describe("Connector Server", function () { describe("/connector/saveItems", function () { // TODO: Test cookies - it("should save an item to the current selected collection", function* () { + it("should save a translated item to the current selected collection", function* () { var collection = yield createDataObject('collection'); yield waitForItemsLoad(win); @@ -98,4 +98,48 @@ describe("Connector Server", function () { yield waitForItemEvent('refresh'); }); }); + + describe("/connector/saveSnapshot", function () { + it("should save a webpage item and snapshot to the current selected collection", function* () { + var collection = yield createDataObject('collection'); + yield waitForItemsLoad(win); + + // saveSnapshot saves parent and child before returning + var ids1, ids2; + var promise = waitForItemEvent('add').then(function (ids) { + ids1 = ids; + return waitForItemEvent('add').then(function (ids) { + ids2 = ids; + }); + }); + yield Zotero.HTTP.request( + 'POST', + connectorServerPath + "/connector/saveSnapshot", + { + headers: { + "Content-Type": "application/json" + }, + body: JSON.stringify({ + url: "http://example.com", + html: "TitleBody" + }) + } + ); + + assert.isTrue(promise.isFulfilled()); + + // Check parent item + assert.lengthOf(ids1, 1); + var item = Zotero.Items.get(ids1[0]); + assert.equal(Zotero.ItemTypes.getName(item.itemTypeID), 'webpage'); + assert.isTrue(collection.hasItem(item.id)); + assert.equal(item.getField('title'), 'Title'); + + // Check attachment + assert.lengthOf(ids2, 1); + item = Zotero.Items.get(ids2[0]); + assert.isTrue(item.isImportedAttachment()); + assert.equal(item.getField('title'), 'Title'); + }); + }); });