8
0

imageuploadengine.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 DataTransfer from '@ckeditor/ckeditor5-clipboard/src/datatransfer';
  12. import FileRepository from '../src/filerepository';
  13. import { AdapterMock, createNativeFileMock, NativeFileReaderMock } from './_utils/mocks';
  14. import { setData as setModelData, getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  15. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  16. import imagePlaceholder from '../theme/icons/image_placeholder.svg';
  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. const base64Sample = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=';
  22. let editor, document, fileRepository, viewDocument, nativeReaderMock, loader, adapterMock;
  23. testUtils.createSinonSandbox();
  24. beforeEach( () => {
  25. testUtils.sinon.stub( window, 'FileReader', () => {
  26. nativeReaderMock = new NativeFileReaderMock();
  27. return nativeReaderMock;
  28. } );
  29. return ClassicTestEditor.create( {
  30. plugins: [ ImageEngine, ImageUploadEngine, Paragraph ]
  31. } )
  32. .then( newEditor => {
  33. editor = newEditor;
  34. document = editor.document;
  35. viewDocument = editor.editing.view;
  36. fileRepository = editor.plugins.get( FileRepository );
  37. fileRepository.createAdapter = newLoader => {
  38. loader = newLoader;
  39. adapterMock = new AdapterMock( loader );
  40. return adapterMock;
  41. };
  42. } );
  43. } );
  44. it( 'should register proper schema rules', () => {
  45. expect( document.schema.check( { name: 'image', attributes: [ 'uploadId' ], inside: '$root' } ) ).to.be.true;
  46. } );
  47. it( 'should register imageUpload command', () => {
  48. expect( editor.commands.get( 'imageUpload' ) ).to.be.instanceOf( ImageUploadCommand );
  49. } );
  50. it( 'should execute imageUpload command when image is pasted', () => {
  51. const spy = sinon.spy( editor, 'execute' );
  52. const fileMock = createNativeFileMock();
  53. const dataTransfer = new DataTransfer( { files: [ fileMock ] } );
  54. setModelData( document, '<paragraph>foo bar baz[]</paragraph>' );
  55. viewDocument.fire( 'input', { dataTransfer } );
  56. sinon.assert.calledOnce( spy );
  57. sinon.assert.calledWith( spy, 'imageUpload' );
  58. const id = fileRepository.getLoader( fileMock ).id;
  59. expect( getModelData( document ) ).to.equal(
  60. `<image uploadId="${ id }"></image><paragraph>foo bar baz[]</paragraph>`
  61. );
  62. } );
  63. it( 'should not execute imageUpload command when file is not an image', () => {
  64. const spy = sinon.spy( editor, 'execute' );
  65. const viewDocument = editor.editing.view;
  66. const fileMock = {
  67. type: 'media/mp3',
  68. size: 1024
  69. };
  70. const dataTransfer = new DataTransfer( { files: [ fileMock ] } );
  71. setModelData( document, '<paragraph>foo bar baz[]</paragraph>' );
  72. viewDocument.fire( 'input', { dataTransfer } );
  73. sinon.assert.notCalled( spy );
  74. } );
  75. it( 'should convert image\'s uploadId attribute from model to view', () => {
  76. setModelData( document, '<image uploadId="1234"></image>' );
  77. expect( getViewData( viewDocument ) ).to.equal(
  78. '[]<figure class="image ck-widget" contenteditable="false">' +
  79. `<img src="data:image/svg+xml;utf8,${ imagePlaceholder }"></img>` +
  80. '</figure>'
  81. );
  82. } );
  83. it( 'should not convert image\'s uploadId attribute if is consumed already', () => {
  84. editor.editing.modelToView.on( 'addAttribute:uploadId:image', ( evt, data, consumable ) => {
  85. consumable.consume( data.item, eventNameToConsumableType( evt.name ) );
  86. }, { priority: 'high' } );
  87. setModelData( document, '<image uploadId="1234"></image>' );
  88. expect( getViewData( viewDocument ) ).to.equal(
  89. '[]<figure class="image ck-widget" contenteditable="false">' +
  90. '<img></img>' +
  91. '</figure>' );
  92. } );
  93. it( 'should replace placeholder with read data once it is present', ( done ) => {
  94. const file = createNativeFileMock();
  95. setModelData( document, '<paragraph>{}foo bar</paragraph>' );
  96. editor.execute( 'imageUpload', { file } );
  97. adapterMock.uploadStartedCallback = () => {
  98. expect( getViewData( viewDocument ) ).to.equal(
  99. '<figure class="image ck-widget" contenteditable="false">' +
  100. `<img src="${ base64Sample }"></img>` +
  101. '</figure>' +
  102. '<p>{}foo bar</p>' );
  103. expect( loader.status ).to.equal( 'uploading' );
  104. done();
  105. };
  106. expect( loader.status ).to.equal( 'reading' );
  107. nativeReaderMock.mockSuccess( base64Sample );
  108. } );
  109. it( 'should replace read data with server response once it is present', ( done ) => {
  110. const file = createNativeFileMock();
  111. setModelData( document, '<paragraph>{}foo bar</paragraph>' );
  112. editor.execute( 'imageUpload', { file } );
  113. adapterMock.uploadStartedCallback = () => {
  114. document.once( 'changesDone', () => {
  115. expect( getViewData( viewDocument ) ).to.equal(
  116. '<figure class="image ck-widget" contenteditable="false"><img src="image.png"></img></figure><p>{}foo bar</p>'
  117. );
  118. expect( loader.status ).to.equal( 'idle' );
  119. done();
  120. } );
  121. adapterMock.mockSuccess( { original: 'image.png' } );
  122. };
  123. nativeReaderMock.mockSuccess( base64Sample );
  124. } );
  125. it( 'should fire notification event in case of error', ( done ) => {
  126. const notification = editor.plugins.get( Notification );
  127. const file = createNativeFileMock();
  128. notification.on( 'show:warning', ( evt, data ) => {
  129. expect( data.message ).to.equal( 'Reading error.' );
  130. evt.stop();
  131. done();
  132. }, { priority: 'high' } );
  133. setModelData( document, '<paragraph>{}foo bar</paragraph>' );
  134. editor.execute( 'imageUpload', { file } );
  135. nativeReaderMock.mockError( 'Reading error.' );
  136. } );
  137. it( 'should not fire notification on abort', ( done ) => {
  138. const notification = editor.plugins.get( Notification );
  139. const file = createNativeFileMock();
  140. const spy = testUtils.sinon.spy();
  141. notification.on( 'show:warning', evt => {
  142. spy();
  143. evt.stop();
  144. }, { priority: 'high' } );
  145. setModelData( document, '<paragraph>{}foo bar</paragraph>' );
  146. editor.execute( 'imageUpload', { file } );
  147. nativeReaderMock.mockAbort();
  148. setTimeout( () => {
  149. sinon.assert.notCalled( spy );
  150. done();
  151. }, 0 );
  152. } );
  153. it( 'should do nothing if image does not have uploadId', () => {
  154. setModelData( document, '<image src="image.png"></image>' );
  155. expect( getViewData( viewDocument ) ).to.equal(
  156. '[]<figure class="image ck-widget" contenteditable="false"><img src="image.png"></img></figure>'
  157. );
  158. } );
  159. } );