new function() { Components.utils.import("resource://gre/modules/osfile.jsm"); /** * Create a new translator that saves the specified items * @param {String} translatorType - "import" or "web" * @param {Object} items - items as translator JSON */ function saveItemsThroughTranslator(translatorType, items) { let tyname; if (translatorType == "web") { tyname = "Web"; } else if (translatorType == "import") { tyname = "Import"; } else { throw new Error("invalid translator type "+translatorType); } let translate = new Zotero.Translate[tyname](); let browser; if (translatorType == "web") { browser = Zotero.Browser.createHiddenBrowser(); translate.setDocument(browser.contentDocument); } else if (translatorType == "import") { translate.setString(""); } translate.setTranslator(buildDummyTranslator( translatorType, "function detectWeb() {}\n"+ "function do"+tyname+"() {\n"+ " var json = JSON.parse('"+JSON.stringify(items).replace(/['\\]/g, "\\$&")+"');\n"+ " for (var i=0; i c.id)); }); }); describe("#_saveAttachment()", function () { it("should save standalone attachment to collection", function* () { var collection = yield createDataObject('collection'); var items = [ { itemType: "attachment", title: "Test", mimeType: "text/html", url: "http://example.com" } ]; var translation = new Zotero.Translate.Import(); translation.setString(""); translation.setTranslator(buildDummyTranslator( "import", "function detectImport() {}\n" + "function doImport() {\n" + " var json = JSON.parse('" + JSON.stringify(items).replace(/['\\]/g, "\\$&") + "');\n" + " for (var i=0; i Zotero.URI.getItemURI(i)); }); getter._itemsLeft = items; assert.equal((getter.nextItem()).uri, itemURIs[0], 'first item comes out first'); assert.equal((getter.nextItem()).uri, itemURIs[1], 'second item comes out second'); assert.isFalse((getter.nextItem()), 'end of item queue'); })); it('should return items with tags in expected format', Zotero.Promise.coroutine(function* () { let getter = new Zotero.Translate.ItemGetter(); let itemWithAutomaticTag, itemWithManualTag, itemWithMultipleTags yield Zotero.DB.executeTransaction(function* () { itemWithAutomaticTag = new Zotero.Item('journalArticle'); itemWithAutomaticTag.addTag('automatic tag', 0); yield itemWithAutomaticTag.save(); itemWithManualTag = new Zotero.Item('journalArticle'); itemWithManualTag.addTag('manual tag', 1); yield itemWithManualTag.save(); itemWithMultipleTags = new Zotero.Item('journalArticle'); itemWithMultipleTags.addTag('tag1', 0); itemWithMultipleTags.addTag('tag2', 1); yield itemWithMultipleTags.save(); }); let legacyMode = [false, true]; for (let i=0; i item.save())); collections = [ new Zotero.Collection, new Zotero.Collection, new Zotero.Collection, new Zotero.Collection ]; collections[0].name = "test1"; collections[1].name = "test2"; collections[2].name = "subTest1"; collections[3].name = "subTest2"; yield collections[0].save(); yield collections[1].save(); collections[2].parentID = collections[0].id; collections[3].parentID = collections[1].id; yield collections[2].save(); yield collections[3].save(); yield collections[0].addItems([items[1].id, items[2].id]); yield collections[1].addItem(items[2].id); yield collections[2].addItem(items[3].id); }); let translatorItem = getter.nextItem(); assert.isArray(translatorItem.collections, 'item in library root has a collections array'); assert.equal(translatorItem.collections.length, 0, 'item in library root does not list any collections'); translatorItem = getter.nextItem(); assert.isArray(translatorItem.collections, 'item in a single collection has a collections array'); assert.equal(translatorItem.collections.length, 1, 'item in a single collection lists one collection'); assert.equal(translatorItem.collections[0], collections[0].key, 'item in a single collection identifies correct collection'); translatorItem = getter.nextItem(); assert.isArray(translatorItem.collections, 'item in two collections has a collections array'); assert.equal(translatorItem.collections.length, 2, 'item in two collections lists two collections'); assert.deepEqual( translatorItem.collections.sort(), [collections[0].key, collections[1].key].sort(), 'item in two collections identifies correct collections' ); translatorItem = getter.nextItem(); assert.isArray(translatorItem.collections, 'item in a nested collection has a collections array'); assert.equal(translatorItem.collections.length, 1, 'item in a single nested collection lists one collection'); assert.equal(translatorItem.collections[0], collections[2].key, 'item in a single collection identifies correct collection'); })); it('should return item relations in expected format', Zotero.Promise.coroutine(function* () { let getter = new Zotero.Translate.ItemGetter(); let items; yield Zotero.DB.executeTransaction(function* () { items = [ new Zotero.Item('journalArticle'), // Item with no relations new Zotero.Item('journalArticle'), // Bidirectional relations new Zotero.Item('journalArticle'), // between these items new Zotero.Item('journalArticle'), // This item is related to two items below new Zotero.Item('journalArticle'), // But this item is not related to the item below new Zotero.Item('journalArticle') ]; yield Zotero.Promise.all(items.map(item => item.save())); yield items[1].addRelatedItem(items[2]); yield items[2].addRelatedItem(items[1]); yield items[3].addRelatedItem(items[4]); yield items[4].addRelatedItem(items[3]); yield items[3].addRelatedItem(items[5]); yield items[5].addRelatedItem(items[3]); }); getter._itemsLeft = items.slice(); let translatorItem = getter.nextItem(); assert.isObject(translatorItem.relations, 'item with no relations has a relations object'); assert.equal(Object.keys(translatorItem.relations).length, 0, 'item with no relations does not list any relations'); translatorItem = getter.nextItem(); assert.isObject(translatorItem.relations, 'item that is the subject of a single relation has a relations object'); assert.equal(Object.keys(translatorItem.relations).length, 1, 'item that is the subject of a single relation lists one relations predicate'); assert.lengthOf(translatorItem.relations['dc:relation'], 1, 'item that is the subject of a single relation lists one "dc:relation" object'); assert.equal(translatorItem.relations['dc:relation'][0], Zotero.URI.getItemURI(items[2]), 'item that is the subject of a single relation identifies correct object URI'); // We currently assign these bidirectionally above, so this is a bit redundant translatorItem = getter.nextItem(); assert.isObject(translatorItem.relations, 'item that is the object of a single relation has a relations object'); assert.equal(Object.keys(translatorItem.relations).length, 1, 'item that is the object of a single relation list one relations predicate'); assert.lengthOf(translatorItem.relations['dc:relation'], 1, 'item that is the object of a single relation lists one "dc:relation" object'); assert.equal(translatorItem.relations['dc:relation'][0], Zotero.URI.getItemURI(items[1]), 'item that is the object of a single relation identifies correct subject URI'); translatorItem = getter.nextItem(); assert.isObject(translatorItem.relations, 'item that is the subject of two relations has a relations object'); assert.equal(Object.keys(translatorItem.relations).length, 1, 'item that is the subject of two relations list one relations predicate'); assert.isDefined(translatorItem.relations['dc:relation'], 'item that is the subject of two relations uses "dc:relation" as the predicate'); assert.isArray(translatorItem.relations['dc:relation'], 'item that is the subject of two relations lists "dc:relation" object as an array'); assert.equal(translatorItem.relations['dc:relation'].length, 2, 'item that is the subject of two relations lists two relations in the "dc:relation" array'); assert.deepEqual( translatorItem.relations['dc:relation'].sort(), [Zotero.URI.getItemURI(items[4]), Zotero.URI.getItemURI(items[5])].sort(), 'item that is the subject of two relations identifies correct object URIs' ); translatorItem = getter.nextItem(); assert.isObject(translatorItem.relations, 'item that is the object of one relation from item with two relations has a relations object'); assert.equal(Object.keys(translatorItem.relations).length, 1, 'item that is the object of one relation from item with two relations list one relations predicate'); assert.isDefined(translatorItem.relations['dc:relation'], 'item that is the object of one relation from item with two relations uses "dc:relation" as the predicate'); assert.lengthOf(translatorItem.relations['dc:relation'], 1, 'item that is the object of one relation from item with two relations lists one "dc:relation" object'); assert.equal(translatorItem.relations['dc:relation'][0], Zotero.URI.getItemURI(items[3]), 'item that is the object of one relation from item with two relations identifies correct subject URI'); })); it('should return standalone note in expected format', Zotero.Promise.coroutine(function* () { let relatedItem, note, collection; yield Zotero.DB.executeTransaction(function* () { relatedItem = new Zotero.Item('journalArticle'); yield relatedItem.save(); note = new Zotero.Item('note'); note.setNote('Note'); note.addTag('automaticTag', 0); note.addTag('manualTag', 1); note.addRelatedItem(relatedItem); yield note.save(); relatedItem.addRelatedItem(note); yield relatedItem.save(); collection = new Zotero.Collection; collection.name = 'test'; yield collection.save(); yield collection.addItem(note.id); }); let legacyMode = [false, true]; for (let i=0; i item.save())); collection = new Zotero.Collection; collection.name = 'test'; yield collection.save(); yield collection.addItem(items[0].id); yield collection.addItem(items[1].id); note = new Zotero.Item('note'); note.setNote('Note'); note.addTag('automaticTag', 0); note.addTag('manualTag', 1); yield note.save(); note.addRelatedItem(relatedItem); relatedItem.addRelatedItem(note); yield note.save(); yield relatedItem.save(); }); let legacyMode = [false, true]; for (let i=0; i