| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- /**
- * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- /* global window */
- import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
- import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
- import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
- import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
- import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
- import ImageEditing from '@ckeditor/ckeditor5-image/src/image/imageediting';
- import Notification from '@ckeditor/ckeditor5-ui/src/notification/notification';
- import { downcastElementToElement } from '@ckeditor/ckeditor5-engine/src/conversion/downcast-converters';
- import CKFinderCommand from '../src/ckfindercommand';
- describe( 'CKFinderCommand', () => {
- let editor, command, model;
- testUtils.createSinonSandbox();
- beforeEach( () => {
- return VirtualTestEditor
- .create( {
- plugins: [ Paragraph, ImageEditing, Notification ]
- } )
- .then( newEditor => {
- editor = newEditor;
- model = editor.model;
- command = new CKFinderCommand( editor );
- const schema = model.schema;
- schema.extend( 'image' );
- setModelData( model, '<paragraph>f[o]o</paragraph>' );
- } );
- } );
- afterEach( () => {
- return editor.destroy();
- } );
- describe( 'isEnabled', () => {
- it( 'should be true when the selection directly in the root', () => {
- model.enqueueChange( 'transparent', () => {
- setModelData( model, '[]' );
- command.refresh();
- expect( command.isEnabled ).to.be.true;
- } );
- } );
- it( 'should be true when the selection is in empty block', () => {
- setModelData( model, '<paragraph>[]</paragraph>' );
- expect( command.isEnabled ).to.be.true;
- } );
- } );
- describe( 'execute()', () => {
- const finderMock = {
- on: ( eventName, callback ) => {
- finderMock[ eventName ] = callback;
- }
- };
- beforeEach( () => {
- window.CKFinder = {
- modal: config => {
- config.onInit( finderMock );
- },
- popup: config => {
- config.onInit( finderMock );
- }
- };
- } );
- it( 'should register proper listeners on CKFinder instance', () => {
- command.execute();
- expect( finderMock ).to.have.property( 'files:choose' );
- expect( finderMock ).to.have.property( 'file:choose:resizedImage' );
- } );
- it( 'should use "CKFinder.modal" as default CKFinder opener method', () => {
- const spy = sinon.spy( window.CKFinder, 'modal' );
- command.execute();
- sinon.assert.calledOnce( spy );
- } );
- it( 'should use "CKFinder.modal" as default CKFinder opener method', () => {
- const spy = sinon.spy( window.CKFinder, 'popup' );
- editor.config.set( 'ckfinder.openerMethod', 'popup' );
- command.execute();
- sinon.assert.calledOnce( spy );
- } );
- it( 'should throw if unsupported CKFinder opener method was set', () => {
- editor.config.set( 'ckfinder.openerMethod', 'foobar' );
- expect( () => {
- command.execute();
- } ).to.throw( CKEditorError, /ckfinder-unknown-openerMethod/ );
- } );
- it( 'should insert single chosen image as image widget', () => {
- const url = 'foo/bar.jpg';
- command.execute();
- mockFilesChooseEvent( [ mockFinderFile( url ) ] );
- expect( getModelData( model ) )
- .to.equal( `[<image src="${ url }"></image>]<paragraph>foo</paragraph>` );
- } );
- it( 'should pass CKFinder config options', () => {
- const spy = sinon.spy( window.CKFinder, 'modal' );
- const connectorPath = 'foo/bar.php';
- editor.config.set( 'ckfinder.config', { connectorPath } );
- command.execute();
- const openerMethodOptions = spy.args[ 0 ][ 0 ];
- expect( openerMethodOptions ).to.have.property( 'chooseFiles', true );
- expect( openerMethodOptions ).to.have.property( 'onInit' );
- expect( openerMethodOptions ).to.have.property( 'connectorPath', connectorPath );
- } );
- it( 'should insert multiple chosen images as image widget', () => {
- const url1 = 'foo/bar1.jpg';
- const url2 = 'foo/bar2.jpg';
- const url3 = 'foo/bar3.jpg';
- command.execute();
- mockFilesChooseEvent( [ mockFinderFile( url1 ), mockFinderFile( url2 ), mockFinderFile( url3 ) ] );
- expect( getModelData( model ) ).to.equal(
- `<image src="${ url1 }"></image><image src="${ url2 }"></image>[<image src="${ url3 }"></image>]<paragraph>foo</paragraph>`
- );
- } );
- it( 'should insert only images from chosen files', () => {
- const url1 = 'foo/bar1.jpg';
- const url2 = 'foo/bar2.pdf';
- const url3 = 'foo/bar3.jpg';
- command.execute();
- mockFilesChooseEvent( [ mockFinderFile( url1 ), mockFinderFile( url2, false ), mockFinderFile( url3 ) ] );
- expect( getModelData( model ) ).to.equal(
- `<image src="${ url1 }"></image>[<image src="${ url3 }"></image>]<paragraph>foo</paragraph>`
- );
- } );
- it( 'should use CKFinder Proxy for privately hosted files', () => {
- const proxyUrl = 'bar/foo.jpg';
- finderMock.request = () => proxyUrl;
- command.execute();
- mockFilesChooseEvent( [ mockFinderFile( false ) ] );
- expect( getModelData( model ) ).to.equal(
- `[<image src="${ proxyUrl }"></image>]<paragraph>foo</paragraph>`
- );
- } );
- it( 'should insert resized image as image widget', () => {
- const url = 'foo/bar.jpg';
- command.execute();
- mockFinderEvent( 'file:choose:resizedImage', { resizedUrl: url } );
- expect( getModelData( model ) )
- .to.equal( `[<image src="${ url }"></image>]<paragraph>foo</paragraph>` );
- } );
- it( 'should show warning notification if no resized image URL was returned', done => {
- const notification = editor.plugins.get( Notification );
- notification.on( 'show:warning', ( evt, data ) => {
- expect( data.message ).to.equal( 'Could not obtain resized image URL. Try different image or folder.' );
- expect( data.title ).to.equal( 'Selecting resized image failed' );
- evt.stop();
- done();
- }, { priority: 'high' } );
- command.execute();
- mockFinderEvent( 'file:choose:resizedImage', { resizedUrl: undefined } );
- expect( getModelData( model ) )
- .to.equal( '<paragraph>f[o]o</paragraph>' );
- } );
- it( 'should not insert image nor crash when image could not be inserted', () => {
- model.schema.register( 'other', {
- allowIn: '$root',
- isLimit: true
- } );
- model.schema.extend( '$text', { allowIn: 'other' } );
- editor.conversion.for( 'downcast' ).add( downcastElementToElement( { model: 'other', view: 'p' } ) );
- setModelData( model, '<other>[]</other>' );
- command.execute();
- mockFilesChooseEvent( [ mockFinderFile( 'foo/bar.jpg' ) ] );
- expect( getModelData( model ) ).to.equal( '<other>[]</other>' );
- } );
- function mockFinderFile( url = 'foo/bar.jpg', isImage = true ) {
- return {
- isImage: () => isImage,
- get: () => url
- };
- }
- function mockFinderEvent( eventName, data ) {
- finderMock[ eventName ]( { data } );
- }
- function mockFilesChooseEvent( files ) {
- const data = {
- files: {
- toArray: () => files
- }
- };
- mockFinderEvent( 'files:choose', data );
- }
- } );
- } );
|