PluginTest.php 12 KB

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