PluginTest.php 13 KB

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