Accept ISO dates in Item::setField()

This commit is contained in:
Dan Stillman 2016-03-11 03:28:38 -05:00
parent 28eaaaf2bf
commit 7a03b1e527
2 changed files with 58 additions and 6 deletions

View file

@ -663,10 +663,16 @@ Zotero.Item.prototype.setField = function(field, value, loadIn) {
switch (field) {
case 'itemTypeID':
case 'dateAdded':
break;
case 'dateAdded':
case 'dateModified':
// Accept ISO dates
if (Zotero.Date.isISODate(value)) {
let d = Zotero.Date.isoToDate(value);
value = Zotero.Date.dateToSQL(d, true);
}
// Make sure it's valid
let date = Zotero.Date.sqlToDate(value, true);
if (!date) throw new Error("Invalid SQL date: " + value);
@ -790,11 +796,18 @@ Zotero.Item.prototype.setField = function(field, value, loadIn) {
}
// Validate access date
else if (fieldID == Zotero.ItemFields.getID('accessDate')) {
if (value && (!Zotero.Date.isSQLDate(value) &&
!Zotero.Date.isSQLDateTime(value) &&
value != 'CURRENT_TIMESTAMP')) {
Zotero.debug("Discarding invalid accessDate '" + value + "' in Item.setField()");
return false;
if (value && value != 'CURRENT_TIMESTAMP') {
// Accept ISO dates
if (Zotero.Date.isISODate(value)) {
let d = Zotero.Date.isoToDate(value);
value = Zotero.Date.dateToSQL(d, true);
}
if (!Zotero.Date.isSQLDate(value) && !Zotero.Date.isSQLDateTime(value)) {
Zotero.logError(`Discarding invalid ${field} '${value}' for `
+ `item ${this.libraryKey} in setField()`);
return false;
}
}
}

View file

@ -135,6 +135,45 @@ describe("Zotero.Item", function () {
item = yield Zotero.Items.getAsync(id);
assert.equal(item.getField("versionNumber"), "1.0");
});
it("should accept ISO 8601 dates", function* () {
var fields = {
accessDate: "2015-06-07T20:56:00Z",
dateAdded: "2015-06-07T20:57:00Z",
dateModified: "2015-06-07T20:58:00Z",
};
var item = createUnsavedDataObject('item');
for (let i in fields) {
item.setField(i, fields[i]);
}
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 accept SQL dates", function* () {
var fields = {
accessDate: "2015-06-07 20:56:00",
dateAdded: "2015-06-07 20:57:00",
dateModified: "2015-06-07 20:58:00",
};
var item = createUnsavedDataObject('item');
for (let i in fields) {
item.setField(i, fields[i]);
item.getField(i, fields[i]);
}
})
it("should ignore unknown accessDate values", function* () {
var fields = {
accessDate: "foo"
};
var item = createUnsavedDataObject('item');
for (let i in fields) {
item.setField(i, fields[i]);
}
assert.strictEqual(item.getField('accessDate'), '');
})
})
describe("#dateAdded", function () {