PluginTest.php 12 KB

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