8
0

ckfindercommand.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 ImageUploadEditing from '@ckeditor/ckeditor5-image/src/imageupload/imageuploadediting';
  13. import LinkEditing from '@ckeditor/ckeditor5-link/src/linkediting';
  14. import Notification from '@ckeditor/ckeditor5-ui/src/notification/notification';
  15. import { downcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/downcast-converters';
  16. import CKFinderCommand from '../src/ckfindercommand';
  17. describe( 'CKFinderCommand', () => {
  18. let editor, command, model;
  19. testUtils.createSinonSandbox();
  20. beforeEach( () => {
  21. return VirtualTestEditor
  22. .create( {
  23. plugins: [ Paragraph, ImageEditing, ImageUploadEditing, LinkEditing, Notification ]
  24. } )
  25. .then( newEditor => {
  26. editor = newEditor;
  27. model = editor.model;
  28. command = new CKFinderCommand( editor );
  29. const schema = model.schema;
  30. schema.extend( 'image' );
  31. setModelData( model, '<paragraph>f[o]o</paragraph>' );
  32. } );
  33. } );
  34. afterEach( () => {
  35. return editor.destroy();
  36. } );
  37. describe( 'isEnabled', () => {
  38. it( 'should be true when the selection directly in the root', () => {
  39. model.enqueueChange( 'transparent', () => {
  40. setModelData( model, '[]' );
  41. command.refresh();
  42. expect( command.isEnabled ).to.be.true;
  43. } );
  44. } );
  45. it( 'should be true when the selection is in empty block', () => {
  46. setModelData( model, '<paragraph>[]</paragraph>' );
  47. expect( command.isEnabled ).to.be.true;
  48. } );
  49. it( 'should be true where only image is allowed', () => {
  50. model.schema.register( 'block', { inheritAllFrom: '$block' } );
  51. model.schema.extend( 'paragraph', { allowIn: 'block' } );
  52. model.schema.extend( 'image', { allowIn: 'block' } );
  53. // Block link attribute.
  54. model.schema.addAttributeCheck( ( ctx, attributeName ) => ( attributeName !== 'linkHref' ) );
  55. editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'block', view: 'block' } ) );
  56. setModelData( model, '<block><paragraph>[]</paragraph></block>' );
  57. expect( command.isEnabled ).to.be.true;
  58. } );
  59. it( 'should be true where only link is allowed', () => {
  60. model.schema.register( 'block', { inheritAllFrom: '$block' } );
  61. model.schema.extend( 'paragraph', { allowIn: 'block' } );
  62. // Block image in block.
  63. model.schema.addChildCheck( ( context, childDefinition ) => {
  64. if ( childDefinition.name === 'image' && context.last.name === 'block' ) {
  65. return false;
  66. }
  67. } );
  68. editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'block', view: 'block' } ) );
  69. setModelData( model, '<block><paragraph>[]</paragraph></block>' );
  70. expect( command.isEnabled ).to.be.true;
  71. } );
  72. it( 'should be false where link & image are not allowed', () => {
  73. model.schema.register( 'block', { inheritAllFrom: '$block' } );
  74. model.schema.extend( 'paragraph', { allowIn: 'block' } );
  75. // Block link attribute - image is not allowed in 'block'.
  76. model.schema.addAttributeCheck( ( ctx, attributeName ) => ( attributeName !== 'linkHref' ) );
  77. editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'block', view: 'block' } ) );
  78. setModelData( model, '<block><paragraph>[]</paragraph></block>' );
  79. expect( command.isEnabled ).to.be.false;
  80. } );
  81. } );
  82. describe( 'execute()', () => {
  83. const finderMock = {
  84. on: ( eventName, callback ) => {
  85. finderMock[ eventName ] = callback;
  86. }
  87. };
  88. beforeEach( () => {
  89. window.CKFinder = {
  90. modal: config => {
  91. config.onInit( finderMock );
  92. },
  93. popup: config => {
  94. config.onInit( finderMock );
  95. }
  96. };
  97. } );
  98. it( 'should register proper listeners on CKFinder instance', () => {
  99. command.execute();
  100. expect( finderMock ).to.have.property( 'files:choose' );
  101. expect( finderMock ).to.have.property( 'file:choose:resizedImage' );
  102. } );
  103. it( 'should use "CKFinder.modal" as default CKFinder opener method', () => {
  104. const spy = sinon.spy( window.CKFinder, 'modal' );
  105. command.execute();
  106. sinon.assert.calledOnce( spy );
  107. } );
  108. it( 'should use "CKFinder.modal" as default CKFinder opener method', () => {
  109. const spy = sinon.spy( window.CKFinder, 'popup' );
  110. editor.config.set( 'ckfinder.openerMethod', 'popup' );
  111. command.execute();
  112. sinon.assert.calledOnce( spy );
  113. } );
  114. it( 'should throw if unsupported CKFinder opener method was set', () => {
  115. editor.config.set( 'ckfinder.openerMethod', 'foobar' );
  116. expect( () => {
  117. command.execute();
  118. } ).to.throw( CKEditorError, /ckfinder-unknown-openerMethod/ );
  119. } );
  120. it( 'should insert single chosen image', () => {
  121. const url = 'foo/bar.jpg';
  122. command.execute();
  123. mockFilesChooseEvent( [ mockFinderFile( url ) ] );
  124. expect( getModelData( model ) )
  125. .to.equal( `[<image src="${ url }"></image>]<paragraph>foo</paragraph>` );
  126. } );
  127. it( 'should insert link if chosen file is not an image', () => {
  128. const url = 'foo/bar.pdf';
  129. command.execute();
  130. mockFilesChooseEvent( [ mockFinderFile( url, false ) ] );
  131. expect( getModelData( model ) )
  132. .to.equal( `<paragraph>f[<$text linkHref="${ url }">o</$text>]o</paragraph>` );
  133. } );
  134. it( 'should pass CKFinder configuration options', () => {
  135. const spy = sinon.spy( window.CKFinder, 'modal' );
  136. const connectorPath = 'foo/bar.php';
  137. editor.config.set( 'ckfinder.options', { connectorPath } );
  138. command.execute();
  139. const openerMethodOptions = spy.args[ 0 ][ 0 ];
  140. expect( openerMethodOptions ).to.have.property( 'chooseFiles', true );
  141. expect( openerMethodOptions ).to.have.property( 'onInit' );
  142. expect( openerMethodOptions ).to.have.property( 'connectorPath', connectorPath );
  143. } );
  144. it( 'should insert multiple chosen images as image widget', () => {
  145. const url1 = 'foo/bar1.jpg';
  146. const url2 = 'foo/bar2.jpg';
  147. const url3 = 'foo/bar3.jpg';
  148. command.execute();
  149. mockFilesChooseEvent( [ mockFinderFile( url1 ), mockFinderFile( url2 ), mockFinderFile( url3 ) ] );
  150. expect( getModelData( model ) ).to.equal(
  151. `<image src="${ url1 }"></image><image src="${ url2 }"></image>[<image src="${ url3 }"></image>]<paragraph>foo</paragraph>`
  152. );
  153. } );
  154. it( 'should insert images and links to a files from chosen files', () => {
  155. const url1 = 'foo/bar1.jpg';
  156. const url2 = 'foo/bar2.pdf';
  157. const url3 = 'foo/bar3.jpg';
  158. command.execute();
  159. mockFilesChooseEvent( [ mockFinderFile( url1 ), mockFinderFile( url2, false ), mockFinderFile( url3 ) ] );
  160. expect( getModelData( model ) ).to.equal(
  161. `<image src="${ url1 }"></image>` +
  162. `[<image src="${ url3 }"></image>]` +
  163. `<paragraph>f<$text linkHref="${ url2 }">o</$text>o</paragraph>`
  164. );
  165. } );
  166. it( 'should use CKFinder Proxy for privately hosted files', () => {
  167. const proxyUrl = 'bar/foo.jpg';
  168. finderMock.request = () => proxyUrl;
  169. command.execute();
  170. mockFilesChooseEvent( [ mockFinderFile( false ) ] );
  171. expect( getModelData( model ) ).to.equal(
  172. `[<image src="${ proxyUrl }"></image>]<paragraph>foo</paragraph>`
  173. );
  174. } );
  175. it( 'should insert resized image as image widget', () => {
  176. const url = 'foo/bar.jpg';
  177. command.execute();
  178. mockFinderEvent( 'file:choose:resizedImage', { resizedUrl: url } );
  179. expect( getModelData( model ) )
  180. .to.equal( `[<image src="${ url }"></image>]<paragraph>foo</paragraph>` );
  181. } );
  182. it( 'should show warning notification if no resized image URL was returned', done => {
  183. const notification = editor.plugins.get( Notification );
  184. notification.on( 'show:warning', ( evt, data ) => {
  185. expect( data.message ).to.equal( 'Could not obtain resized image URL. Try different image or folder.' );
  186. expect( data.title ).to.equal( 'Selecting resized image failed' );
  187. evt.stop();
  188. done();
  189. }, { priority: 'high' } );
  190. command.execute();
  191. mockFinderEvent( 'file:choose:resizedImage', { resizedUrl: undefined } );
  192. expect( getModelData( model ) )
  193. .to.equal( '<paragraph>f[o]o</paragraph>' );
  194. } );
  195. it( 'should show warning notification if image cannot be inserted', done => {
  196. model.schema.register( 'block', { inheritAllFrom: '$block' } );
  197. model.schema.extend( 'paragraph', { allowIn: 'block' } );
  198. // Block image in block.
  199. model.schema.addChildCheck( ( context, childDefinition ) => {
  200. if ( childDefinition.name === 'image' && context.last.name === 'block' ) {
  201. return false;
  202. }
  203. } );
  204. editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'block', view: 'block' } ) );
  205. setModelData( model, '<block><paragraph>[]</paragraph></block>' );
  206. const notification = editor.plugins.get( Notification );
  207. notification.on( 'show:warning', ( evt, data ) => {
  208. expect( data.message ).to.equal( 'Could not insert image at current selection.' );
  209. expect( data.title ).to.equal( 'Inserting image failed' );
  210. evt.stop();
  211. done();
  212. }, { priority: 'high' } );
  213. command.execute();
  214. mockFinderEvent( 'file:choose:resizedImage', { resizedUrl: 'foo/bar.jpg' } );
  215. expect( getModelData( model ) )
  216. .to.equal( '<paragraph>f[o]o</paragraph>' );
  217. } );
  218. it( 'should not insert image nor crash when image could not be inserted', () => {
  219. model.schema.register( 'other', {
  220. allowIn: '$root',
  221. isLimit: true
  222. } );
  223. model.schema.extend( '$text', { allowIn: 'other' } );
  224. editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'other', view: 'p' } ) );
  225. setModelData( model, '<other>[]</other>' );
  226. command.execute();
  227. mockFilesChooseEvent( [ mockFinderFile( 'foo/bar.jpg' ) ] );
  228. expect( getModelData( model ) ).to.equal( '<other>[]</other>' );
  229. } );
  230. function mockFinderFile( url = 'foo/bar.jpg', isImage = true ) {
  231. return {
  232. isImage: () => isImage,
  233. get: () => url
  234. };
  235. }
  236. function mockFinderEvent( eventName, data ) {
  237. finderMock[ eventName ]( { data } );
  238. }
  239. function mockFilesChooseEvent( files ) {
  240. const data = {
  241. files: {
  242. toArray: () => files
  243. }
  244. };
  245. mockFinderEvent( 'files:choose', data );
  246. }
  247. } );
  248. } );