8
0

imageuploadcommand.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 true when the selection is inside isLimit element which allows image', () => {
  87. model.schema.register( 'outerObject', { isObject: true, isBlock: true, allowIn: '$root' } );
  88. model.schema.register( 'limit', { isLimit: true, allowIn: 'outerObject' } );
  89. model.schema.extend( '$block', { allowIn: 'limit' } );
  90. editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'outerObject', view: 'outerObject' } ) );
  91. editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'limit', view: 'limit' } ) );
  92. setModelData( model, '<outerObject><limit>[]</limit></outerObject>' );
  93. expect( command.isEnabled ).to.be.true;
  94. } );
  95. it( 'should be true when the selection is inside isLimit element which allows image', () => {
  96. model.schema.register( 'outerObject', { isObject: true, isBlock: true, allowIn: '$root' } );
  97. model.schema.register( 'limit', { isLimit: true, allowIn: 'outerObject' } );
  98. model.schema.extend( '$block', { allowIn: 'limit' } );
  99. editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'outerObject', view: 'outerObject' } ) );
  100. editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'limit', view: 'limit' } ) );
  101. setModelData( model, '<outerObject><limit><paragraph>foo[]</paragraph></limit></outerObject>' );
  102. expect( command.isEnabled ).to.be.true;
  103. } );
  104. it( 'should be false when schema disallows image', () => {
  105. model.schema.register( 'block', { inheritAllFrom: '$block' } );
  106. model.schema.extend( 'paragraph', { allowIn: 'block' } );
  107. // Block image in block.
  108. model.schema.addChildCheck( ( context, childDefinition ) => {
  109. if ( childDefinition.name === 'image' && context.last.name === 'block' ) {
  110. return false;
  111. }
  112. } );
  113. editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'block', view: 'block' } ) );
  114. setModelData( model, '<block><paragraph>[]</paragraph></block>' );
  115. expect( command.isEnabled ).to.be.false;
  116. } );
  117. } );
  118. describe( 'execute()', () => {
  119. it( 'should insert image at selection position as other widgets', () => {
  120. const file = createNativeFileMock();
  121. setModelData( model, '<paragraph>f[o]o</paragraph>' );
  122. command.execute( { file } );
  123. const id = fileRepository.getLoader( file ).id;
  124. expect( getModelData( model ) )
  125. .to.equal( `[<image uploadId="${ id }"></image>]<paragraph>foo</paragraph>` );
  126. } );
  127. it( 'should use parent batch', () => {
  128. const file = createNativeFileMock();
  129. setModelData( model, '<paragraph>[]foo</paragraph>' );
  130. model.change( writer => {
  131. expect( writer.batch.operations ).to.length( 0 );
  132. command.execute( { file } );
  133. expect( writer.batch.operations ).to.length.above( 0 );
  134. } );
  135. } );
  136. it( 'should not insert image nor crash when image could not be inserted', () => {
  137. const file = createNativeFileMock();
  138. model.schema.register( 'other', {
  139. allowIn: '$root',
  140. isLimit: true
  141. } );
  142. model.schema.extend( '$text', { allowIn: 'other' } );
  143. editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'other', view: 'p' } ) );
  144. setModelData( model, '<other>[]</other>' );
  145. command.execute( { file } );
  146. expect( getModelData( model ) ).to.equal( '<other>[]</other>' );
  147. } );
  148. it( 'should not throw when upload adapter is not set (FileRepository will log an error anyway)', () => {
  149. const file = createNativeFileMock();
  150. fileRepository.createUploadAdapter = undefined;
  151. const logStub = testUtils.sinon.stub( log, 'error' );
  152. setModelData( model, '<paragraph>fo[]o</paragraph>' );
  153. expect( () => {
  154. command.execute( { file } );
  155. } ).to.not.throw();
  156. expect( getModelData( model ) ).to.equal( '<paragraph>fo[]o</paragraph>' );
  157. expect( logStub.calledOnce ).to.be.true;
  158. } );
  159. } );
  160. } );