attachmentsTest.js (10535B)
1 describe("Zotero.Attachments", function() { 2 var win; 3 4 before(function* () { 5 // Hidden browser, which requires a browser window, needed for charset detection 6 // (until we figure out a better way) 7 win = yield loadBrowserWindow(); 8 }); 9 after(function () { 10 if (win) { 11 win.close(); 12 } 13 }); 14 15 describe("#importFromFile()", function () { 16 it("should create a child attachment from a text file", function* () { 17 // Create test file 18 var contents = "Test"; 19 var tmpFile = Zotero.getTempDirectory(); 20 tmpFile.append('test.txt'); 21 yield Zotero.File.putContentsAsync(tmpFile, contents); 22 23 // Create parent item 24 var item = new Zotero.Item('book'); 25 var parentItemID = yield item.saveTx(); 26 27 // Create attachment and compare content 28 var item = yield Zotero.Attachments.importFromFile({ 29 file: tmpFile, 30 parentItemID: parentItemID 31 }); 32 var storedFile = item.getFile(); 33 assert.equal((yield Zotero.File.getContentsAsync(storedFile)), contents); 34 35 // Clean up 36 yield Zotero.Items.erase(item.id); 37 }); 38 39 it("should create a top-level attachment from a PNG file", function* () { 40 var file = getTestDataDirectory(); 41 file.append('test.png'); 42 var contents = yield Zotero.File.getBinaryContentsAsync(file); 43 44 // Create attachment and compare content 45 var item = yield Zotero.Attachments.importFromFile({ 46 file: file 47 }); 48 var storedFile = item.getFile(); 49 assert.equal((yield Zotero.File.getBinaryContentsAsync(storedFile)), contents); 50 51 // Clean up 52 yield Zotero.Items.erase(item.id); 53 }); 54 55 it("should create a top-level attachment from a PNG file in a collection", function* () { 56 var file = getTestDataDirectory(); 57 file.append('test.png'); 58 var contents = yield Zotero.File.getBinaryContentsAsync(file); 59 60 var collection = yield createDataObject('collection'); 61 62 // Create attachment and compare content 63 var item = yield Zotero.Attachments.importFromFile({ 64 file: file, 65 collections: [collection.id] 66 }); 67 var storedFile = item.getFile(); 68 assert.equal((yield Zotero.File.getBinaryContentsAsync(storedFile)), contents); 69 70 // Clean up 71 yield Zotero.Items.erase(item.id); 72 }); 73 74 it("should create a child attachment from a PNG file", function* () { 75 var file = getTestDataDirectory(); 76 file.append('test.png'); 77 var contents = yield Zotero.File.getBinaryContentsAsync(file); 78 79 // Create parent item 80 var item = new Zotero.Item('book'); 81 var parentItemID = yield item.saveTx(); 82 83 // Create attachment and compare content 84 var item = yield Zotero.Attachments.importFromFile({ 85 file: file, 86 parentItemID: parentItemID 87 }); 88 var storedFile = item.getFile(); 89 assert.equal((yield Zotero.File.getBinaryContentsAsync(storedFile)), contents); 90 91 // Clean up 92 yield Zotero.Items.erase(item.id); 93 }); 94 }) 95 96 describe("#linkFromFile()", function () { 97 it("should link to a file in My Library", function* () { 98 var item = yield createDataObject('item'); 99 100 var file = getTestDataDirectory(); 101 file.append('test.png'); 102 var attachment = yield Zotero.Attachments.linkFromFile({ 103 file: file, 104 parentItemID: item.id 105 }); 106 107 assert.equal(attachment.getFilePath(), file.path); 108 }) 109 110 it.skip("should throw an error for a non-user library", function* () { 111 // Should create a group library for use by all tests 112 }) 113 }) 114 115 describe("#importSnapshotFromFile()", function () { 116 it("should import an HTML file", function* () { 117 var item = yield createDataObject('item'); 118 var file = getTestDataDirectory(); 119 file.append('test.html'); 120 var attachment = yield Zotero.Attachments.importSnapshotFromFile({ 121 title: 'Snapshot', 122 url: 'http://example.com', 123 file, 124 parentItemID: item.id, 125 contentType: 'text/html', 126 charset: 'utf-8' 127 }); 128 129 var matches = yield Zotero.Fulltext.findTextInItems([attachment.id], 'test'); 130 assert.lengthOf(matches, 1); 131 assert.propertyVal(matches[0], 'id', attachment.id); 132 }); 133 134 it("should detect charset for an HTML file", function* () { 135 var item = yield createDataObject('item'); 136 var file = getTestDataDirectory(); 137 file.append('test.html'); 138 var attachment = yield Zotero.Attachments.importSnapshotFromFile({ 139 title: 'Snapshot', 140 url: 'http://example.com', 141 file, 142 parentItemID: item.id, 143 contentType: 'text/html' 144 }); 145 146 assert.equal(attachment.attachmentCharset, 'utf-8'); 147 148 var matches = yield Zotero.Fulltext.findTextInItems([attachment.id], 'test'); 149 assert.lengthOf(matches, 1); 150 assert.propertyVal(matches[0], 'id', attachment.id); 151 }); 152 }); 153 154 describe("#linkFromDocument", function () { 155 it("should add a link attachment for the current webpage", function* () { 156 var item = yield createDataObject('item'); 157 158 var uri = OS.Path.join(getTestDataDirectory().path, "snapshot", "index.html"); 159 var deferred = Zotero.Promise.defer(); 160 win.addEventListener('pageshow', () => deferred.resolve()); 161 win.loadURI(uri); 162 yield deferred.promise; 163 164 var file = getTestDataDirectory(); 165 file.append('test.png'); 166 var attachment = yield Zotero.Attachments.linkFromDocument({ 167 document: win.content.document, 168 parentItemID: item.id 169 }); 170 171 assert.equal(attachment.getField('url'), "file://" + uri); 172 173 // Check indexing 174 var matches = yield Zotero.Fulltext.findTextInItems([attachment.id], 'share your research'); 175 assert.lengthOf(matches, 1); 176 assert.propertyVal(matches[0], 'id', attachment.id); 177 }) 178 }) 179 180 describe("#importFromDocument()", function () { 181 it("should save a document with embedded files", function* () { 182 var item = yield createDataObject('item'); 183 184 var uri = OS.Path.join(getTestDataDirectory().path, "snapshot", "index.html"); 185 var deferred = Zotero.Promise.defer(); 186 win.addEventListener('pageshow', () => deferred.resolve()); 187 win.loadURI(uri); 188 yield deferred.promise; 189 190 var file = getTestDataDirectory(); 191 file.append('test.png'); 192 var attachment = yield Zotero.Attachments.importFromDocument({ 193 document: win.content.document, 194 parentItemID: item.id 195 }); 196 197 assert.equal(attachment.getField('url'), "file://" + uri); 198 199 // Check indexing 200 var matches = yield Zotero.Fulltext.findTextInItems([attachment.id], 'share your research'); 201 assert.lengthOf(matches, 1); 202 assert.propertyVal(matches[0], 'id', attachment.id); 203 204 // Check for embedded files 205 var storageDir = Zotero.Attachments.getStorageDirectory(attachment).path; 206 var file = yield attachment.getFilePathAsync(); 207 assert.equal(OS.Path.basename(file), 'index.html'); 208 assert.isTrue(yield OS.File.exists(OS.Path.join(storageDir, 'img.gif'))); 209 }); 210 }); 211 212 describe("#getBaseDirectoryRelativePath()", function () { 213 it("should handle base directory at Windows drive root", function () { 214 Zotero.Prefs.set('baseAttachmentPath', "C:\\"); 215 var path = Zotero.Attachments.getBaseDirectoryRelativePath("C:\\file.txt"); 216 assert.equal(path, Zotero.Attachments.BASE_PATH_PLACEHOLDER + "file.txt"); 217 }); 218 219 it("should convert backslashes to forward slashes", function () { 220 Zotero.Prefs.set('baseAttachmentPath', "C:\\foo\\bar"); 221 var path = Zotero.Attachments.getBaseDirectoryRelativePath("C:\\foo\\bar\\test\\file.txt"); 222 assert.equal(path, Zotero.Attachments.BASE_PATH_PLACEHOLDER + "test/file.txt"); 223 }); 224 }); 225 226 describe("#getTotalFileSize", function () { 227 it("should return the size for a single-file attachment", function* () { 228 var file = getTestDataDirectory(); 229 file.append('test.png'); 230 231 // Create attachment and compare content 232 var item = yield Zotero.Attachments.importFromFile({ 233 file: file 234 }); 235 236 assert.equal((yield Zotero.Attachments.getTotalFileSize(item)), file.fileSize); 237 }) 238 }) 239 240 describe("#hasMultipleFiles and #getNumFiles()", function () { 241 it("should return false and 1 for a single file", function* () { 242 var file = getTestDataDirectory(); 243 file.append('test.png'); 244 245 // Create attachment and compare content 246 var item = yield Zotero.Attachments.importFromFile({ 247 file: file 248 }); 249 250 assert.isFalse(yield Zotero.Attachments.hasMultipleFiles(item)); 251 assert.equal((yield Zotero.Attachments.getNumFiles(item)), 1); 252 }) 253 254 it("should return false and 1 for single HTML file with hidden file", function* () { 255 var file = getTestDataDirectory(); 256 file.append('test.html'); 257 258 // Create attachment and compare content 259 var item = yield Zotero.Attachments.importFromFile({ 260 file: file 261 }); 262 var path = OS.Path.join(OS.Path.dirname(item.getFilePath()), '.zotero-ft-cache'); 263 yield Zotero.File.putContentsAsync(path, ""); 264 265 assert.isFalse(yield Zotero.Attachments.hasMultipleFiles(item)); 266 assert.equal((yield Zotero.Attachments.getNumFiles(item)), 1); 267 }) 268 269 it("should return true and 2 for multiple files", function* () { 270 var file = getTestDataDirectory(); 271 file.append('test.html'); 272 273 // Create attachment and compare content 274 var item = yield Zotero.Attachments.importFromFile({ 275 file: file 276 }); 277 var path = OS.Path.join(OS.Path.dirname(item.getFilePath()), 'test.png'); 278 yield Zotero.File.putContentsAsync(path, ""); 279 280 assert.isTrue(yield Zotero.Attachments.hasMultipleFiles(item)); 281 assert.equal((yield Zotero.Attachments.getNumFiles(item)), 2); 282 }) 283 }); 284 285 describe("#createDirectoryForItem()", function () { 286 it("should create missing directory", function* () { 287 var item = yield importFileAttachment('test.png'); 288 var path = OS.Path.dirname(item.getFilePath()); 289 yield OS.File.removeDir(path); 290 yield Zotero.Attachments.createDirectoryForItem(item); 291 assert.isTrue(yield OS.File.exists(path)); 292 }); 293 294 it("should delete all existing files", function* () { 295 var item = yield importFileAttachment('test.html'); 296 var path = OS.Path.dirname(item.getFilePath()); 297 var files = ['a', 'b', 'c', 'd']; 298 for (let file of files) { 299 yield Zotero.File.putContentsAsync(OS.Path.join(path, file), file); 300 } 301 yield Zotero.Attachments.createDirectoryForItem(item); 302 assert.isTrue(yield Zotero.File.directoryIsEmpty(path)); 303 assert.isTrue(yield OS.File.exists(path)); 304 }); 305 306 it("should handle empty directory", function* () { 307 var item = yield importFileAttachment('test.png'); 308 var file = item.getFilePath(); 309 var dir = OS.Path.dirname(item.getFilePath()); 310 yield OS.File.remove(file); 311 yield Zotero.Attachments.createDirectoryForItem(item); 312 assert.isTrue(yield OS.File.exists(dir)); 313 }); 314 }); 315 })