8
0

plugincollection.js 13 KB

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