ckfindercommand.js 12 KB

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