Fix Zotero.Utilities tests, which were being skipped accidentally

Unfortunately this will need to be partly redone, since retrieveItem(), and
therefore itemToCSLJSON(), and therefore itemToExportFormat(), need to be
synchronous. The item data load statements in itemToExportFormat() will
probably need to be performed earlier, when they can be async, and made
available to the session for retrieval by retrieveItem(), but I'll let someone
more familiar with the citation infrastructure do that.

This restores some code in retrieveItem() that may have been accidentally
removed in a merge, though it probably won't be useful anymore anyway.

Addresses #529
This commit is contained in:
Dan Stillman 2016-02-04 03:49:56 -05:00
parent d2d7f2368c
commit 72c927c840
4 changed files with 43 additions and 42 deletions

View File

@ -517,12 +517,19 @@ Zotero.Cite.System.prototype = {
return embeddedCitation; return embeddedCitation;
} }
} }
} else {
// is an item ID
//if(this._cache[item]) return this._cache[item];
try {
zoteroItem = Zotero.Items.get(item);
} catch(e) {}
} }
if(!zoteroItem) { if(!zoteroItem) {
throw "Zotero.Cite.System.retrieveItem called on non-item "+item; throw "Zotero.Cite.System.retrieveItem called on non-item "+item;
} }
throw new Error("Unimplemented");
var cslItem = Zotero.Utilities.itemToCSLJSON(zoteroItem); var cslItem = Zotero.Utilities.itemToCSLJSON(zoteroItem);
// TEMP: citeproc-js currently expects the id property to be the item DB id // TEMP: citeproc-js currently expects the id property to be the item DB id

View File

@ -1586,7 +1586,8 @@ Zotero.Utilities = {
/** /**
* Converts an item from toArray() format to citeproc-js JSON * Converts an item from toArray() format to citeproc-js JSON
* @param {Zotero.Item} zoteroItem * @param {Zotero.Item} zoteroItem
* @return {Object} The CSL item * @return {Object|Promise<Object>} A CSL item, or a promise for a CSL item if a Zotero.Item
* is passed
*/ */
"itemToCSLJSON":function(zoteroItem) { "itemToCSLJSON":function(zoteroItem) {
if (zoteroItem instanceof Zotero.Item) { if (zoteroItem instanceof Zotero.Item) {
@ -1836,8 +1837,8 @@ Zotero.Utilities = {
var nameMappings = cslItem[CSL_NAMES_MAPPINGS[field]]; var nameMappings = cslItem[CSL_NAMES_MAPPINGS[field]];
for(var i in nameMappings) { for(var i in nameMappings) {
var cslAuthor = nameMappings[i], var cslAuthor = nameMappings[i];
creator = isZoteroItem ? new Zotero.Creator() : {}; let creator = {};
if(cslAuthor.family || cslAuthor.given) { if(cslAuthor.family || cslAuthor.given) {
if(cslAuthor.family) creator.lastName = cslAuthor.family; if(cslAuthor.family) creator.lastName = cslAuthor.family;
if(cslAuthor.given) creator.firstName = cslAuthor.given; if(cslAuthor.given) creator.firstName = cslAuthor.given;
@ -1847,9 +1848,10 @@ Zotero.Utilities = {
} else { } else {
continue; continue;
} }
creator.creatorTypeID = creatorTypeID;
if(isZoteroItem) { if(isZoteroItem) {
item.setCreator(item.getCreators().length, creator, creatorTypeID); item.setCreator(item.getCreators().length, creator);
} else { } else {
creator.creatorType = Zotero.CreatorTypes.getName(creatorTypeID); creator.creatorType = Zotero.CreatorTypes.getName(creatorTypeID);
if(Zotero.isFx && !Zotero.isBookmarklet && Zotero.platformMajorVersion >= 32) { if(Zotero.isFx && !Zotero.isBookmarklet && Zotero.platformMajorVersion >= 32) {

View File

@ -608,15 +608,18 @@ Zotero.Utilities.Internal = {
* *
* @param {Zotero.Item} zoteroItem * @param {Zotero.Item} zoteroItem
* @param {Boolean} legacy Add mappings for legacy (pre-4.0.27) translators * @param {Boolean} legacy Add mappings for legacy (pre-4.0.27) translators
* @return {Object} * @return {Promise<Object>}
*/ */
"itemToExportFormat": new function() { "itemToExportFormat": new function() {
return Zotero.Promise.coroutine(function* (zoteroItem, legacy) { return Zotero.Promise.coroutine(function* (zoteroItem, legacy) {
var item = yield zoteroItem.toJSON(); var item = yield zoteroItem.toJSON();
item.uri = Zotero.URI.getItemURI(zoteroItem); item.uri = Zotero.URI.getItemURI(zoteroItem);
delete item.key; delete item.key;
if (!zoteroItem.isAttachment() && !zoteroItem.isNote()) { if (!zoteroItem.isAttachment() && !zoteroItem.isNote()) {
yield zoteroItem.loadChildItems();
// Include attachments // Include attachments
item.attachments = []; item.attachments = [];
let attachments = zoteroItem.getAttachments(); let attachments = zoteroItem.getAttachments();

View File

@ -272,7 +272,7 @@ describe("Zotero.Utilities", function() {
assert.isUndefined(cslJSON.PMID, 'field labels are case-sensitive'); assert.isUndefined(cslJSON.PMID, 'field labels are case-sensitive');
})); }));
it("should parse particles in creator names", function() { it("should parse particles in creator names", function* () {
let creators = [ let creators = [
{ {
// No particles // No particles
@ -339,7 +339,7 @@ describe("Zotero.Utilities", function() {
} }
]; ];
let data = populateDBWithSampleData({ let data = yield populateDBWithSampleData({
item: { item: {
itemType: 'journalArticle', itemType: 'journalArticle',
creators: creators creators: creators
@ -347,7 +347,7 @@ describe("Zotero.Utilities", function() {
}); });
let item = Zotero.Items.get(data.item.id); let item = Zotero.Items.get(data.item.id);
let cslCreators = Zotero.Utilities.itemToCSLJSON(item).author; let cslCreators = (yield Zotero.Utilities.itemToCSLJSON(item)).author;
assert.deepEqual(cslCreators[0], creators[0].expect, 'simple name is not parsed'); assert.deepEqual(cslCreators[0], creators[0].expect, 'simple name is not parsed');
assert.deepEqual(cslCreators[1], creators[1].expect, 'name with dropping and non-dropping particles is parsed'); assert.deepEqual(cslCreators[1], creators[1].expect, 'name with dropping and non-dropping particles is parsed');
@ -357,63 +357,52 @@ describe("Zotero.Utilities", function() {
assert.deepEqual(cslCreators[5], creators[5].expect, 'protected last name prevents parsing'); assert.deepEqual(cslCreators[5], creators[5].expect, 'protected last name prevents parsing');
}); });
}); });
describe("itemFromCSLJSON", function() { describe("itemFromCSLJSON", function () {
it("should stably perform itemToCSLJSON -> itemFromCSLJSON -> itemToCSLJSON", function() { it("should stably perform itemToCSLJSON -> itemFromCSLJSON -> itemToCSLJSON", function* () {
let data = loadSampleData('citeProcJSExport'); let data = loadSampleData('citeProcJSExport');
Zotero.DB.beginTransaction();
for (let i in data) { for (let i in data) {
let item = data[i]; let json = data[i];
let zItem = new Zotero.Item(); let item = new Zotero.Item();
Zotero.Utilities.itemFromCSLJSON(zItem, item); Zotero.Utilities.itemFromCSLJSON(item, json);
zItem = Zotero.Items.get(zItem.save()); yield item.saveTx();
let newItem = Zotero.Utilities.itemToCSLJSON(zItem); let newJSON = yield Zotero.Utilities.itemToCSLJSON(item);
delete newItem.id; delete newJSON.id;
delete item.id; delete json.id;
assert.deepEqual(newItem, item, i + ' export -> import -> export is stable'); assert.deepEqual(newJSON, json, i + ' export -> import -> export is stable');
} }
Zotero.DB.commitTransaction();
}); });
it("should import exported standalone note", function() { it("should import exported standalone note", function* () {
let note = new Zotero.Item('note'); let note = new Zotero.Item('note');
note.setNote('Some note longer than 50 characters, which will become the title.'); note.setNote('Some note longer than 50 characters, which will become the title.');
note = Zotero.Items.get(note.save()); yield note.saveTx();
let jsonNote = Zotero.Utilities.itemToCSLJSON(note); let jsonNote = yield Zotero.Utilities.itemToCSLJSON(note);
let zItem = new Zotero.Item(); let item = new Zotero.Item();
Zotero.Utilities.itemFromCSLJSON(zItem, jsonNote); Zotero.Utilities.itemFromCSLJSON(item, jsonNote);
zItem = Zotero.Items.get(zItem.save());
assert.equal(zItem.getField('title'), jsonNote.title, 'title imported correctly'); assert.equal(item.getField('title'), jsonNote.title, 'title imported correctly');
}); });
it("should import exported standalone attachment", function() { it("should import exported standalone attachment", function* () {
let file = getTestDataDirectory(); let attachment = yield importFileAttachment("empty.pdf");
file.append("empty.pdf");
let attachment = Zotero.Items.get(Zotero.Attachments.importFromFile(file));
attachment.setField('title', 'Empty'); attachment.setField('title', 'Empty');
attachment.setField('accessDate', '2001-02-03 12:13:14'); attachment.setField('accessDate', '2001-02-03 12:13:14');
attachment.setField('url', 'http://example.com'); attachment.setField('url', 'http://example.com');
attachment.setNote('Note'); attachment.setNote('Note');
yield attachment.saveTx();
attachment.save(); let jsonAttachment = yield Zotero.Utilities.itemToCSLJSON(attachment);
let jsonAttachment = Zotero.Utilities.itemToCSLJSON(attachment); let item = new Zotero.Item();
Zotero.Utilities.itemFromCSLJSON(item, jsonAttachment);
let zItem = new Zotero.Item(); assert.equal(item.getField('title'), jsonAttachment.title, 'title imported correctly');
Zotero.Utilities.itemFromCSLJSON(zItem, jsonAttachment);
zItem = Zotero.Items.get(zItem.save());
assert.equal(zItem.getField('title'), jsonAttachment.title, 'title imported correctly');
}); });
>>>>>>> 4.0:test/tests/utilities.js
}); });
}); });