plugincollection.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import testUtils from '../tests/_utils/utils';
  6. import Editor from '../src/editor/editor';
  7. import PluginCollection from '../src/plugincollection';
  8. import Plugin from '../src/plugin';
  9. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  10. import log from '@ckeditor/ckeditor5-utils/src/log';
  11. let editor, availablePlugins;
  12. let PluginA, PluginB, PluginC, PluginD, PluginE, PluginF, PluginG, PluginH, PluginI, PluginJ, PluginK, PluginX;
  13. class TestError extends Error {}
  14. class ChildPlugin extends Plugin {}
  15. class GrandPlugin extends ChildPlugin {}
  16. testUtils.createSinonSandbox();
  17. before( () => {
  18. PluginA = createPlugin( 'A' );
  19. PluginB = createPlugin( 'B' );
  20. PluginC = createPlugin( 'C' );
  21. PluginD = createPlugin( 'D' );
  22. PluginE = createPlugin( 'E' );
  23. PluginF = createPlugin( 'F' );
  24. PluginG = createPlugin( 'G', GrandPlugin );
  25. PluginH = createPlugin( 'H' );
  26. PluginI = createPlugin( 'I' );
  27. PluginJ = createPlugin( 'J' );
  28. PluginK = createPlugin( 'K' );
  29. PluginX = class extends Plugin {
  30. static get pluginName() {
  31. return 'X';
  32. }
  33. constructor( editor ) {
  34. super( editor );
  35. throw new TestError( 'Some error inside a plugin' );
  36. }
  37. };
  38. PluginC.requires = [ PluginB ];
  39. PluginD.requires = [ PluginA, PluginC ];
  40. PluginF.requires = [ PluginE ];
  41. PluginE.requires = [ PluginF ];
  42. PluginH.requires = [ PluginI ];
  43. PluginJ.requires = [ 'K' ];
  44. PluginK.requires = [ PluginA ];
  45. editor = new Editor( {
  46. plugins: []
  47. } );
  48. } );
  49. describe( 'PluginCollection', () => {
  50. beforeEach( () => {
  51. availablePlugins = [
  52. PluginA,
  53. PluginB,
  54. PluginC,
  55. PluginD,
  56. PluginE,
  57. PluginF,
  58. PluginG,
  59. PluginH,
  60. PluginI,
  61. PluginJ,
  62. PluginK,
  63. PluginX
  64. ];
  65. } );
  66. describe( 'load()', () => {
  67. it( 'should not fail when trying to load 0 plugins (empty array)', () => {
  68. let plugins = new PluginCollection( editor, availablePlugins );
  69. return plugins.load( [] )
  70. .then( () => {
  71. expect( getPlugins( plugins ) ).to.be.empty;
  72. } );
  73. } );
  74. it( 'should add collection items for loaded plugins', () => {
  75. let plugins = new PluginCollection( editor, availablePlugins );
  76. return plugins.load( [ PluginA, PluginB ] )
  77. .then( () => {
  78. expect( getPlugins( plugins ).length ).to.equal( 2 );
  79. expect( plugins.get( PluginA ) ).to.be.an.instanceof( PluginA );
  80. expect( plugins.get( PluginB ) ).to.be.an.instanceof( PluginB );
  81. } );
  82. } );
  83. it( 'should add collection items for loaded plugins using plugin names', () => {
  84. let plugins = new PluginCollection( editor, availablePlugins );
  85. return plugins.load( [ 'A', 'B' ] )
  86. .then( () => {
  87. expect( getPlugins( plugins ).length ).to.equal( 2 );
  88. expect( plugins.get( 'A' ) ).to.be.an.instanceof( PluginA );
  89. expect( plugins.get( 'B' ) ).to.be.an.instanceof( PluginB );
  90. } );
  91. } );
  92. it( 'should load dependency plugins', () => {
  93. let plugins = new PluginCollection( editor, availablePlugins );
  94. let spy = sinon.spy( plugins, '_add' );
  95. return plugins.load( [ PluginA, PluginC ] )
  96. .then( ( loadedPlugins ) => {
  97. expect( getPlugins( plugins ).length ).to.equal( 3 );
  98. expect( getPluginNames( getPluginsFromSpy( spy ) ) )
  99. .to.deep.equal( [ 'A', 'B', 'C' ], 'order by plugins._add()' );
  100. expect( getPluginNames( loadedPlugins ) )
  101. .to.deep.equal( [ 'A', 'B', 'C' ], 'order by returned value' );
  102. } );
  103. } );
  104. it( 'should load dependency plugins defined by plugin names', () => {
  105. let plugins = new PluginCollection( editor, availablePlugins );
  106. let spy = sinon.spy( plugins, '_add' );
  107. return plugins.load( [ 'J' ] )
  108. .then( ( loadedPlugins ) => {
  109. expect( getPlugins( plugins ).length ).to.equal( 3 );
  110. expect( getPluginNames( getPluginsFromSpy( spy ) ) )
  111. .to.deep.equal( [ 'A', 'K', 'J' ], 'order by plugins._add()' );
  112. expect( getPluginNames( loadedPlugins ) )
  113. .to.deep.equal( [ 'A', 'K', 'J' ], 'order by returned value' );
  114. } );
  115. } );
  116. it( 'should be ok when dependencies are loaded first', () => {
  117. let plugins = new PluginCollection( editor, availablePlugins );
  118. let spy = sinon.spy( plugins, '_add' );
  119. return plugins.load( [ PluginA, PluginB, PluginC ] )
  120. .then( ( loadedPlugins ) => {
  121. expect( getPlugins( plugins ).length ).to.equal( 3 );
  122. expect( getPluginNames( getPluginsFromSpy( spy ) ) )
  123. .to.deep.equal( [ 'A', 'B', 'C' ], 'order by plugins._add()' );
  124. expect( getPluginNames( loadedPlugins ) )
  125. .to.deep.equal( [ 'A', 'B', 'C' ], 'order by returned value' );
  126. } );
  127. } );
  128. it( 'should load deep dependency plugins', () => {
  129. let plugins = new PluginCollection( editor, availablePlugins );
  130. let spy = sinon.spy( plugins, '_add' );
  131. return plugins.load( [ PluginD ] )
  132. .then( ( loadedPlugins ) => {
  133. expect( getPlugins( plugins ).length ).to.equal( 4 );
  134. // The order must have dependencies first.
  135. expect( getPluginNames( getPluginsFromSpy( spy ) ) )
  136. .to.deep.equal( [ 'A', 'B', 'C', 'D' ], 'order by plugins._add()' );
  137. expect( getPluginNames( loadedPlugins ) )
  138. .to.deep.equal( [ 'A', 'B', 'C', 'D' ], 'order by returned value' );
  139. } );
  140. } );
  141. it( 'should handle cross dependency plugins', () => {
  142. let plugins = new PluginCollection( editor, availablePlugins );
  143. let spy = sinon.spy( plugins, '_add' );
  144. return plugins.load( [ PluginA, PluginE ] )
  145. .then( ( loadedPlugins ) => {
  146. expect( getPlugins( plugins ).length ).to.equal( 3 );
  147. // The order must have dependencies first.
  148. expect( getPluginNames( getPluginsFromSpy( spy ) ) )
  149. .to.deep.equal( [ 'A', 'F', 'E' ], 'order by plugins._add()' );
  150. expect( getPluginNames( loadedPlugins ) )
  151. .to.deep.equal( [ 'A', 'F', 'E' ], 'order by returned value' );
  152. } );
  153. } );
  154. it( 'should load grand child classes', () => {
  155. let plugins = new PluginCollection( editor, availablePlugins );
  156. return plugins.load( [ PluginG ] )
  157. .then( () => {
  158. expect( getPlugins( plugins ).length ).to.equal( 1 );
  159. } );
  160. } );
  161. it( 'should set the `editor` property on loaded plugins', () => {
  162. let plugins = new PluginCollection( editor, availablePlugins );
  163. return plugins.load( [ PluginA, PluginB ] )
  164. .then( () => {
  165. expect( plugins.get( PluginA ).editor ).to.equal( editor );
  166. expect( plugins.get( PluginB ).editor ).to.equal( editor );
  167. } );
  168. } );
  169. it( 'should reject on broken plugins (forward the error thrown in a plugin)', () => {
  170. let logSpy = testUtils.sinon.stub( log, 'error' );
  171. let plugins = new PluginCollection( editor, availablePlugins );
  172. return plugins.load( [ PluginA, PluginX, PluginB ] )
  173. // Throw here, so if by any chance plugins.load() was resolved correctly catch() will be stil executed.
  174. .then( () => {
  175. throw new Error( 'Test error: this promise should not be resolved successfully' );
  176. } )
  177. .catch( ( err ) => {
  178. expect( err ).to.be.an.instanceof( TestError );
  179. expect( err ).to.have.property( 'message', 'Some error inside a plugin' );
  180. sinon.assert.calledOnce( logSpy );
  181. expect( logSpy.args[ 0 ][ 0 ] ).to.match( /^plugincollection-load:/ );
  182. } );
  183. } );
  184. it( 'should reject when loading a module which is not a plugin', () => {
  185. class Y {}
  186. availablePlugins.push( Y );
  187. let logSpy = testUtils.sinon.stub( log, 'error' );
  188. let plugins = new PluginCollection( editor, availablePlugins );
  189. return plugins.load( [ Y ] )
  190. // Throw here, so if by any chance plugins.load() was resolved correctly catch() will be stil executed.
  191. .then( () => {
  192. throw new Error( 'Test error: this promise should not be resolved successfully' );
  193. } )
  194. .catch( ( err ) => {
  195. expect( err ).to.be.an.instanceof( CKEditorError );
  196. expect( err.message ).to.match( /^plugincollection-instance/ );
  197. sinon.assert.calledOnce( logSpy );
  198. expect( logSpy.args[ 0 ][ 0 ] ).to.match( /^plugincollection-load:/ );
  199. } );
  200. } );
  201. it( 'should reject when loading non-existing plugin', () => {
  202. let logSpy = testUtils.sinon.stub( log, 'error' );
  203. let plugins = new PluginCollection( editor, availablePlugins );
  204. return plugins.load( [ 'Non-Existing-Plugin' ] )
  205. // Throw here, so if by any chance plugins.load() was resolved correctly catch() will be stil executed.
  206. .then( () => {
  207. throw new Error( 'Test error: this promise should not be resolved successfully' );
  208. } )
  209. .catch( ( err ) => {
  210. expect( err ).to.be.an.instanceof( CKEditorError );
  211. expect( err.message ).to.match( /^plugincollection-instance/ );
  212. sinon.assert.calledOnce( logSpy );
  213. expect( logSpy.args[ 0 ][ 0 ] ).to.match( /^plugincollection-load:/ );
  214. } );
  215. } );
  216. it( 'should load allowed plugins (plugins and removedPlugins are constructors)', () => {
  217. let plugins = new PluginCollection( editor, availablePlugins );
  218. return plugins.load( [ PluginA, PluginB, PluginC ], [ PluginA ] )
  219. .then( () => {
  220. expect( getPlugins( plugins ).length ).to.equal( 2 );
  221. expect( plugins.get( PluginB ) ).to.be.an.instanceof( PluginB );
  222. expect( plugins.get( PluginC ) ).to.be.an.instanceof( PluginC );
  223. } );
  224. } );
  225. it( 'should load allowed plugins (plugins are constructors, removedPlugins are plugin names)', () => {
  226. let plugins = new PluginCollection( editor, availablePlugins );
  227. return plugins.load( [ PluginA, PluginB, PluginC ], [ 'A' ] )
  228. .then( () => {
  229. expect( getPlugins( plugins ).length ).to.equal( 2 );
  230. expect( plugins.get( PluginB ) ).to.be.an.instanceof( PluginB );
  231. expect( plugins.get( PluginC ) ).to.be.an.instanceof( PluginC );
  232. } );
  233. } );
  234. it( 'should load allowed plugins (plugins and removedPlugins are plugin names)', () => {
  235. let plugins = new PluginCollection( editor, availablePlugins );
  236. return plugins.load( [ 'A', 'B', 'C' ], [ 'A' ] )
  237. .then( () => {
  238. expect( getPlugins( plugins ).length ).to.equal( 2 );
  239. expect( plugins.get( PluginB ) ).to.be.an.instanceof( PluginB );
  240. expect( plugins.get( PluginC ) ).to.be.an.instanceof( PluginC );
  241. } );
  242. } );
  243. it( 'should load allowed plugins (plugins are plugin names, removedPlugins are constructors)', () => {
  244. let plugins = new PluginCollection( editor, availablePlugins );
  245. return plugins.load( [ 'A', 'B', 'C' ], [ PluginA ] )
  246. .then( () => {
  247. expect( getPlugins( plugins ).length ).to.equal( 2 );
  248. expect( plugins.get( PluginB ) ).to.be.an.instanceof( PluginB );
  249. expect( plugins.get( PluginC ) ).to.be.an.instanceof( PluginC );
  250. } );
  251. } );
  252. it( 'should reject when loaded plugin requires not allowed plugins', () => {
  253. let logSpy = testUtils.sinon.stub( log, 'error' );
  254. let plugins = new PluginCollection( editor, availablePlugins );
  255. return plugins.load( [ PluginA, PluginB, PluginC, PluginD ], [ PluginA, PluginB ] )
  256. // Throw here, so if by any chance plugins.load() was resolved correctly catch() will be stil executed.
  257. .then( () => {
  258. throw new Error( 'Test error: this promise should not be resolved successfully' );
  259. } )
  260. .catch( ( err ) => {
  261. expect( err ).to.be.an.instanceof( CKEditorError );
  262. expect( err.message ).to.match( /^plugincollection-instance/ );
  263. expect( logSpy.calledTwice ).to.equal( true );
  264. } );
  265. } );
  266. } );
  267. describe( 'get()', () => {
  268. it( 'retrieves plugin by its constructor', () => {
  269. class SomePlugin extends Plugin {}
  270. availablePlugins.push( SomePlugin );
  271. let plugins = new PluginCollection( editor, availablePlugins );
  272. return plugins.load( [ SomePlugin ] )
  273. .then( () => {
  274. expect( plugins.get( SomePlugin ) ).to.be.instanceOf( SomePlugin );
  275. } );
  276. } );
  277. it( 'retrieves plugin by its name and constructor', () => {
  278. class SomePlugin extends Plugin {}
  279. SomePlugin.pluginName = 'foo/bar';
  280. availablePlugins.push( SomePlugin );
  281. let plugins = new PluginCollection( editor, availablePlugins );
  282. return plugins.load( [ SomePlugin ] )
  283. .then( () => {
  284. expect( plugins.get( 'foo/bar' ) ).to.be.instanceOf( SomePlugin );
  285. expect( plugins.get( SomePlugin ) ).to.be.instanceOf( SomePlugin );
  286. } );
  287. } );
  288. } );
  289. describe( 'iterator', () => {
  290. it( 'exists', () => {
  291. let plugins = new PluginCollection( editor, availablePlugins );
  292. expect( plugins ).to.have.property( Symbol.iterator );
  293. } );
  294. it( 'returns only plugins by constructors', () => {
  295. class SomePlugin1 extends Plugin {}
  296. class SomePlugin2 extends Plugin {}
  297. SomePlugin2.pluginName = 'foo/bar';
  298. availablePlugins.push( SomePlugin1 );
  299. availablePlugins.push( SomePlugin2 );
  300. let plugins = new PluginCollection( editor, availablePlugins );
  301. return plugins.load( [ SomePlugin1, SomePlugin2 ] )
  302. .then( () => {
  303. const pluginConstructors = Array.from( plugins )
  304. .map( entry => entry[ 0 ] );
  305. expect( pluginConstructors ).to.have.members( [ SomePlugin1, SomePlugin2 ] );
  306. } );
  307. } );
  308. } );
  309. } );
  310. function createPlugin( name ) {
  311. const P = class extends Plugin {
  312. constructor( editor ) {
  313. super( editor );
  314. this.pluginName = name;
  315. }
  316. };
  317. P.pluginName = name;
  318. return P;
  319. }
  320. function getPlugins( pluginCollection ) {
  321. return Array.from( pluginCollection )
  322. .map( entry => entry[ 1 ] ); // Get instances.
  323. }
  324. function getPluginsFromSpy( addSpy ) {
  325. return addSpy.args
  326. .map( ( arg ) => arg[ 0 ] )
  327. // Entries may be kept twice in the plugins map - once as a pluginName => plugin, once as pluginClass => plugin.
  328. // Return only pluginClass => plugin entries as these will always represent all plugins.
  329. .filter( ( plugin ) => typeof plugin == 'function' );
  330. }
  331. function getPluginNames( plugins ) {
  332. return plugins.map( ( plugin ) => plugin.pluginName );
  333. }