ckfindercommand.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global window */
  6. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  7. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  9. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  10. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  11. import ImageEditing from '@ckeditor/ckeditor5-image/src/image/imageediting';
  12. import Notification from '@ckeditor/ckeditor5-ui/src/notification/notification';
  13. import { downcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/downcast-converters';
  14. import CKFinderCommand from '../src/ckfindercommand';
  15. describe( 'CKFinderCommand', () => {
  16. let editor, command, model;
  17. testUtils.createSinonSandbox();
  18. beforeEach( () => {
  19. return VirtualTestEditor
  20. .create( {
  21. plugins: [ Paragraph, ImageEditing, Notification ]
  22. } )
  23. .then( newEditor => {
  24. editor = newEditor;
  25. model = editor.model;
  26. command = new CKFinderCommand( editor );
  27. const schema = model.schema;
  28. schema.extend( 'image' );
  29. setModelData( model, '<paragraph>f[o]o</paragraph>' );
  30. } );
  31. } );
  32. afterEach( () => {
  33. return editor.destroy();
  34. } );
  35. describe( 'isEnabled', () => {
  36. it( 'should be true when the selection directly in the root', () => {
  37. model.enqueueChange( 'transparent', () => {
  38. setModelData( model, '[]' );
  39. command.refresh();
  40. expect( command.isEnabled ).to.be.true;
  41. } );
  42. } );
  43. it( 'should be true when the selection is in empty block', () => {
  44. setModelData( model, '<paragraph>[]</paragraph>' );
  45. expect( command.isEnabled ).to.be.true;
  46. } );
  47. } );
  48. describe( 'execute()', () => {
  49. const finderMock = {
  50. on: ( eventName, callback ) => {
  51. finderMock[ eventName ] = callback;
  52. }
  53. };
  54. beforeEach( () => {
  55. window.CKFinder = {
  56. modal: config => {
  57. config.onInit( finderMock );
  58. },
  59. popup: config => {
  60. config.onInit( finderMock );
  61. }
  62. };
  63. } );
  64. it( 'should register proper listeners on CKFinder instance', () => {
  65. command.execute();
  66. expect( finderMock ).to.have.property( 'files:choose' );
  67. expect( finderMock ).to.have.property( 'file:choose:resizedImage' );
  68. } );
  69. it( 'should use "CKFinder.modal" as default CKFinder opener method', () => {
  70. const spy = sinon.spy( window.CKFinder, 'modal' );
  71. command.execute();
  72. sinon.assert.calledOnce( spy );
  73. } );
  74. it( 'should use "CKFinder.modal" as default CKFinder opener method', () => {
  75. const spy = sinon.spy( window.CKFinder, 'popup' );
  76. editor.config.set( 'ckfinder.openerMethod', 'popup' );
  77. command.execute();
  78. sinon.assert.calledOnce( spy );
  79. } );
  80. it( 'should throw if unsupported CKFinder opener method was set', () => {
  81. editor.config.set( 'ckfinder.openerMethod', 'foobar' );
  82. expect( () => {
  83. command.execute();
  84. } ).to.throw( CKEditorError, /ckfinder-unknown-openerMethod/ );
  85. } );
  86. it( 'should insert single chosen image as image widget', () => {
  87. const url = 'foo/bar.jpg';
  88. command.execute();
  89. mockFilesChooseEvent( [ mockFinderFile( url ) ] );
  90. expect( getModelData( model ) )
  91. .to.equal( `[<image src="${ url }"></image>]<paragraph>foo</paragraph>` );
  92. } );
  93. it( 'should pass CKFinder config options', () => {
  94. const spy = sinon.spy( window.CKFinder, 'modal' );
  95. const connectorPath = 'foo/bar.php';
  96. editor.config.set( 'ckfinder.config', { connectorPath } );
  97. command.execute();
  98. const openerMethodOptions = spy.args[ 0 ][ 0 ];
  99. expect( openerMethodOptions ).to.have.property( 'chooseFiles', true );
  100. expect( openerMethodOptions ).to.have.property( 'onInit' );
  101. expect( openerMethodOptions ).to.have.property( 'connectorPath', connectorPath );
  102. } );
  103. it( 'should insert multiple chosen images as image widget', () => {
  104. const url1 = 'foo/bar1.jpg';
  105. const url2 = 'foo/bar2.jpg';
  106. const url3 = 'foo/bar3.jpg';
  107. command.execute();
  108. mockFilesChooseEvent( [ mockFinderFile( url1 ), mockFinderFile( url2 ), mockFinderFile( url3 ) ] );
  109. expect( getModelData( model ) ).to.equal(
  110. `<image src="${ url1 }"></image><image src="${ url2 }"></image>[<image src="${ url3 }"></image>]<paragraph>foo</paragraph>`
  111. );
  112. } );
  113. it( 'should insert only images from chosen files', () => {
  114. const url1 = 'foo/bar1.jpg';
  115. const url2 = 'foo/bar2.pdf';
  116. const url3 = 'foo/bar3.jpg';
  117. command.execute();
  118. mockFilesChooseEvent( [ mockFinderFile( url1 ), mockFinderFile( url2, false ), mockFinderFile( url3 ) ] );
  119. expect( getModelData( model ) ).to.equal(
  120. `<image src="${ url1 }"></image>[<image src="${ url3 }"></image>]<paragraph>foo</paragraph>`
  121. );
  122. } );
  123. it( 'should use CKFinder Proxy for privately hosted files', () => {
  124. const proxyUrl = 'bar/foo.jpg';
  125. finderMock.request = () => proxyUrl;
  126. command.execute();
  127. mockFilesChooseEvent( [ mockFinderFile( false ) ] );
  128. expect( getModelData( model ) ).to.equal(
  129. `[<image src="${ proxyUrl }"></image>]<paragraph>foo</paragraph>`
  130. );
  131. } );
  132. it( 'should insert resized image as image widget', () => {
  133. const url = 'foo/bar.jpg';
  134. command.execute();
  135. mockFinderEvent( 'file:choose:resizedImage', { resizedUrl: url } );
  136. expect( getModelData( model ) )
  137. .to.equal( `[<image src="${ url }"></image>]<paragraph>foo</paragraph>` );
  138. } );
  139. it( 'should show warning notification if no resized image URL was returned', done => {
  140. const notification = editor.plugins.get( Notification );
  141. notification.on( 'show:warning', ( evt, data ) => {
  142. expect( data.message ).to.equal( 'Could not obtain resized image URL. Try different image or folder.' );
  143. expect( data.title ).to.equal( 'Selecting resized image failed' );
  144. evt.stop();
  145. done();
  146. }, { priority: 'high' } );
  147. command.execute();
  148. mockFinderEvent( 'file:choose:resizedImage', { resizedUrl: undefined } );
  149. expect( getModelData( model ) )
  150. .to.equal( '<paragraph>f[o]o</paragraph>' );
  151. } );
  152. it( 'should not insert image nor crash when image could not be inserted', () => {
  153. model.schema.register( 'other', {
  154. allowIn: '$root',
  155. isLimit: true
  156. } );
  157. model.schema.extend( '$text', { allowIn: 'other' } );
  158. editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'other', view: 'p' } ) );
  159. setModelData( model, '<other>[]</other>' );
  160. command.execute();
  161. mockFilesChooseEvent( [ mockFinderFile( 'foo/bar.jpg' ) ] );
  162. expect( getModelData( model ) ).to.equal( '<other>[]</other>' );
  163. } );
  164. function mockFinderFile( url = 'foo/bar.jpg', isImage = true ) {
  165. return {
  166. isImage: () => isImage,
  167. get: () => url
  168. };
  169. }
  170. function mockFinderEvent( eventName, data ) {
  171. finderMock[ eventName ]( { data } );
  172. }
  173. function mockFilesChooseEvent( files ) {
  174. const data = {
  175. files: {
  176. toArray: () => files
  177. }
  178. };
  179. mockFinderEvent( 'files:choose', data );
  180. }
  181. } );
  182. } );