|
4 | 4 | 'use strict';
|
5 | 5 |
|
6 | 6 | ( function() {
|
7 |
| - var FileReaderBackup = window.FileReader, |
| 7 | + var File = window.File, |
| 8 | + Blob = window.Blob, |
| 9 | + FormData = window.FormData, |
| 10 | + FileReaderBackup = window.FileReader, |
8 | 11 | XMLHttpRequestBackup = window.XMLHttpRequest,
|
9 | 12 | FileLoader, resumeAfter,
|
10 | 13 | pngBase64 = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAAAXNSR0IArs4c6QAAAAxJREFUCNdjYGBgAAAABAABJzQnCgAAAABJRU5ErkJggg==',
|
|
19 | 22 | }
|
20 | 23 | };
|
21 | 24 |
|
| 25 | + function createFileMock() { |
| 26 | + window.File = File = function( data, name ) { |
| 27 | + var file = new Blob( data , {} ); |
| 28 | + file.name = name; |
| 29 | + |
| 30 | + return file; |
| 31 | + }; |
| 32 | + } |
| 33 | + |
| 34 | + function createFormDataMock() { |
| 35 | + window.FormData = function() { |
| 36 | + var entries = {}, |
| 37 | + mock = { |
| 38 | + get: function( name ) { |
| 39 | + return entries[ name ] || null; |
| 40 | + }, |
| 41 | + append: function( name, value, fileName ) { |
| 42 | + if ( value instanceof File && ( value.name === fileName || !fileName ) ) |
| 43 | + entries[ name ] = value; |
| 44 | + else if ( value instanceof Blob ) { |
| 45 | + fileName = fileName || value.name || 'blob'; |
| 46 | + |
| 47 | + entries [ name ] = new File( [ value ], fileName ); |
| 48 | + } |
| 49 | + else |
| 50 | + entries[ name ] = value + ''; |
| 51 | + }, |
| 52 | + has: function( name ) { |
| 53 | + return Object.prototype.hasOwnProperty.call( entries, name ); |
| 54 | + } |
| 55 | + }; |
| 56 | + |
| 57 | + return mock; |
| 58 | + }; |
| 59 | + } |
| 60 | + |
22 | 61 | function createFileReaderMock( scenario ) {
|
23 | 62 | var isAborted = false;
|
24 | 63 |
|
|
197 | 236 | assert.ignore();
|
198 | 237 | }
|
199 | 238 |
|
| 239 | + // IE doesn't support File constructor, so there is a need to mimic it. |
| 240 | + if ( typeof MSBlobBuilder === 'function' ) |
| 241 | + createFileMock(); |
| 242 | + |
| 243 | + // FormData in IE & Chrome 47- supports only adding data, not getting it, so mocking (polyfilling?) is required. |
| 244 | + // Note that mocking is needed only for tests, as CKEditor.fileTools uses only append method |
| 245 | + if ( !FormData.prototype.get || !FormData.prototype.has ) |
| 246 | + createFormDataMock(); |
| 247 | + |
200 | 248 | FileLoader = CKEDITOR.fileTools.fileLoader;
|
201 | 249 | resumeAfter = bender.tools.resumeAfter;
|
202 | 250 | testFile = bender.tools.getTestPngFile();
|
|
348 | 396 | wait();
|
349 | 397 | },
|
350 | 398 |
|
| 399 | + 'test upload with custom field name (#13518)': function() { |
| 400 | + var loader = new FileLoader( editorMock, pngBase64, 'name.png' ); |
| 401 | + |
| 402 | + attachListener( editorMock, 'fileUploadRequest', function( evt ) { |
| 403 | + var requestData = evt.data.requestData; |
| 404 | + |
| 405 | + requestData.myFile = requestData.upload; |
| 406 | + |
| 407 | + delete requestData.upload; |
| 408 | + } ); |
| 409 | + |
| 410 | + createXMLHttpRequestMock( [ 'load' ] ); |
| 411 | + |
| 412 | + resumeAfter( loader, 'uploaded', function() { |
| 413 | + assert.isTrue( lastFormData.has( 'myFile' ) ); |
| 414 | + assert.isFalse( lastFormData.has( 'upload' ) ); |
| 415 | + |
| 416 | + // FormData converts all Blob objects into File ones, so we must "revert" it |
| 417 | + objectAssert.areEqual( new Blob( [ lastFormData.get( 'myFile' ) ], {} ), loader.file ); |
| 418 | + }, 3 ); |
| 419 | + |
| 420 | + loader.upload( 'http:\/\/url\/' ); |
| 421 | + |
| 422 | + wait(); |
| 423 | + }, |
| 424 | + |
| 425 | + 'test upload with additional request parameters provided (#13518)': function() { |
| 426 | + var loader = new FileLoader( editorMock, pngBase64, 'name.png' ); |
| 427 | + |
| 428 | + createXMLHttpRequestMock( [ 'load' ] ); |
| 429 | + |
| 430 | + resumeAfter( loader, 'uploaded', function() { |
| 431 | + assert.areSame( 'test', lastFormData.get( 'test' ) ); |
| 432 | + }, 3 ); |
| 433 | + |
| 434 | + loader.upload( 'http:\/\/url\/', { test: 'test' } ); |
| 435 | + |
| 436 | + wait(); |
| 437 | + }, |
| 438 | + |
| 439 | + 'test if name of file is correctly attached (#13518)': function() { |
| 440 | + var name = 'customName.png', |
| 441 | + loader = new FileLoader( editorMock, pngBase64, name ); |
| 442 | + |
| 443 | + createXMLHttpRequestMock( [ 'load' ] ); |
| 444 | + |
| 445 | + resumeAfter( loader, 'uploaded', function() { |
| 446 | + assert.areSame( name, lastFormData.get( 'upload' ).name ); |
| 447 | + }, 3 ); |
| 448 | + |
| 449 | + loader.upload( 'http:\/\/url\/' ); |
| 450 | + |
| 451 | + wait(); |
| 452 | + }, |
| 453 | + |
351 | 454 | 'test upload response not encoded (#13030)': function() {
|
352 | 455 | var loader = new FileLoader( editorMock, pngBase64, 'na me.png' ),
|
353 | 456 | observer = observeEvents( loader );
|
|
768 | 871 | wait();
|
769 | 872 | },
|
770 | 873 |
|
771 |
| - 'test response with custom fields (#13519)': function() { |
772 |
| - var loader = new FileLoader( editorMock, testFile ), |
773 |
| - response = { |
774 |
| - fileName: 'name2.png', |
775 |
| - uploaded: 1, |
776 |
| - url: 'http:\/\/url\/name2.png', |
777 |
| - foo: 'bar' |
778 |
| - }; |
779 |
| - |
780 |
| - createXMLHttpRequestMock( [ 'progress', 'load' ], |
781 |
| - { responseText: JSON.stringify( response ) } ); |
782 |
| - |
783 |
| - resumeAfter( loader, 'uploaded', function() { |
784 |
| - objectAssert.areEqual( response, loader.responseData ); |
785 |
| - } ); |
786 |
| - |
787 |
| - loader.upload( 'http:\/\/url\/' ); |
788 |
| - |
789 |
| - wait(); |
790 |
| - }, |
791 |
| - |
792 | 874 | 'test error 404 with message': function() {
|
793 | 875 | editorMock.lang = { filetools: { httpError404: 'httpError404' } };
|
794 | 876 |
|
|
887 | 969 | wait();
|
888 | 970 | },
|
889 | 971 |
|
| 972 | + 'test additional data passed to xhr via fileUploadRequest listener (#13518)': function() { |
| 973 | + var loader = new FileLoader( editorMock, testFile ), |
| 974 | + file = new File( [], 'a' ); |
| 975 | + |
| 976 | + attachListener( editorMock, 'fileUploadRequest', function( evt ) { |
| 977 | + var requestData = evt.data.requestData; |
| 978 | + |
| 979 | + requestData.customField = 'test'; |
| 980 | + requestData.customFile = file; |
| 981 | + } ); |
| 982 | + |
| 983 | + createXMLHttpRequestMock( [ 'load' ] ); |
| 984 | + |
| 985 | + resumeAfter( loader, 'uploaded', function() { |
| 986 | + assert.areSame( 'test', lastFormData.get( 'customField' ) ); |
| 987 | + objectAssert.areEqual( file, lastFormData.get( 'customFile' ) ); |
| 988 | + } ); |
| 989 | + |
| 990 | + loader.upload( 'http:\/\/url\/' ); |
| 991 | + |
| 992 | + wait(); |
| 993 | + }, |
| 994 | + |
890 | 995 | 'test additional data in fileUploadResponse (#13519)': function() {
|
891 | 996 | var data,
|
892 | 997 | loader = new FileLoader( editorMock, testFile );
|
|
0 commit comments