PluginTest.php 14 KB

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