PluginTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  10. * @link https://cakephp.org CakePHP(tm) Project
  11. * @since 2.0.0
  12. * @license https://opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Core;
  15. use Cake\Core\BasePlugin;
  16. use Cake\Core\Configure;
  17. use Cake\Core\Plugin;
  18. use Cake\TestSuite\TestCase;
  19. use TestPlugin\Plugin as TestPlugin;
  20. /**
  21. * PluginTest class
  22. */
  23. class PluginTest extends TestCase
  24. {
  25. /**
  26. * Setup
  27. *
  28. * @return void
  29. */
  30. public function setUp()
  31. {
  32. parent::setUp();
  33. Plugin::unload();
  34. }
  35. /**
  36. * Reverts the changes done to the environment while testing
  37. *
  38. * @return void
  39. */
  40. public function tearDown()
  41. {
  42. parent::tearDown();
  43. Plugin::unload();
  44. }
  45. /**
  46. * Tests loading a single plugin
  47. *
  48. * @return void
  49. */
  50. public function testLoad()
  51. {
  52. $this->deprecated(function () {
  53. Plugin::load('TestPlugin');
  54. $expected = ['TestPlugin'];
  55. $this->assertEquals($expected, Plugin::loaded());
  56. });
  57. }
  58. /**
  59. * Tests loading a plugin with a class
  60. *
  61. * @return void
  62. */
  63. public function testLoadConcreteClass()
  64. {
  65. $this->deprecated(function () {
  66. Plugin::load('TestPlugin');
  67. $instance = Plugin::getCollection()->get('TestPlugin');
  68. $this->assertSame(TestPlugin::class, get_class($instance));
  69. });
  70. }
  71. /**
  72. * Tests loading a plugin without a class
  73. *
  74. * @return void
  75. */
  76. public function testLoadDynamicClass()
  77. {
  78. $this->deprecated(function () {
  79. Plugin::load('TestPluginTwo');
  80. $instance = Plugin::getCollection()->get('TestPluginTwo');
  81. $this->assertSame(BasePlugin::class, get_class($instance));
  82. });
  83. }
  84. /**
  85. * Tests unloading plugins
  86. *
  87. * @return void
  88. */
  89. public function testUnload()
  90. {
  91. $this->loadPlugins(['TestPlugin' => ['bootstrap' => false, 'routes' => false]]);
  92. $expected = ['TestPlugin'];
  93. $this->assertEquals($expected, Plugin::loaded());
  94. Plugin::unload('TestPlugin');
  95. $this->assertEquals([], Plugin::loaded());
  96. $this->loadPlugins(['TestPlugin' => ['bootstrap' => false, 'routes' => false]]);
  97. $expected = ['TestPlugin'];
  98. $this->assertEquals($expected, Plugin::loaded());
  99. Plugin::unload('TestFakePlugin');
  100. $this->assertEquals($expected, Plugin::loaded());
  101. }
  102. /**
  103. * Test load() with the autoload option.
  104. *
  105. * @return void
  106. */
  107. public function testLoadWithAutoload()
  108. {
  109. $this->deprecated(function () {
  110. $this->assertFalse(class_exists('Company\TestPluginFive\Utility\Hello'));
  111. Plugin::load('Company/TestPluginFive', [
  112. 'autoload' => true,
  113. ]);
  114. $this->assertTrue(
  115. class_exists('Company\TestPluginFive\Utility\Hello'),
  116. 'Class should be loaded'
  117. );
  118. });
  119. }
  120. /**
  121. * Test load() with the autoload option.
  122. *
  123. * @return void
  124. */
  125. public function testLoadWithAutoloadAndBootstrap()
  126. {
  127. $this->deprecated(function () {
  128. Plugin::load(
  129. 'Company/TestPluginFive',
  130. [
  131. 'autoload' => true,
  132. 'bootstrap' => true
  133. ]
  134. );
  135. $this->assertTrue(Configure::read('PluginTest.test_plugin_five.autoload'));
  136. $this->assertEquals('loaded plugin five bootstrap', Configure::read('PluginTest.test_plugin_five.bootstrap'));
  137. $this->assertTrue(
  138. class_exists('Company\TestPluginFive\Utility\Hello'),
  139. 'Class should be loaded'
  140. );
  141. });
  142. }
  143. /**
  144. * Tests loading a plugin and its bootstrap file
  145. *
  146. * @return void
  147. */
  148. public function testLoadWithBootstrap()
  149. {
  150. $this->deprecated(function () {
  151. Plugin::load('TestPlugin', ['bootstrap' => true]);
  152. $this->assertTrue(Plugin::loaded('TestPlugin'));
  153. $this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
  154. Plugin::load('Company/TestPluginThree', ['bootstrap' => true]);
  155. $this->assertTrue(Plugin::loaded('Company/TestPluginThree'));
  156. $this->assertEquals('loaded plugin three bootstrap', Configure::read('PluginTest.test_plugin_three.bootstrap'));
  157. });
  158. }
  159. /**
  160. * Tests loading a plugin and its bootstrap file
  161. *
  162. * @return void
  163. */
  164. public function testLoadWithBootstrapDisableBootstrapHook()
  165. {
  166. $this->deprecated(function () {
  167. Plugin::load('TestPlugin', ['bootstrap' => true]);
  168. $this->assertTrue(Plugin::loaded('TestPlugin'));
  169. $this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
  170. $plugin = Plugin::getCollection()->get('TestPlugin');
  171. $this->assertFalse($plugin->isEnabled('bootstrap'), 'Should be disabled as hook has been run.');
  172. });
  173. }
  174. /**
  175. * Tests loading a plugin with bootstrap file and routes file
  176. *
  177. * @deprecated
  178. * @return void
  179. */
  180. public function testLoadSingleWithBootstrapAndRoutes()
  181. {
  182. $this->deprecated(function () {
  183. Plugin::load('TestPlugin', ['bootstrap' => true, 'routes' => true]);
  184. $this->assertTrue(Plugin::loaded('TestPlugin'));
  185. $this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
  186. Plugin::routes();
  187. $this->assertEquals('loaded plugin routes', Configure::read('PluginTest.test_plugin.routes'));
  188. });
  189. }
  190. /**
  191. * Test load() with path configuration data
  192. *
  193. * @return void
  194. */
  195. public function testLoadSingleWithPathConfig()
  196. {
  197. $this->deprecated(function () {
  198. Configure::write('plugins.TestPlugin', APP);
  199. Plugin::load('TestPlugin');
  200. $this->assertEquals(APP . 'src' . DS, Plugin::classPath('TestPlugin'));
  201. });
  202. }
  203. /**
  204. * Tests loading multiple plugins at once
  205. *
  206. * @return void
  207. */
  208. public function testLoadMultiple()
  209. {
  210. $this->deprecated(function () {
  211. Plugin::load(['TestPlugin', 'TestPluginTwo']);
  212. $expected = ['TestPlugin', 'TestPluginTwo'];
  213. $this->assertEquals($expected, Plugin::loaded());
  214. });
  215. }
  216. /**
  217. * Tests loading multiple plugins and their bootstrap files
  218. *
  219. * @return void
  220. */
  221. public function testLoadMultipleWithDefaults()
  222. {
  223. $this->deprecated(function () {
  224. Plugin::load(['TestPlugin', 'TestPluginTwo'], ['bootstrap' => true, 'routes' => false]);
  225. $expected = ['TestPlugin', 'TestPluginTwo'];
  226. $this->assertEquals($expected, Plugin::loaded());
  227. $this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
  228. $this->assertEquals('loaded plugin two bootstrap', Configure::read('PluginTest.test_plugin_two.bootstrap'));
  229. });
  230. }
  231. /**
  232. * Tests loading multiple plugins with default loading params and some overrides
  233. *
  234. * @return void
  235. */
  236. public function testLoadMultipleWithDefaultsAndOverride()
  237. {
  238. $this->deprecated(function () {
  239. Plugin::load(
  240. ['TestPlugin', 'TestPluginTwo' => ['routes' => false]],
  241. ['bootstrap' => true, 'routes' => true]
  242. );
  243. $expected = ['TestPlugin', 'TestPluginTwo'];
  244. $this->assertEquals($expected, Plugin::loaded());
  245. $this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
  246. $this->assertNull(Configure::read('PluginTest.test_plugin_two.bootstrap'));
  247. });
  248. }
  249. /**
  250. * Test ignoring missing bootstrap/routes file
  251. *
  252. * @deprecated
  253. * @return void
  254. */
  255. public function testIgnoreMissingFiles()
  256. {
  257. $this->deprecated(function () {
  258. Plugin::loadAll([[
  259. 'bootstrap' => true,
  260. 'routes' => true,
  261. 'ignoreMissing' => true
  262. ]]);
  263. $this->assertTrue(Plugin::routes());
  264. });
  265. }
  266. /**
  267. * Tests that Plugin::load() throws an exception on unknown plugin
  268. *
  269. * @return void
  270. */
  271. public function testLoadNotFound()
  272. {
  273. $this->deprecated(function () {
  274. $this->expectException(\Cake\Core\Exception\MissingPluginException::class);
  275. Plugin::load('MissingPlugin');
  276. });
  277. }
  278. /**
  279. * Tests that Plugin::path() returns the correct path for the loaded plugins
  280. *
  281. * @return void
  282. */
  283. public function testPath()
  284. {
  285. $this->loadPlugins(['TestPlugin', 'TestPluginTwo', 'Company/TestPluginThree']);
  286. $expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS;
  287. $this->assertPathEquals(Plugin::path('TestPlugin'), $expected);
  288. $expected = TEST_APP . 'Plugin' . DS . 'TestPluginTwo' . DS;
  289. $this->assertPathEquals(Plugin::path('TestPluginTwo'), $expected);
  290. $expected = TEST_APP . 'Plugin' . DS . 'Company' . DS . 'TestPluginThree' . DS;
  291. $this->assertPathEquals(Plugin::path('Company/TestPluginThree'), $expected);
  292. }
  293. /**
  294. * Tests that Plugin::path() throws an exception on unknown plugin
  295. *
  296. * @return void
  297. */
  298. public function testPathNotFound()
  299. {
  300. $this->expectException(\Cake\Core\Exception\MissingPluginException::class);
  301. Plugin::path('TestPlugin');
  302. }
  303. /**
  304. * Tests that Plugin::classPath() returns the correct path for the loaded plugins
  305. *
  306. * @return void
  307. */
  308. public function testClassPath()
  309. {
  310. $this->loadPlugins(['TestPlugin', 'TestPluginTwo', 'Company/TestPluginThree']);
  311. $expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS . 'src' . DS;
  312. $this->assertPathEquals(Plugin::classPath('TestPlugin'), $expected);
  313. $expected = TEST_APP . 'Plugin' . DS . 'TestPluginTwo' . DS . 'src' . DS;
  314. $this->assertPathEquals(Plugin::classPath('TestPluginTwo'), $expected);
  315. $expected = TEST_APP . 'Plugin' . DS . 'Company' . DS . 'TestPluginThree' . DS . 'src' . DS;
  316. $this->assertPathEquals(Plugin::classPath('Company/TestPluginThree'), $expected);
  317. }
  318. /**
  319. * Tests that Plugin::classPath() throws an exception on unknown plugin
  320. *
  321. * @return void
  322. */
  323. public function testClassPathNotFound()
  324. {
  325. $this->expectException(\Cake\Core\Exception\MissingPluginException::class);
  326. Plugin::classPath('TestPlugin');
  327. }
  328. /**
  329. * Tests that Plugin::loadAll() will load all plugins in the configured folder
  330. *
  331. * @return void
  332. */
  333. public function testLoadAll()
  334. {
  335. $this->deprecated(function () {
  336. Plugin::loadAll();
  337. $expected = [
  338. 'Company', 'ParentPlugin', 'PluginJs', 'TestPlugin',
  339. 'TestPluginFour', 'TestPluginTwo', 'TestTheme'
  340. ];
  341. $this->assertEquals($expected, Plugin::loaded());
  342. });
  343. }
  344. /**
  345. * Test loadAll() with path configuration data
  346. *
  347. * @return void
  348. */
  349. public function testLoadAllWithPathConfig()
  350. {
  351. $this->deprecated(function () {
  352. Configure::write('plugins.FakePlugin', APP);
  353. Plugin::loadAll();
  354. $this->assertContains('FakePlugin', Plugin::loaded());
  355. });
  356. }
  357. /**
  358. * Test that plugins don't reload using loadAll();
  359. *
  360. * @return void
  361. */
  362. public function testLoadAllWithPluginAlreadyLoaded()
  363. {
  364. $this->deprecated(function () {
  365. Plugin::load('Company/TestPluginThree', ['bootstrap' => false]);
  366. Plugin::loadAll(['bootstrap' => true, 'ignoreMissing' => true]);
  367. $this->assertEmpty(Configure::read('PluginTest.test_plugin_three.bootstrap'));
  368. });
  369. }
  370. /**
  371. * Tests that Plugin::loadAll() will load all plugins in the configured folder with bootstrap loading
  372. *
  373. * @return void
  374. */
  375. public function testLoadAllWithDefaults()
  376. {
  377. $this->deprecated(function () {
  378. $defaults = ['bootstrap' => true, 'ignoreMissing' => true];
  379. Plugin::loadAll([$defaults]);
  380. $expected = [
  381. 'Company', 'ParentPlugin', 'PluginJs', 'TestPlugin',
  382. 'TestPluginFour', 'TestPluginTwo', 'TestTheme'
  383. ];
  384. $this->assertEquals($expected, Plugin::loaded());
  385. $this->assertEquals('loaded js plugin bootstrap', Configure::read('PluginTest.js_plugin.bootstrap'));
  386. $this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
  387. $this->assertEquals('loaded plugin two bootstrap', Configure::read('PluginTest.test_plugin_two.bootstrap'));
  388. });
  389. }
  390. /**
  391. * Tests that Plugin::loadAll() will load all plugins in the configured folder wit defaults
  392. * and overrides for a plugin
  393. *
  394. * @deprecated
  395. * @return void
  396. */
  397. public function testLoadAllWithDefaultsAndOverride()
  398. {
  399. $this->deprecated(function () {
  400. Plugin::loadAll([
  401. ['bootstrap' => true, 'ignoreMissing' => true],
  402. 'TestPlugin' => ['routes' => true],
  403. 'TestPluginFour' => ['bootstrap' => true, 'classBase' => '']
  404. ]);
  405. Plugin::routes();
  406. $expected = [
  407. 'Company', 'ParentPlugin', 'PluginJs', 'TestPlugin',
  408. 'TestPluginFour', 'TestPluginTwo', 'TestTheme'
  409. ];
  410. $this->assertEquals($expected, Plugin::loaded());
  411. $this->assertEquals('loaded js plugin bootstrap', Configure::read('PluginTest.js_plugin.bootstrap'));
  412. $this->assertEquals('loaded plugin routes', Configure::read('PluginTest.test_plugin.routes'));
  413. $this->assertNull(Configure::read('PluginTest.test_plugin.bootstrap'));
  414. $this->assertEquals('loaded plugin two bootstrap', Configure::read('PluginTest.test_plugin_two.bootstrap'));
  415. $this->assertEquals('loaded plugin four bootstrap', Configure::read('PluginTest.test_plugin_four.bootstrap'));
  416. // TestPluginThree won't get loaded by loadAll() since it's in a sub directory.
  417. $this->assertNull(Configure::read('PluginTest.test_plugin_three.bootstrap'));
  418. });
  419. }
  420. }