2017-04-06 11:19:25 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
describe("Zotero.Integration", function () {
|
|
|
|
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
|
2017-06-01 10:03:51 +00:00
|
|
|
const INTEGRATION_TYPE_ITEM = 1;
|
|
|
|
const INTEGRATION_TYPE_BIBLIOGRAPHY = 2;
|
|
|
|
const INTEGRATION_TYPE_TEMP = 3;
|
2017-04-14 09:43:21 +00:00
|
|
|
/**
|
|
|
|
* To be used as a reference for Zotero-Word Integration plugins
|
2018-03-28 13:22:52 +00:00
|
|
|
*
|
|
|
|
* NOTE: Functions must return promises instead of values!
|
|
|
|
* The functions defined for the dummy are promisified below
|
2017-04-14 09:43:21 +00:00
|
|
|
*/
|
2017-04-06 11:19:25 +00:00
|
|
|
var DocumentPluginDummy = {};
|
2017-04-14 09:43:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The Application class corresponds to a word processing application.
|
|
|
|
*/
|
2017-04-06 11:19:25 +00:00
|
|
|
DocumentPluginDummy.Application = function() {
|
|
|
|
this.doc = new DocumentPluginDummy.Document();
|
|
|
|
this.primaryFieldType = "Field";
|
|
|
|
this.secondaryFieldType = "Bookmark";
|
2018-03-28 13:22:52 +00:00
|
|
|
this.supportedNotes = ['footnotes', 'endnotes'];
|
2020-09-25 12:26:02 +00:00
|
|
|
// Will display an option to switch word processors in the Doc Prefs
|
2019-05-15 01:06:18 +00:00
|
|
|
this.supportsImportExport = true;
|
2020-09-25 12:26:02 +00:00
|
|
|
// Will allow inserting notes
|
|
|
|
this.supportsTextInsertion = true;
|
2017-04-06 11:19:25 +00:00
|
|
|
this.fields = [];
|
|
|
|
};
|
|
|
|
DocumentPluginDummy.Application.prototype = {
|
2017-04-14 09:43:21 +00:00
|
|
|
/**
|
|
|
|
* Gets the active document.
|
|
|
|
* @returns {DocumentPluginDummy.Document}
|
|
|
|
*/
|
2017-04-06 11:19:25 +00:00
|
|
|
getActiveDocument: function() {return this.doc},
|
2017-04-14 09:43:21 +00:00
|
|
|
/**
|
|
|
|
* Gets the document by some app-specific identifier.
|
|
|
|
* @param {String|Number} docID
|
|
|
|
*/
|
|
|
|
getDocument: function(docID) {return this.doc},
|
2017-04-06 11:19:25 +00:00
|
|
|
QueryInterface: function() {return this},
|
|
|
|
};
|
2017-04-14 09:43:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The Document class corresponds to a single word processing document.
|
|
|
|
*/
|
2017-04-06 11:19:25 +00:00
|
|
|
DocumentPluginDummy.Document = function() {this.fields = []};
|
|
|
|
DocumentPluginDummy.Document.prototype = {
|
2017-04-14 09:43:21 +00:00
|
|
|
/**
|
|
|
|
* Displays a dialog in the word processing application
|
|
|
|
* @param {String} dialogText
|
|
|
|
* @param {Number} icon - one of the constants defined in integration.js for dialog icons
|
|
|
|
* @param {Number} buttons - one of the constants defined in integration.js for dialog buttons
|
|
|
|
* @returns {Number}
|
|
|
|
* - Yes: 2, No: 1, Cancel: 0
|
|
|
|
* - Yes: 1, No: 0
|
|
|
|
* - Ok: 1, Cancel: 0
|
|
|
|
* - Ok: 0
|
|
|
|
*/
|
|
|
|
displayAlert: (dialogText, icon, buttons) => 0,
|
|
|
|
/**
|
|
|
|
* Brings this document to the foreground (if necessary to return after displaying a dialog)
|
|
|
|
*/
|
2017-04-06 11:19:25 +00:00
|
|
|
activate: () => 0,
|
2017-04-14 09:43:21 +00:00
|
|
|
/**
|
|
|
|
* Determines whether a field can be inserted at the current position.
|
|
|
|
* @param {String} fieldType
|
|
|
|
* @returns {Boolean}
|
|
|
|
*/
|
|
|
|
canInsertField: (fieldType) => true,
|
|
|
|
/**
|
|
|
|
* Returns the field in which the cursor resides, or NULL if none.
|
|
|
|
* @param {String} fieldType
|
|
|
|
* @returns {Boolean}
|
|
|
|
*/
|
|
|
|
cursorInField: (fieldType) => false,
|
|
|
|
/**
|
|
|
|
* Get document data property from the current document
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
2017-04-06 11:19:25 +00:00
|
|
|
getDocumentData: function() {return this.data},
|
2017-04-14 09:43:21 +00:00
|
|
|
/**
|
|
|
|
* Set document data property
|
|
|
|
* @param {String} data
|
|
|
|
*/
|
2017-04-06 11:19:25 +00:00
|
|
|
setDocumentData: function(data) {this.data = data},
|
2017-04-14 09:43:21 +00:00
|
|
|
/**
|
2020-09-25 12:26:02 +00:00
|
|
|
* Inserts a field at cursor position and initializes the field object.
|
|
|
|
* If Document.insertText() was called previously inserts the field
|
|
|
|
* directly after the inserted text.
|
2017-04-14 09:43:21 +00:00
|
|
|
* @param {String} fieldType
|
2017-05-02 12:09:12 +00:00
|
|
|
* @param {Integer} noteType
|
2017-04-14 09:43:21 +00:00
|
|
|
* @returns {DocumentPluginDummy.Field}
|
|
|
|
*/
|
2020-09-25 12:26:02 +00:00
|
|
|
insertField: function(fieldType, noteType) {
|
2017-05-02 12:09:12 +00:00
|
|
|
if (typeof noteType != "number") {
|
|
|
|
throw new Error("noteType must be an integer");
|
|
|
|
}
|
2020-09-25 12:26:02 +00:00
|
|
|
var field = new DocumentPluginDummy.Field(this);
|
2018-03-28 13:22:52 +00:00
|
|
|
this.fields.push(field);
|
|
|
|
return field;
|
2017-04-14 09:43:21 +00:00
|
|
|
},
|
2020-09-25 12:26:02 +00:00
|
|
|
/**
|
|
|
|
* Inserts rich text at cursor position. If Document.insertField() was called
|
|
|
|
* previously inserts the text directly after the inserted field.
|
|
|
|
* @param {String} text
|
|
|
|
*/
|
|
|
|
insertText: function (text) { return; },
|
|
|
|
/**
|
|
|
|
* Converts placeholders (which are text with links to https://www.zotero.org/?[placeholderID])
|
|
|
|
* to fields and sets their field codes to strings in `codes` in the reverse order of their appearance
|
|
|
|
* @param {String[]} codes
|
2020-10-12 14:01:24 +00:00
|
|
|
* @param {String[]} placeholderIDs - the order of placeholders to be replaced
|
2020-09-25 12:26:02 +00:00
|
|
|
* @param {Number} noteType - controls whether citations should be in-text or in footnotes/endnotes
|
2022-07-26 07:53:05 +00:00
|
|
|
* @param {String} fieldType
|
2020-09-25 12:26:02 +00:00
|
|
|
* @return {Field[]}
|
|
|
|
*/
|
2022-07-26 07:53:05 +00:00
|
|
|
convertPlaceholdersToFields: function (codes, placeholderIDs, noteType, fieldType) {
|
2020-09-25 12:26:02 +00:00
|
|
|
return codes.map(code => {
|
|
|
|
let field = new DocumentPluginDummy.Field(this);
|
|
|
|
field.code = code;
|
|
|
|
this.fields.push(field);
|
|
|
|
return field;
|
|
|
|
});
|
|
|
|
},
|
2017-04-14 09:43:21 +00:00
|
|
|
/**
|
|
|
|
* Gets all fields present in the document.
|
|
|
|
* @param {String} fieldType
|
2018-03-28 13:22:52 +00:00
|
|
|
* @returns {DocumentPluginDummy.Field[]}
|
2017-04-14 09:43:21 +00:00
|
|
|
*/
|
2020-09-25 12:26:02 +00:00
|
|
|
getFields: function (fieldType) {return Array.from(this.fields)},
|
2017-04-14 09:43:21 +00:00
|
|
|
/**
|
|
|
|
* Sets the bibliography style, overwriting the current values for this document
|
|
|
|
*/
|
|
|
|
setBibliographyStyle: (firstLineIndent, bodyIndent, lineSpacing, entrySpacing,
|
|
|
|
tabStops, tabStopsCount) => 0,
|
|
|
|
/**
|
|
|
|
* Converts all fields in a document to a different fieldType or noteType
|
2018-03-28 13:22:52 +00:00
|
|
|
* @params {DocumentPluginDummy.Field[]} fields
|
2017-04-14 09:43:21 +00:00
|
|
|
*/
|
|
|
|
convert: (fields, toFieldType, toNoteType, count) => 0,
|
|
|
|
/**
|
|
|
|
* Cleans up the document state and resumes processor for editing
|
|
|
|
*/
|
|
|
|
cleanup: () => 0,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Informs the document processor that the operation is complete
|
|
|
|
*/
|
2017-04-06 11:19:25 +00:00
|
|
|
complete: () => 0,
|
2019-05-15 01:06:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts field text in document to their underlying codes and appends
|
|
|
|
* document preferences and bibliography style as paragraphs at the end
|
|
|
|
* of the document. Prefixes:
|
|
|
|
* - Bibliography style: "BIBLIOGRAPHY_STYLE "
|
|
|
|
* - Document preferences: "DOCUMENT_PREFERENCES "
|
|
|
|
*
|
|
|
|
* All Zotero exported text must be converted to a hyperlink
|
|
|
|
* (with any url, e.g. http://www.zotero.org)
|
|
|
|
*/
|
|
|
|
exportDocument: (fieldType) => 0,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a document from an exported form described in #exportDocument()
|
|
|
|
* to a Zotero editable form. Bibliography Style and Document Preferences
|
|
|
|
* text is removed and stored internally within the doc. The citation codes are
|
|
|
|
* also stored within the doc in appropriate representation.
|
|
|
|
*
|
|
|
|
* Note that no citation text updates are needed. Zotero will issue field updates
|
|
|
|
* manually.
|
|
|
|
*
|
|
|
|
* @returns {Boolean} whether the document contained importable data
|
|
|
|
*/
|
|
|
|
importDocument: (fieldType) => 0,
|
2017-04-06 11:19:25 +00:00
|
|
|
};
|
2017-04-14 09:43:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The Field class corresponds to a field containing an individual citation
|
|
|
|
* or bibliography
|
|
|
|
*/
|
2017-04-06 11:19:25 +00:00
|
|
|
DocumentPluginDummy.Field = function(doc) {
|
|
|
|
this.doc = doc;
|
2017-04-11 13:16:37 +00:00
|
|
|
this.code = '';
|
|
|
|
// This is actually required and current integration code depends on text being non-empty upon insertion.
|
|
|
|
// insertBibliography will fail if there is no placeholder text.
|
|
|
|
this.text = '{Placeholder}';
|
2017-04-06 11:19:25 +00:00
|
|
|
this.wrappedJSObject = this;
|
|
|
|
};
|
|
|
|
DocumentPluginDummy.Field.noteIndex = 0;
|
|
|
|
DocumentPluginDummy.Field.prototype = {
|
2017-04-14 09:43:21 +00:00
|
|
|
/**
|
|
|
|
* Deletes this field and its contents.
|
|
|
|
*/
|
2019-04-08 13:43:29 +00:00
|
|
|
delete: function() {this.doc.fields = this.doc.fields.filter((field) => field !== this)},
|
2017-04-14 09:43:21 +00:00
|
|
|
/**
|
|
|
|
* Removes this field, but maintains the field's contents.
|
|
|
|
*/
|
2017-04-06 11:19:25 +00:00
|
|
|
removeCode: function() {this.code = ""},
|
2017-04-14 09:43:21 +00:00
|
|
|
/**
|
|
|
|
* Selects this field.
|
|
|
|
*/
|
2017-04-06 11:19:25 +00:00
|
|
|
select: () => 0,
|
2017-04-14 09:43:21 +00:00
|
|
|
/**
|
|
|
|
* Sets the text inside this field to a specified plain text string or pseudo-RTF formatted text
|
|
|
|
* string.
|
|
|
|
* @param {String} text
|
|
|
|
* @param {Boolean} isRich
|
|
|
|
*/
|
|
|
|
setText: function(text, isRich) {this.text = text},
|
|
|
|
/**
|
|
|
|
* Gets the text inside this field, preferably with formatting, but potentially without
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
2017-04-06 11:19:25 +00:00
|
|
|
getText: function() {return this.text},
|
2017-04-14 09:43:21 +00:00
|
|
|
/**
|
|
|
|
* Sets field's code
|
|
|
|
* @param {String} code
|
|
|
|
*/
|
2017-04-06 11:19:25 +00:00
|
|
|
setCode: function(code) {this.code = code},
|
2017-04-14 09:43:21 +00:00
|
|
|
/**
|
|
|
|
* Gets field's code.
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
2017-04-06 11:19:25 +00:00
|
|
|
getCode: function() {return this.code},
|
2017-04-14 09:43:21 +00:00
|
|
|
/**
|
|
|
|
* Returns true if this field and the passed field are actually references to the same field.
|
|
|
|
* @param {DocumentPluginDummy.Field} field
|
|
|
|
* @returns {Boolean}
|
|
|
|
*/
|
2017-04-06 11:19:25 +00:00
|
|
|
equals: function(field) {return this == field},
|
2017-04-14 09:43:21 +00:00
|
|
|
/**
|
|
|
|
* This field's note index, if it is in a footnote or endnote; otherwise zero.
|
|
|
|
* @returns {Number}
|
|
|
|
*/
|
|
|
|
getNoteIndex: () => 0,
|
2017-04-06 11:19:25 +00:00
|
|
|
};
|
|
|
|
|
2018-03-28 13:22:52 +00:00
|
|
|
// Processing functions for logging and promisification
|
|
|
|
for (let cls of ['Application', 'Document', 'Field']) {
|
2017-04-06 11:19:25 +00:00
|
|
|
for (let methodName in DocumentPluginDummy[cls].prototype) {
|
|
|
|
if (methodName !== 'QueryInterface') {
|
|
|
|
let method = DocumentPluginDummy[cls].prototype[methodName];
|
2018-03-28 13:22:52 +00:00
|
|
|
DocumentPluginDummy[cls].prototype[methodName] = async function() {
|
2017-04-06 11:19:25 +00:00
|
|
|
try {
|
|
|
|
Zotero.debug(`DocumentPluginDummy: ${cls}.${methodName} invoked with args ${JSON.stringify(arguments)}`, 2);
|
|
|
|
} catch (e) {
|
|
|
|
Zotero.debug(`DocumentPluginDummy: ${cls}.${methodName} invoked with args ${arguments}`, 2);
|
|
|
|
}
|
|
|
|
var result = method.apply(this, arguments);
|
|
|
|
try {
|
|
|
|
Zotero.debug(`Result: ${JSON.stringify(result)}`, 2);
|
|
|
|
} catch (e) {
|
|
|
|
Zotero.debug(`Result: ${result}`, 2);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var testItems;
|
|
|
|
var applications = {};
|
|
|
|
var addEditCitationSpy, displayDialogStub;
|
|
|
|
var styleID = "http://www.zotero.org/styles/cell";
|
|
|
|
var stylePath = OS.Path.join(getTestDataDirectory().path, 'cell.csl');
|
|
|
|
|
|
|
|
var commandList = [
|
|
|
|
'addCitation', 'editCitation', 'addEditCitation',
|
|
|
|
'addBibliography', 'editBibliography', 'addEditBibliography',
|
|
|
|
'refresh', 'removeCodes', 'setDocPrefs'
|
|
|
|
];
|
|
|
|
|
|
|
|
function execCommand(command, docID) {
|
|
|
|
if (! commandList.includes(command)) {
|
|
|
|
throw new Error(`${command} is not a valid document command`);
|
|
|
|
}
|
|
|
|
if (typeof docID === "undefined") {
|
|
|
|
throw new Error(`docID cannot be undefined`)
|
|
|
|
}
|
2017-06-01 10:03:51 +00:00
|
|
|
Zotero.debug(`execCommand '${command}': ${docID}`, 2);
|
2017-04-06 11:19:25 +00:00
|
|
|
return Zotero.Integration.execCommand("dummy", command, docID);
|
|
|
|
}
|
|
|
|
|
|
|
|
var dialogResults = {
|
|
|
|
addCitationDialog: {},
|
|
|
|
quickFormat: {},
|
|
|
|
integrationDocPrefs: {},
|
|
|
|
selectItemsDialog: {},
|
|
|
|
editBibliographyDialog: {}
|
|
|
|
};
|
|
|
|
|
2018-03-28 13:22:52 +00:00
|
|
|
async function initDoc(docID, options={}) {
|
2017-04-06 11:19:25 +00:00
|
|
|
applications[docID] = new DocumentPluginDummy.Application();
|
|
|
|
var data = new Zotero.Integration.DocumentData();
|
|
|
|
data.prefs = {
|
|
|
|
noteType: 0,
|
|
|
|
fieldType: "Field",
|
|
|
|
automaticJournalAbbreviations: true
|
|
|
|
};
|
|
|
|
data.style = {styleID, locale: 'en-US', hasBibliography: true, bibliographyStyleHasBeenSet: true};
|
|
|
|
data.sessionID = Zotero.Utilities.randomString(10);
|
|
|
|
Object.assign(data, options);
|
2018-03-28 13:22:52 +00:00
|
|
|
await (await applications[docID].getDocument(docID)).setDocumentData(data.serialize());
|
2017-04-06 11:19:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function setDefaultIntegrationDocPrefs() {
|
|
|
|
dialogResults.integrationDocPrefs = {
|
|
|
|
style: "http://www.zotero.org/styles/cell",
|
|
|
|
locale: 'en-US',
|
|
|
|
fieldType: 'Field',
|
|
|
|
automaticJournalAbbreviations: false,
|
|
|
|
useEndnotes: 0
|
|
|
|
};
|
|
|
|
}
|
|
|
|
setDefaultIntegrationDocPrefs();
|
|
|
|
|
|
|
|
function setAddEditItems(items) {
|
|
|
|
if (items.length == undefined) items = [items];
|
2018-03-26 12:31:26 +00:00
|
|
|
dialogResults.quickFormat = async function(dialogName, io) {
|
2017-06-01 10:03:51 +00:00
|
|
|
io.citation.citationItems = items.map(function(item) {
|
|
|
|
item = Zotero.Cite.getItem(item.id);
|
|
|
|
return {id: item.id, uris: item.cslURIs, itemData: item.cslItemData};
|
|
|
|
});
|
2019-04-08 13:43:29 +00:00
|
|
|
try {
|
|
|
|
await io.previewFn(io.citation);
|
|
|
|
}
|
|
|
|
catch (e) {}
|
2017-06-01 10:03:51 +00:00
|
|
|
io._acceptDeferred.resolve(() => {});
|
|
|
|
};
|
2017-04-06 11:19:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
before(function* () {
|
|
|
|
yield Zotero.Styles.init();
|
|
|
|
yield Zotero.Styles.install({file: stylePath}, styleID, true);
|
|
|
|
|
|
|
|
testItems = [];
|
|
|
|
for (let i = 0; i < 5; i++) {
|
2017-06-06 09:07:12 +00:00
|
|
|
let testItem = yield createDataObject('item', {libraryID: Zotero.Libraries.userLibraryID});
|
2019-07-04 12:16:18 +00:00
|
|
|
testItem.setField('title', `title${i}`);
|
2017-06-06 09:07:12 +00:00
|
|
|
testItem.setCreator(0, {creatorType: 'author', name: `Author No${i}`});
|
|
|
|
testItems.push(testItem);
|
2017-04-06 11:19:25 +00:00
|
|
|
}
|
|
|
|
setAddEditItems(testItems[0]);
|
|
|
|
|
2017-05-31 15:44:35 +00:00
|
|
|
sinon.stub(Zotero.Integration, 'getApplication').callsFake(function(agent, command, docID) {
|
2017-04-06 11:19:25 +00:00
|
|
|
if (!applications[docID]) {
|
|
|
|
applications[docID] = new DocumentPluginDummy.Application();
|
|
|
|
}
|
|
|
|
return applications[docID];
|
|
|
|
});
|
|
|
|
|
2018-03-26 12:31:26 +00:00
|
|
|
displayDialogStub = sinon.stub(Zotero.Integration, 'displayDialog');
|
|
|
|
displayDialogStub.callsFake(async function(dialogName, prefs, io) {
|
2017-05-25 07:48:43 +00:00
|
|
|
Zotero.debug(`Display dialog: ${dialogName}`, 2);
|
2017-04-06 11:19:25 +00:00
|
|
|
var ioResult = dialogResults[dialogName.substring(dialogName.lastIndexOf('/')+1, dialogName.length-4)];
|
|
|
|
if (typeof ioResult == 'function') {
|
2018-03-26 12:31:26 +00:00
|
|
|
await ioResult(dialogName, io);
|
2017-06-01 10:03:51 +00:00
|
|
|
} else {
|
|
|
|
Object.assign(io, ioResult);
|
2017-04-06 11:19:25 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-05-15 15:19:46 +00:00
|
|
|
addEditCitationSpy = sinon.spy(Zotero.Integration.Interface.prototype, 'addEditCitation');
|
2018-03-30 12:31:53 +00:00
|
|
|
|
|
|
|
sinon.stub(Zotero.Integration.Progress.prototype, 'show');
|
2017-04-06 11:19:25 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
after(function() {
|
2018-03-30 12:31:53 +00:00
|
|
|
Zotero.Integration.Progress.prototype.show.restore();
|
2017-04-06 11:19:25 +00:00
|
|
|
Zotero.Integration.getApplication.restore();
|
|
|
|
displayDialogStub.restore();
|
|
|
|
addEditCitationSpy.restore();
|
|
|
|
});
|
|
|
|
|
2017-06-01 10:03:51 +00:00
|
|
|
describe('Interface', function() {
|
|
|
|
describe('#execCommand', function() {
|
2017-04-06 11:19:25 +00:00
|
|
|
var setDocumentDataSpy;
|
|
|
|
var docID = this.fullTitle();
|
|
|
|
|
|
|
|
before(function() {
|
|
|
|
setDocumentDataSpy = sinon.spy(DocumentPluginDummy.Document.prototype, 'setDocumentData');
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function() {
|
2019-05-25 11:28:28 +00:00
|
|
|
setDocumentDataSpy.resetHistory();
|
2017-04-06 11:19:25 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
after(function() {
|
|
|
|
setDocumentDataSpy.restore();
|
|
|
|
});
|
|
|
|
|
2017-05-24 08:54:53 +00:00
|
|
|
it('should call doc.setDocumentData once', function* () {
|
2017-04-06 11:19:25 +00:00
|
|
|
yield execCommand('addEditCitation', docID);
|
2017-05-03 11:34:00 +00:00
|
|
|
assert.isTrue(setDocumentDataSpy.calledOnce);
|
2017-04-06 11:19:25 +00:00
|
|
|
});
|
2017-04-12 08:40:28 +00:00
|
|
|
|
|
|
|
describe('when style used in the document does not exist', function() {
|
|
|
|
var docID = this.fullTitle();
|
|
|
|
var displayAlertStub;
|
|
|
|
var style;
|
|
|
|
before(function* () {
|
2018-03-28 13:22:52 +00:00
|
|
|
displayAlertStub = sinon.stub(DocumentPluginDummy.Document.prototype, 'displayAlert').resolves(0);
|
2017-04-12 08:40:28 +00:00
|
|
|
});
|
|
|
|
|
2018-03-28 13:22:52 +00:00
|
|
|
beforeEach(async function () {
|
2017-04-12 08:40:28 +00:00
|
|
|
// 🦉birds?
|
|
|
|
style = {styleID: "http://www.example.com/csl/waterbirds", locale: 'en-US'};
|
|
|
|
|
|
|
|
// Make sure style not in library
|
|
|
|
try {
|
|
|
|
Zotero.Styles.get(style.styleID).remove();
|
|
|
|
} catch (e) {}
|
2018-03-28 13:22:52 +00:00
|
|
|
await initDoc(docID, {style});
|
2018-03-26 12:31:26 +00:00
|
|
|
displayDialogStub.resetHistory();
|
2019-05-25 11:28:28 +00:00
|
|
|
displayAlertStub.resetHistory();
|
2017-04-12 08:40:28 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
after(function* () {
|
|
|
|
displayAlertStub.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when the style is not from a trusted source', function() {
|
2017-05-24 08:54:53 +00:00
|
|
|
it('should download the style and if user clicks YES', function* () {
|
2018-08-14 08:40:19 +00:00
|
|
|
var styleInstallStub = sinon.stub(Zotero.Styles, "install").resolves({
|
|
|
|
styleTitle: 'Waterbirds',
|
|
|
|
styleID: 'waterbirds'
|
|
|
|
});
|
2017-04-12 08:40:28 +00:00
|
|
|
var style = Zotero.Styles.get(styleID);
|
|
|
|
var styleGetCalledOnce = false;
|
2017-05-31 15:44:35 +00:00
|
|
|
var styleGetStub = sinon.stub(Zotero.Styles, 'get').callsFake(function() {
|
2017-04-12 08:40:28 +00:00
|
|
|
if (!styleGetCalledOnce) {
|
|
|
|
styleGetCalledOnce = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return style;
|
|
|
|
});
|
2018-03-28 13:22:52 +00:00
|
|
|
displayAlertStub.resolves(1);
|
2017-04-12 08:40:28 +00:00
|
|
|
yield execCommand('addEditCitation', docID);
|
|
|
|
assert.isTrue(displayAlertStub.calledOnce);
|
|
|
|
assert.isFalse(displayDialogStub.calledWith(applications[docID].doc, 'chrome://zotero/content/integration/integrationDocPrefs.xul'));
|
|
|
|
assert.isTrue(styleInstallStub.calledOnce);
|
|
|
|
assert.isOk(Zotero.Styles.get(style.styleID));
|
|
|
|
styleInstallStub.restore();
|
|
|
|
styleGetStub.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should prompt with the document preferences dialog if user clicks NO', function* () {
|
2018-03-28 13:22:52 +00:00
|
|
|
displayAlertStub.resolves(0);
|
2017-04-12 08:40:28 +00:00
|
|
|
yield execCommand('addEditCitation', docID);
|
|
|
|
assert.isTrue(displayAlertStub.calledOnce);
|
|
|
|
// Prefs to select a new style and quickFormat
|
|
|
|
assert.isTrue(displayDialogStub.calledTwice);
|
|
|
|
assert.isNotOk(Zotero.Styles.get(style.styleID));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should download the style without prompting if it is from zotero.org', function* (){
|
2018-03-28 13:22:52 +00:00
|
|
|
yield initDoc(docID, {styleID: "http://www.zotero.org/styles/waterbirds", locale: 'en-US'});
|
2018-08-13 11:22:06 +00:00
|
|
|
var styleInstallStub = sinon.stub(Zotero.Styles, "install").resolves({
|
|
|
|
styleTitle: 'Waterbirds',
|
|
|
|
styleID: 'waterbirds'
|
|
|
|
});
|
2017-04-12 08:40:28 +00:00
|
|
|
var style = Zotero.Styles.get(styleID);
|
|
|
|
var styleGetCalledOnce = false;
|
2017-05-31 15:44:35 +00:00
|
|
|
var styleGetStub = sinon.stub(Zotero.Styles, 'get').callsFake(function() {
|
2017-04-12 08:40:28 +00:00
|
|
|
if (!styleGetCalledOnce) {
|
|
|
|
styleGetCalledOnce = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return style;
|
|
|
|
});
|
2018-03-28 13:22:52 +00:00
|
|
|
displayAlertStub.resolves(1);
|
2017-04-12 08:40:28 +00:00
|
|
|
yield execCommand('addEditCitation', docID);
|
|
|
|
assert.isFalse(displayAlertStub.called);
|
|
|
|
assert.isFalse(displayDialogStub.calledWith(applications[docID].doc, 'chrome://zotero/content/integration/integrationDocPrefs.xul'));
|
|
|
|
assert.isTrue(styleInstallStub.calledOnce);
|
|
|
|
assert.isOk(Zotero.Styles.get(style.styleID));
|
|
|
|
styleInstallStub.restore();
|
|
|
|
styleGetStub.restore();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-04-11 13:16:37 +00:00
|
|
|
|
2017-06-01 10:03:51 +00:00
|
|
|
describe('#addEditCitation', function() {
|
|
|
|
var insertMultipleCitations = Zotero.Promise.coroutine(function *() {
|
|
|
|
var docID = this.test.fullTitle();
|
2018-03-28 13:22:52 +00:00
|
|
|
if (!(docID in applications)) yield initDoc(docID);
|
2017-06-01 10:03:51 +00:00
|
|
|
var doc = applications[docID].doc;
|
|
|
|
|
|
|
|
setAddEditItems(testItems[0]);
|
|
|
|
yield execCommand('addEditCitation', docID);
|
|
|
|
assert.equal(doc.fields.length, 1);
|
2018-03-28 13:22:52 +00:00
|
|
|
var citation = yield (new Zotero.Integration.CitationField(doc.fields[0], doc.fields[0].code)).unserialize();
|
2017-06-01 10:03:51 +00:00
|
|
|
assert.equal(citation.citationItems.length, 1);
|
|
|
|
assert.equal(citation.citationItems[0].id, testItems[0].id);
|
|
|
|
|
|
|
|
setAddEditItems(testItems.slice(1, 3));
|
|
|
|
yield execCommand('addEditCitation', docID);
|
|
|
|
assert.equal(doc.fields.length, 2);
|
2018-03-28 13:22:52 +00:00
|
|
|
citation = yield (new Zotero.Integration.CitationField(doc.fields[1], doc.fields[1].code)).unserialize();
|
2017-06-01 10:03:51 +00:00
|
|
|
assert.equal(citation.citationItems.length, 2);
|
|
|
|
for (let i = 1; i < 3; i++) {
|
|
|
|
assert.equal(citation.citationItems[i-1].id, testItems[i].id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
it('should insert citation if not in field', insertMultipleCitations);
|
2019-02-12 14:32:11 +00:00
|
|
|
|
2017-06-01 10:03:51 +00:00
|
|
|
it('should edit citation if in citation field', function* () {
|
|
|
|
yield insertMultipleCitations.call(this);
|
|
|
|
var docID = this.test.fullTitle();
|
|
|
|
var doc = applications[docID].doc;
|
|
|
|
|
2018-03-28 13:22:52 +00:00
|
|
|
sinon.stub(doc, 'cursorInField').resolves(doc.fields[0]);
|
|
|
|
sinon.stub(doc, 'canInsertField').resolves(false);
|
2017-06-01 10:03:51 +00:00
|
|
|
|
|
|
|
setAddEditItems(testItems.slice(3, 5));
|
|
|
|
yield execCommand('addEditCitation', docID);
|
|
|
|
assert.equal(doc.fields.length, 2);
|
2018-03-28 13:22:52 +00:00
|
|
|
var citation = yield (new Zotero.Integration.CitationField(doc.fields[0], doc.fields[0].code)).unserialize();
|
2017-06-01 10:03:51 +00:00
|
|
|
assert.equal(citation.citationItems.length, 2);
|
|
|
|
assert.equal(citation.citationItems[0].id, testItems[3].id);
|
|
|
|
});
|
|
|
|
|
2019-02-12 14:32:11 +00:00
|
|
|
it('should write an implicitly updated citation into the document', function* () {
|
|
|
|
yield insertMultipleCitations.call(this);
|
|
|
|
var docID = this.test.fullTitle();
|
|
|
|
var doc = applications[docID].doc;
|
|
|
|
|
|
|
|
testItems[3].setCreator(0, {creatorType: 'author', lastName: 'Smith', firstName: 'Robert'});
|
|
|
|
testItems[3].setField('date', '2019-01-01');
|
|
|
|
|
|
|
|
setAddEditItems(testItems[3]);
|
|
|
|
yield execCommand('addEditCitation', docID);
|
|
|
|
assert.equal(doc.fields[2].text, "(Smith, 2019)");
|
|
|
|
|
|
|
|
sinon.stub(doc, 'cursorInField').resolves(doc.fields[0]);
|
|
|
|
sinon.stub(doc, 'canInsertField').resolves(false);
|
|
|
|
|
|
|
|
testItems[4].setCreator(0, {creatorType: 'author', lastName: 'Smith', firstName: 'Robert'});
|
|
|
|
testItems[4].setField('date', '2019-01-01');
|
|
|
|
|
|
|
|
setAddEditItems(testItems[4]);
|
|
|
|
yield execCommand('addEditCitation', docID);
|
|
|
|
assert.equal(doc.fields.length, 3);
|
2019-02-12 15:09:35 +00:00
|
|
|
assert.equal(doc.fields[0].text, "(Smith, 2019a)");
|
|
|
|
assert.equal(doc.fields[2].text, "(Smith, 2019b)");
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should place an implicitly updated citation correctly after multiple new insertions', function* () {
|
|
|
|
yield insertMultipleCitations.call(this);
|
|
|
|
var docID = this.test.fullTitle();
|
|
|
|
var doc = applications[docID].doc;
|
|
|
|
|
|
|
|
testItems[3].setCreator(0, {creatorType: 'author', lastName: 'Smith', firstName: 'Robert'});
|
|
|
|
testItems[3].setField('date', '2019-01-01');
|
|
|
|
|
|
|
|
setAddEditItems(testItems[3]);
|
|
|
|
yield execCommand('addEditCitation', docID);
|
|
|
|
assert.equal(doc.fields[2].text, "(Smith, 2019)");
|
|
|
|
|
|
|
|
sinon.stub(doc, 'cursorInField').resolves(doc.fields[0]);
|
|
|
|
sinon.stub(doc, 'canInsertField').resolves(false);
|
|
|
|
|
|
|
|
doc.fields[1].code = doc.fields[0].code;
|
|
|
|
doc.fields[1].text = doc.fields[0].text;
|
|
|
|
|
|
|
|
testItems[4].setCreator(0, {creatorType: 'author', lastName: 'Smith', firstName: 'Robert'});
|
|
|
|
testItems[4].setField('date', '2019-01-01');
|
|
|
|
|
|
|
|
setAddEditItems(testItems[4]);
|
|
|
|
yield execCommand('addEditCitation', docID);
|
|
|
|
assert.equal(doc.fields.length, 3);
|
2019-02-12 14:32:11 +00:00
|
|
|
assert.equal(doc.fields[0].text, "(Smith, 2019a)");
|
|
|
|
assert.equal(doc.fields[2].text, "(Smith, 2019b)");
|
|
|
|
});
|
|
|
|
|
2017-06-01 10:03:51 +00:00
|
|
|
it('should update bibliography if present', function* () {
|
|
|
|
yield insertMultipleCitations.call(this);
|
|
|
|
var docID = this.test.fullTitle();
|
|
|
|
var doc = applications[docID].doc;
|
|
|
|
|
|
|
|
let getCiteprocBibliographySpy =
|
|
|
|
sinon.spy(Zotero.Integration.Bibliography.prototype, 'getCiteprocBibliography');
|
|
|
|
|
|
|
|
yield execCommand('addEditBibliography', docID);
|
|
|
|
assert.isTrue(getCiteprocBibliographySpy.calledOnce);
|
|
|
|
|
|
|
|
assert.equal(getCiteprocBibliographySpy.lastCall.returnValue[0].entry_ids.length, 3);
|
2019-05-25 11:28:28 +00:00
|
|
|
getCiteprocBibliographySpy.resetHistory();
|
2017-06-01 10:03:51 +00:00
|
|
|
|
|
|
|
setAddEditItems(testItems[3]);
|
|
|
|
yield execCommand('addEditCitation', docID);
|
|
|
|
assert.equal(getCiteprocBibliographySpy.lastCall.returnValue[0].entry_ids.length, 4);
|
|
|
|
|
|
|
|
getCiteprocBibliographySpy.restore();
|
|
|
|
});
|
2019-02-10 02:07:10 +00:00
|
|
|
|
|
|
|
it('should update bibliography sort order on change to item', function* () {
|
|
|
|
yield insertMultipleCitations.call(this);
|
|
|
|
var docID = this.test.fullTitle();
|
|
|
|
var doc = applications[docID].doc;
|
|
|
|
|
|
|
|
let getCiteprocBibliographySpy =
|
|
|
|
sinon.spy(Zotero.Integration.Bibliography.prototype, 'getCiteprocBibliography');
|
|
|
|
|
|
|
|
yield execCommand('addEditBibliography', docID);
|
|
|
|
assert.isTrue(getCiteprocBibliographySpy.calledOnce);
|
|
|
|
|
|
|
|
assert.equal(getCiteprocBibliographySpy.lastCall.returnValue[0].entry_ids.length, 3);
|
2019-05-25 11:28:28 +00:00
|
|
|
getCiteprocBibliographySpy.resetHistory();
|
2019-02-10 02:07:10 +00:00
|
|
|
|
|
|
|
sinon.stub(doc, 'cursorInField').resolves(doc.fields[1]);
|
|
|
|
sinon.stub(doc, 'canInsertField').resolves(false);
|
|
|
|
|
|
|
|
testItems[1].setCreator(0, {creatorType: 'author', name: 'Aaaaa'});
|
|
|
|
testItems[1].setField('title', 'Bbbbb');
|
|
|
|
|
|
|
|
setAddEditItems(testItems.slice(1, 3));
|
|
|
|
yield execCommand('addEditCitation', docID);
|
|
|
|
|
|
|
|
assert.equal(getCiteprocBibliographySpy.lastCall.returnValue[0].entry_ids.length, 3);
|
|
|
|
assert.equal(getCiteprocBibliographySpy.lastCall.returnValue[1][0], "Aaaaa Bbbbb.");
|
|
|
|
|
|
|
|
getCiteprocBibliographySpy.restore();
|
|
|
|
});
|
2017-06-06 09:07:12 +00:00
|
|
|
|
|
|
|
describe('when original citation text has been modified', function() {
|
|
|
|
var displayAlertStub;
|
|
|
|
before(function* () {
|
2018-03-28 13:22:52 +00:00
|
|
|
displayAlertStub = sinon.stub(DocumentPluginDummy.Document.prototype, 'displayAlert').resolves(0);
|
2017-06-06 09:07:12 +00:00
|
|
|
});
|
|
|
|
beforeEach(function() {
|
2019-05-25 11:28:28 +00:00
|
|
|
displayAlertStub.resetHistory();
|
2017-06-06 09:07:12 +00:00
|
|
|
});
|
|
|
|
after(function() {
|
|
|
|
displayAlertStub.restore();
|
|
|
|
});
|
|
|
|
it('should keep modification if "Cancel" selected in editCitation triggered alert', async function () {
|
|
|
|
await insertMultipleCitations.call(this);
|
|
|
|
var docID = this.test.fullTitle();
|
|
|
|
var doc = applications[docID].doc;
|
|
|
|
|
|
|
|
doc.fields[0].text = "modified";
|
2018-03-28 13:22:52 +00:00
|
|
|
sinon.stub(doc, 'cursorInField').resolves(doc.fields[0]);
|
|
|
|
sinon.stub(doc, 'canInsertField').resolves(false);
|
2017-06-06 09:07:12 +00:00
|
|
|
|
|
|
|
await execCommand('addEditCitation', docID);
|
|
|
|
assert.equal(doc.fields.length, 2);
|
|
|
|
assert.equal(doc.fields[0].text, "modified");
|
|
|
|
});
|
|
|
|
it('should display citation dialog if "OK" selected in editCitation triggered alert', async function () {
|
|
|
|
await insertMultipleCitations.call(this);
|
|
|
|
var docID = this.test.fullTitle();
|
|
|
|
var doc = applications[docID].doc;
|
|
|
|
|
|
|
|
let origText = doc.fields[0].text;
|
|
|
|
doc.fields[0].text = "modified";
|
|
|
|
// Return OK
|
2018-03-28 13:22:52 +00:00
|
|
|
displayAlertStub.resolves(1);
|
|
|
|
sinon.stub(doc, 'cursorInField').resolves(doc.fields[0]);
|
|
|
|
sinon.stub(doc, 'canInsertField').resolves(false);
|
2017-06-06 09:07:12 +00:00
|
|
|
setAddEditItems(testItems[0]);
|
|
|
|
|
|
|
|
await execCommand('addEditCitation', docID);
|
|
|
|
assert.isTrue(displayAlertStub.called);
|
|
|
|
assert.equal(doc.fields.length, 2);
|
|
|
|
assert.equal(doc.fields[0].text, origText);
|
|
|
|
});
|
|
|
|
it('should set dontUpdate: true if "yes" selected in refresh prompt', async function() {
|
|
|
|
await insertMultipleCitations.call(this);
|
|
|
|
var docID = this.test.fullTitle();
|
|
|
|
var doc = applications[docID].doc;
|
|
|
|
|
2018-03-28 13:22:52 +00:00
|
|
|
var citation = await (new Zotero.Integration.CitationField(doc.fields[0], doc.fields[0].code)).unserialize();
|
2017-06-06 09:07:12 +00:00
|
|
|
assert.isNotOk(citation.properties.dontUpdate);
|
|
|
|
doc.fields[0].text = "modified";
|
|
|
|
// Return Yes
|
2018-03-28 13:22:52 +00:00
|
|
|
displayAlertStub.resolves(1);
|
2017-06-06 09:07:12 +00:00
|
|
|
|
|
|
|
await execCommand('refresh', docID);
|
|
|
|
assert.isTrue(displayAlertStub.called);
|
|
|
|
assert.equal(doc.fields.length, 2);
|
|
|
|
assert.equal(doc.fields[0].text, "modified");
|
2018-03-28 13:22:52 +00:00
|
|
|
var citation = await (new Zotero.Integration.CitationField(doc.fields[0], doc.fields[0].code)).unserialize();
|
2017-06-06 09:07:12 +00:00
|
|
|
assert.isOk(citation.properties.dontUpdate);
|
|
|
|
});
|
|
|
|
it('should reset citation text if "no" selected in refresh prompt', async function() {
|
|
|
|
await insertMultipleCitations.call(this);
|
|
|
|
var docID = this.test.fullTitle();
|
|
|
|
var doc = applications[docID].doc;
|
|
|
|
|
2018-03-28 13:22:52 +00:00
|
|
|
var citation = await (new Zotero.Integration.CitationField(doc.fields[0], doc.fields[0].code)).unserialize();
|
2017-06-06 09:07:12 +00:00
|
|
|
assert.isNotOk(citation.properties.dontUpdate);
|
|
|
|
let origText = doc.fields[0].text;
|
|
|
|
doc.fields[0].text = "modified";
|
|
|
|
// Return No
|
2018-03-28 13:22:52 +00:00
|
|
|
displayAlertStub.resolves(0);
|
2017-06-06 09:07:12 +00:00
|
|
|
|
|
|
|
await execCommand('refresh', docID);
|
|
|
|
assert.isTrue(displayAlertStub.called);
|
|
|
|
assert.equal(doc.fields.length, 2);
|
|
|
|
assert.equal(doc.fields[0].text, origText);
|
2018-03-28 13:22:52 +00:00
|
|
|
var citation = await (new Zotero.Integration.CitationField(doc.fields[0], doc.fields[0].code)).unserialize();
|
2017-06-06 09:07:12 +00:00
|
|
|
assert.isNotOk(citation.properties.dontUpdate);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-03-28 11:55:46 +00:00
|
|
|
describe('when there are copy-pasted citations', function() {
|
2018-03-26 12:31:26 +00:00
|
|
|
it('should resolve duplicate citationIDs and mark both as new citations', async function() {
|
|
|
|
var docID = this.test.fullTitle();
|
|
|
|
if (!(docID in applications)) initDoc(docID);
|
|
|
|
var doc = applications[docID].doc;
|
|
|
|
|
|
|
|
setAddEditItems(testItems[0]);
|
|
|
|
await execCommand('addEditCitation', docID);
|
|
|
|
assert.equal(doc.fields.length, 1);
|
|
|
|
// Add a duplicate
|
|
|
|
doc.fields.push(new DocumentPluginDummy.Field(doc));
|
|
|
|
doc.fields[1].code = doc.fields[0].code;
|
|
|
|
doc.fields[1].text = doc.fields[0].text;
|
|
|
|
|
2020-07-30 12:42:54 +00:00
|
|
|
var originalUpdateDocument = Zotero.Integration.Session.prototype.updateDocument;
|
|
|
|
var stubUpdateDocument = sinon.stub(Zotero.Integration.Session.prototype, 'updateDocument');
|
2018-03-26 12:31:26 +00:00
|
|
|
try {
|
|
|
|
var indicesLength;
|
|
|
|
stubUpdateDocument.callsFake(function() {
|
2020-07-30 12:42:54 +00:00
|
|
|
indicesLength = Object.keys(this.newIndices).length;
|
2018-03-26 12:31:26 +00:00
|
|
|
return originalUpdateDocument.apply(this, arguments);
|
|
|
|
});
|
|
|
|
|
|
|
|
setAddEditItems(testItems[1]);
|
|
|
|
await execCommand('addEditCitation', docID);
|
|
|
|
assert.equal(indicesLength, 3);
|
|
|
|
} finally {
|
|
|
|
stubUpdateDocument.restore();
|
|
|
|
}
|
|
|
|
});
|
2018-03-28 11:55:46 +00:00
|
|
|
|
|
|
|
it('should successfully process citations copied in from another doc', async function() {
|
|
|
|
var docID = this.test.fullTitle();
|
|
|
|
if (!(docID in applications)) initDoc(docID);
|
|
|
|
var doc = applications[docID].doc;
|
|
|
|
|
|
|
|
setAddEditItems(testItems[0]);
|
|
|
|
await execCommand('addEditCitation', docID);
|
|
|
|
assert.equal(doc.fields.length, 1);
|
|
|
|
doc.fields.push(new DocumentPluginDummy.Field(doc));
|
|
|
|
// Add a "citation copied from somewhere else"
|
|
|
|
// the content doesn't really matter, just make sure that the citationID is different
|
|
|
|
var newCitationID = Zotero.Utilities.randomString();
|
|
|
|
doc.fields[1].code = doc.fields[0].code;
|
|
|
|
doc.fields[1].code = doc.fields[1].code.replace(/"citationID":"[A-Za-z0-9^"]*"/,
|
|
|
|
`"citationID":"${newCitationID}"`);
|
|
|
|
doc.fields[1].text = doc.fields[0].text;
|
|
|
|
|
2020-07-30 12:42:54 +00:00
|
|
|
var originalUpdateDocument = Zotero.Integration.Session.prototype.updateDocument;
|
|
|
|
var stubUpdateDocument = sinon.stub(Zotero.Integration.Session.prototype, 'updateDocument');
|
2018-03-28 11:55:46 +00:00
|
|
|
try {
|
|
|
|
var indices;
|
|
|
|
stubUpdateDocument.callsFake(function() {
|
2020-07-30 12:42:54 +00:00
|
|
|
indices = Object.keys(this.newIndices);
|
2018-03-28 11:55:46 +00:00
|
|
|
return originalUpdateDocument.apply(this, arguments);
|
|
|
|
});
|
|
|
|
|
|
|
|
setAddEditItems(testItems[1]);
|
|
|
|
await execCommand('addEditCitation', docID);
|
|
|
|
assert.equal(indices.length, 2);
|
|
|
|
assert.include(indices, '1');
|
|
|
|
assert.include(indices, '2');
|
|
|
|
} finally {
|
|
|
|
stubUpdateDocument.restore();
|
|
|
|
}
|
|
|
|
});
|
2019-04-08 13:43:29 +00:00
|
|
|
|
|
|
|
it('should successfully insert a citation after canceled citation insert', async function () {
|
|
|
|
var docID = this.test.fullTitle();
|
|
|
|
if (!(docID in applications)) initDoc(docID);
|
|
|
|
var doc = applications[docID].doc;
|
|
|
|
|
|
|
|
setAddEditItems(testItems[0]);
|
|
|
|
await execCommand('addEditCitation', docID);
|
|
|
|
assert.equal(doc.fields.length, 1);
|
|
|
|
doc.fields.push(new DocumentPluginDummy.Field(doc));
|
|
|
|
// Add a "citation copied from somewhere else"
|
|
|
|
// the content doesn't really matter, just make sure that the citationID is different
|
|
|
|
var newCitationID = Zotero.Utilities.randomString();
|
|
|
|
doc.fields[1].code = doc.fields[0].code;
|
|
|
|
doc.fields[1].code = doc.fields[1].code.replace(/"citationID":"[A-Za-z0-9^"]*"/,
|
|
|
|
`"citationID":"${newCitationID}"`);
|
|
|
|
doc.fields[1].text = doc.fields[0].text;
|
|
|
|
|
|
|
|
setAddEditItems([]);
|
|
|
|
await execCommand('addEditCitation', docID);
|
|
|
|
|
|
|
|
setAddEditItems(testItems[1]);
|
|
|
|
await execCommand('addEditCitation', docID);
|
|
|
|
assert.notEqual(doc.fields[2].code, "TEMP");
|
|
|
|
});
|
2018-03-26 12:31:26 +00:00
|
|
|
});
|
|
|
|
|
2017-06-06 09:07:12 +00:00
|
|
|
describe('when delayCitationUpdates is set', function() {
|
|
|
|
it('should insert a citation with wave underlining', function* (){
|
|
|
|
yield insertMultipleCitations.call(this);
|
|
|
|
var docID = this.test.fullTitle();
|
|
|
|
var doc = applications[docID].doc;
|
|
|
|
var data = new Zotero.Integration.DocumentData(doc.data);
|
|
|
|
data.prefs.delayCitationUpdates = true;
|
|
|
|
doc.data = data.serialize();
|
|
|
|
|
|
|
|
var setTextSpy = sinon.spy(DocumentPluginDummy.Field.prototype, 'setText');
|
|
|
|
setAddEditItems(testItems[3]);
|
|
|
|
yield execCommand('addEditCitation', docID);
|
|
|
|
assert.isTrue(setTextSpy.lastCall.args[0].includes('\\uldash'));
|
|
|
|
|
|
|
|
setTextSpy.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not write to any other fields besides the one being updated', function* () {
|
|
|
|
yield insertMultipleCitations.call(this);
|
|
|
|
var docID = this.test.fullTitle();
|
|
|
|
var doc = applications[docID].doc;
|
|
|
|
var data = new Zotero.Integration.DocumentData(doc.data);
|
|
|
|
data.prefs.delayCitationUpdates = true;
|
|
|
|
doc.data = data.serialize();
|
|
|
|
|
|
|
|
var setTextSpy = sinon.spy(DocumentPluginDummy.Field.prototype, 'setText');
|
|
|
|
var setCodeSpy = sinon.spy(DocumentPluginDummy.Field.prototype, 'setCode');
|
|
|
|
|
|
|
|
setAddEditItems(testItems[3]);
|
|
|
|
yield execCommand('addEditCitation', docID);
|
|
|
|
var field = setTextSpy.firstCall.thisValue;
|
|
|
|
|
|
|
|
for (let i = 0; i < setTextSpy.callCount; i++) {
|
2018-03-28 13:22:52 +00:00
|
|
|
assert.isTrue(yield field.equals(setTextSpy.getCall(i).thisValue));
|
2017-06-06 09:07:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < setCodeSpy.callCount; i++) {
|
2018-03-28 13:22:52 +00:00
|
|
|
assert.isTrue(yield field.equals(setCodeSpy.getCall(i).thisValue));
|
2017-06-06 09:07:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setTextSpy.restore();
|
|
|
|
setCodeSpy.restore();
|
|
|
|
})
|
|
|
|
});
|
2019-07-04 12:16:18 +00:00
|
|
|
|
|
|
|
describe('with retracted items', function () {
|
|
|
|
it('should display a retraction warning for a retracted item in a document', async function () {
|
|
|
|
var docID = this.test.fullTitle();
|
|
|
|
await initDoc(docID);
|
|
|
|
var doc = applications[docID].doc;
|
|
|
|
setAddEditItems(testItems[0]);
|
|
|
|
await execCommand('addEditCitation', docID);
|
|
|
|
|
|
|
|
let stub1 = sinon.stub(Zotero.Retractions, 'isRetracted').returns(true);
|
|
|
|
let stub2 = sinon.stub(Zotero.Retractions, 'shouldShowCitationWarning').returns(true);
|
|
|
|
|
|
|
|
let promise = execCommand('refresh', docID);
|
|
|
|
await assert.isFulfilled(waitForDialog());
|
|
|
|
|
|
|
|
stub1.restore();
|
|
|
|
stub2.restore();
|
|
|
|
await promise;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should display a retraction warning for an embedded retracted item in a document', async function () {
|
|
|
|
var docID = this.test.fullTitle();
|
|
|
|
await initDoc(docID);
|
|
|
|
var doc = applications[docID].doc;
|
|
|
|
let testItem = await createDataObject('item', { libraryID: Zotero.Libraries.userLibraryID });
|
|
|
|
testItem.setField('title', `embedded title`);
|
|
|
|
testItem.setCreator(0, { creatorType: 'author', name: `Embedded Author` });
|
|
|
|
setAddEditItems(testItem);
|
|
|
|
await execCommand('addEditCitation', docID);
|
|
|
|
await testItem.eraseTx();
|
|
|
|
|
|
|
|
let stub = sinon.stub(Zotero.Retractions, 'getRetractionsFromJSON').resolves([0]);
|
|
|
|
|
|
|
|
let promise = execCommand('refresh', docID);
|
|
|
|
await assert.isFulfilled(waitForDialog());
|
|
|
|
|
|
|
|
stub.restore();
|
|
|
|
await promise;
|
|
|
|
});
|
|
|
|
|
2019-07-04 12:46:20 +00:00
|
|
|
it('should not display retraction warning when disabled for a retracted item', async function () {
|
2019-07-04 12:16:18 +00:00
|
|
|
var docID = this.test.fullTitle();
|
|
|
|
await initDoc(docID);
|
|
|
|
var doc = applications[docID].doc;
|
|
|
|
let testItem = await createDataObject('item', { libraryID: Zotero.Libraries.userLibraryID });
|
|
|
|
testItem.setField('title', `title`);
|
|
|
|
testItem.setCreator(0, { creatorType: 'author', name: `Author` });
|
|
|
|
await Zotero.Retractions.disableCitationWarningsForItem(testItem);
|
|
|
|
setAddEditItems(testItem);
|
|
|
|
await execCommand('addEditCitation', docID);
|
|
|
|
|
|
|
|
await assert.isFulfilled(execCommand('refresh', docID));
|
|
|
|
|
|
|
|
await testItem.eraseTx();
|
|
|
|
});
|
|
|
|
});
|
2017-06-01 10:03:51 +00:00
|
|
|
});
|
|
|
|
|
2019-07-04 12:46:20 +00:00
|
|
|
describe('#addEditBibliography', function() {
|
2017-04-11 13:16:37 +00:00
|
|
|
var docID = this.fullTitle();
|
|
|
|
beforeEach(function* () {
|
2019-07-04 12:38:40 +00:00
|
|
|
setAddEditItems(testItems[0]);
|
2018-03-28 13:22:52 +00:00
|
|
|
yield initDoc(docID);
|
2017-04-11 13:16:37 +00:00
|
|
|
yield execCommand('addEditCitation', docID);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should insert bibliography if no bibliography field present', function* () {
|
2018-03-26 12:31:26 +00:00
|
|
|
displayDialogStub.resetHistory();
|
2017-04-11 13:16:37 +00:00
|
|
|
yield execCommand('addEditBibliography', docID);
|
2017-06-01 10:03:51 +00:00
|
|
|
assert.isFalse(displayDialogStub.called);
|
2017-04-11 13:16:37 +00:00
|
|
|
var biblPresent = false;
|
|
|
|
for (let i = applications[docID].doc.fields.length-1; i >= 0; i--) {
|
2018-03-28 13:22:52 +00:00
|
|
|
let field = yield Zotero.Integration.Field.loadExisting(applications[docID].doc.fields[i]);
|
2017-06-01 10:03:51 +00:00
|
|
|
if (field.type == INTEGRATION_TYPE_BIBLIOGRAPHY) {
|
2017-04-11 13:16:37 +00:00
|
|
|
biblPresent = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert.isTrue(biblPresent);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should display the edit bibliography dialog if bibliography present', function* () {
|
|
|
|
yield execCommand('addEditBibliography', docID);
|
2018-03-26 12:31:26 +00:00
|
|
|
displayDialogStub.resetHistory();
|
2017-04-11 13:16:37 +00:00
|
|
|
yield execCommand('addEditBibliography', docID);
|
|
|
|
assert.isTrue(displayDialogStub.calledOnce);
|
2017-05-25 07:48:43 +00:00
|
|
|
assert.isTrue(displayDialogStub.lastCall.args[0].includes('editBibliographyDialog'));
|
2017-04-11 13:16:37 +00:00
|
|
|
});
|
|
|
|
});
|
2017-04-12 08:40:28 +00:00
|
|
|
});
|
2017-04-20 10:39:19 +00:00
|
|
|
|
|
|
|
describe("DocumentData", function() {
|
2017-04-20 12:29:56 +00:00
|
|
|
it('should properly unserialize old XML document data', function() {
|
2017-05-02 13:29:19 +00:00
|
|
|
var serializedXMLData = "<data data-version=\"3\" zotero-version=\"5.0.SOURCE\"><session id=\"F0NFmZ32\"/><style id=\"http://www.zotero.org/styles/cell\" hasBibliography=\"1\" bibliographyStyleHasBeenSet=\"1\"/><prefs><pref name=\"fieldType\" value=\"ReferenceMark\"/><pref name=\"automaticJournalAbbreviations\" value=\"true\"/><pref name=\"noteType\" value=\"0\"/></prefs></data>";
|
2017-04-20 10:39:19 +00:00
|
|
|
var data = new Zotero.Integration.DocumentData(serializedXMLData);
|
|
|
|
var expectedData = {
|
|
|
|
style: {
|
|
|
|
styleID: 'http://www.zotero.org/styles/cell',
|
|
|
|
locale: null,
|
|
|
|
hasBibliography: true,
|
|
|
|
bibliographyStyleHasBeenSet: true
|
|
|
|
},
|
|
|
|
prefs: {
|
|
|
|
fieldType: 'ReferenceMark',
|
|
|
|
automaticJournalAbbreviations: true,
|
2017-05-02 12:09:12 +00:00
|
|
|
noteType: 0
|
2017-04-20 10:39:19 +00:00
|
|
|
},
|
|
|
|
sessionID: 'F0NFmZ32',
|
|
|
|
zoteroVersion: '5.0.SOURCE',
|
|
|
|
dataVersion: '3'
|
|
|
|
};
|
|
|
|
// Convert to JSON to remove functions from DocumentData object
|
|
|
|
assert.equal(JSON.stringify(data), JSON.stringify(expectedData));
|
|
|
|
});
|
|
|
|
|
2017-04-20 12:29:56 +00:00
|
|
|
it('should properly unserialize JSON document data', function() {
|
|
|
|
var expectedData = JSON.stringify({
|
|
|
|
style: {
|
|
|
|
styleID: 'http://www.zotero.org/styles/cell',
|
|
|
|
locale: 'en-US',
|
|
|
|
hasBibliography: true,
|
|
|
|
bibliographyStyleHasBeenSet: true
|
|
|
|
},
|
|
|
|
prefs: {
|
|
|
|
fieldType: 'ReferenceMark',
|
|
|
|
automaticJournalAbbreviations: false,
|
|
|
|
noteType: 0
|
|
|
|
},
|
|
|
|
sessionID: 'owl-sesh',
|
|
|
|
zoteroVersion: '5.0.SOURCE',
|
|
|
|
dataVersion: 4
|
|
|
|
});
|
|
|
|
var data = new Zotero.Integration.DocumentData(expectedData);
|
|
|
|
// Convert to JSON to remove functions from DocumentData object
|
|
|
|
assert.equal(JSON.stringify(data), expectedData);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should properly serialize document data to XML (data ver 3)', function() {
|
2017-04-20 10:39:19 +00:00
|
|
|
sinon.spy(Zotero, 'debug');
|
|
|
|
var data = new Zotero.Integration.DocumentData();
|
|
|
|
data.sessionID = "owl-sesh";
|
|
|
|
data.zoteroVersion = Zotero.version;
|
|
|
|
data.dataVersion = 3;
|
|
|
|
data.style = {
|
|
|
|
styleID: 'http://www.zotero.org/styles/cell',
|
|
|
|
locale: 'en-US',
|
|
|
|
hasBibliography: false,
|
|
|
|
bibliographyStyleHasBeenSet: true
|
|
|
|
};
|
|
|
|
data.prefs = {
|
|
|
|
noteType: 1,
|
|
|
|
fieldType: "Field",
|
|
|
|
automaticJournalAbbreviations: true
|
|
|
|
};
|
|
|
|
|
2017-04-20 12:29:56 +00:00
|
|
|
var serializedData = data.serialize();
|
|
|
|
// Make sure we serialized to XML here
|
|
|
|
assert.equal(serializedData[0], '<');
|
2017-04-20 10:39:19 +00:00
|
|
|
// Serialize and unserialize (above test makes sure unserialize works properly).
|
2017-04-20 12:29:56 +00:00
|
|
|
var processedData = new Zotero.Integration.DocumentData(serializedData);
|
2017-04-20 10:39:19 +00:00
|
|
|
|
|
|
|
// This isn't ideal, but currently how it works. Better serialization which properly retains types
|
|
|
|
// coming with official 5.0 release.
|
|
|
|
data.dataVersion = "3";
|
|
|
|
|
|
|
|
// Convert to JSON to remove functions from DocumentData objects
|
|
|
|
assert.equal(JSON.stringify(processedData), JSON.stringify(data));
|
|
|
|
|
|
|
|
// Make sure we are not triggering debug traces in Utilities.htmlSpecialChars()
|
|
|
|
assert.isFalse(Zotero.debug.calledWith(sinon.match.string, 1));
|
|
|
|
Zotero.debug.restore();
|
|
|
|
});
|
2017-04-20 12:29:56 +00:00
|
|
|
|
|
|
|
it('should properly serialize document data to JSON (data ver 4)', function() {
|
|
|
|
var data = new Zotero.Integration.DocumentData();
|
|
|
|
// data version 4 triggers serialization to JSON
|
|
|
|
// (e.g. when we've retrieved data from the doc and it was ver 4 already)
|
|
|
|
data.dataVersion = 4;
|
|
|
|
data.sessionID = "owl-sesh";
|
|
|
|
data.style = {
|
|
|
|
styleID: 'http://www.zotero.org/styles/cell',
|
|
|
|
locale: 'en-US',
|
|
|
|
hasBibliography: false,
|
|
|
|
bibliographyStyleHasBeenSet: true
|
|
|
|
};
|
|
|
|
data.prefs = {
|
|
|
|
noteType: 1,
|
|
|
|
fieldType: "Field",
|
|
|
|
automaticJournalAbbreviations: true
|
|
|
|
};
|
|
|
|
|
|
|
|
// Serialize and unserialize (above tests makes sure unserialize works properly).
|
|
|
|
var processedData = new Zotero.Integration.DocumentData(data.serialize());
|
|
|
|
|
|
|
|
// Added in serialization routine
|
|
|
|
data.zoteroVersion = Zotero.version;
|
|
|
|
|
|
|
|
// Convert to JSON to remove functions from DocumentData objects
|
|
|
|
assert.deepEqual(JSON.parse(JSON.stringify(processedData)), JSON.parse(JSON.stringify(data)));
|
|
|
|
});
|
2017-04-20 10:39:19 +00:00
|
|
|
})
|
2017-04-06 11:19:25 +00:00
|
|
|
});
|