PluginTest.php 13 KB

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