plugincollection.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals setTimeout */
  6. import testUtils from '../tests/_utils/utils';
  7. import Editor from '../src/editor/editor';
  8. import PluginCollection from '../src/plugincollection';
  9. import Plugin from '../src/plugin';
  10. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  11. import log from '@ckeditor/ckeditor5-utils/src/log';
  12. let editor, availablePlugins;
  13. let PluginA, PluginB, PluginC, PluginD, PluginE, PluginF, PluginG, PluginH, PluginI, PluginJ, PluginK, PluginX, PluginFoo, AnotherPluginFoo;
  14. class TestError extends Error {}
  15. class ChildPlugin extends Plugin {}
  16. class GrandPlugin extends ChildPlugin {}
  17. testUtils.createSinonSandbox();
  18. before( () => {
  19. PluginA = createPlugin( 'A' );
  20. PluginB = createPlugin( 'B' );
  21. PluginC = createPlugin( 'C' );
  22. PluginD = createPlugin( 'D' );
  23. PluginE = createPlugin( 'E' );
  24. PluginF = createPlugin( 'F' );
  25. PluginG = createPlugin( 'G', GrandPlugin );
  26. PluginH = createPlugin( 'H' );
  27. PluginI = createPlugin( 'I' );
  28. PluginJ = createPlugin( 'J' );
  29. PluginK = createPlugin( 'K' );
  30. PluginX = class extends Plugin {
  31. constructor( editor ) {
  32. super( editor );
  33. throw new TestError( 'Some error inside a plugin' );
  34. }
  35. };
  36. PluginFoo = createPlugin( 'Foo' );
  37. AnotherPluginFoo = createPlugin( 'Foo' );
  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. PluginFoo.requires = [];
  64. } );
  65. describe( 'load()', () => {
  66. it( 'should not fail when trying to load 0 plugins (empty array)', () => {
  67. const plugins = new PluginCollection( editor, availablePlugins );
  68. return plugins.load( [] )
  69. .then( () => {
  70. expect( getPlugins( plugins ) ).to.be.empty;
  71. } );
  72. } );
  73. it( 'should add collection items for loaded plugins', () => {
  74. const plugins = new PluginCollection( editor, availablePlugins );
  75. return plugins.load( [ PluginA, PluginB ] )
  76. .then( () => {
  77. expect( getPlugins( plugins ).length ).to.equal( 2 );
  78. expect( plugins.get( PluginA ) ).to.be.an.instanceof( PluginA );
  79. expect( plugins.get( PluginB ) ).to.be.an.instanceof( PluginB );
  80. } );
  81. } );
  82. it( 'should add collection items for loaded plugins using plugin names', () => {
  83. const plugins = new PluginCollection( editor, availablePlugins );
  84. return plugins.load( [ 'A', 'B' ] )
  85. .then( () => {
  86. expect( getPlugins( plugins ).length ).to.equal( 2 );
  87. expect( plugins.get( 'A' ) ).to.be.an.instanceof( PluginA );
  88. expect( plugins.get( 'B' ) ).to.be.an.instanceof( PluginB );
  89. } );
  90. } );
  91. it( 'should load dependency plugins', () => {
  92. const plugins = new PluginCollection( editor, availablePlugins );
  93. const spy = sinon.spy( plugins, '_add' );
  94. return plugins.load( [ PluginA, PluginC ] )
  95. .then( loadedPlugins => {
  96. expect( getPlugins( plugins ).length ).to.equal( 3 );
  97. expect( getPluginNames( getPluginsFromSpy( spy ) ) )
  98. .to.deep.equal( [ 'A', 'B', 'C' ], 'order by plugins._add()' );
  99. expect( getPluginNames( loadedPlugins ) )
  100. .to.deep.equal( [ 'A', 'B', 'C' ], 'order by returned value' );
  101. } );
  102. } );
  103. it( 'should load dependency plugins defined by plugin names', () => {
  104. const plugins = new PluginCollection( editor, availablePlugins );
  105. const spy = sinon.spy( plugins, '_add' );
  106. return plugins.load( [ 'J' ] )
  107. .then( loadedPlugins => {
  108. expect( getPlugins( plugins ).length ).to.equal( 3 );
  109. expect( getPluginNames( getPluginsFromSpy( spy ) ) )
  110. .to.deep.equal( [ 'A', 'K', 'J' ], 'order by plugins._add()' );
  111. expect( getPluginNames( loadedPlugins ) )
  112. .to.deep.equal( [ 'A', 'K', 'J' ], 'order by returned value' );
  113. } );
  114. } );
  115. it( 'should be ok when dependencies are loaded first', () => {
  116. const plugins = new PluginCollection( editor, availablePlugins );
  117. const spy = sinon.spy( plugins, '_add' );
  118. return plugins.load( [ PluginA, PluginB, PluginC ] )
  119. .then( loadedPlugins => {
  120. expect( getPlugins( plugins ).length ).to.equal( 3 );
  121. expect( getPluginNames( getPluginsFromSpy( spy ) ) )
  122. .to.deep.equal( [ 'A', 'B', 'C' ], 'order by plugins._add()' );
  123. expect( getPluginNames( loadedPlugins ) )
  124. .to.deep.equal( [ 'A', 'B', 'C' ], 'order by returned value' );
  125. } );
  126. } );
  127. it( 'should load deep dependency plugins', () => {
  128. const plugins = new PluginCollection( editor, availablePlugins );
  129. const spy = sinon.spy( plugins, '_add' );
  130. return plugins.load( [ PluginD ] )
  131. .then( loadedPlugins => {
  132. expect( getPlugins( plugins ).length ).to.equal( 4 );
  133. // The order must have dependencies first.
  134. expect( getPluginNames( getPluginsFromSpy( spy ) ) )
  135. .to.deep.equal( [ 'A', 'B', 'C', 'D' ], 'order by plugins._add()' );
  136. expect( getPluginNames( loadedPlugins ) )
  137. .to.deep.equal( [ 'A', 'B', 'C', 'D' ], 'order by returned value' );
  138. } );
  139. } );
  140. it( 'should handle cross dependency plugins', () => {
  141. const plugins = new PluginCollection( editor, availablePlugins );
  142. const spy = sinon.spy( plugins, '_add' );
  143. return plugins.load( [ PluginA, PluginE ] )
  144. .then( loadedPlugins => {
  145. expect( getPlugins( plugins ).length ).to.equal( 3 );
  146. // The order must have dependencies first.
  147. expect( getPluginNames( getPluginsFromSpy( spy ) ) )
  148. .to.deep.equal( [ 'A', 'F', 'E' ], 'order by plugins._add()' );
  149. expect( getPluginNames( loadedPlugins ) )
  150. .to.deep.equal( [ 'A', 'F', 'E' ], 'order by returned value' );
  151. } );
  152. } );
  153. it( 'should load grand child classes', () => {
  154. const plugins = new PluginCollection( editor, availablePlugins );
  155. return plugins.load( [ PluginG ] )
  156. .then( () => {
  157. expect( getPlugins( plugins ).length ).to.equal( 1 );
  158. } );
  159. } );
  160. it( 'should load plugin which does not extend the base Plugin class', () => {
  161. class Y { }
  162. const plugins = new PluginCollection( editor, availablePlugins );
  163. return plugins.load( [ Y ] )
  164. .then( () => {
  165. expect( getPlugins( plugins ).length ).to.equal( 1 );
  166. } );
  167. } );
  168. it( 'should load plugin which is a simple function', () => {
  169. function pluginAsFunction( editor ) {
  170. this.editor = editor;
  171. }
  172. const plugins = new PluginCollection( editor, availablePlugins );
  173. return plugins.load( [ pluginAsFunction ] )
  174. .then( () => {
  175. expect( getPlugins( plugins ).length ).to.equal( 1 );
  176. } );
  177. } );
  178. it( 'should set the `editor` property on loaded plugins', () => {
  179. const plugins = new PluginCollection( editor, availablePlugins );
  180. function pluginAsFunction( editor ) {
  181. this.editor = editor;
  182. }
  183. class Y {
  184. constructor( editor ) {
  185. this.editor = editor;
  186. }
  187. }
  188. return plugins.load( [ PluginA, PluginB, pluginAsFunction, Y ] )
  189. .then( () => {
  190. expect( plugins.get( PluginA ).editor ).to.equal( editor );
  191. expect( plugins.get( PluginB ).editor ).to.equal( editor );
  192. expect( plugins.get( pluginAsFunction ).editor ).to.equal( editor );
  193. expect( plugins.get( Y ).editor ).to.equal( editor );
  194. } );
  195. } );
  196. it( 'should reject on broken plugins (forward the error thrown in a plugin)', () => {
  197. const logSpy = testUtils.sinon.stub( log, 'error' );
  198. const plugins = new PluginCollection( editor, availablePlugins );
  199. return plugins.load( [ PluginA, PluginX, PluginB ] )
  200. // Throw here, so if by any chance plugins.load() was resolved correctly catch() will be stil executed.
  201. .then( () => {
  202. throw new Error( 'Test error: this promise should not be resolved successfully' );
  203. } )
  204. .catch( err => {
  205. expect( err ).to.be.an.instanceof( TestError );
  206. expect( err ).to.have.property( 'message', 'Some error inside a plugin' );
  207. sinon.assert.calledOnce( logSpy );
  208. expect( logSpy.args[ 0 ][ 0 ] ).to.match( /^plugincollection-load:/ );
  209. } );
  210. } );
  211. it( 'should reject when loading non-existent plugin', () => {
  212. const logSpy = testUtils.sinon.stub( log, 'error' );
  213. const plugins = new PluginCollection( editor, availablePlugins );
  214. return plugins.load( [ 'NonExistentPlugin' ] )
  215. // Throw here, so if by any chance plugins.load() was resolved correctly catch() will be stil executed.
  216. .then( () => {
  217. throw new Error( 'Test error: this promise should not be resolved successfully' );
  218. } )
  219. .catch( err => {
  220. expect( err ).to.be.an.instanceof( CKEditorError );
  221. expect( err.message ).to.match( /^plugincollection-plugin-not-found/ );
  222. sinon.assert.calledOnce( logSpy );
  223. expect( logSpy.args[ 0 ][ 0 ] ).to.match( /^plugincollection-plugin-not-found:/ );
  224. } );
  225. } );
  226. it( 'should load chosen plugins (plugins and removePlugins are constructors)', () => {
  227. const plugins = new PluginCollection( editor, availablePlugins );
  228. return plugins.load( [ PluginA, PluginB, PluginC ], [ PluginA ] )
  229. .then( () => {
  230. expect( getPlugins( plugins ).length ).to.equal( 2 );
  231. expect( plugins.get( PluginB ) ).to.be.an.instanceof( PluginB );
  232. expect( plugins.get( PluginC ) ).to.be.an.instanceof( PluginC );
  233. } );
  234. } );
  235. it( 'should load chosen plugins (plugins are constructors, removePlugins are names)', () => {
  236. const plugins = new PluginCollection( editor, availablePlugins );
  237. return plugins.load( [ PluginA, PluginB, PluginC ], [ 'A' ] )
  238. .then( () => {
  239. expect( getPlugins( plugins ).length ).to.equal( 2 );
  240. expect( plugins.get( PluginB ) ).to.be.an.instanceof( PluginB );
  241. expect( plugins.get( PluginC ) ).to.be.an.instanceof( PluginC );
  242. } );
  243. } );
  244. it( 'should load chosen plugins (plugins and removePlugins are names)', () => {
  245. const plugins = new PluginCollection( editor, availablePlugins );
  246. return plugins.load( [ 'A', 'B', 'C' ], [ 'A' ] )
  247. .then( () => {
  248. expect( getPlugins( plugins ).length ).to.equal( 2 );
  249. expect( plugins.get( PluginB ) ).to.be.an.instanceof( PluginB );
  250. expect( plugins.get( PluginC ) ).to.be.an.instanceof( PluginC );
  251. } );
  252. } );
  253. it( 'should load chosen plugins (plugins are names, removePlugins are constructors)', () => {
  254. const plugins = new PluginCollection( editor, availablePlugins );
  255. return plugins.load( [ 'A', 'B', 'C' ], [ PluginA ] )
  256. .then( () => {
  257. expect( getPlugins( plugins ).length ).to.equal( 2 );
  258. expect( plugins.get( PluginB ) ).to.be.an.instanceof( PluginB );
  259. expect( plugins.get( PluginC ) ).to.be.an.instanceof( PluginC );
  260. } );
  261. } );
  262. it( 'should load chosen plugins (plugins are names, removePlugins contains an anonymous plugin)', () => {
  263. class AnonymousPlugin {}
  264. const plugins = new PluginCollection( editor, [ AnonymousPlugin ].concat( availablePlugins ) );
  265. return plugins.load( [ AnonymousPlugin, 'A', 'B' ], [ AnonymousPlugin ] )
  266. .then( () => {
  267. expect( getPlugins( plugins ).length ).to.equal( 2 );
  268. expect( plugins.get( PluginA ) ).to.be.an.instanceof( PluginA );
  269. expect( plugins.get( PluginB ) ).to.be.an.instanceof( PluginB );
  270. } );
  271. } );
  272. it( 'should reject when loaded plugin requires not allowed plugins', () => {
  273. const logSpy = testUtils.sinon.stub( log, 'error' );
  274. const plugins = new PluginCollection( editor, availablePlugins );
  275. return plugins.load( [ PluginA, PluginB, PluginC, PluginD ], [ PluginA, PluginB ] )
  276. // Throw here, so if by any chance plugins.load() was resolved correctly catch() will be stil executed.
  277. .then( () => {
  278. throw new Error( 'Test error: this promise should not be resolved successfully' );
  279. } )
  280. .catch( err => {
  281. expect( err ).to.be.an.instanceof( CKEditorError );
  282. expect( err.message ).to.match( /^plugincollection-required/ );
  283. expect( logSpy.calledTwice ).to.equal( true );
  284. } );
  285. } );
  286. it( 'logs if tries to load more than one plugin with the same name', () => {
  287. const logSpy = testUtils.sinon.stub( log, 'warn' );
  288. const plugins = new PluginCollection( editor );
  289. return plugins.load( [ PluginFoo, AnotherPluginFoo ] )
  290. .then( () => {
  291. expect( getPlugins( plugins ).length ).to.equal( 2 );
  292. expect( plugins.get( 'Foo' ) ).to.be.an.instanceof( PluginFoo );
  293. expect( plugins.get( PluginFoo ) ).to.be.an.instanceof( PluginFoo );
  294. expect( plugins.get( AnotherPluginFoo ) ).to.be.an.instanceof( AnotherPluginFoo );
  295. expect( logSpy.calledOnce ).to.equal( true );
  296. expect( logSpy.firstCall.args[ 0 ] ).to.match( /^plugincollection-plugin-name-conflict:/ );
  297. const warnData = logSpy.firstCall.args[ 1 ];
  298. expect( warnData.pluginName ).to.equal( 'Foo' );
  299. expect( warnData.plugin1 ).to.equal( PluginFoo );
  300. expect( warnData.plugin2 ).to.equal( AnotherPluginFoo );
  301. } );
  302. } );
  303. it( 'logs if tries to load more than one plugin with the same name (plugin requires plugin with the same name)', () => {
  304. PluginFoo.requires = [ AnotherPluginFoo ];
  305. const logSpy = testUtils.sinon.stub( log, 'warn' );
  306. const plugins = new PluginCollection( editor );
  307. return plugins.load( [ PluginFoo ] )
  308. .then( () => {
  309. expect( getPlugins( plugins ).length ).to.equal( 2 );
  310. expect( plugins.get( 'Foo' ) ).to.be.an.instanceof( AnotherPluginFoo );
  311. expect( plugins.get( AnotherPluginFoo ) ).to.be.an.instanceof( AnotherPluginFoo );
  312. expect( plugins.get( PluginFoo ) ).to.be.an.instanceof( PluginFoo );
  313. expect( logSpy.calledOnce ).to.equal( true );
  314. expect( logSpy.firstCall.args[ 0 ] ).to.match( /^plugincollection-plugin-name-conflict:/ );
  315. } );
  316. } );
  317. it(
  318. 'logs if tries to load more than one plugin with the same name (plugin with the same name is built-in the PluginCollection)',
  319. () => {
  320. availablePlugins = [ PluginFoo ];
  321. const logSpy = testUtils.sinon.stub( log, 'warn' );
  322. const plugins = new PluginCollection( editor, availablePlugins );
  323. return plugins.load( [ 'Foo', AnotherPluginFoo ] )
  324. .then( () => {
  325. expect( getPlugins( plugins ).length ).to.equal( 2 );
  326. expect( plugins.get( 'Foo' ) ).to.be.an.instanceof( PluginFoo );
  327. expect( plugins.get( PluginFoo ) ).to.be.an.instanceof( PluginFoo );
  328. expect( plugins.get( AnotherPluginFoo ) ).to.be.an.instanceof( AnotherPluginFoo );
  329. expect( logSpy.calledOnce ).to.equal( true );
  330. expect( logSpy.firstCall.args[ 0 ] ).to.match( /^plugincollection-plugin-name-conflict:/ );
  331. } );
  332. }
  333. );
  334. } );
  335. describe( 'get()', () => {
  336. it( 'retrieves plugin by its constructor', () => {
  337. class SomePlugin extends Plugin {}
  338. availablePlugins.push( SomePlugin );
  339. const plugins = new PluginCollection( editor, availablePlugins );
  340. return plugins.load( [ SomePlugin ] )
  341. .then( () => {
  342. expect( plugins.get( SomePlugin ) ).to.be.instanceOf( SomePlugin );
  343. } );
  344. } );
  345. it( 'retrieves plugin by its name and constructor', () => {
  346. class SomePlugin extends Plugin {}
  347. SomePlugin.pluginName = 'foo/bar';
  348. availablePlugins.push( SomePlugin );
  349. const plugins = new PluginCollection( editor, availablePlugins );
  350. return plugins.load( [ SomePlugin ] )
  351. .then( () => {
  352. expect( plugins.get( 'foo/bar' ) ).to.be.instanceOf( SomePlugin );
  353. expect( plugins.get( SomePlugin ) ).to.be.instanceOf( SomePlugin );
  354. } );
  355. } );
  356. } );
  357. describe( 'destroy()', () => {
  358. it( 'calls Plugin#destroy() method on every loaded plugin', () => {
  359. let destroySpyForPluginA, destroySpyForPluginB;
  360. const plugins = new PluginCollection( editor, [] );
  361. return plugins.load( [ PluginA, PluginB ] )
  362. .then( () => {
  363. destroySpyForPluginA = sinon.spy( plugins.get( PluginA ), 'destroy' );
  364. destroySpyForPluginB = sinon.spy( plugins.get( PluginB ), 'destroy' );
  365. return plugins.destroy();
  366. } )
  367. .then( () => {
  368. expect( destroySpyForPluginA.calledOnce ).to.equal( true );
  369. expect( destroySpyForPluginB.calledOnce ).to.equal( true );
  370. } );
  371. } );
  372. it( 'waits until all plugins are destroyed', () => {
  373. const destroyedPlugins = [];
  374. class AsynchronousPluginA extends Plugin {
  375. destroy() {
  376. return new Promise( resolve => {
  377. setTimeout( () => {
  378. super.destroy();
  379. destroyedPlugins.push( 'AsynchronousPluginA.destroy()' );
  380. resolve();
  381. } );
  382. } );
  383. }
  384. }
  385. class AsynchronousPluginB extends Plugin {
  386. destroy() {
  387. return new Promise( resolve => {
  388. setTimeout( () => {
  389. super.destroy();
  390. destroyedPlugins.push( 'AsynchronousPluginB.destroy()' );
  391. resolve();
  392. } );
  393. } );
  394. }
  395. }
  396. const plugins = new PluginCollection( editor, [] );
  397. return plugins.load( [ AsynchronousPluginA, AsynchronousPluginB ] )
  398. .then( () => plugins.destroy() )
  399. .then( () => {
  400. expect( destroyedPlugins ).to.contain( 'AsynchronousPluginB.destroy()' );
  401. expect( destroyedPlugins ).to.contain( 'AsynchronousPluginA.destroy()' );
  402. } );
  403. } );
  404. it( 'does not execute Plugin#destroy() for plugins which do not have this method', () => {
  405. class FooPlugin {
  406. constructor( editor ) {
  407. this.editor = editor;
  408. }
  409. }
  410. const plugins = new PluginCollection( editor, [] );
  411. return plugins.load( [ PluginA, FooPlugin ] )
  412. .then( () => plugins.destroy() )
  413. .then( destroyedPlugins => {
  414. expect( destroyedPlugins.length ).to.equal( 1 );
  415. } );
  416. } );
  417. } );
  418. describe( 'iterator', () => {
  419. it( 'exists', () => {
  420. const plugins = new PluginCollection( editor, availablePlugins );
  421. expect( plugins ).to.have.property( Symbol.iterator );
  422. } );
  423. it( 'returns only plugins by constructors', () => {
  424. class SomePlugin1 extends Plugin {}
  425. class SomePlugin2 extends Plugin {}
  426. SomePlugin2.pluginName = 'foo/bar';
  427. availablePlugins.push( SomePlugin1 );
  428. availablePlugins.push( SomePlugin2 );
  429. const plugins = new PluginCollection( editor, availablePlugins );
  430. return plugins.load( [ SomePlugin1, SomePlugin2 ] )
  431. .then( () => {
  432. const pluginConstructors = Array.from( plugins )
  433. .map( entry => entry[ 0 ] );
  434. expect( pluginConstructors ).to.have.members( [ SomePlugin1, SomePlugin2 ] );
  435. } );
  436. } );
  437. } );
  438. } );
  439. function createPlugin( name ) {
  440. const P = class extends Plugin {
  441. constructor( editor ) {
  442. super( editor );
  443. this.pluginName = name;
  444. }
  445. };
  446. P.pluginName = name;
  447. return P;
  448. }
  449. function getPlugins( pluginCollection ) {
  450. return Array.from( pluginCollection )
  451. .map( entry => entry[ 1 ] ); // Get instances.
  452. }
  453. function getPluginsFromSpy( addSpy ) {
  454. return addSpy.args
  455. .map( arg => arg[ 0 ] )
  456. // Entries may be kept twice in the plugins map - once as a pluginName => plugin, once as pluginClass => plugin.
  457. // Return only pluginClass => plugin entries as these will always represent all plugins.
  458. .filter( plugin => typeof plugin == 'function' );
  459. }
  460. function getPluginNames( plugins ) {
  461. return plugins.map( plugin => plugin.pluginName );
  462. }