Mendeley import: Remove code to patch after earlier imports (#2234)
Fixes #2233, Mendeley import: Invalid-field-for-type error
This commit is contained in:
parent
c9d4ff4314
commit
882ecc205e
6 changed files with 248 additions and 11 deletions
|
@ -1235,17 +1235,6 @@ Zotero_Import_Mendeley.prototype._saveItems = async function (libraryID, json) {
|
|||
if (item.isRegularItem()) {
|
||||
lastExistingParentItem = item;
|
||||
|
||||
// Fill in Publication if empty, since it was missed in an early version of the
|
||||
// online importer
|
||||
if (itemJSON.publicationTitle && !item.getField('publicationTitle')) {
|
||||
item.setField('publicationTitle', itemJSON.publicationTitle);
|
||||
}
|
||||
|
||||
// Add "★" tag if not present, since it was previously missed in the online importer
|
||||
if ((itemJSON.tags || []).includes('\u2605') && !item.hasTag('\u2605')) {
|
||||
item.addTag('\u2605');
|
||||
}
|
||||
|
||||
// Update any child items to point to the existing item's key instead of the
|
||||
// new generated one
|
||||
this._updateParentKeys('item', json, i + 1, itemJSON.key, item.key);
|
||||
|
|
14
test/tests/data/mendeleyMock/folders-simple.json
Normal file
14
test/tests/data/mendeleyMock/folders-simple.json
Normal file
|
@ -0,0 +1,14 @@
|
|||
[
|
||||
{
|
||||
"id": "8d2f262d-49b3-4dfc-8968-0bb71bcd92ea",
|
||||
"name": "folder1",
|
||||
"created": "2021-11-03T15:05:53.000Z",
|
||||
"modified": "2021-11-03T15:05:54.000Z"
|
||||
},
|
||||
{
|
||||
"id": "6f6009f7-c03f-4b6e-83a5-98edcbee391e",
|
||||
"name": "folder2",
|
||||
"created": "2021-11-03T15:05:53.000Z",
|
||||
"modified": "2021-11-03T15:05:53.000Z"
|
||||
}
|
||||
]
|
37
test/tests/data/mendeleyMock/items-simple.json
Normal file
37
test/tests/data/mendeleyMock/items-simple.json
Normal file
|
@ -0,0 +1,37 @@
|
|||
[
|
||||
{
|
||||
"authored": false,
|
||||
"confirmed": true,
|
||||
"created": "2021-11-02T09:54:28.353Z",
|
||||
"file_attached": false,
|
||||
"hidden": false,
|
||||
"id": "7fea3cb3-f97d-3f16-8fad-f59caaa71688",
|
||||
"last_modified": "2021-11-02T12:26:30.025Z",
|
||||
"private_publication": false,
|
||||
"profile_id": "8dbf0832-8723-4c48-b532-20c0b7f6e01a",
|
||||
"read": false,
|
||||
"source": "lorem ipsum",
|
||||
"starred": false,
|
||||
"title": "Foo Bar",
|
||||
"type": "journal",
|
||||
"folder_uuids": [
|
||||
"8d2f262d-49b3-4dfc-8968-0bb71bcd92ea"
|
||||
],
|
||||
"year": 1987
|
||||
}, {
|
||||
"authored": false,
|
||||
"confirmed": true,
|
||||
"created": "2021-11-04T11:53:10.353Z",
|
||||
"file_attached": false,
|
||||
"hidden": false,
|
||||
"id": "07a74c26-28d1-4d9f-a60d-3f3bc5ef76ef",
|
||||
"last_modified": "2021-11-04T11:53:10.353Z",
|
||||
"private_publication": false,
|
||||
"profile_id": "8dbf0832-8723-4c48-b532-20c0b7f6e01a",
|
||||
"read": false,
|
||||
"starred": false,
|
||||
"title": "Sample Report",
|
||||
"type": "report",
|
||||
"year": 2002
|
||||
}
|
||||
]
|
19
test/tests/data/mendeleyMock/items-updated.json
Normal file
19
test/tests/data/mendeleyMock/items-updated.json
Normal file
|
@ -0,0 +1,19 @@
|
|||
[
|
||||
{
|
||||
"authored": false,
|
||||
"confirmed": true,
|
||||
"created": "2021-11-04T11:53:10.353Z",
|
||||
"file_attached": false,
|
||||
"hidden": false,
|
||||
"id": "07a74c26-28d1-4d9f-a60d-3f3bc5ef76ef",
|
||||
"last_modified": "2021-11-05T11:53:10.353Z",
|
||||
"private_publication": false,
|
||||
"profile_id": "8dbf0832-8723-4c48-b532-20c0b7f6e01a",
|
||||
"read": false,
|
||||
"starred": true,
|
||||
"title": "Report updated to Journal Article",
|
||||
"type": "journal",
|
||||
"source": "lorem ipsum",
|
||||
"year": 2002
|
||||
}
|
||||
]
|
24
test/tests/data/mendeleyMock/user.json
Normal file
24
test/tests/data/mendeleyMock/user.json
Normal file
|
@ -0,0 +1,24 @@
|
|||
{
|
||||
"academic_status": "Unspecified",
|
||||
"created": "2021-04-26T09:07:29.000Z",
|
||||
"disciplines":
|
||||
[
|
||||
{
|
||||
"name": "Unspecified",
|
||||
"subdisciplines":
|
||||
[]
|
||||
}
|
||||
],
|
||||
"display_name": "some name",
|
||||
"email": "zotero@zotero.com",
|
||||
"first_name": "some",
|
||||
"id": "97d3193e-2d2a-4700-abd6-d82335ef551d",
|
||||
"last_name": "name",
|
||||
"marketing": true,
|
||||
"member_type": "free",
|
||||
"photos": [],
|
||||
"scopus_author_ids": [],
|
||||
"user_type": "normal",
|
||||
"verified": false,
|
||||
"web_user_id": 1234567890
|
||||
}
|
154
test/tests/mendeleyImport.js
Normal file
154
test/tests/mendeleyImport.js
Normal file
|
@ -0,0 +1,154 @@
|
|||
/* global setHTTPResponse:false, sinon: false, Zotero_Import_Mendeley: false */
|
||||
|
||||
describe('Zotero_Import_Mendeley', function () {
|
||||
var server, importer;
|
||||
|
||||
before(async () => {
|
||||
Components.utils.import('chrome://zotero/content/import/mendeley/mendeleyImport.js');
|
||||
importer = new Zotero_Import_Mendeley();
|
||||
importer.mendeleyCode = 'CODE';
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
Zotero.HTTP.mock = sinon.FakeXMLHttpRequest;
|
||||
server = sinon.fakeServer.create();
|
||||
server.autoRespond = true;
|
||||
setHTTPResponse(server, 'https://www.zotero.org/', {
|
||||
method: 'POST',
|
||||
url: `utils/mendeley/oauth`,
|
||||
status: 200,
|
||||
headers: {},
|
||||
json: {
|
||||
access_token: 'ACCESS_TOKEN', // eslint-disable-line camelcase
|
||||
token_type: 'bearer', // eslint-disable-line camelcase
|
||||
expires_in: 3600, // eslint-disable-line camelcase
|
||||
refresh_token: 'REFRESH_TOKEN', // eslint-disable-line camelcase
|
||||
msso: null,
|
||||
scope: 'all'
|
||||
}
|
||||
});
|
||||
|
||||
setHTTPResponse(server, 'https://api.mendeley.com/', {
|
||||
method: 'GET',
|
||||
url: `folders?limit=500`,
|
||||
status: 200,
|
||||
headers: {},
|
||||
json: JSON.parse(
|
||||
await Zotero.File.getContentsFromURLAsync('resource://zotero-unit-tests/data/mendeleyMock/folders-simple.json')
|
||||
)
|
||||
});
|
||||
|
||||
setHTTPResponse(server, 'https://api.mendeley.com/', {
|
||||
method: 'GET',
|
||||
url: `annotations?limit=200`,
|
||||
status: 200,
|
||||
headers: {},
|
||||
json: []
|
||||
});
|
||||
|
||||
setHTTPResponse(server, 'https://api.mendeley.com/', {
|
||||
method: 'GET',
|
||||
url: `documents?view=all&limit=500`,
|
||||
status: 200,
|
||||
headers: {},
|
||||
json: JSON.parse(
|
||||
await Zotero.File.getContentsFromURLAsync('resource://zotero-unit-tests/data/mendeleyMock/items-simple.json')
|
||||
)
|
||||
});
|
||||
|
||||
setHTTPResponse(server, 'https://api.mendeley.com/', {
|
||||
method: 'GET',
|
||||
url: `profiles/v2/me?`,
|
||||
status: 200,
|
||||
headers: {},
|
||||
json: JSON.parse(
|
||||
await Zotero.File.getContentsFromURLAsync('resource://zotero-unit-tests/data/mendeleyMock/user.json')
|
||||
)
|
||||
});
|
||||
|
||||
setHTTPResponse(server, 'https://api.mendeley.com/', {
|
||||
method: 'GET',
|
||||
url: `groups/v2?type=all&limit=500`,
|
||||
status: 200,
|
||||
headers: {},
|
||||
json: []
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
Zotero.HTTP.mock = null;
|
||||
});
|
||||
|
||||
describe('#import', () => {
|
||||
it("should import items & collections", async () => {
|
||||
|
||||
await importer.translate({
|
||||
libraryID: Zotero.Libraries.userLibraryID,
|
||||
collections: null,
|
||||
linkFiles: false,
|
||||
});
|
||||
|
||||
const journal = (await Zotero.Relations
|
||||
.getByPredicateAndObject('item', 'mendeleyDB:documentUUID', '7fea3cb3-f97d-3f16-8fad-f59caaa71688'))
|
||||
.filter(item => item.libraryID == Zotero.Libraries.userLibraryID && !item.deleted)
|
||||
.shift();
|
||||
|
||||
const report = (await Zotero.Relations
|
||||
.getByPredicateAndObject('item', 'mendeleyDB:documentUUID', '07a74c26-28d1-4d9f-a60d-3f3bc5ef76ef'))
|
||||
.filter(item => item.libraryID == Zotero.Libraries.userLibraryID && !item.deleted)
|
||||
.shift();
|
||||
|
||||
assert.equal(journal.getField('title'), 'Foo Bar');
|
||||
assert.equal(journal.itemTypeID, Zotero.ItemTypes.getID('journalArticle'));
|
||||
assert.equal(report.getField('title'), 'Sample Report');
|
||||
assert.equal(report.itemTypeID, Zotero.ItemTypes.getID('report'));
|
||||
|
||||
const parentCollection = await Zotero.Collections.getAsync(
|
||||
journal.getCollections().pop()
|
||||
);
|
||||
|
||||
assert.equal(parentCollection.name, 'folder1');
|
||||
});
|
||||
|
||||
it("should update previously imported item", async () => {
|
||||
const importer = new Zotero_Import_Mendeley();
|
||||
importer.mendeleyCode = 'CODE';
|
||||
await importer.translate({
|
||||
libraryID: Zotero.Libraries.userLibraryID,
|
||||
collections: null,
|
||||
linkFiles: false,
|
||||
});
|
||||
|
||||
const report = (await Zotero.Relations
|
||||
.getByPredicateAndObject('item', 'mendeleyDB:documentUUID', '07a74c26-28d1-4d9f-a60d-3f3bc5ef76ef'))
|
||||
.filter(item => item.libraryID == Zotero.Libraries.userLibraryID && !item.deleted)
|
||||
.shift();
|
||||
|
||||
assert.equal(report.getField('title'), 'Sample Report');
|
||||
assert.equal(report.getField('year'), '2002');
|
||||
assert.equal(report.itemTypeID, Zotero.ItemTypes.getID('report'));
|
||||
assert.lengthOf(report.getTags(), 0);
|
||||
|
||||
setHTTPResponse(server, 'https://api.mendeley.com/', {
|
||||
method: 'GET',
|
||||
url: `documents?view=all&limit=500`,
|
||||
status: 200,
|
||||
headers: {},
|
||||
json: JSON.parse(
|
||||
await Zotero.File.getContentsFromURLAsync('resource://zotero-unit-tests/data/mendeleyMock/items-updated.json')
|
||||
)
|
||||
});
|
||||
|
||||
await importer.translate({
|
||||
libraryID: Zotero.Libraries.userLibraryID,
|
||||
collections: null,
|
||||
linkFiles: false,
|
||||
});
|
||||
|
||||
assert.equal(report.getField('title'), 'Report updated to Journal Article');
|
||||
assert.equal(report.itemTypeID, Zotero.ItemTypes.getID('journalArticle'));
|
||||
assert.equal(report.getField('year'), '2002');
|
||||
assert.sameMembers(report.getTags().map(t => t.tag), ['\u2605']);
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue