PluginTest.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. * Sets the plugins folder for this test
  26. *
  27. * @return void
  28. */
  29. public function setUp() {
  30. parent::setUp();
  31. App::objects('Plugin', null, false);
  32. }
  33. /**
  34. * Reverts the changes done to the environment while testing
  35. *
  36. * @return void
  37. */
  38. public function tearDown() {
  39. parent::tearDown();
  40. Plugin::unload();
  41. }
  42. /**
  43. * Tests loading a single plugin
  44. *
  45. * @return void
  46. */
  47. public function testLoadSingle() {
  48. Plugin::unload();
  49. Plugin::load('TestPlugin');
  50. $expected = array('TestPlugin');
  51. $this->assertEquals($expected, Plugin::loaded());
  52. }
  53. /**
  54. * Tests unloading plugins
  55. *
  56. * @return void
  57. */
  58. public function testUnload() {
  59. Plugin::load('TestPlugin');
  60. $expected = array('TestPlugin');
  61. $this->assertEquals($expected, Plugin::loaded());
  62. Plugin::unload('TestPlugin');
  63. $this->assertEquals(array(), Plugin::loaded());
  64. Plugin::load('TestPlugin');
  65. $expected = array('TestPlugin');
  66. $this->assertEquals($expected, Plugin::loaded());
  67. Plugin::unload('TestFakePlugin');
  68. $this->assertEquals($expected, Plugin::loaded());
  69. }
  70. /**
  71. * Test load() with the autoload option.
  72. *
  73. * @return void
  74. */
  75. public function testLoadSingleWithAutoload() {
  76. $this->assertFalse(class_exists('Company\TestPluginThree\Utility\Hello'));
  77. Plugin::load('Company\TestPluginThree', [
  78. 'autoload' => true,
  79. ]);
  80. $this->assertTrue(
  81. class_exists('Company\TestPluginThree\Utility\Hello'),
  82. 'Class should be loaded'
  83. );
  84. }
  85. /**
  86. * Tests loading a plugin and its bootstrap file
  87. *
  88. * @return void
  89. */
  90. public function testLoadSingleWithBootstrap() {
  91. Plugin::load('TestPlugin', array('bootstrap' => true));
  92. $this->assertTrue(Plugin::loaded('TestPlugin'));
  93. $this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
  94. Plugin::load('Company\TestPluginThree', array('bootstrap' => true));
  95. $this->assertTrue(Plugin::loaded('Company\TestPluginThree'));
  96. $this->assertEquals('loaded plugin three bootstrap', Configure::read('PluginTest.test_plugin_three.bootstrap'));
  97. }
  98. /**
  99. * Tests loading a plugin with bootstrap file and routes file
  100. *
  101. * @return void
  102. */
  103. public function testLoadSingleWithBootstrapAndRoutes() {
  104. Plugin::load('TestPlugin', array('bootstrap' => true, 'routes' => true));
  105. $this->assertTrue(Plugin::loaded('TestPlugin'));
  106. $this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
  107. Plugin::routes();
  108. $this->assertEquals('loaded plugin routes', Configure::read('PluginTest.test_plugin.routes'));
  109. }
  110. /**
  111. * Tests loading multiple plugins at once
  112. *
  113. * @return void
  114. */
  115. public function testLoadMultiple() {
  116. Plugin::load(array('TestPlugin', 'TestPluginTwo'));
  117. $expected = array('TestPlugin', 'TestPluginTwo');
  118. $this->assertEquals($expected, Plugin::loaded());
  119. }
  120. /**
  121. * Tests loading multiple plugins and their bootstrap files
  122. *
  123. * @return void
  124. */
  125. public function testLoadMultipleWithDefaults() {
  126. Plugin::load(array('TestPlugin', 'TestPluginTwo'), array('bootstrap' => true, 'routes' => false));
  127. $expected = array('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. Plugin::load(
  139. array('TestPlugin', 'TestPluginTwo' => array('routes' => false)),
  140. array('bootstrap' => true, 'routes' => true)
  141. );
  142. $expected = array('TestPlugin', 'TestPluginTwo');
  143. $this->assertEquals($expected, Plugin::loaded());
  144. $this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
  145. $this->assertEquals(null, Configure::read('PluginTest.test_plugin_two.bootstrap'));
  146. }
  147. /**
  148. * Tests that loading a missing routes file throws a warning
  149. *
  150. * @return void
  151. * @expectedException \PHPUNIT_FRAMEWORK_ERROR_WARNING
  152. */
  153. public function testLoadMultipleWithDefaultsMissingFile() {
  154. Plugin::load(array('TestPlugin', 'TestPluginTwo'), array('bootstrap' => true, 'routes' => true));
  155. Plugin::routes();
  156. }
  157. /**
  158. * Test ignoring missing bootstrap/routes file
  159. *
  160. * @return void
  161. */
  162. public function testIgnoreMissingFiles() {
  163. Plugin::loadAll(array(array(
  164. 'bootstrap' => true,
  165. 'routes' => true,
  166. 'ignoreMissing' => true
  167. )));
  168. Plugin::routes();
  169. }
  170. /**
  171. * Tests that Plugin::load() throws an exception on unknown plugin
  172. *
  173. * @return void
  174. * @expectedException \Cake\Core\Error\MissingPluginException
  175. */
  176. public function testLoadNotFound() {
  177. Plugin::load('MissingPlugin');
  178. }
  179. /**
  180. * Tests that Plugin::path() returns the correct path for the loaded plugins
  181. *
  182. * @return void
  183. */
  184. public function testPath() {
  185. Plugin::load(array('TestPlugin', 'TestPluginTwo', 'Company\TestPluginThree'));
  186. $expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS;
  187. $this->assertPathEquals(Plugin::path('TestPlugin'), $expected);
  188. $expected = TEST_APP . 'Plugin' . DS . 'TestPluginTwo' . DS;
  189. $this->assertPathEquals(Plugin::path('TestPluginTwo'), $expected);
  190. $expected = TEST_APP . 'Plugin' . DS . 'Company' . DS . 'TestPluginThree' . DS;
  191. $this->assertPathEquals(Plugin::path('Company\TestPluginThree'), $expected);
  192. }
  193. /**
  194. * Tests that Plugin::path() throws an exception on unknown plugin
  195. *
  196. * @return void
  197. * @expectedException \Cake\Core\Error\MissingPluginException
  198. */
  199. public function testPathNotFound() {
  200. Plugin::path('TestPlugin');
  201. }
  202. /**
  203. * Tests that Plugin::classPath() returns the correct path for the loaded plugins
  204. *
  205. * @return void
  206. */
  207. public function testClassPath() {
  208. Plugin::load(array('TestPlugin', 'TestPluginTwo', 'Company\TestPluginThree'));
  209. $expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS . 'src' . DS;
  210. $this->assertPathEquals(Plugin::classPath('TestPlugin'), $expected);
  211. $expected = TEST_APP . 'Plugin' . DS . 'TestPluginTwo' . DS . 'src' . DS;
  212. $this->assertPathEquals(Plugin::classPath('TestPluginTwo'), $expected);
  213. $expected = TEST_APP . 'Plugin' . DS . 'Company' . DS . 'TestPluginThree' . DS . 'src' . DS;
  214. $this->assertPathEquals(Plugin::classPath('Company\TestPluginThree'), $expected);
  215. }
  216. /**
  217. * Tests that Plugin::classPath() throws an exception on unknown plugin
  218. *
  219. * @return void
  220. * @expectedException \Cake\Core\Error\MissingPluginException
  221. */
  222. public function testClassPathNotFound() {
  223. Plugin::classPath('TestPlugin');
  224. }
  225. /**
  226. * Tests that Plugin::loadAll() will load all plgins in the configured folder
  227. *
  228. * @return void
  229. */
  230. public function testLoadAll() {
  231. Plugin::loadAll();
  232. $expected = ['Company', 'PluginJs', 'TestPlugin', 'TestPluginFour', 'TestPluginTwo', 'TestTheme'];
  233. $this->assertEquals($expected, Plugin::loaded());
  234. }
  235. /**
  236. * Test that plugins don't reload using loadAll();
  237. *
  238. * @return void
  239. */
  240. public function testLoadAllWithPluginAlreadyLoaded() {
  241. Plugin::load('Company\TestPluginThree', ['bootstrap' => false]);
  242. Plugin::loadAll(['bootstrap' => true, 'ignoreMissing' => true]);
  243. $this->assertEmpty(Configure::read('PluginTest.test_plugin_three.bootstrap'));
  244. }
  245. /**
  246. * Tests that Plugin::loadAll() will load all plgins in the configured folder with bootstrap loading
  247. *
  248. * @return void
  249. */
  250. public function testLoadAllWithDefaults() {
  251. $defaults = array('bootstrap' => true, 'ignoreMissing' => true);
  252. Plugin::loadAll(array($defaults));
  253. $expected = ['Company', 'PluginJs', 'TestPlugin', 'TestPluginFour', 'TestPluginTwo', 'TestTheme'];
  254. $this->assertEquals($expected, Plugin::loaded());
  255. $this->assertEquals('loaded js plugin bootstrap', Configure::read('PluginTest.js_plugin.bootstrap'));
  256. $this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
  257. $this->assertEquals('loaded plugin two bootstrap', Configure::read('PluginTest.test_plugin_two.bootstrap'));
  258. }
  259. /**
  260. * Tests that Plugin::loadAll() will load all plgins in the configured folder wit defaults
  261. * and overrides for a plugin
  262. *
  263. * @return void
  264. */
  265. public function testLoadAllWithDefaultsAndOverride() {
  266. Plugin::loadAll(array(
  267. array('bootstrap' => true, 'ignoreMissing' => true),
  268. 'TestPlugin' => array('routes' => true),
  269. 'TestPluginFour' => array('bootstrap' => true, 'classBase' => '')
  270. ));
  271. Plugin::routes();
  272. $expected = ['Company', 'PluginJs', 'TestPlugin', 'TestPluginFour', 'TestPluginTwo', 'TestTheme'];
  273. $this->assertEquals($expected, Plugin::loaded());
  274. $this->assertEquals('loaded js plugin bootstrap', Configure::read('PluginTest.js_plugin.bootstrap'));
  275. $this->assertEquals('loaded plugin routes', Configure::read('PluginTest.test_plugin.routes'));
  276. $this->assertEquals(null, Configure::read('PluginTest.test_plugin.bootstrap'));
  277. $this->assertEquals('loaded plugin two bootstrap', Configure::read('PluginTest.test_plugin_two.bootstrap'));
  278. $this->assertEquals('loaded plugin four bootstrap', Configure::read('PluginTest.test_plugin_four.bootstrap'));
  279. // TestPluginThree won't get loaded by loadAll() since it's in a sub directory.
  280. $this->assertEquals(null, Configure::read('PluginTest.test_plugin_three.bootstrap'));
  281. }
  282. }