PluginTest.php 11 KB

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