8
0

imageuploadcommand.js 6.5 KB

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