Fix creator migration from Extra

This commit is contained in:
Dan Stillman 2020-03-15 13:51:04 -04:00
parent 982bcff357
commit 23c936f016
4 changed files with 42 additions and 5 deletions

View file

@ -4631,7 +4631,7 @@ Zotero.Item.prototype.migrateExtraFields = function () {
this.setField(field, value); this.setField(field, value);
} }
if (creators.length) { if (creators.length) {
this.setCreators([...item.getCreators(), ...creators]); this.setCreators([...this.getCreators(), ...creators]);
} }
this.setField('extra', extra); this.setField('extra', extra);
if (!this.hasChanged()) { if (!this.hasChanged()) {

View file

@ -1057,7 +1057,7 @@ Zotero.Utilities.Internal = {
creatorType: possibleCreatorType creatorType: possibleCreatorType
}; };
if (value.includes('||')) { if (value.includes('||')) {
let [first, last] = value.split(/\s*\|\|\s*/); let [last, first] = value.split(/\s*\|\|\s*/);
c.firstName = first; c.firstName = first;
c.lastName = last; c.lastName = last;
} }

View file

@ -74,6 +74,43 @@ describe("Zotero.Schema", function() {
assert.equal(item.getField('extra'), 'number-of-pages: 11\nThis is another line.'); assert.equal(item.getField('extra'), 'number-of-pages: 11\nThis is another line.');
assert.isFalse(item.synced); assert.isFalse(item.synced);
}); });
it("should migrate creator", async function () {
var item = await createDataObject('item', { itemType: 'book' });
item.setCreators([
{
firstName: 'Abc',
lastName: 'Def',
creatorType: 'author',
fieldMode: 0
}
]);
item.setField('extra', 'editor: Last || First\nFoo: Bar');
item.synced = true;
await item.saveTx();
schema.version++;
schema.itemTypes.find(x => x.itemType == 'book').fields.splice(0, 1, { field: 'fooBar' })
var newLocales = {};
Object.keys(schema.locales).forEach((locale) => {
var o = schema.locales[locale];
o.fields.fooBar = 'Foo Bar';
newLocales[locale] = o;
});
await Zotero.Schema._updateGlobalSchemaForTest(schema);
await Zotero.Schema.migrateExtraFields();
var creators = item.getCreators();
assert.lengthOf(creators, 2);
assert.propertyVal(creators[0], 'firstName', 'Abc');
assert.propertyVal(creators[0], 'lastName', 'Def');
assert.propertyVal(creators[0], 'creatorTypeID', Zotero.CreatorTypes.getID('author'));
assert.propertyVal(creators[1], 'firstName', 'First');
assert.propertyVal(creators[1], 'lastName', 'Last');
assert.propertyVal(creators[1], 'creatorTypeID', Zotero.CreatorTypes.getID('editor'));
assert.equal(item.getField('extra'), 'Foo: Bar');
assert.isFalse(item.synced);
});
}); });
}); });

View file

@ -224,7 +224,7 @@ describe("Zotero.Utilities.Internal", function () {
}); });
it("should extract a CSL name", function () { it("should extract a CSL name", function () {
var str = 'container-author: First || Last'; var str = 'container-author: Last || First';
var { creators, extra } = Zotero.Utilities.Internal.extractExtraFields(str); var { creators, extra } = Zotero.Utilities.Internal.extractExtraFields(str);
assert.lengthOf(creators, 1); assert.lengthOf(creators, 1);
assert.propertyVal(creators[0], 'creatorType', 'bookAuthor'); assert.propertyVal(creators[0], 'creatorType', 'bookAuthor');
@ -235,7 +235,7 @@ describe("Zotero.Utilities.Internal", function () {
it("should extract a CSL name that's valid for a given item type", function () { it("should extract a CSL name that's valid for a given item type", function () {
var item = createUnsavedDataObject('item', { itemType: 'bookSection' }); var item = createUnsavedDataObject('item', { itemType: 'bookSection' });
var str = 'container-author: First || Last'; var str = 'container-author: Last || First';
var { creators, extra } = Zotero.Utilities.Internal.extractExtraFields(str, item); var { creators, extra } = Zotero.Utilities.Internal.extractExtraFields(str, item);
assert.lengthOf(creators, 1); assert.lengthOf(creators, 1);
assert.propertyVal(creators[0], 'creatorType', 'bookAuthor'); assert.propertyVal(creators[0], 'creatorType', 'bookAuthor');
@ -246,7 +246,7 @@ describe("Zotero.Utilities.Internal", function () {
it("shouldn't extract a CSL name that's not valid for a given item type", function () { it("shouldn't extract a CSL name that's not valid for a given item type", function () {
var item = createUnsavedDataObject('item', { itemType: 'journalArticle' }); var item = createUnsavedDataObject('item', { itemType: 'journalArticle' });
var str = 'container-author: First || Last'; var str = 'container-author: Last || First';
var { creators, extra } = Zotero.Utilities.Internal.extractExtraFields(str, item); var { creators, extra } = Zotero.Utilities.Internal.extractExtraFields(str, item);
assert.lengthOf(creators, 0); assert.lengthOf(creators, 0);
assert.strictEqual(extra, str); assert.strictEqual(extra, str);