ckfindercommand.js 12 KB

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