imageuploadcommand.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  6. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  7. import ImageUploadCommand from '../../src/imageupload/imageuploadcommand';
  8. import FileRepository from '@ckeditor/ckeditor5-upload/src/filerepository';
  9. import { createNativeFileMock, UploadAdapterMock } from '@ckeditor/ckeditor5-upload/tests/_utils/mocks';
  10. import { setData as setModelData, getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  11. import Image from '../../src/image/imageediting';
  12. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  13. import { downcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/downcast-converters';
  14. import ModelPosition from '@ckeditor/ckeditor5-engine/src/model/position';
  15. import log from '@ckeditor/ckeditor5-utils/src/log';
  16. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  17. describe( 'ImageUploadCommand', () => {
  18. let editor, command, model, doc, fileRepository;
  19. testUtils.createSinonSandbox();
  20. class UploadAdapterPluginMock extends Plugin {
  21. init() {
  22. fileRepository = this.editor.plugins.get( FileRepository );
  23. fileRepository.createUploadAdapter = loader => {
  24. return new UploadAdapterMock( loader );
  25. };
  26. }
  27. }
  28. beforeEach( () => {
  29. return VirtualTestEditor
  30. .create( {
  31. plugins: [ FileRepository, Image, Paragraph, UploadAdapterPluginMock ]
  32. } )
  33. .then( newEditor => {
  34. editor = newEditor;
  35. model = editor.model;
  36. doc = model.document;
  37. command = new ImageUploadCommand( editor );
  38. const schema = model.schema;
  39. schema.extend( 'image', { allowAttributes: 'uploadId' } );
  40. } );
  41. } );
  42. afterEach( () => {
  43. return editor.destroy();
  44. } );
  45. describe( 'isEnabled', () => {
  46. it( 'should be true when the selection directly in the root', () => {
  47. model.enqueueChange( 'transparent', () => {
  48. setModelData( model, '[]' );
  49. command.refresh();
  50. expect( command.isEnabled ).to.be.true;
  51. } );
  52. } );
  53. it( 'should be true when the selection is in empty block', () => {
  54. setModelData( model, '<paragraph>[]</paragraph>' );
  55. expect( command.isEnabled ).to.be.true;
  56. } );
  57. it( 'should be true when the selection directly in a paragraph', () => {
  58. setModelData( model, '<paragraph>foo[]</paragraph>' );
  59. expect( command.isEnabled ).to.be.true;
  60. } );
  61. it( 'should be true when the selection directly in a block', () => {
  62. model.schema.register( 'block', { inheritAllFrom: '$block' } );
  63. model.schema.extend( '$text', { allowIn: 'block' } );
  64. editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'block', view: 'block' } ) );
  65. setModelData( model, '<block>foo[]</block>' );
  66. expect( command.isEnabled ).to.be.true;
  67. } );
  68. it( 'should be false when the selection is on other image', () => {
  69. setModelData( model, '[<image></image>]' );
  70. expect( command.isEnabled ).to.be.false;
  71. } );
  72. it( 'should be false when the selection is inside other image', () => {
  73. model.schema.register( 'caption', {
  74. allowIn: 'image',
  75. allowContentOf: '$block',
  76. isLimit: true
  77. } );
  78. editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'caption', view: 'figcaption' } ) );
  79. setModelData( model, '<image><caption>[]</caption></image>' );
  80. expect( command.isEnabled ).to.be.false;
  81. } );
  82. it( 'should be false when schema disallows image', () => {
  83. model.schema.register( 'block', { inheritAllFrom: '$block' } );
  84. model.schema.extend( 'paragraph', { allowIn: 'block' } );
  85. // Block image in block.
  86. model.schema.addChildCheck( ( context, childDefinition ) => {
  87. if ( childDefinition.name === 'image' && context.last.name === 'block' ) {
  88. return false;
  89. }
  90. } );
  91. editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'block', view: 'block' } ) );
  92. setModelData( model, '<block><paragraph>[]</paragraph></block>' );
  93. expect( command.isEnabled ).to.be.false;
  94. } );
  95. } );
  96. describe( 'execute()', () => {
  97. it( 'should insert image at selection position as other widgets', () => {
  98. const file = createNativeFileMock();
  99. setModelData( model, '<paragraph>f[o]o</paragraph>' );
  100. command.execute( { files: file } );
  101. const id = fileRepository.getLoader( file ).id;
  102. expect( getModelData( model ) )
  103. .to.equal( `[<image uploadId="${ id }"></image>]<paragraph>foo</paragraph>` );
  104. } );
  105. it( 'should insert directly at specified position (options.insertAt)', () => {
  106. const file = createNativeFileMock();
  107. setModelData( model, '<paragraph>f[]oo</paragraph>' );
  108. const insertAt = new ModelPosition( doc.getRoot(), [ 0, 2 ] ); // fo[]o
  109. command.execute( { files: file, insertAt } );
  110. const id = fileRepository.getLoader( file ).id;
  111. expect( getModelData( model ) )
  112. .to.equal( `<paragraph>fo</paragraph>[<image uploadId="${ id }"></image>]<paragraph>o</paragraph>` );
  113. } );
  114. it( 'should use parent batch', () => {
  115. const file = createNativeFileMock();
  116. setModelData( model, '<paragraph>[]foo</paragraph>' );
  117. model.change( writer => {
  118. expect( writer.batch.operations ).to.length( 0 );
  119. command.execute( { files: file } );
  120. expect( writer.batch.operations ).to.length.above( 0 );
  121. } );
  122. } );
  123. it( 'should not insert image nor crash when image could not be inserted', () => {
  124. const file = createNativeFileMock();
  125. model.schema.register( 'other', {
  126. allowIn: '$root',
  127. isLimit: true
  128. } );
  129. model.schema.extend( '$text', { allowIn: 'other' } );
  130. editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'other', view: 'p' } ) );
  131. setModelData( model, '<other>[]</other>' );
  132. command.execute( { files: file } );
  133. expect( getModelData( model ) ).to.equal( '<other>[]</other>' );
  134. } );
  135. it( 'should not throw when upload adapter is not set (FileRepository will log an error anyway)', () => {
  136. const file = createNativeFileMock();
  137. fileRepository.createUploadAdapter = undefined;
  138. const logStub = testUtils.sinon.stub( log, 'error' );
  139. setModelData( model, '<paragraph>fo[]o</paragraph>' );
  140. expect( () => {
  141. command.execute( { files: file } );
  142. } ).to.not.throw();
  143. expect( getModelData( model ) ).to.equal( '<paragraph>fo[]o</paragraph>' );
  144. expect( logStub.calledOnce ).to.be.true;
  145. } );
  146. } );
  147. } );