PluginTest.php 12 KB

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