ckfindercommand.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  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 Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  10. import ImageEditing from '@ckeditor/ckeditor5-image/src/image/imageediting';
  11. import ImageUploadEditing from '@ckeditor/ckeditor5-image/src/imageupload/imageuploadediting';
  12. import LinkEditing from '@ckeditor/ckeditor5-link/src/linkediting';
  13. import Notification from '@ckeditor/ckeditor5-ui/src/notification/notification';
  14. import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
  15. import CKFinderCommand from '../src/ckfindercommand';
  16. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  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, Clipboard ]
  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' ).elementToElement( { 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' ).elementToElement( { 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' ).elementToElement( { model: 'block', view: 'block' } );
  76. setModelData( model, '<block><paragraph>[]</paragraph></block>' );
  77. expect( command.isEnabled ).to.be.false;
  78. } );
  79. it( 'should be true when imageInsert or link command is enabled', () => {
  80. setModelData( model, '<paragraph>[]</paragraph>' );
  81. const insertImage = editor.commands.get( 'imageInsert' );
  82. const linkCommand = editor.commands.get( 'link' );
  83. insertImage.isEnabled = false;
  84. linkCommand.isEnabled = false;
  85. command.refresh();
  86. expect( command.isEnabled ).to.be.false;
  87. linkCommand.isEnabled = false;
  88. insertImage.isEnabled = true;
  89. command.refresh();
  90. expect( command.isEnabled ).to.be.true;
  91. linkCommand.isEnabled = true;
  92. insertImage.isEnabled = false;
  93. command.refresh();
  94. expect( command.isEnabled ).to.be.true;
  95. } );
  96. it( 'should be true when imageInsert command is not available', () => {
  97. setModelData( model, '<paragraph>[]</paragraph>' );
  98. const insertImage = editor.commands.get( 'imageInsert' );
  99. editor.commands._commands.delete( 'link' );
  100. insertImage.isEnabled = false;
  101. command.refresh();
  102. expect( command.isEnabled ).to.be.false;
  103. insertImage.isEnabled = true;
  104. command.refresh();
  105. expect( command.isEnabled ).to.be.true;
  106. } );
  107. } );
  108. describe( 'execute()', () => {
  109. const finderMock = {
  110. on: ( eventName, callback ) => {
  111. finderMock[ eventName ] = callback;
  112. }
  113. };
  114. beforeEach( () => {
  115. window.CKFinder = {
  116. modal: config => {
  117. config.onInit( finderMock );
  118. },
  119. popup: config => {
  120. config.onInit( finderMock );
  121. }
  122. };
  123. } );
  124. it( 'should register proper listeners on CKFinder instance', () => {
  125. command.execute();
  126. expect( finderMock ).to.have.property( 'files:choose' );
  127. expect( finderMock ).to.have.property( 'file:choose:resizedImage' );
  128. } );
  129. it( 'should use CKFinder.modal() as default CKFinder opener method', () => {
  130. const spy = sinon.spy( window.CKFinder, 'modal' );
  131. command.execute();
  132. sinon.assert.calledOnce( spy );
  133. } );
  134. it( 'should use CKFinder.popup() when ckfinder.openerMethod is set to it', () => {
  135. const spy = sinon.spy( window.CKFinder, 'popup' );
  136. editor.config.set( 'ckfinder.openerMethod', 'popup' );
  137. command.execute();
  138. sinon.assert.calledOnce( spy );
  139. } );
  140. it( 'should throw if unsupported CKFinder opener method was set', () => {
  141. editor.config.set( 'ckfinder.openerMethod', 'foobar' );
  142. expectToThrowCKEditorError( () => {
  143. command.execute();
  144. }, /ckfinder-unknown-openerMethod/, editor );
  145. } );
  146. it( 'should insert single chosen image', () => {
  147. const url = 'foo/bar.jpg';
  148. command.execute();
  149. mockFilesChooseEvent( [ mockFinderFile( url ) ] );
  150. expect( getModelData( model ) )
  151. .to.equal( `[<image src="${ url }"></image>]<paragraph>foo</paragraph>` );
  152. } );
  153. it( 'should insert link if chosen file is not an image', () => {
  154. const url = 'foo/bar.pdf';
  155. command.execute();
  156. mockFilesChooseEvent( [ mockFinderFile( url, false ) ] );
  157. expect( getModelData( model ) )
  158. .to.equal( `<paragraph>f[<$text linkHref="${ url }">o</$text>]o</paragraph>` );
  159. } );
  160. it( 'should pass CKFinder configuration options', () => {
  161. const spy = sinon.spy( window.CKFinder, 'modal' );
  162. const connectorPath = 'foo/bar.php';
  163. editor.config.set( 'ckfinder.options', { connectorPath } );
  164. command.execute();
  165. const openerMethodOptions = spy.args[ 0 ][ 0 ];
  166. expect( openerMethodOptions ).to.have.property( 'chooseFiles', true );
  167. expect( openerMethodOptions ).to.have.property( 'onInit' );
  168. expect( openerMethodOptions ).to.have.property( 'connectorPath', connectorPath );
  169. } );
  170. it( 'should call user-defined config.onInit() function', () => {
  171. const spy = sinon.spy();
  172. editor.config.set( 'ckfinder.options.onInit', spy );
  173. command.execute();
  174. sinon.assert.calledOnce( spy );
  175. } );
  176. it( 'should pass CKFinder instance to a user-defined config.onInit() function', () => {
  177. const spy = sinon.spy();
  178. editor.config.set( 'ckfinder.options.onInit', spy );
  179. command.execute();
  180. sinon.assert.calledWithExactly( spy, finderMock );
  181. } );
  182. it( 'should pass editor default language to the CKFinder instance', () => {
  183. const spy = sinon.spy( window.CKFinder, 'modal' );
  184. command.execute();
  185. const openerMethodOptions = spy.args[ 0 ][ 0 ];
  186. expect( openerMethodOptions ).to.have.property( 'language', 'en' );
  187. } );
  188. it( 'should pass editor language set by configuration to the CKFinder instance', () => {
  189. const finderMock = {
  190. on: ( eventName, callback ) => {
  191. finderMock[ eventName ] = callback;
  192. }
  193. };
  194. return VirtualTestEditor
  195. .create( {
  196. plugins: [ Paragraph, ImageEditing, ImageUploadEditing, LinkEditing, Notification, Clipboard ],
  197. language: 'pl'
  198. } )
  199. .then( newEditor => {
  200. editor = newEditor;
  201. window.CKFinder = {
  202. modal: config => {
  203. config.onInit( finderMock );
  204. }
  205. };
  206. command = new CKFinderCommand( editor );
  207. setModelData( editor.model, '<paragraph>f[o]o</paragraph>' );
  208. const spy = sinon.spy( window.CKFinder, 'modal' );
  209. command.execute();
  210. const openerMethodOptions = spy.args[ 0 ][ 0 ];
  211. expect( openerMethodOptions ).to.have.property( 'language', 'pl' );
  212. } );
  213. } );
  214. it( 'should not pass editor language if it is set in ckfinder.options', () => {
  215. const spy = sinon.spy( window.CKFinder, 'modal' );
  216. editor.config.set( 'ckfinder.options.language', 'pl' );
  217. command.execute();
  218. const openerMethodOptions = spy.args[ 0 ][ 0 ];
  219. expect( openerMethodOptions ).to.have.property( 'language', 'pl' );
  220. } );
  221. it( 'should insert multiple chosen images as image widget', () => {
  222. const url1 = 'foo/bar1.jpg';
  223. const url2 = 'foo/bar2.jpg';
  224. const url3 = 'foo/bar3.jpg';
  225. command.execute();
  226. mockFilesChooseEvent( [ mockFinderFile( url1 ), mockFinderFile( url2 ), mockFinderFile( url3 ) ] );
  227. expect( getModelData( model ) ).to.equal(
  228. `<image src="${ url1 }"></image><image src="${ url2 }"></image>[<image src="${ url3 }"></image>]<paragraph>foo</paragraph>`
  229. );
  230. } );
  231. it( 'should insert images and links to a files from chosen files', () => {
  232. const url1 = 'foo/bar1.jpg';
  233. const url2 = 'foo/bar2.pdf';
  234. const url3 = 'foo/bar3.jpg';
  235. command.execute();
  236. mockFilesChooseEvent( [ mockFinderFile( url1 ), mockFinderFile( url2, false ), mockFinderFile( url3 ) ] );
  237. expect( getModelData( model ) ).to.equal(
  238. `<image src="${ url1 }"></image>` +
  239. `[<image src="${ url3 }"></image>]` +
  240. `<paragraph>f<$text linkHref="${ url2 }">o</$text>o</paragraph>`
  241. );
  242. } );
  243. it( 'should use CKFinder Proxy for privately hosted files', () => {
  244. const proxyUrl = 'bar/foo.jpg';
  245. finderMock.request = () => proxyUrl;
  246. command.execute();
  247. mockFilesChooseEvent( [ mockFinderFile( false ) ] );
  248. expect( getModelData( model ) ).to.equal(
  249. `[<image src="${ proxyUrl }"></image>]<paragraph>foo</paragraph>`
  250. );
  251. } );
  252. it( 'should insert resized image as image widget', () => {
  253. const url = 'foo/bar.jpg';
  254. command.execute();
  255. mockFinderEvent( 'file:choose:resizedImage', { resizedUrl: url } );
  256. expect( getModelData( model ) )
  257. .to.equal( `[<image src="${ url }"></image>]<paragraph>foo</paragraph>` );
  258. } );
  259. it( 'should show warning notification if no resized image URL was returned', done => {
  260. const notification = editor.plugins.get( Notification );
  261. notification.on( 'show:warning', ( evt, data ) => {
  262. expect( data.message ).to.equal( 'Could not obtain resized image URL.' );
  263. expect( data.title ).to.equal( 'Selecting resized image failed' );
  264. evt.stop();
  265. done();
  266. }, { priority: 'high' } );
  267. command.execute();
  268. mockFinderEvent( 'file:choose:resizedImage', { resizedUrl: undefined } );
  269. expect( getModelData( model ) )
  270. .to.equal( '<paragraph>f[o]o</paragraph>' );
  271. } );
  272. it( 'should show warning notification if image cannot be inserted', done => {
  273. model.schema.register( 'block', { inheritAllFrom: '$block' } );
  274. model.schema.extend( 'paragraph', { allowIn: 'block' } );
  275. // Block image in block.
  276. model.schema.addChildCheck( ( context, childDefinition ) => {
  277. if ( childDefinition.name === 'image' && context.last.name === 'block' ) {
  278. return false;
  279. }
  280. } );
  281. editor.conversion.for( 'downcast' ).elementToElement( { model: 'block', view: 'block' } );
  282. setModelData( model, '<block><paragraph>[]</paragraph></block>' );
  283. const notification = editor.plugins.get( Notification );
  284. notification.on( 'show:warning', ( evt, data ) => {
  285. expect( data.message ).to.equal( 'Could not insert image at the current position.' );
  286. expect( data.title ).to.equal( 'Inserting image failed' );
  287. evt.stop();
  288. done();
  289. }, { priority: 'high' } );
  290. command.execute();
  291. mockFinderEvent( 'file:choose:resizedImage', { resizedUrl: 'foo/bar.jpg' } );
  292. expect( getModelData( model ) )
  293. .to.equal( '<paragraph>f[o]o</paragraph>' );
  294. } );
  295. it( 'should show warning notification if link command is not available', done => {
  296. // Remove link command.
  297. editor.commands._commands.delete( 'link' );
  298. const notification = editor.plugins.get( Notification );
  299. notification.on( 'show:warning', ( evt, data ) => {
  300. expect( data.message ).to.equal( 'The "link" command must be available to insert links.' );
  301. expect( data.title ).to.equal( 'Inserting link failed' );
  302. evt.stop();
  303. done();
  304. }, { priority: 'high' } );
  305. command.execute();
  306. mockFilesChooseEvent( [ mockFinderFile( 'foo/bar.pdf', false ) ] );
  307. } );
  308. it( 'should show warning notification if link command is not available', done => {
  309. // Remove link command.
  310. editor.commands._commands.delete( 'imageInsert' );
  311. const notification = editor.plugins.get( Notification );
  312. notification.on( 'show:warning', ( evt, data ) => {
  313. expect( data.message ).to.equal( 'The "imageInsert" command must be available to insert images.' );
  314. expect( data.title ).to.equal( 'Inserting image failed' );
  315. evt.stop();
  316. done();
  317. }, { priority: 'high' } );
  318. command.execute();
  319. mockFilesChooseEvent( [ mockFinderFile( 'foo/bar.png', true ) ] );
  320. } );
  321. it( 'should not insert image nor crash when image could not be inserted', () => {
  322. model.schema.register( 'other', {
  323. allowIn: '$root',
  324. isLimit: true
  325. } );
  326. model.schema.extend( '$text', { allowIn: 'other' } );
  327. editor.conversion.for( 'downcast' ).elementToElement( { model: 'other', view: 'p' } );
  328. setModelData( model, '<other>[]</other>' );
  329. command.execute();
  330. mockFilesChooseEvent( [ mockFinderFile( 'foo/bar.jpg' ) ] );
  331. expect( getModelData( model ) ).to.equal( '<other>[]</other>' );
  332. } );
  333. it( 'does not alter the original config', () => {
  334. editor.config.set( 'ckfinder', { options: { foo: 'bar' } } );
  335. command.execute();
  336. expect( editor.config.get( 'ckfinder.options' ) ).to.deep.equal( { foo: 'bar' } );
  337. } );
  338. function mockFinderFile( url = 'foo/bar.jpg', isImage = true ) {
  339. return {
  340. isImage: () => isImage,
  341. getUrl: () => url
  342. };
  343. }
  344. function mockFinderEvent( eventName, data ) {
  345. finderMock[ eventName ]( { data } );
  346. }
  347. function mockFilesChooseEvent( files ) {
  348. const data = {
  349. files: {
  350. toArray: () => files
  351. }
  352. };
  353. mockFinderEvent( 'files:choose', data );
  354. }
  355. } );
  356. } );