PluginTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @since 2.0.0
  12. * @license http://www.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. */
  22. class PluginTest extends TestCase
  23. {
  24. /**
  25. * Reverts the changes done to the environment while testing
  26. *
  27. * @return void
  28. */
  29. public function tearDown()
  30. {
  31. parent::tearDown();
  32. Plugin::unload();
  33. }
  34. /**
  35. * Tests loading a single plugin
  36. *
  37. * @return void
  38. */
  39. public function testLoadSingle()
  40. {
  41. Plugin::unload();
  42. Plugin::load('TestPlugin');
  43. $expected = ['TestPlugin'];
  44. $this->assertEquals($expected, Plugin::loaded());
  45. }
  46. /**
  47. * Tests unloading plugins
  48. *
  49. * @return void
  50. */
  51. public function testUnload()
  52. {
  53. Plugin::load('TestPlugin');
  54. $expected = ['TestPlugin'];
  55. $this->assertEquals($expected, Plugin::loaded());
  56. Plugin::unload('TestPlugin');
  57. $this->assertEquals([], Plugin::loaded());
  58. Plugin::load('TestPlugin');
  59. $expected = ['TestPlugin'];
  60. $this->assertEquals($expected, Plugin::loaded());
  61. Plugin::unload('TestFakePlugin');
  62. $this->assertEquals($expected, Plugin::loaded());
  63. }
  64. /**
  65. * Test load() with the autoload option.
  66. *
  67. * @return void
  68. */
  69. public function testLoadSingleWithAutoload()
  70. {
  71. $this->assertFalse(class_exists('Company\TestPluginFive\Utility\Hello'));
  72. Plugin::load('Company/TestPluginFive', [
  73. 'autoload' => true,
  74. ]);
  75. $this->assertTrue(
  76. class_exists('Company\TestPluginFive\Utility\Hello'),
  77. 'Class should be loaded'
  78. );
  79. }
  80. /**
  81. * Test load() with the autoload option.
  82. *
  83. * @return void
  84. */
  85. public function testLoadSingleWithAutoloadAndBootstrap()
  86. {
  87. Plugin::load(
  88. 'Company/TestPluginFive',
  89. [
  90. 'autoload' => true,
  91. 'bootstrap' => true
  92. ]
  93. );
  94. $this->assertTrue(Configure::read('PluginTest.test_plugin_five.autoload'));
  95. $this->assertEquals('loaded plugin five bootstrap', Configure::read('PluginTest.test_plugin_five.bootstrap'));
  96. $this->assertTrue(
  97. class_exists('Company\TestPluginFive\Utility\Hello'),
  98. 'Class should be loaded'
  99. );
  100. }
  101. /**
  102. * Tests loading a plugin and its bootstrap file
  103. *
  104. * @return void
  105. */
  106. public function testLoadSingleWithBootstrap()
  107. {
  108. Plugin::load('TestPlugin', ['bootstrap' => true]);
  109. $this->assertTrue(Plugin::loaded('TestPlugin'));
  110. $this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
  111. Plugin::load('Company/TestPluginThree', ['bootstrap' => true]);
  112. $this->assertTrue(Plugin::loaded('Company/TestPluginThree'));
  113. $this->assertEquals('loaded plugin three bootstrap', Configure::read('PluginTest.test_plugin_three.bootstrap'));
  114. }
  115. /**
  116. * Tests loading a plugin with bootstrap file and routes file
  117. *
  118. * @return void
  119. */
  120. public function testLoadSingleWithBootstrapAndRoutes()
  121. {
  122. Plugin::load('TestPlugin', ['bootstrap' => true, 'routes' => true]);
  123. $this->assertTrue(Plugin::loaded('TestPlugin'));
  124. $this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
  125. Plugin::routes();
  126. $this->assertEquals('loaded plugin routes', Configure::read('PluginTest.test_plugin.routes'));
  127. }
  128. /**
  129. * Test load() with path configuration data
  130. *
  131. * @return void
  132. */
  133. public function testLoadSingleWithPathConfig()
  134. {
  135. Configure::write('plugins.TestPlugin', APP);
  136. Plugin::load('TestPlugin');
  137. $this->assertEquals(APP . 'src' . DS, Plugin::classPath('TestPlugin'));
  138. }
  139. /**
  140. * Tests loading multiple plugins at once
  141. *
  142. * @return void
  143. */
  144. public function testLoadMultiple()
  145. {
  146. Plugin::load(['TestPlugin', 'TestPluginTwo']);
  147. $expected = ['TestPlugin', 'TestPluginTwo'];
  148. $this->assertEquals($expected, Plugin::loaded());
  149. }
  150. /**
  151. * Tests loading multiple plugins and their bootstrap files
  152. *
  153. * @return void
  154. */
  155. public function testLoadMultipleWithDefaults()
  156. {
  157. Plugin::load(['TestPlugin', 'TestPluginTwo'], ['bootstrap' => true, 'routes' => false]);
  158. $expected = ['TestPlugin', 'TestPluginTwo'];
  159. $this->assertEquals($expected, Plugin::loaded());
  160. $this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
  161. $this->assertEquals('loaded plugin two bootstrap', Configure::read('PluginTest.test_plugin_two.bootstrap'));
  162. }
  163. /**
  164. * Tests loading multiple plugins with default loading params and some overrides
  165. *
  166. * @return void
  167. */
  168. public function testLoadMultipleWithDefaultsAndOverride()
  169. {
  170. Plugin::load(
  171. ['TestPlugin', 'TestPluginTwo' => ['routes' => false]],
  172. ['bootstrap' => true, 'routes' => true]
  173. );
  174. $expected = ['TestPlugin', 'TestPluginTwo'];
  175. $this->assertEquals($expected, Plugin::loaded());
  176. $this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
  177. $this->assertEquals(null, Configure::read('PluginTest.test_plugin_two.bootstrap'));
  178. }
  179. /**
  180. * Tests that loading a missing routes file throws a warning
  181. *
  182. * @return void
  183. * @expectedException \PHPUNIT_FRAMEWORK_ERROR_WARNING
  184. */
  185. public function testLoadMultipleWithDefaultsMissingFile()
  186. {
  187. Plugin::load(['TestPlugin', 'TestPluginTwo'], ['bootstrap' => true, 'routes' => true]);
  188. Plugin::routes();
  189. }
  190. /**
  191. * Test ignoring missing bootstrap/routes file
  192. *
  193. * @return void
  194. */
  195. public function testIgnoreMissingFiles()
  196. {
  197. Plugin::loadAll([[
  198. 'bootstrap' => true,
  199. 'routes' => true,
  200. 'ignoreMissing' => true
  201. ]]);
  202. $this->assertTrue(Plugin::routes());
  203. }
  204. /**
  205. * Tests that Plugin::load() throws an exception on unknown plugin
  206. *
  207. * @return void
  208. * @expectedException \Cake\Core\Exception\MissingPluginException
  209. */
  210. public function testLoadNotFound()
  211. {
  212. Plugin::load('MissingPlugin');
  213. }
  214. /**
  215. * Tests that Plugin::path() returns the correct path for the loaded plugins
  216. *
  217. * @return void
  218. */
  219. public function testPath()
  220. {
  221. Plugin::load(['TestPlugin', 'TestPluginTwo', 'Company/TestPluginThree']);
  222. $expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS;
  223. $this->assertPathEquals(Plugin::path('TestPlugin'), $expected);
  224. $expected = TEST_APP . 'Plugin' . DS . 'TestPluginTwo' . DS;
  225. $this->assertPathEquals(Plugin::path('TestPluginTwo'), $expected);
  226. $expected = TEST_APP . 'Plugin' . DS . 'Company' . DS . 'TestPluginThree' . DS;
  227. $this->assertPathEquals(Plugin::path('Company/TestPluginThree'), $expected);
  228. }
  229. /**
  230. * Tests that Plugin::path() throws an exception on unknown plugin
  231. *
  232. * @return void
  233. * @expectedException \Cake\Core\Exception\MissingPluginException
  234. */
  235. public function testPathNotFound()
  236. {
  237. Plugin::path('TestPlugin');
  238. }
  239. /**
  240. * Tests that Plugin::classPath() returns the correct path for the loaded plugins
  241. *
  242. * @return void
  243. */
  244. public function testClassPath()
  245. {
  246. Plugin::load(['TestPlugin', 'TestPluginTwo', 'Company/TestPluginThree']);
  247. $expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS . 'src' . DS;
  248. $this->assertPathEquals(Plugin::classPath('TestPlugin'), $expected);
  249. $expected = TEST_APP . 'Plugin' . DS . 'TestPluginTwo' . DS . 'src' . DS;
  250. $this->assertPathEquals(Plugin::classPath('TestPluginTwo'), $expected);
  251. $expected = TEST_APP . 'Plugin' . DS . 'Company' . DS . 'TestPluginThree' . DS . 'src' . DS;
  252. $this->assertPathEquals(Plugin::classPath('Company/TestPluginThree'), $expected);
  253. }
  254. /**
  255. * Tests that Plugin::classPath() throws an exception on unknown plugin
  256. *
  257. * @return void
  258. * @expectedException \Cake\Core\Exception\MissingPluginException
  259. */
  260. public function testClassPathNotFound()
  261. {
  262. Plugin::classPath('TestPlugin');
  263. }
  264. /**
  265. * Tests that Plugin::loadAll() will load all plugins in the configured folder
  266. *
  267. * @return void
  268. */
  269. public function testLoadAll()
  270. {
  271. Plugin::loadAll();
  272. $expected = ['Company', 'PluginJs', 'TestPlugin', 'TestPluginFour', 'TestPluginTwo', 'TestTheme'];
  273. $this->assertEquals($expected, Plugin::loaded());
  274. }
  275. /**
  276. * Test loadAll() with path configuration data
  277. *
  278. * @return void
  279. */
  280. public function testLoadAllWithPathConfig()
  281. {
  282. Configure::write('plugins.FakePlugin', APP);
  283. Plugin::loadAll();
  284. $this->assertContains('FakePlugin', Plugin::loaded());
  285. }
  286. /**
  287. * Test that plugins don't reload using loadAll();
  288. *
  289. * @return void
  290. */
  291. public function testLoadAllWithPluginAlreadyLoaded()
  292. {
  293. Plugin::load('Company/TestPluginThree', ['bootstrap' => false]);
  294. Plugin::loadAll(['bootstrap' => true, 'ignoreMissing' => true]);
  295. $this->assertEmpty(Configure::read('PluginTest.test_plugin_three.bootstrap'));
  296. }
  297. /**
  298. * Tests that Plugin::loadAll() will load all plugins in the configured folder with bootstrap loading
  299. *
  300. * @return void
  301. */
  302. public function testLoadAllWithDefaults()
  303. {
  304. $defaults = ['bootstrap' => true, 'ignoreMissing' => true];
  305. Plugin::loadAll([$defaults]);
  306. $expected = ['Company', 'PluginJs', 'TestPlugin', 'TestPluginFour', 'TestPluginTwo', 'TestTheme'];
  307. $this->assertEquals($expected, Plugin::loaded());
  308. $this->assertEquals('loaded js plugin bootstrap', Configure::read('PluginTest.js_plugin.bootstrap'));
  309. $this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
  310. $this->assertEquals('loaded plugin two bootstrap', Configure::read('PluginTest.test_plugin_two.bootstrap'));
  311. }
  312. /**
  313. * Tests that Plugin::loadAll() will load all plugins in the configured folder wit defaults
  314. * and overrides for a plugin
  315. *
  316. * @return void
  317. */
  318. public function testLoadAllWithDefaultsAndOverride()
  319. {
  320. Plugin::loadAll([
  321. ['bootstrap' => true, 'ignoreMissing' => true],
  322. 'TestPlugin' => ['routes' => true],
  323. 'TestPluginFour' => ['bootstrap' => true, 'classBase' => '']
  324. ]);
  325. Plugin::routes();
  326. $expected = ['Company', 'PluginJs', 'TestPlugin', 'TestPluginFour', 'TestPluginTwo', 'TestTheme'];
  327. $this->assertEquals($expected, Plugin::loaded());
  328. $this->assertEquals('loaded js plugin bootstrap', Configure::read('PluginTest.js_plugin.bootstrap'));
  329. $this->assertEquals('loaded plugin routes', Configure::read('PluginTest.test_plugin.routes'));
  330. $this->assertEquals(null, Configure::read('PluginTest.test_plugin.bootstrap'));
  331. $this->assertEquals('loaded plugin two bootstrap', Configure::read('PluginTest.test_plugin_two.bootstrap'));
  332. $this->assertEquals('loaded plugin four bootstrap', Configure::read('PluginTest.test_plugin_four.bootstrap'));
  333. // TestPluginThree won't get loaded by loadAll() since it's in a sub directory.
  334. $this->assertEquals(null, Configure::read('PluginTest.test_plugin_three.bootstrap'));
  335. }
  336. }