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.
This commit is contained in:
Dan Stillman 2015-06-07 17:39:40 -04:00
parent 337a835fed
commit 8448203583
3 changed files with 67 additions and 2 deletions

View file

@ -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':

View file

@ -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);

View file

@ -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 nonISO 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({