8
0

imageuploadengine.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals window, setTimeout */
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  7. import ImageEngine from '@ckeditor/ckeditor5-image/src/image/imageengine';
  8. import ImageUploadEngine from '../src/imageuploadengine';
  9. import ImageUploadCommand from '../src/imageuploadcommand';
  10. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  11. import UndoEngine from '@ckeditor/ckeditor5-undo/src/undoengine';
  12. import DataTransfer from '@ckeditor/ckeditor5-clipboard/src/datatransfer';
  13. import FileRepository from '../src/filerepository';
  14. import { AdapterMock, createNativeFileMock, NativeFileReaderMock } from './_utils/mocks';
  15. import { setData as setModelData, getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  16. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  17. import { eventNameToConsumableType } from '@ckeditor/ckeditor5-engine/src/conversion/model-to-view-converters';
  18. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  19. import Notification from '@ckeditor/ckeditor5-ui/src/notification/notification';
  20. describe( 'ImageUploadEngine', () => {
  21. // eslint-disable-next-line max-len
  22. const base64Sample = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=';
  23. let editor, document, fileRepository, viewDocument, nativeReaderMock, loader, adapterMock;
  24. testUtils.createSinonSandbox();
  25. beforeEach( () => {
  26. testUtils.sinon.stub( window, 'FileReader', () => {
  27. nativeReaderMock = new NativeFileReaderMock();
  28. return nativeReaderMock;
  29. } );
  30. return ClassicTestEditor.create( {
  31. plugins: [ ImageEngine, ImageUploadEngine, Paragraph, UndoEngine ]
  32. } )
  33. .then( newEditor => {
  34. editor = newEditor;
  35. document = editor.document;
  36. viewDocument = editor.editing.view;
  37. fileRepository = editor.plugins.get( FileRepository );
  38. fileRepository.createAdapter = newLoader => {
  39. loader = newLoader;
  40. adapterMock = new AdapterMock( loader );
  41. return adapterMock;
  42. };
  43. } );
  44. } );
  45. it( 'should register proper schema rules', () => {
  46. expect( document.schema.check( { name: 'image', attributes: [ 'uploadId' ], inside: '$root' } ) ).to.be.true;
  47. } );
  48. it( 'should register imageUpload command', () => {
  49. expect( editor.commands.get( 'imageUpload' ) ).to.be.instanceOf( ImageUploadCommand );
  50. } );
  51. it( 'should execute imageUpload command when image is pasted', () => {
  52. const spy = sinon.spy( editor, 'execute' );
  53. const fileMock = createNativeFileMock();
  54. const dataTransfer = new DataTransfer( { files: [ fileMock ] } );
  55. setModelData( document, '<paragraph>[]foo bar baz</paragraph>' );
  56. viewDocument.fire( 'clipboardInput', { dataTransfer } );
  57. sinon.assert.calledOnce( spy );
  58. sinon.assert.calledWith( spy, 'imageUpload' );
  59. const id = fileRepository.getLoader( fileMock ).id;
  60. expect( getModelData( document ) ).to.equal(
  61. `[<image uploadId="${ id }" uploadStatus="reading"></image>]<paragraph>foo bar baz</paragraph>`
  62. );
  63. } );
  64. it( 'should not execute imageUpload command when file is not an image', () => {
  65. const spy = sinon.spy( editor, 'execute' );
  66. const viewDocument = editor.editing.view;
  67. const fileMock = {
  68. type: 'media/mp3',
  69. size: 1024
  70. };
  71. const dataTransfer = new DataTransfer( { files: [ fileMock ] } );
  72. setModelData( document, '<paragraph>foo bar baz[]</paragraph>' );
  73. viewDocument.fire( 'clipboardInput', { dataTransfer } );
  74. sinon.assert.notCalled( spy );
  75. } );
  76. it( 'should not convert image\'s uploadId attribute if is consumed already', () => {
  77. editor.editing.modelToView.on( 'addAttribute:uploadId:image', ( evt, data, consumable ) => {
  78. consumable.consume( data.item, eventNameToConsumableType( evt.name ) );
  79. }, { priority: 'high' } );
  80. setModelData( document, '<image uploadId="1234"></image>' );
  81. expect( getViewData( viewDocument ) ).to.equal(
  82. '[<figure class="image ck-widget" contenteditable="false">' +
  83. '<img></img>' +
  84. '</figure>]' );
  85. } );
  86. it( 'should use read data once it is present', done => {
  87. const file = createNativeFileMock();
  88. setModelData( document, '<paragraph>{}foo bar</paragraph>' );
  89. editor.execute( 'imageUpload', { file } );
  90. document.once( 'changesDone', () => {
  91. expect( getViewData( viewDocument ) ).to.equal(
  92. '[<figure class="image ck-widget" contenteditable="false">' +
  93. `<img src="${ base64Sample }"></img>` +
  94. '</figure>]' +
  95. '<p>foo bar</p>' );
  96. expect( loader.status ).to.equal( 'uploading' );
  97. done();
  98. } );
  99. expect( loader.status ).to.equal( 'reading' );
  100. nativeReaderMock.mockSuccess( base64Sample );
  101. } );
  102. it( 'should replace read data with server response once it is present', done => {
  103. const file = createNativeFileMock();
  104. setModelData( document, '<paragraph>{}foo bar</paragraph>' );
  105. editor.execute( 'imageUpload', { file } );
  106. document.once( 'changesDone', () => {
  107. document.once( 'changesDone', () => {
  108. expect( getViewData( viewDocument ) ).to.equal(
  109. '[<figure class="image ck-widget" contenteditable="false"><img src="image.png"></img></figure>]<p>foo bar</p>'
  110. );
  111. expect( loader.status ).to.equal( 'idle' );
  112. done();
  113. } );
  114. adapterMock.mockSuccess( { original: 'image.png' } );
  115. } );
  116. nativeReaderMock.mockSuccess( base64Sample );
  117. } );
  118. it( 'should fire notification event in case of error', done => {
  119. const notification = editor.plugins.get( Notification );
  120. const file = createNativeFileMock();
  121. notification.on( 'show:warning', ( evt, data ) => {
  122. expect( data.message ).to.equal( 'Reading error.' );
  123. expect( data.title ).to.equal( 'Upload failed' );
  124. evt.stop();
  125. done();
  126. }, { priority: 'high' } );
  127. setModelData( document, '<paragraph>{}foo bar</paragraph>' );
  128. editor.execute( 'imageUpload', { file } );
  129. nativeReaderMock.mockError( 'Reading error.' );
  130. } );
  131. it( 'should not fire notification on abort', done => {
  132. const notification = editor.plugins.get( Notification );
  133. const file = createNativeFileMock();
  134. const spy = testUtils.sinon.spy();
  135. notification.on( 'show:warning', evt => {
  136. spy();
  137. evt.stop();
  138. }, { priority: 'high' } );
  139. setModelData( document, '<paragraph>{}foo bar</paragraph>' );
  140. editor.execute( 'imageUpload', { file } );
  141. nativeReaderMock.abort();
  142. setTimeout( () => {
  143. sinon.assert.notCalled( spy );
  144. done();
  145. }, 0 );
  146. } );
  147. it( 'should do nothing if image does not have uploadId', () => {
  148. setModelData( document, '<image src="image.png"></image>' );
  149. expect( getViewData( viewDocument ) ).to.equal(
  150. '[<figure class="image ck-widget" contenteditable="false"><img src="image.png"></img></figure>]'
  151. );
  152. } );
  153. it( 'should remove image in case of upload error', done => {
  154. const file = createNativeFileMock();
  155. const spy = testUtils.sinon.spy();
  156. const notification = editor.plugins.get( Notification );
  157. setModelData( document, '<paragraph>{}foo bar</paragraph>' );
  158. notification.on( 'show:warning', evt => {
  159. spy();
  160. evt.stop();
  161. }, { priority: 'high' } );
  162. editor.execute( 'imageUpload', { file } );
  163. document.once( 'changesDone', () => {
  164. document.once( 'changesDone', () => {
  165. expect( getModelData( document ) ).to.equal( '<paragraph>[]foo bar</paragraph>' );
  166. sinon.assert.calledOnce( spy );
  167. done();
  168. } );
  169. } );
  170. nativeReaderMock.mockError( 'Upload error.' );
  171. } );
  172. it( 'should abort upload if image is removed', () => {
  173. const file = createNativeFileMock();
  174. setModelData( document, '<paragraph>{}foo bar</paragraph>' );
  175. editor.execute( 'imageUpload', { file } );
  176. const abortSpy = testUtils.sinon.spy( loader, 'abort' );
  177. expect( loader.status ).to.equal( 'reading' );
  178. nativeReaderMock.mockSuccess( base64Sample );
  179. const image = document.getRoot().getChild( 0 );
  180. document.enqueueChanges( () => {
  181. const batch = document.batch();
  182. batch.remove( image );
  183. } );
  184. expect( loader.status ).to.equal( 'aborted' );
  185. sinon.assert.calledOnce( abortSpy );
  186. } );
  187. it( 'image should be permanently removed if it is removed by user during upload', done => {
  188. const file = createNativeFileMock();
  189. const notification = editor.plugins.get( Notification );
  190. setModelData( document, '<paragraph>{}foo bar</paragraph>' );
  191. // Prevent popping up alert window.
  192. notification.on( 'show:warning', evt => {
  193. evt.stop();
  194. }, { priority: 'high' } );
  195. editor.execute( 'imageUpload', { file } );
  196. document.once( 'changesDone', () => {
  197. // This is called after "manual" remove.
  198. document.once( 'changesDone', () => {
  199. // This is called after attributes are removed.
  200. let undone = false;
  201. document.once( 'changesDone', () => {
  202. if ( !undone ) {
  203. undone = true;
  204. // This is called after abort remove.
  205. expect( getModelData( document ) ).to.equal( '<paragraph>[]foo bar</paragraph>' );
  206. editor.execute( 'undo' );
  207. // Expect that the image has not been brought back.
  208. expect( getModelData( document ) ).to.equal( '<paragraph>[]foo bar</paragraph>' );
  209. done();
  210. }
  211. } );
  212. } );
  213. } );
  214. const image = document.getRoot().getChild( 0 );
  215. document.enqueueChanges( () => {
  216. const batch = document.batch();
  217. batch.remove( image );
  218. } );
  219. } );
  220. it( 'should create responsive image if server return multiple images', done => {
  221. const file = createNativeFileMock();
  222. setModelData( document, '<paragraph>{}foo bar</paragraph>' );
  223. editor.execute( 'imageUpload', { file } );
  224. document.once( 'changesDone', () => {
  225. document.once( 'changesDone', () => {
  226. expect( getViewData( viewDocument ) ).to.equal(
  227. '[<figure class="image ck-widget" contenteditable="false">' +
  228. '<img sizes="100vw" src="image.png" srcset="image-500.png 500w, image-800.png 800w"></img>' +
  229. '</figure>]<p>foo bar</p>'
  230. );
  231. expect( loader.status ).to.equal( 'idle' );
  232. done();
  233. } );
  234. adapterMock.mockSuccess( { original: 'image.png', 500: 'image-500.png', 800: 'image-800.png' } );
  235. } );
  236. nativeReaderMock.mockSuccess( base64Sample );
  237. } );
  238. } );