PluginTest.php 15 KB

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