PluginTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. * @return void
  164. */
  165. public function testLoadSingleWithBootstrapAndRoutes()
  166. {
  167. Plugin::load('TestPlugin', ['bootstrap' => true, 'routes' => true]);
  168. $this->assertTrue(Plugin::loaded('TestPlugin'));
  169. $this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
  170. Plugin::routes();
  171. $this->assertEquals('loaded plugin routes', Configure::read('PluginTest.test_plugin.routes'));
  172. }
  173. /**
  174. * Test load() with path configuration data
  175. *
  176. * @return void
  177. */
  178. public function testLoadSingleWithPathConfig()
  179. {
  180. Configure::write('plugins.TestPlugin', APP);
  181. Plugin::load('TestPlugin');
  182. $this->assertEquals(APP . 'src' . DS, Plugin::classPath('TestPlugin'));
  183. }
  184. /**
  185. * Tests loading multiple plugins at once
  186. *
  187. * @return void
  188. */
  189. public function testLoadMultiple()
  190. {
  191. Plugin::load(['TestPlugin', 'TestPluginTwo']);
  192. $expected = ['TestPlugin', 'TestPluginTwo'];
  193. $this->assertEquals($expected, Plugin::loaded());
  194. }
  195. /**
  196. * Tests loading multiple plugins and their bootstrap files
  197. *
  198. * @return void
  199. */
  200. public function testLoadMultipleWithDefaults()
  201. {
  202. Plugin::load(['TestPlugin', 'TestPluginTwo'], ['bootstrap' => true, 'routes' => false]);
  203. $expected = ['TestPlugin', 'TestPluginTwo'];
  204. $this->assertEquals($expected, Plugin::loaded());
  205. $this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
  206. $this->assertEquals('loaded plugin two bootstrap', Configure::read('PluginTest.test_plugin_two.bootstrap'));
  207. }
  208. /**
  209. * Tests loading multiple plugins with default loading params and some overrides
  210. *
  211. * @return void
  212. */
  213. public function testLoadMultipleWithDefaultsAndOverride()
  214. {
  215. Plugin::load(
  216. ['TestPlugin', 'TestPluginTwo' => ['routes' => false]],
  217. ['bootstrap' => true, 'routes' => true]
  218. );
  219. $expected = ['TestPlugin', 'TestPluginTwo'];
  220. $this->assertEquals($expected, Plugin::loaded());
  221. $this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
  222. $this->assertNull(Configure::read('PluginTest.test_plugin_two.bootstrap'));
  223. }
  224. /**
  225. * Test ignoring missing bootstrap/routes file
  226. *
  227. * @return void
  228. */
  229. public function testIgnoreMissingFiles()
  230. {
  231. Plugin::loadAll([[
  232. 'bootstrap' => true,
  233. 'routes' => true,
  234. 'ignoreMissing' => true
  235. ]]);
  236. $this->assertTrue(Plugin::routes());
  237. }
  238. /**
  239. * Tests that Plugin::load() throws an exception on unknown plugin
  240. *
  241. * @return void
  242. */
  243. public function testLoadNotFound()
  244. {
  245. $this->expectException(\Cake\Core\Exception\MissingPluginException::class);
  246. Plugin::load('MissingPlugin');
  247. }
  248. /**
  249. * Tests that Plugin::path() returns the correct path for the loaded plugins
  250. *
  251. * @return void
  252. */
  253. public function testPath()
  254. {
  255. Plugin::load(['TestPlugin', 'TestPluginTwo', 'Company/TestPluginThree']);
  256. $expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS;
  257. $this->assertPathEquals(Plugin::path('TestPlugin'), $expected);
  258. $expected = TEST_APP . 'Plugin' . DS . 'TestPluginTwo' . DS;
  259. $this->assertPathEquals(Plugin::path('TestPluginTwo'), $expected);
  260. $expected = TEST_APP . 'Plugin' . DS . 'Company' . DS . 'TestPluginThree' . DS;
  261. $this->assertPathEquals(Plugin::path('Company/TestPluginThree'), $expected);
  262. }
  263. /**
  264. * Tests that Plugin::path() throws an exception on unknown plugin
  265. *
  266. * @return void
  267. */
  268. public function testPathNotFound()
  269. {
  270. $this->expectException(\Cake\Core\Exception\MissingPluginException::class);
  271. Plugin::path('TestPlugin');
  272. }
  273. /**
  274. * Tests that Plugin::classPath() returns the correct path for the loaded plugins
  275. *
  276. * @return void
  277. */
  278. public function testClassPath()
  279. {
  280. Plugin::load(['TestPlugin', 'TestPluginTwo', 'Company/TestPluginThree']);
  281. $expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS . 'src' . DS;
  282. $this->assertPathEquals(Plugin::classPath('TestPlugin'), $expected);
  283. $expected = TEST_APP . 'Plugin' . DS . 'TestPluginTwo' . DS . 'src' . DS;
  284. $this->assertPathEquals(Plugin::classPath('TestPluginTwo'), $expected);
  285. $expected = TEST_APP . 'Plugin' . DS . 'Company' . DS . 'TestPluginThree' . DS . 'src' . DS;
  286. $this->assertPathEquals(Plugin::classPath('Company/TestPluginThree'), $expected);
  287. }
  288. /**
  289. * Tests that Plugin::classPath() throws an exception on unknown plugin
  290. *
  291. * @return void
  292. */
  293. public function testClassPathNotFound()
  294. {
  295. $this->expectException(\Cake\Core\Exception\MissingPluginException::class);
  296. Plugin::classPath('TestPlugin');
  297. }
  298. /**
  299. * Tests that Plugin::loadAll() will load all plugins in the configured folder
  300. *
  301. * @return void
  302. */
  303. public function testLoadAll()
  304. {
  305. Plugin::loadAll();
  306. $expected = [
  307. 'Company', 'ParentPlugin', 'PluginJs', 'TestPlugin',
  308. 'TestPluginFour', 'TestPluginTwo', 'TestTheme'
  309. ];
  310. $this->assertEquals($expected, Plugin::loaded());
  311. }
  312. /**
  313. * Test loadAll() with path configuration data
  314. *
  315. * @return void
  316. */
  317. public function testLoadAllWithPathConfig()
  318. {
  319. Configure::write('plugins.FakePlugin', APP);
  320. Plugin::loadAll();
  321. $this->assertContains('FakePlugin', Plugin::loaded());
  322. }
  323. /**
  324. * Test that plugins don't reload using loadAll();
  325. *
  326. * @return void
  327. */
  328. public function testLoadAllWithPluginAlreadyLoaded()
  329. {
  330. Plugin::load('Company/TestPluginThree', ['bootstrap' => false]);
  331. Plugin::loadAll(['bootstrap' => true, 'ignoreMissing' => true]);
  332. $this->assertEmpty(Configure::read('PluginTest.test_plugin_three.bootstrap'));
  333. }
  334. /**
  335. * Tests that Plugin::loadAll() will load all plugins in the configured folder with bootstrap loading
  336. *
  337. * @return void
  338. */
  339. public function testLoadAllWithDefaults()
  340. {
  341. $defaults = ['bootstrap' => true, 'ignoreMissing' => true];
  342. Plugin::loadAll([$defaults]);
  343. $expected = [
  344. 'Company', 'ParentPlugin', 'PluginJs', 'TestPlugin',
  345. 'TestPluginFour', 'TestPluginTwo', 'TestTheme'
  346. ];
  347. $this->assertEquals($expected, Plugin::loaded());
  348. $this->assertEquals('loaded js plugin bootstrap', Configure::read('PluginTest.js_plugin.bootstrap'));
  349. $this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
  350. $this->assertEquals('loaded plugin two bootstrap', Configure::read('PluginTest.test_plugin_two.bootstrap'));
  351. }
  352. /**
  353. * Tests that Plugin::loadAll() will load all plugins in the configured folder wit defaults
  354. * and overrides for a plugin
  355. *
  356. * @return void
  357. */
  358. public function testLoadAllWithDefaultsAndOverride()
  359. {
  360. Plugin::loadAll([
  361. ['bootstrap' => true, 'ignoreMissing' => true],
  362. 'TestPlugin' => ['routes' => true],
  363. 'TestPluginFour' => ['bootstrap' => true, 'classBase' => '']
  364. ]);
  365. Plugin::routes();
  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 routes', Configure::read('PluginTest.test_plugin.routes'));
  373. $this->assertNull(Configure::read('PluginTest.test_plugin.bootstrap'));
  374. $this->assertEquals('loaded plugin two bootstrap', Configure::read('PluginTest.test_plugin_two.bootstrap'));
  375. $this->assertEquals('loaded plugin four bootstrap', Configure::read('PluginTest.test_plugin_four.bootstrap'));
  376. // TestPluginThree won't get loaded by loadAll() since it's in a sub directory.
  377. $this->assertNull(Configure::read('PluginTest.test_plugin_three.bootstrap'));
  378. }
  379. }