PluginTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 testLoadSingle()
  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 testLoadSingleWithAutoload()
  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 testLoadSingleWithAutoloadAndBootstrap()
  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 testLoadSingleWithBootstrap()
  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 with bootstrap file and routes file
  116. *
  117. * @return void
  118. */
  119. public function testLoadSingleWithBootstrapAndRoutes()
  120. {
  121. Plugin::load('TestPlugin', ['bootstrap' => true, 'routes' => true]);
  122. $this->assertTrue(Plugin::loaded('TestPlugin'));
  123. $this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
  124. Plugin::routes();
  125. $this->assertEquals('loaded plugin routes', Configure::read('PluginTest.test_plugin.routes'));
  126. }
  127. /**
  128. * Test load() with path configuration data
  129. *
  130. * @return void
  131. */
  132. public function testLoadSingleWithPathConfig()
  133. {
  134. Configure::write('plugins.TestPlugin', APP);
  135. Plugin::load('TestPlugin');
  136. $this->assertEquals(APP . 'src' . DS, Plugin::classPath('TestPlugin'));
  137. }
  138. /**
  139. * Tests loading multiple plugins at once
  140. *
  141. * @return void
  142. */
  143. public function testLoadMultiple()
  144. {
  145. Plugin::load(['TestPlugin', 'TestPluginTwo']);
  146. $expected = ['TestPlugin', 'TestPluginTwo'];
  147. $this->assertEquals($expected, Plugin::loaded());
  148. }
  149. /**
  150. * Tests loading multiple plugins and their bootstrap files
  151. *
  152. * @return void
  153. */
  154. public function testLoadMultipleWithDefaults()
  155. {
  156. Plugin::load(['TestPlugin', 'TestPluginTwo'], ['bootstrap' => true, 'routes' => false]);
  157. $expected = ['TestPlugin', 'TestPluginTwo'];
  158. $this->assertEquals($expected, Plugin::loaded());
  159. $this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
  160. $this->assertEquals('loaded plugin two bootstrap', Configure::read('PluginTest.test_plugin_two.bootstrap'));
  161. }
  162. /**
  163. * Tests loading multiple plugins with default loading params and some overrides
  164. *
  165. * @return void
  166. */
  167. public function testLoadMultipleWithDefaultsAndOverride()
  168. {
  169. Plugin::load(
  170. ['TestPlugin', 'TestPluginTwo' => ['routes' => false]],
  171. ['bootstrap' => true, 'routes' => true]
  172. );
  173. $expected = ['TestPlugin', 'TestPluginTwo'];
  174. $this->assertEquals($expected, Plugin::loaded());
  175. $this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
  176. $this->assertNull(Configure::read('PluginTest.test_plugin_two.bootstrap'));
  177. }
  178. /**
  179. * Test ignoring missing bootstrap/routes file
  180. *
  181. * @return void
  182. */
  183. public function testIgnoreMissingFiles()
  184. {
  185. Plugin::loadAll([[
  186. 'bootstrap' => true,
  187. 'routes' => true,
  188. 'ignoreMissing' => true
  189. ]]);
  190. $this->assertTrue(Plugin::routes());
  191. }
  192. /**
  193. * Tests that Plugin::load() throws an exception on unknown plugin
  194. *
  195. * @return void
  196. */
  197. public function testLoadNotFound()
  198. {
  199. $this->expectException(\Cake\Core\Exception\MissingPluginException::class);
  200. Plugin::load('MissingPlugin');
  201. }
  202. /**
  203. * Tests that Plugin::path() returns the correct path for the loaded plugins
  204. *
  205. * @return void
  206. */
  207. public function testPath()
  208. {
  209. Plugin::load(['TestPlugin', 'TestPluginTwo', 'Company/TestPluginThree']);
  210. $expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS;
  211. $this->assertPathEquals(Plugin::path('TestPlugin'), $expected);
  212. $expected = TEST_APP . 'Plugin' . DS . 'TestPluginTwo' . DS;
  213. $this->assertPathEquals(Plugin::path('TestPluginTwo'), $expected);
  214. $expected = TEST_APP . 'Plugin' . DS . 'Company' . DS . 'TestPluginThree' . DS;
  215. $this->assertPathEquals(Plugin::path('Company/TestPluginThree'), $expected);
  216. }
  217. /**
  218. * Tests that Plugin::path() throws an exception on unknown plugin
  219. *
  220. * @return void
  221. */
  222. public function testPathNotFound()
  223. {
  224. $this->expectException(\Cake\Core\Exception\MissingPluginException::class);
  225. Plugin::path('TestPlugin');
  226. }
  227. /**
  228. * Tests that Plugin::classPath() returns the correct path for the loaded plugins
  229. *
  230. * @return void
  231. */
  232. public function testClassPath()
  233. {
  234. Plugin::load(['TestPlugin', 'TestPluginTwo', 'Company/TestPluginThree']);
  235. $expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS . 'src' . DS;
  236. $this->assertPathEquals(Plugin::classPath('TestPlugin'), $expected);
  237. $expected = TEST_APP . 'Plugin' . DS . 'TestPluginTwo' . DS . 'src' . DS;
  238. $this->assertPathEquals(Plugin::classPath('TestPluginTwo'), $expected);
  239. $expected = TEST_APP . 'Plugin' . DS . 'Company' . DS . 'TestPluginThree' . DS . 'src' . DS;
  240. $this->assertPathEquals(Plugin::classPath('Company/TestPluginThree'), $expected);
  241. }
  242. /**
  243. * Tests that Plugin::classPath() throws an exception on unknown plugin
  244. *
  245. * @return void
  246. */
  247. public function testClassPathNotFound()
  248. {
  249. $this->expectException(\Cake\Core\Exception\MissingPluginException::class);
  250. Plugin::classPath('TestPlugin');
  251. }
  252. /**
  253. * Tests that Plugin::loadAll() will load all plugins in the configured folder
  254. *
  255. * @return void
  256. */
  257. public function testLoadAll()
  258. {
  259. Plugin::loadAll();
  260. $expected = ['Company', 'PluginJs', 'TestPlugin', 'TestPluginFour', 'TestPluginTwo', 'TestTheme'];
  261. $this->assertEquals($expected, Plugin::loaded());
  262. }
  263. /**
  264. * Test loadAll() with path configuration data
  265. *
  266. * @return void
  267. */
  268. public function testLoadAllWithPathConfig()
  269. {
  270. Configure::write('plugins.FakePlugin', APP);
  271. Plugin::loadAll();
  272. $this->assertContains('FakePlugin', Plugin::loaded());
  273. }
  274. /**
  275. * Test that plugins don't reload using loadAll();
  276. *
  277. * @return void
  278. */
  279. public function testLoadAllWithPluginAlreadyLoaded()
  280. {
  281. Plugin::load('Company/TestPluginThree', ['bootstrap' => false]);
  282. Plugin::loadAll(['bootstrap' => true, 'ignoreMissing' => true]);
  283. $this->assertEmpty(Configure::read('PluginTest.test_plugin_three.bootstrap'));
  284. }
  285. /**
  286. * Tests that Plugin::loadAll() will load all plugins in the configured folder with bootstrap loading
  287. *
  288. * @return void
  289. */
  290. public function testLoadAllWithDefaults()
  291. {
  292. $defaults = ['bootstrap' => true, 'ignoreMissing' => true];
  293. Plugin::loadAll([$defaults]);
  294. $expected = ['Company', 'PluginJs', 'TestPlugin', 'TestPluginFour', 'TestPluginTwo', 'TestTheme'];
  295. $this->assertEquals($expected, Plugin::loaded());
  296. $this->assertEquals('loaded js plugin bootstrap', Configure::read('PluginTest.js_plugin.bootstrap'));
  297. $this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
  298. $this->assertEquals('loaded plugin two bootstrap', Configure::read('PluginTest.test_plugin_two.bootstrap'));
  299. }
  300. /**
  301. * Tests that Plugin::loadAll() will load all plugins in the configured folder wit defaults
  302. * and overrides for a plugin
  303. *
  304. * @return void
  305. */
  306. public function testLoadAllWithDefaultsAndOverride()
  307. {
  308. Plugin::loadAll([
  309. ['bootstrap' => true, 'ignoreMissing' => true],
  310. 'TestPlugin' => ['routes' => true],
  311. 'TestPluginFour' => ['bootstrap' => true, 'classBase' => '']
  312. ]);
  313. Plugin::routes();
  314. $expected = ['Company', 'PluginJs', 'TestPlugin', 'TestPluginFour', 'TestPluginTwo', 'TestTheme'];
  315. $this->assertEquals($expected, Plugin::loaded());
  316. $this->assertEquals('loaded js plugin bootstrap', Configure::read('PluginTest.js_plugin.bootstrap'));
  317. $this->assertEquals('loaded plugin routes', Configure::read('PluginTest.test_plugin.routes'));
  318. $this->assertNull(Configure::read('PluginTest.test_plugin.bootstrap'));
  319. $this->assertEquals('loaded plugin two bootstrap', Configure::read('PluginTest.test_plugin_two.bootstrap'));
  320. $this->assertEquals('loaded plugin four bootstrap', Configure::read('PluginTest.test_plugin_four.bootstrap'));
  321. // TestPluginThree won't get loaded by loadAll() since it's in a sub directory.
  322. $this->assertNull(Configure::read('PluginTest.test_plugin_three.bootstrap'));
  323. }
  324. }