2015-04-26 21:42:29 +00:00
|
|
|
describe("Zotero.Attachments", function() {
|
|
|
|
var win;
|
|
|
|
|
2015-06-02 00:03:40 +00:00
|
|
|
before(function* () {
|
2015-04-26 21:42:29 +00:00
|
|
|
// Hidden browser, which requires a browser window, needed for charset detection
|
|
|
|
// (until we figure out a better way)
|
2015-06-02 00:03:40 +00:00
|
|
|
win = yield loadBrowserWindow();
|
2015-04-26 21:42:29 +00:00
|
|
|
});
|
|
|
|
after(function () {
|
|
|
|
if (win) {
|
|
|
|
win.close();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("#importFromFile()", function () {
|
|
|
|
it("should create a child attachment from a text file", function* () {
|
|
|
|
// Create test file
|
|
|
|
var contents = "Test";
|
|
|
|
var tmpFile = Zotero.getTempDirectory();
|
|
|
|
tmpFile.append('test.txt');
|
|
|
|
yield Zotero.File.putContentsAsync(tmpFile, contents);
|
|
|
|
|
|
|
|
// Create parent item
|
|
|
|
var item = new Zotero.Item('book');
|
2015-05-10 08:20:47 +00:00
|
|
|
var parentItemID = yield item.saveTx();
|
2015-04-26 21:42:29 +00:00
|
|
|
|
|
|
|
// Create attachment and compare content
|
2015-05-29 09:31:54 +00:00
|
|
|
var item = yield Zotero.Attachments.importFromFile({
|
2015-05-23 08:25:47 +00:00
|
|
|
file: tmpFile,
|
|
|
|
parentItemID: parentItemID
|
|
|
|
});
|
2015-04-26 21:42:29 +00:00
|
|
|
var storedFile = item.getFile();
|
|
|
|
assert.equal((yield Zotero.File.getContentsAsync(storedFile)), contents);
|
|
|
|
|
|
|
|
// Clean up
|
2015-05-29 09:31:54 +00:00
|
|
|
yield Zotero.Items.erase(item.id);
|
2015-04-26 21:42:29 +00:00
|
|
|
});
|
|
|
|
|
2015-05-23 08:25:47 +00:00
|
|
|
it("should create a top-level attachment from a PNG file", function* () {
|
|
|
|
var file = getTestDataDirectory();
|
|
|
|
file.append('test.png');
|
|
|
|
var contents = yield Zotero.File.getBinaryContentsAsync(file);
|
|
|
|
|
|
|
|
// Create attachment and compare content
|
2015-05-29 09:31:54 +00:00
|
|
|
var item = yield Zotero.Attachments.importFromFile({
|
2015-05-23 08:25:47 +00:00
|
|
|
file: file
|
|
|
|
});
|
|
|
|
var storedFile = item.getFile();
|
|
|
|
assert.equal((yield Zotero.File.getBinaryContentsAsync(storedFile)), contents);
|
|
|
|
|
|
|
|
// Clean up
|
2015-05-29 09:31:54 +00:00
|
|
|
yield Zotero.Items.erase(item.id);
|
2015-05-23 08:25:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should create a top-level attachment from a PNG file in a collection", function* () {
|
|
|
|
var file = getTestDataDirectory();
|
|
|
|
file.append('test.png');
|
|
|
|
var contents = yield Zotero.File.getBinaryContentsAsync(file);
|
|
|
|
|
|
|
|
var collection = yield createDataObject('collection');
|
|
|
|
|
|
|
|
// Create attachment and compare content
|
2015-05-29 09:31:54 +00:00
|
|
|
var item = yield Zotero.Attachments.importFromFile({
|
2015-05-23 08:25:47 +00:00
|
|
|
file: file,
|
|
|
|
collections: [collection.id]
|
|
|
|
});
|
|
|
|
var storedFile = item.getFile();
|
|
|
|
assert.equal((yield Zotero.File.getBinaryContentsAsync(storedFile)), contents);
|
|
|
|
|
|
|
|
// Clean up
|
2015-05-29 09:31:54 +00:00
|
|
|
yield Zotero.Items.erase(item.id);
|
2015-05-23 08:25:47 +00:00
|
|
|
});
|
|
|
|
|
2015-04-26 21:42:29 +00:00
|
|
|
it("should create a child attachment from a PNG file", function* () {
|
|
|
|
var file = getTestDataDirectory();
|
|
|
|
file.append('test.png');
|
|
|
|
var contents = yield Zotero.File.getBinaryContentsAsync(file);
|
|
|
|
|
|
|
|
// Create parent item
|
|
|
|
var item = new Zotero.Item('book');
|
2015-05-10 08:20:47 +00:00
|
|
|
var parentItemID = yield item.saveTx();
|
2015-04-26 21:42:29 +00:00
|
|
|
|
|
|
|
// Create attachment and compare content
|
2015-05-29 09:31:54 +00:00
|
|
|
var item = yield Zotero.Attachments.importFromFile({
|
2015-05-23 08:25:47 +00:00
|
|
|
file: file,
|
|
|
|
parentItemID: parentItemID
|
|
|
|
});
|
2015-04-26 21:42:29 +00:00
|
|
|
var storedFile = item.getFile();
|
|
|
|
assert.equal((yield Zotero.File.getBinaryContentsAsync(storedFile)), contents);
|
|
|
|
|
|
|
|
// Clean up
|
2015-05-29 09:31:54 +00:00
|
|
|
yield Zotero.Items.erase(item.id);
|
2015-04-26 21:42:29 +00:00
|
|
|
});
|
|
|
|
})
|
2015-05-29 05:07:23 +00:00
|
|
|
|
2016-03-22 04:40:59 +00:00
|
|
|
describe("#linkFromFile()", function () {
|
2015-05-29 05:07:23 +00:00
|
|
|
it("should link to a file in My Library", function* () {
|
|
|
|
var item = yield createDataObject('item');
|
|
|
|
|
|
|
|
var file = getTestDataDirectory();
|
|
|
|
file.append('test.png');
|
|
|
|
var attachment = yield Zotero.Attachments.linkFromFile({
|
|
|
|
file: file,
|
|
|
|
parentItemID: item.id
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(attachment.getFilePath(), file.path);
|
|
|
|
})
|
|
|
|
|
|
|
|
it.skip("should throw an error for a non-user library", function* () {
|
|
|
|
// Should create a group library for use by all tests
|
|
|
|
})
|
|
|
|
})
|
2015-08-09 08:52:14 +00:00
|
|
|
|
2016-03-22 04:40:59 +00:00
|
|
|
describe("#importSnapshotFromFile()", function () {
|
|
|
|
it("should import an HTML file", function* () {
|
|
|
|
var item = yield createDataObject('item');
|
|
|
|
var file = getTestDataDirectory();
|
|
|
|
file.append('test.html');
|
|
|
|
var attachment = yield Zotero.Attachments.importSnapshotFromFile({
|
|
|
|
title: 'Snapshot',
|
|
|
|
url: 'http://example.com',
|
|
|
|
file,
|
|
|
|
parentItemID: item.id,
|
|
|
|
contentType: 'text/html',
|
|
|
|
charset: 'utf-8'
|
|
|
|
});
|
|
|
|
|
|
|
|
var matches = yield Zotero.Fulltext.findTextInItems([attachment.id], 'test');
|
|
|
|
assert.lengthOf(matches, 1);
|
|
|
|
assert.propertyVal(matches[0], 'id', attachment.id);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should detect charset for an HTML file", function* () {
|
|
|
|
var item = yield createDataObject('item');
|
|
|
|
var file = getTestDataDirectory();
|
|
|
|
file.append('test.html');
|
|
|
|
var attachment = yield Zotero.Attachments.importSnapshotFromFile({
|
|
|
|
title: 'Snapshot',
|
|
|
|
url: 'http://example.com',
|
|
|
|
file,
|
|
|
|
parentItemID: item.id,
|
|
|
|
contentType: 'text/html'
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(attachment.attachmentCharset, 'utf-8');
|
|
|
|
|
|
|
|
var matches = yield Zotero.Fulltext.findTextInItems([attachment.id], 'test');
|
|
|
|
assert.lengthOf(matches, 1);
|
|
|
|
assert.propertyVal(matches[0], 'id', attachment.id);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-02-11 07:54:52 +00:00
|
|
|
describe("#linkFromDocument", function () {
|
|
|
|
it("should add a link attachment for the current webpage", function* () {
|
|
|
|
var item = yield createDataObject('item');
|
|
|
|
|
|
|
|
var uri = OS.Path.join(getTestDataDirectory().path, "snapshot", "index.html");
|
|
|
|
var deferred = Zotero.Promise.defer();
|
|
|
|
win.addEventListener('pageshow', () => deferred.resolve());
|
|
|
|
win.loadURI(uri);
|
|
|
|
yield deferred.promise;
|
|
|
|
|
|
|
|
var file = getTestDataDirectory();
|
|
|
|
file.append('test.png');
|
|
|
|
var attachment = yield Zotero.Attachments.linkFromDocument({
|
|
|
|
document: win.content.document,
|
|
|
|
parentItemID: item.id
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(attachment.getField('url'), "file://" + uri);
|
|
|
|
|
|
|
|
// Check indexing
|
|
|
|
var matches = yield Zotero.Fulltext.findTextInItems([attachment.id], 'works');
|
|
|
|
assert.lengthOf(matches, 1);
|
|
|
|
assert.propertyVal(matches[0], 'id', attachment.id);
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-08-09 08:52:14 +00:00
|
|
|
describe("#getTotalFileSize", function () {
|
|
|
|
it("should return the size for a single-file attachment", function* () {
|
|
|
|
var file = getTestDataDirectory();
|
|
|
|
file.append('test.png');
|
|
|
|
|
|
|
|
// Create attachment and compare content
|
|
|
|
var item = yield Zotero.Attachments.importFromFile({
|
|
|
|
file: file
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal((yield Zotero.Attachments.getTotalFileSize(item)), file.fileSize);
|
|
|
|
})
|
|
|
|
})
|
2015-08-10 05:55:55 +00:00
|
|
|
|
|
|
|
describe("#hasMultipleFiles and #getNumFiles()", function () {
|
|
|
|
it("should return false and 1 for a single file", function* () {
|
|
|
|
var file = getTestDataDirectory();
|
|
|
|
file.append('test.png');
|
|
|
|
|
|
|
|
// Create attachment and compare content
|
|
|
|
var item = yield Zotero.Attachments.importFromFile({
|
|
|
|
file: file
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.isFalse(yield Zotero.Attachments.hasMultipleFiles(item));
|
|
|
|
assert.equal((yield Zotero.Attachments.getNumFiles(item)), 1);
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should return false and 1 for single HTML file with hidden file", function* () {
|
|
|
|
var file = getTestDataDirectory();
|
|
|
|
file.append('test.html');
|
|
|
|
|
|
|
|
// Create attachment and compare content
|
|
|
|
var item = yield Zotero.Attachments.importFromFile({
|
|
|
|
file: file
|
|
|
|
});
|
|
|
|
var path = OS.Path.join(OS.Path.dirname(item.getFilePath()), '.zotero-ft-cache');
|
|
|
|
yield Zotero.File.putContentsAsync(path, "");
|
|
|
|
|
|
|
|
assert.isFalse(yield Zotero.Attachments.hasMultipleFiles(item));
|
|
|
|
assert.equal((yield Zotero.Attachments.getNumFiles(item)), 1);
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should return true and 2 for multiple files", function* () {
|
|
|
|
var file = getTestDataDirectory();
|
|
|
|
file.append('test.html');
|
|
|
|
|
|
|
|
// Create attachment and compare content
|
|
|
|
var item = yield Zotero.Attachments.importFromFile({
|
|
|
|
file: file
|
|
|
|
});
|
|
|
|
var path = OS.Path.join(OS.Path.dirname(item.getFilePath()), 'test.png');
|
|
|
|
yield Zotero.File.putContentsAsync(path, "");
|
|
|
|
|
|
|
|
assert.isTrue(yield Zotero.Attachments.hasMultipleFiles(item));
|
|
|
|
assert.equal((yield Zotero.Attachments.getNumFiles(item)), 2);
|
|
|
|
})
|
|
|
|
})
|
2015-04-26 21:42:29 +00:00
|
|
|
})
|