PluginTest.php 11 KB

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