PluginTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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\Configure;
  16. use Cake\Core\Plugin;
  17. use Cake\TestSuite\TestCase;
  18. /**
  19. * PluginTest class
  20. */
  21. class PluginTest extends TestCase
  22. {
  23. /**
  24. * Reverts the changes done to the environment while testing
  25. *
  26. * @return void
  27. */
  28. public function tearDown()
  29. {
  30. parent::tearDown();
  31. Plugin::unload();
  32. }
  33. /**
  34. * Tests loading a single plugin
  35. *
  36. * @return void
  37. */
  38. public function testLoad()
  39. {
  40. Plugin::unload();
  41. Plugin::load('TestPlugin');
  42. $expected = ['TestPlugin'];
  43. $this->assertEquals($expected, Plugin::loaded());
  44. }
  45. /**
  46. * Tests unloading plugins
  47. *
  48. * @return void
  49. */
  50. public function testUnload()
  51. {
  52. Plugin::load('TestPlugin');
  53. $expected = ['TestPlugin'];
  54. $this->assertEquals($expected, Plugin::loaded());
  55. Plugin::unload('TestPlugin');
  56. $this->assertEquals([], Plugin::loaded());
  57. Plugin::load('TestPlugin');
  58. $expected = ['TestPlugin'];
  59. $this->assertEquals($expected, Plugin::loaded());
  60. Plugin::unload('TestFakePlugin');
  61. $this->assertEquals($expected, Plugin::loaded());
  62. }
  63. /**
  64. * Test load() with the autoload option.
  65. *
  66. * @return void
  67. */
  68. public function testLoadWithAutoload()
  69. {
  70. $this->assertFalse(class_exists('Company\TestPluginFive\Utility\Hello'));
  71. Plugin::load('Company/TestPluginFive', [
  72. 'autoload' => true,
  73. ]);
  74. $this->assertTrue(
  75. class_exists('Company\TestPluginFive\Utility\Hello'),
  76. 'Class should be loaded'
  77. );
  78. }
  79. /**
  80. * Test load() with the autoload option.
  81. *
  82. * @return void
  83. */
  84. public function testLoadWithAutoloadAndBootstrap()
  85. {
  86. Plugin::load(
  87. 'Company/TestPluginFive',
  88. [
  89. 'autoload' => true,
  90. 'bootstrap' => true
  91. ]
  92. );
  93. $this->assertTrue(Configure::read('PluginTest.test_plugin_five.autoload'));
  94. $this->assertEquals('loaded plugin five bootstrap', Configure::read('PluginTest.test_plugin_five.bootstrap'));
  95. $this->assertTrue(
  96. class_exists('Company\TestPluginFive\Utility\Hello'),
  97. 'Class should be loaded'
  98. );
  99. }
  100. /**
  101. * Tests loading a plugin and its bootstrap file
  102. *
  103. * @return void
  104. */
  105. public function testLoadWithBootstrap()
  106. {
  107. Plugin::load('TestPlugin', ['bootstrap' => true]);
  108. $this->assertTrue(Plugin::loaded('TestPlugin'));
  109. $this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
  110. Plugin::load('Company/TestPluginThree', ['bootstrap' => true]);
  111. $this->assertTrue(Plugin::loaded('Company/TestPluginThree'));
  112. $this->assertEquals('loaded plugin three bootstrap', Configure::read('PluginTest.test_plugin_three.bootstrap'));
  113. }
  114. /**
  115. * Tests loading a plugin and its bootstrap file
  116. *
  117. * @return void
  118. */
  119. public function testLoadWithBootstrapDisableBootstrapHook()
  120. {
  121. Plugin::load('TestPlugin', ['bootstrap' => true]);
  122. $this->assertTrue(Plugin::loaded('TestPlugin'));
  123. $this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
  124. $plugin = Plugin::getCollection()->get('TestPlugin');
  125. $this->assertFalse($plugin->isEnabled('bootstrap'), 'Should be disabled as hook has been run.');
  126. }
  127. /**
  128. * Tests loading a plugin with bootstrap file and routes file
  129. *
  130. * @return void
  131. */
  132. public function testLoadSingleWithBootstrapAndRoutes()
  133. {
  134. Plugin::load('TestPlugin', ['bootstrap' => true, 'routes' => true]);
  135. $this->assertTrue(Plugin::loaded('TestPlugin'));
  136. $this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
  137. Plugin::routes();
  138. $this->assertEquals('loaded plugin routes', Configure::read('PluginTest.test_plugin.routes'));
  139. }
  140. /**
  141. * Test load() with path configuration data
  142. *
  143. * @return void
  144. */
  145. public function testLoadSingleWithPathConfig()
  146. {
  147. Configure::write('plugins.TestPlugin', APP);
  148. Plugin::load('TestPlugin');
  149. $this->assertEquals(APP . 'src' . DS, Plugin::classPath('TestPlugin'));
  150. }
  151. /**
  152. * Tests loading multiple plugins at once
  153. *
  154. * @return void
  155. */
  156. public function testLoadMultiple()
  157. {
  158. Plugin::load(['TestPlugin', 'TestPluginTwo']);
  159. $expected = ['TestPlugin', 'TestPluginTwo'];
  160. $this->assertEquals($expected, Plugin::loaded());
  161. }
  162. /**
  163. * Tests loading multiple plugins and their bootstrap files
  164. *
  165. * @return void
  166. */
  167. public function testLoadMultipleWithDefaults()
  168. {
  169. Plugin::load(['TestPlugin', 'TestPluginTwo'], ['bootstrap' => true, 'routes' => false]);
  170. $expected = ['TestPlugin', 'TestPluginTwo'];
  171. $this->assertEquals($expected, Plugin::loaded());
  172. $this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
  173. $this->assertEquals('loaded plugin two bootstrap', Configure::read('PluginTest.test_plugin_two.bootstrap'));
  174. }
  175. /**
  176. * Tests loading multiple plugins with default loading params and some overrides
  177. *
  178. * @return void
  179. */
  180. public function testLoadMultipleWithDefaultsAndOverride()
  181. {
  182. Plugin::load(
  183. ['TestPlugin', 'TestPluginTwo' => ['routes' => false]],
  184. ['bootstrap' => true, 'routes' => true]
  185. );
  186. $expected = ['TestPlugin', 'TestPluginTwo'];
  187. $this->assertEquals($expected, Plugin::loaded());
  188. $this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
  189. $this->assertNull(Configure::read('PluginTest.test_plugin_two.bootstrap'));
  190. }
  191. /**
  192. * Test ignoring missing bootstrap/routes file
  193. *
  194. * @return void
  195. */
  196. public function testIgnoreMissingFiles()
  197. {
  198. Plugin::loadAll([[
  199. 'bootstrap' => true,
  200. 'routes' => true,
  201. 'ignoreMissing' => true
  202. ]]);
  203. $this->assertTrue(Plugin::routes());
  204. }
  205. /**
  206. * Tests that Plugin::load() throws an exception on unknown plugin
  207. *
  208. * @return void
  209. */
  210. public function testLoadNotFound()
  211. {
  212. $this->expectException(\Cake\Core\Exception\MissingPluginException::class);
  213. Plugin::load('MissingPlugin');
  214. }
  215. /**
  216. * Tests that Plugin::path() returns the correct path for the loaded plugins
  217. *
  218. * @return void
  219. */
  220. public function testPath()
  221. {
  222. Plugin::load(['TestPlugin', 'TestPluginTwo', 'Company/TestPluginThree']);
  223. $expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS;
  224. $this->assertPathEquals(Plugin::path('TestPlugin'), $expected);
  225. $expected = TEST_APP . 'Plugin' . DS . 'TestPluginTwo' . DS;
  226. $this->assertPathEquals(Plugin::path('TestPluginTwo'), $expected);
  227. $expected = TEST_APP . 'Plugin' . DS . 'Company' . DS . 'TestPluginThree' . DS;
  228. $this->assertPathEquals(Plugin::path('Company/TestPluginThree'), $expected);
  229. }
  230. /**
  231. * Tests that Plugin::path() throws an exception on unknown plugin
  232. *
  233. * @return void
  234. */
  235. public function testPathNotFound()
  236. {
  237. $this->expectException(\Cake\Core\Exception\MissingPluginException::class);
  238. Plugin::path('TestPlugin');
  239. }
  240. /**
  241. * Tests that Plugin::classPath() returns the correct path for the loaded plugins
  242. *
  243. * @return void
  244. */
  245. public function testClassPath()
  246. {
  247. Plugin::load(['TestPlugin', 'TestPluginTwo', 'Company/TestPluginThree']);
  248. $expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS . 'src' . DS;
  249. $this->assertPathEquals(Plugin::classPath('TestPlugin'), $expected);
  250. $expected = TEST_APP . 'Plugin' . DS . 'TestPluginTwo' . DS . 'src' . DS;
  251. $this->assertPathEquals(Plugin::classPath('TestPluginTwo'), $expected);
  252. $expected = TEST_APP . 'Plugin' . DS . 'Company' . DS . 'TestPluginThree' . DS . 'src' . DS;
  253. $this->assertPathEquals(Plugin::classPath('Company/TestPluginThree'), $expected);
  254. }
  255. /**
  256. * Tests that Plugin::classPath() throws an exception on unknown plugin
  257. *
  258. * @return void
  259. */
  260. public function testClassPathNotFound()
  261. {
  262. $this->expectException(\Cake\Core\Exception\MissingPluginException::class);
  263. Plugin::classPath('TestPlugin');
  264. }
  265. /**
  266. * Tests that Plugin::loadAll() will load all plugins in the configured folder
  267. *
  268. * @return void
  269. */
  270. public function testLoadAll()
  271. {
  272. Plugin::loadAll();
  273. $expected = [
  274. 'Company', 'ParentPlugin', 'PluginJs', 'TestPlugin',
  275. 'TestPluginFour', 'TestPluginTwo', 'TestTheme'
  276. ];
  277. $this->assertEquals($expected, Plugin::loaded());
  278. }
  279. /**
  280. * Test loadAll() with path configuration data
  281. *
  282. * @return void
  283. */
  284. public function testLoadAllWithPathConfig()
  285. {
  286. Configure::write('plugins.FakePlugin', APP);
  287. Plugin::loadAll();
  288. $this->assertContains('FakePlugin', Plugin::loaded());
  289. }
  290. /**
  291. * Test that plugins don't reload using loadAll();
  292. *
  293. * @return void
  294. */
  295. public function testLoadAllWithPluginAlreadyLoaded()
  296. {
  297. Plugin::load('Company/TestPluginThree', ['bootstrap' => false]);
  298. Plugin::loadAll(['bootstrap' => true, 'ignoreMissing' => true]);
  299. $this->assertEmpty(Configure::read('PluginTest.test_plugin_three.bootstrap'));
  300. }
  301. /**
  302. * Tests that Plugin::loadAll() will load all plugins in the configured folder with bootstrap loading
  303. *
  304. * @return void
  305. */
  306. public function testLoadAllWithDefaults()
  307. {
  308. $defaults = ['bootstrap' => true, 'ignoreMissing' => true];
  309. Plugin::loadAll([$defaults]);
  310. $expected = [
  311. 'Company', 'ParentPlugin', 'PluginJs', 'TestPlugin',
  312. 'TestPluginFour', 'TestPluginTwo', 'TestTheme'
  313. ];
  314. $this->assertEquals($expected, Plugin::loaded());
  315. $this->assertEquals('loaded js plugin bootstrap', Configure::read('PluginTest.js_plugin.bootstrap'));
  316. $this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
  317. $this->assertEquals('loaded plugin two bootstrap', Configure::read('PluginTest.test_plugin_two.bootstrap'));
  318. }
  319. /**
  320. * Tests that Plugin::loadAll() will load all plugins in the configured folder wit defaults
  321. * and overrides for a plugin
  322. *
  323. * @return void
  324. */
  325. public function testLoadAllWithDefaultsAndOverride()
  326. {
  327. Plugin::loadAll([
  328. ['bootstrap' => true, 'ignoreMissing' => true],
  329. 'TestPlugin' => ['routes' => true],
  330. 'TestPluginFour' => ['bootstrap' => true, 'classBase' => '']
  331. ]);
  332. Plugin::routes();
  333. $expected = [
  334. 'Company', 'ParentPlugin', 'PluginJs', 'TestPlugin',
  335. 'TestPluginFour', 'TestPluginTwo', 'TestTheme'
  336. ];
  337. $this->assertEquals($expected, Plugin::loaded());
  338. $this->assertEquals('loaded js plugin bootstrap', Configure::read('PluginTest.js_plugin.bootstrap'));
  339. $this->assertEquals('loaded plugin routes', Configure::read('PluginTest.test_plugin.routes'));
  340. $this->assertNull(Configure::read('PluginTest.test_plugin.bootstrap'));
  341. $this->assertEquals('loaded plugin two bootstrap', Configure::read('PluginTest.test_plugin_two.bootstrap'));
  342. $this->assertEquals('loaded plugin four bootstrap', Configure::read('PluginTest.test_plugin_four.bootstrap'));
  343. // TestPluginThree won't get loaded by loadAll() since it's in a sub directory.
  344. $this->assertNull(Configure::read('PluginTest.test_plugin_three.bootstrap'));
  345. }
  346. }