commit 8448203583ff12af2219a673942a5aec2e8b08ff
parent 337a835fedd75bcbbd73cb68588a576520f4ae51
Author: Dan Stillman <dstillman@zotero.org>
Date: Sun, 7 Jun 2015 17:39:40 -0400
Expect ISO 8601 access dates in Zotero.Item::fromJSON()
This causes translator tests to fail, because the sample data currently
has SQL access dates instead.
Diffstat:
3 files changed, 67 insertions(+), 2 deletions(-)
diff --git a/chrome/content/zotero/xpcom/data/item.js b/chrome/content/zotero/xpcom/data/item.js
@@ -3841,9 +3841,22 @@ Zotero.Item.prototype.fromJSON = Zotero.Promise.coroutine(function* (json) {
case 'mtime':
break;
+ case 'accessDate':
case 'dateAdded':
case 'dateModified':
- this[field] = Zotero.Date.dateToSQL(Zotero.Date.isoToDate(val), true);
+ let d = Zotero.Date.isoToDate(val);
+ if (!d) {
+ Zotero.logError("Discarding invalid " + field + " '" + val
+ + "' for item " + this.libraryKey);
+ continue;
+ }
+ val = Zotero.Date.dateToSQL(d, true);
+ if (field == 'accessDate') {
+ this.setField(field, val);
+ }
+ else {
+ this[field] = val;
+ }
break;
case 'parentItem':
diff --git a/chrome/content/zotero/xpcom/date.js b/chrome/content/zotero/xpcom/date.js
@@ -195,10 +195,11 @@ Zotero.Date = new function(){
* Adapted from http://delete.me.uk/2005/03/iso8601.html (AFL-licensed)
*
* @param {String} isoDate ISO 8601 date
- * @return {Date} JS Date
+ * @return {Date|False} - JS Date, or false if not a valid date
*/
this.isoToDate = function (isoDate) {
var d = isoDate.match(_re8601);
+ if (!d) return false;
var offset = 0;
var date = new Date(d[1], 0, 1);
diff --git a/test/tests/itemTest.js b/test/tests/itemTest.js
@@ -754,6 +754,57 @@ describe("Zotero.Item", function () {
})
describe("#fromJSON()", function () {
+ it("should accept ISO 8601 dates", function* () {
+ var json = {
+ itemType: "journalArticle",
+ accessDate: "2015-06-07T20:56:00Z",
+ dateAdded: "2015-06-07T20:57:00Z",
+ dateModified: "2015-06-07T20:58:00Z",
+ };
+ var item = new Zotero.Item;
+ yield item.fromJSON(json);
+ assert.equal(item.getField('accessDate'), '2015-06-07 20:56:00');
+ assert.equal(item.dateAdded, '2015-06-07 20:57:00');
+ assert.equal(item.dateModified, '2015-06-07 20:58:00');
+ })
+
+ it("should ignore non–ISO 8601 dates", function* () {
+ var json = {
+ itemType: "journalArticle",
+ accessDate: "2015-06-07 20:56:00",
+ dateAdded: "2015-06-07 20:57:00",
+ dateModified: "2015-06-07 20:58:00",
+ };
+ var item = new Zotero.Item;
+ yield item.fromJSON(json);
+ assert.strictEqual(item.getField('accessDate'), '');
+ // DEBUG: Should these be null, or empty string like other fields from getField()?
+ assert.isNull(item.dateAdded);
+ assert.isNull(item.dateModified);
+ })
+
+ it("should set creators", function* () {
+ var json = {
+ itemType: "journalArticle",
+ creators: [
+ {
+ firstName: "First",
+ lastName: "Last",
+ creatorType: "author"
+ },
+ {
+ name: "Test Name",
+ creatorType: "editor"
+ }
+ ]
+ };
+
+ var item = new Zotero.Item;
+ yield item.fromJSON(json);
+ var id = yield item.saveTx();
+ assert.sameDeepMembers(item.getCreatorsJSON(), json.creators);
+ })
+
it("should map a base field to an item-specific field", function* () {
var item = new Zotero.Item("bookSection");
yield item.fromJSON({