PluginTest.php 12 KB

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