PluginTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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. $this->clearPlugins();
  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. $this->clearPlugins();
  44. }
  45. /**
  46. * Tests loading a single plugin
  47. *
  48. * @return void
  49. */
  50. public function testLoad()
  51. {
  52. $this->deprecated(function () {
  53. Plugin::load('TestPlugin');
  54. $expected = ['TestPlugin'];
  55. $this->assertEquals($expected, Plugin::loaded());
  56. });
  57. }
  58. /**
  59. * Tests loading a plugin with a class
  60. *
  61. * @return void
  62. */
  63. public function testLoadConcreteClass()
  64. {
  65. $this->deprecated(function () {
  66. Plugin::load('TestPlugin');
  67. $instance = Plugin::getCollection()->get('TestPlugin');
  68. $this->assertSame(TestPlugin::class, get_class($instance));
  69. });
  70. }
  71. /**
  72. * Tests loading a plugin without a class
  73. *
  74. * @return void
  75. */
  76. public function testLoadDynamicClass()
  77. {
  78. $this->deprecated(function () {
  79. Plugin::load('TestPluginTwo');
  80. $instance = Plugin::getCollection()->get('TestPluginTwo');
  81. $this->assertSame(BasePlugin::class, get_class($instance));
  82. });
  83. }
  84. /**
  85. * Tests unloading plugins
  86. *
  87. * @deprecated
  88. * @return void
  89. */
  90. public function testUnload()
  91. {
  92. $this->deprecated(function () {
  93. $this->loadPlugins(['TestPlugin' => ['bootstrap' => false, 'routes' => false]]);
  94. $expected = ['TestPlugin'];
  95. $this->assertEquals($expected, Plugin::loaded());
  96. $this->assertTrue(Plugin::isLoaded('TestPlugin'));
  97. Plugin::unload('TestPlugin');
  98. $this->assertEquals([], Plugin::loaded());
  99. $this->assertFalse(Plugin::isLoaded('TestPlugin'));
  100. $this->loadPlugins(['TestPlugin' => ['bootstrap' => false, 'routes' => false]]);
  101. $expected = ['TestPlugin'];
  102. $this->assertEquals($expected, Plugin::loaded());
  103. Plugin::unload('TestFakePlugin');
  104. $this->assertEquals($expected, Plugin::loaded());
  105. $this->assertFalse(Plugin::isLoaded('TestFakePlugin'));
  106. });
  107. }
  108. /**
  109. * Test load() with the autoload option.
  110. *
  111. * @return void
  112. */
  113. public function testLoadWithAutoload()
  114. {
  115. $this->deprecated(function () {
  116. $this->assertFalse(class_exists('Company\TestPluginFive\Utility\Hello'));
  117. Plugin::load('Company/TestPluginFive', [
  118. 'autoload' => true,
  119. ]);
  120. $this->assertTrue(
  121. class_exists('Company\TestPluginFive\Utility\Hello'),
  122. 'Class should be loaded'
  123. );
  124. });
  125. }
  126. /**
  127. * Test load() with the autoload option.
  128. *
  129. * @return void
  130. */
  131. public function testLoadWithAutoloadAndBootstrap()
  132. {
  133. $this->deprecated(function () {
  134. Plugin::load(
  135. 'Company/TestPluginFive',
  136. [
  137. 'autoload' => true,
  138. 'bootstrap' => true,
  139. ]
  140. );
  141. $this->assertTrue(Configure::read('PluginTest.test_plugin_five.autoload'));
  142. $this->assertEquals('loaded plugin five bootstrap', Configure::read('PluginTest.test_plugin_five.bootstrap'));
  143. $this->assertTrue(
  144. class_exists('Company\TestPluginFive\Utility\Hello'),
  145. 'Class should be loaded'
  146. );
  147. });
  148. }
  149. /**
  150. * Tests deprecated usage of loaded()
  151. *
  152. * @deprecated
  153. * @return void
  154. */
  155. public function testIsLoaded()
  156. {
  157. $this->deprecated(function () {
  158. Plugin::load('TestPlugin');
  159. $this->assertTrue(Plugin::loaded('TestPlugin'));
  160. $this->assertFalse(Plugin::loaded('Unknown'));
  161. });
  162. }
  163. /**
  164. * Tests loading a plugin and its bootstrap file
  165. *
  166. * @deprecated
  167. * @return void
  168. */
  169. public function testLoadWithBootstrap()
  170. {
  171. $this->deprecated(function () {
  172. Plugin::load('TestPlugin', ['bootstrap' => true]);
  173. $this->assertTrue(Plugin::isLoaded('TestPlugin'));
  174. $this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
  175. Plugin::load('Company/TestPluginThree', ['bootstrap' => true]);
  176. $this->assertTrue(Plugin::isLoaded('Company/TestPluginThree'));
  177. $this->assertEquals('loaded plugin three bootstrap', Configure::read('PluginTest.test_plugin_three.bootstrap'));
  178. });
  179. }
  180. /**
  181. * Tests loading a plugin and its bootstrap file
  182. *
  183. * @deprecated
  184. * @return void
  185. */
  186. public function testLoadWithBootstrapDisableBootstrapHook()
  187. {
  188. $this->deprecated(function () {
  189. Plugin::load('TestPlugin', ['bootstrap' => true]);
  190. $this->assertTrue(Plugin::isLoaded('TestPlugin'));
  191. $this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
  192. $plugin = Plugin::getCollection()->get('TestPlugin');
  193. $this->assertFalse($plugin->isEnabled('bootstrap'), 'Should be disabled as hook has been run.');
  194. });
  195. }
  196. /**
  197. * Tests loading a plugin with bootstrap file and routes file
  198. *
  199. * @deprecated
  200. * @return void
  201. */
  202. public function testLoadSingleWithBootstrapAndRoutes()
  203. {
  204. $this->deprecated(function () {
  205. Plugin::load('TestPlugin', ['bootstrap' => true, 'routes' => true]);
  206. $this->assertTrue(Plugin::loaded('TestPlugin'));
  207. $this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
  208. Plugin::routes();
  209. $this->assertEquals('loaded plugin routes', Configure::read('PluginTest.test_plugin.routes'));
  210. });
  211. }
  212. /**
  213. * Test load() with path configuration data
  214. *
  215. * @return void
  216. */
  217. public function testLoadSingleWithPathConfig()
  218. {
  219. $this->deprecated(function () {
  220. Configure::write('plugins.TestPlugin', APP);
  221. Plugin::load('TestPlugin');
  222. $this->assertEquals(APP . 'src' . DS, Plugin::classPath('TestPlugin'));
  223. });
  224. }
  225. /**
  226. * Tests loading multiple plugins at once
  227. *
  228. * @return void
  229. */
  230. public function testLoadMultiple()
  231. {
  232. $this->deprecated(function () {
  233. Plugin::load(['TestPlugin', 'TestPluginTwo']);
  234. $expected = ['TestPlugin', 'TestPluginTwo'];
  235. $this->assertEquals($expected, Plugin::loaded());
  236. });
  237. }
  238. /**
  239. * Tests loading multiple plugins and their bootstrap files
  240. *
  241. * @return void
  242. */
  243. public function testLoadMultipleWithDefaults()
  244. {
  245. $this->deprecated(function () {
  246. Plugin::load(['TestPlugin', 'TestPluginTwo'], ['bootstrap' => true, 'routes' => false]);
  247. $expected = ['TestPlugin', 'TestPluginTwo'];
  248. $this->assertEquals($expected, Plugin::loaded());
  249. $this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
  250. $this->assertEquals('loaded plugin two bootstrap', Configure::read('PluginTest.test_plugin_two.bootstrap'));
  251. });
  252. }
  253. /**
  254. * Tests loading multiple plugins with default loading params and some overrides
  255. *
  256. * @return void
  257. */
  258. public function testLoadMultipleWithDefaultsAndOverride()
  259. {
  260. $this->deprecated(function () {
  261. Plugin::load(
  262. ['TestPlugin', 'TestPluginTwo' => ['routes' => false]],
  263. ['bootstrap' => true, 'routes' => true]
  264. );
  265. $expected = ['TestPlugin', 'TestPluginTwo'];
  266. $this->assertEquals($expected, Plugin::loaded());
  267. $this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
  268. $this->assertNull(Configure::read('PluginTest.test_plugin_two.bootstrap'));
  269. });
  270. }
  271. /**
  272. * Test ignoring missing bootstrap/routes file
  273. *
  274. * @deprecated
  275. * @return void
  276. */
  277. public function testIgnoreMissingFiles()
  278. {
  279. $this->deprecated(function () {
  280. Plugin::loadAll([[
  281. 'bootstrap' => true,
  282. 'routes' => true,
  283. 'ignoreMissing' => true,
  284. ]]);
  285. $this->assertTrue(Plugin::routes());
  286. });
  287. }
  288. /**
  289. * Tests that Plugin::load() throws an exception on unknown plugin
  290. *
  291. * @return void
  292. */
  293. public function testLoadNotFound()
  294. {
  295. $this->deprecated(function () {
  296. $this->expectException(\Cake\Core\Exception\MissingPluginException::class);
  297. Plugin::load('MissingPlugin');
  298. });
  299. }
  300. /**
  301. * Tests that Plugin::path() returns the correct path for the loaded plugins
  302. *
  303. * @return void
  304. */
  305. public function testPath()
  306. {
  307. $this->loadPlugins(['TestPlugin', 'TestPluginTwo', 'Company/TestPluginThree']);
  308. $expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS;
  309. $this->assertPathEquals(Plugin::path('TestPlugin'), $expected);
  310. $expected = TEST_APP . 'Plugin' . DS . 'TestPluginTwo' . DS;
  311. $this->assertPathEquals(Plugin::path('TestPluginTwo'), $expected);
  312. $expected = TEST_APP . 'Plugin' . DS . 'Company' . DS . 'TestPluginThree' . DS;
  313. $this->assertPathEquals(Plugin::path('Company/TestPluginThree'), $expected);
  314. }
  315. /**
  316. * Tests that Plugin::path() throws an exception on unknown plugin
  317. *
  318. * @return void
  319. */
  320. public function testPathNotFound()
  321. {
  322. $this->expectException(\Cake\Core\Exception\MissingPluginException::class);
  323. Plugin::path('TestPlugin');
  324. }
  325. /**
  326. * Tests that Plugin::classPath() returns the correct path for the loaded plugins
  327. *
  328. * @return void
  329. */
  330. public function testClassPath()
  331. {
  332. $this->loadPlugins(['TestPlugin', 'TestPluginTwo', 'Company/TestPluginThree']);
  333. $expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS . 'src' . DS;
  334. $this->assertPathEquals(Plugin::classPath('TestPlugin'), $expected);
  335. $expected = TEST_APP . 'Plugin' . DS . 'TestPluginTwo' . DS . 'src' . DS;
  336. $this->assertPathEquals(Plugin::classPath('TestPluginTwo'), $expected);
  337. $expected = TEST_APP . 'Plugin' . DS . 'Company' . DS . 'TestPluginThree' . DS . 'src' . DS;
  338. $this->assertPathEquals(Plugin::classPath('Company/TestPluginThree'), $expected);
  339. }
  340. /**
  341. * Tests that Plugin::classPath() throws an exception on unknown plugin
  342. *
  343. * @return void
  344. */
  345. public function testClassPathNotFound()
  346. {
  347. $this->expectException(\Cake\Core\Exception\MissingPluginException::class);
  348. Plugin::classPath('TestPlugin');
  349. }
  350. /**
  351. * Tests that Plugin::loadAll() will load all plugins in the configured folder
  352. *
  353. * @return void
  354. */
  355. public function testLoadAll()
  356. {
  357. $this->deprecated(function () {
  358. Plugin::loadAll();
  359. $expected = [
  360. 'Company', 'ParentPlugin', 'PluginJs', 'TestPlugin',
  361. 'TestPluginFour', 'TestPluginTwo', 'TestTheme',
  362. ];
  363. $this->assertEquals($expected, Plugin::loaded());
  364. });
  365. }
  366. /**
  367. * Test loadAll() with path configuration data
  368. *
  369. * @return void
  370. */
  371. public function testLoadAllWithPathConfig()
  372. {
  373. $this->deprecated(function () {
  374. Configure::write('plugins.FakePlugin', APP);
  375. Plugin::loadAll();
  376. $this->assertContains('FakePlugin', Plugin::loaded());
  377. });
  378. }
  379. /**
  380. * Test that plugins don't reload using loadAll();
  381. *
  382. * @return void
  383. */
  384. public function testLoadAllWithPluginAlreadyLoaded()
  385. {
  386. $this->deprecated(function () {
  387. Plugin::load('Company/TestPluginThree', ['bootstrap' => false]);
  388. Plugin::loadAll(['bootstrap' => true, 'ignoreMissing' => true]);
  389. $this->assertEmpty(Configure::read('PluginTest.test_plugin_three.bootstrap'));
  390. });
  391. }
  392. /**
  393. * Tests that Plugin::loadAll() will load all plugins in the configured folder with bootstrap loading
  394. *
  395. * @return void
  396. */
  397. public function testLoadAllWithDefaults()
  398. {
  399. $this->deprecated(function () {
  400. $defaults = ['bootstrap' => true, 'ignoreMissing' => true];
  401. Plugin::loadAll([$defaults]);
  402. $expected = [
  403. 'Company', 'ParentPlugin', 'PluginJs', 'TestPlugin',
  404. 'TestPluginFour', 'TestPluginTwo', 'TestTheme',
  405. ];
  406. $this->assertEquals($expected, Plugin::loaded());
  407. $this->assertEquals('loaded js plugin bootstrap', Configure::read('PluginTest.js_plugin.bootstrap'));
  408. $this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
  409. $this->assertEquals('loaded plugin two bootstrap', Configure::read('PluginTest.test_plugin_two.bootstrap'));
  410. });
  411. }
  412. /**
  413. * Tests that Plugin::loadAll() will load all plugins in the configured folder wit defaults
  414. * and overrides for a plugin
  415. *
  416. * @deprecated
  417. * @return void
  418. */
  419. public function testLoadAllWithDefaultsAndOverride()
  420. {
  421. $this->deprecated(function () {
  422. Plugin::loadAll([
  423. ['bootstrap' => true, 'ignoreMissing' => true],
  424. 'TestPlugin' => ['routes' => true],
  425. 'TestPluginFour' => ['bootstrap' => true, 'classBase' => ''],
  426. ]);
  427. Plugin::routes();
  428. $expected = [
  429. 'Company', 'ParentPlugin', 'PluginJs', 'TestPlugin',
  430. 'TestPluginFour', 'TestPluginTwo', 'TestTheme',
  431. ];
  432. $this->assertEquals($expected, Plugin::loaded());
  433. $this->assertEquals('loaded js plugin bootstrap', Configure::read('PluginTest.js_plugin.bootstrap'));
  434. $this->assertEquals('loaded plugin routes', Configure::read('PluginTest.test_plugin.routes'));
  435. $this->assertNull(Configure::read('PluginTest.test_plugin.bootstrap'));
  436. $this->assertEquals('loaded plugin two bootstrap', Configure::read('PluginTest.test_plugin_two.bootstrap'));
  437. $this->assertEquals('loaded plugin four bootstrap', Configure::read('PluginTest.test_plugin_four.bootstrap'));
  438. // TestPluginThree won't get loaded by loadAll() since it's in a sub directory.
  439. $this->assertNull(Configure::read('PluginTest.test_plugin_three.bootstrap'));
  440. });
  441. }
  442. }