zotero/test/tests/importExportTest.js

144 lines
No EOL
5.6 KiB
JavaScript

describe("Import/Export", function () {
describe("Zotero RDF", function () {
var namespaces = {
rdf: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
dc: 'http://purl.org/dc/elements/1.1/'
};
it("should export related items", async function () {
// Create related items
var item1 = new Zotero.Item('book');
item1.setField('title', 'A');
item1.setField('ISBN', 1421402831);
await item1.saveTx();
var item2 = new Zotero.Item('webpage');
item2.setField('title', 'B');
item2.setField('url', 'http://example.com');
await item2.saveTx();
item1.addRelatedItem(item2);
item2.addRelatedItem(item1);
await item1.saveTx();
await item2.saveTx();
var note1 = await createDataObject('item', { itemType: 'note', parentID: item1.id, note: 'C' });
var note2 = await createDataObject('item', { itemType: 'note', parentID: item1.id, note: 'D' });
note1.addRelatedItem(note2);
note2.addRelatedItem(note1);
await note1.saveTx();
await note2.saveTx();
// Export
var file = OS.Path.join(await getTempDirectory(), 'export.rdf');
var translator = Zotero.Translators.get('14763d24-8ba0-45df-8f52-b8d1108e7ac9');
var displayOptions = {
exportNotes: true
};
var translation = new Zotero.Translate.Export();
translation.setItems([item1, item2]);
translation.setLocation(Zotero.File.pathToFile(file));
translation.setTranslator(translator);
translation.setDisplayOptions(displayOptions);
await translation.translate();
// Parse exported file and look for dc:relation elements
var dp = new DOMParser();
var doc = dp.parseFromString(Zotero.File.getContents(file), 'text/xml');
var item1Node = doc.querySelector(`Book`);
var item2Node = doc.querySelector(`Document`);
var note1Node = doc.querySelector(`Memo[*|about="#item_${note1.id}"]`);
var note2Node = doc.querySelector(`Memo[*|about="#item_${note2.id}"]`);
assert.equal(
Zotero.Utilities.xpath(item1Node, './dc:relation', namespaces)[0]
.getAttributeNS(namespaces.rdf, 'resource'),
'http://example.com'
);
assert.equal(
Zotero.Utilities.xpath(item2Node, './dc:relation', namespaces)[0]
.getAttributeNS(namespaces.rdf, 'resource'),
'urn:isbn:1-4214-0283-1'
);
assert.equal(
Zotero.Utilities.xpath(note1Node, './dc:relation', namespaces)[0]
.getAttributeNS(namespaces.rdf, 'resource'),
'#item_' + note2.id
);
assert.equal(
Zotero.Utilities.xpath(note2Node, './dc:relation', namespaces)[0]
.getAttributeNS(namespaces.rdf, 'resource'),
'#item_' + note1.id
);
});
// Not currently supported
it.skip("should import related items", async function () {
var libraryID = Zotero.Libraries.userLibraryID;
var file = OS.Path.join(getTestDataDirectory().path, 'zotero_rdf.xml');
translation = new Zotero.Translate.Import();
translation.setLocation(Zotero.File.pathToFile(file));
let translators = await translation.getTranslators();
translation.setTranslator(translators[0]);
var newItems = await translation.translate({ libraryID });
assert.lengthOf(newItems, 2); // DEBUG: why aren't child items returned here?
// Parent item
assert.lengthOf(newItems[0].relatedItems, 1);
assert.lengthOf(newItems[1].relatedItems, 1);
assert.sameMembers(newItems[0].relatedItems, [newItems[1]]);
assert.sameMembers(newItems[1].relatedItems, [newItems[0]]);
var notes = newItems[0].getNotes();
assert.lengthOf(notes, 2);
var newNote1 = Zotero.Items.get(notes[0]);
var newNote2 = Zotero.Items.get(notes[1]);
assert.lengthOf(newNote1.relatedItems, 1);
assert.lengthOf(newNote2.relatedItems, 1);
assert.sameMembers(newNote1.relatedItems, [newNote2]);
assert.sameMembers(newNote2.relatedItems, [newNote1]);
});
it("should import standalone PDF attachment", async function () {
var rdf = `<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:z="http://www.zotero.org/namespaces/export#"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:dcterms="http://purl.org/dc/terms/"
xmlns:link="http://purl.org/rss/1.0/modules/link/"
xmlns:bib="http://purl.org/net/biblio#"
xmlns:foaf="http://xmlns.com/foaf/0.1/"
xmlns:prism="http://prismstandard.org/namespaces/1.2/basic/">
<z:Attachment rdf:about="#item_66055">
<z:itemType>attachment</z:itemType>
<rdf:resource rdf:resource="files/1234/test.pdf"/>
<dc:identifier>
<dcterms:URI>
<rdf:value>https://example.com</rdf:value>
</dcterms:URI>
</dc:identifier>
<dcterms:dateSubmitted>2022-07-22 06:36:31</dcterms:dateSubmitted>
<dc:title>Test PDF</dc:title>
<z:linkMode>1</z:linkMode>
<link:type>application/pdf</link:type>
</z:Attachment>
</rdf:RDF>
`;
var libraryID = Zotero.Libraries.userLibraryID;
var tempDir = await getTempDirectory();
var file = OS.Path.join(tempDir, 'export.rdf');
await Zotero.File.putContentsAsync(file, rdf);
var folder = OS.Path.join(tempDir, 'files', '1234');
await OS.File.makeDir(folder, { from: tempDir });
await OS.File.copy(
OS.Path.join(OS.Path.join(getTestDataDirectory().path, 'test.pdf')),
OS.Path.join(folder, 'test.pdf')
);
var translation = new Zotero.Translate.Import();
translation.setLocation(Zotero.File.pathToFile(file));
let translators = await translation.getTranslators();
translation.setTranslator(translators[0]);
var newItem = (await translation.translate({ libraryID }))[0];
assert.equal(newItem.itemType, 'attachment');
assert.equal(newItem.getField('title'), 'Test PDF');
assert.ok(await newItem.getFilePathAsync());
});
});
});