CakePluginTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. /**
  3. * CakePluginTest file.
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package Cake.Test.Case.Core
  16. * @since CakePHP(tm) v 2.0
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('CakePlugin', 'Core');
  20. /**
  21. * CakePluginTest class
  22. *
  23. */
  24. class CakePluginTest extends CakeTestCase {
  25. /**
  26. * Sets the plugins folder for this test
  27. *
  28. * @return void
  29. */
  30. public function setUp() {
  31. App::build(array(
  32. 'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  33. ), true);
  34. App::objects('plugins', null, false);
  35. }
  36. /**
  37. * Reverts the changes done to the environment while testing
  38. *
  39. * @return void
  40. */
  41. public function tearDown() {
  42. App::build();
  43. CakePlugin::unload();
  44. Configure::delete('CakePluginTest');
  45. }
  46. /**
  47. * Tests loading a single plugin
  48. *
  49. * @return void
  50. */
  51. public function testLoadSingle() {
  52. CakePlugin::unload();
  53. CakePlugin::load('TestPlugin');
  54. $expected = array('TestPlugin');
  55. $this->assertEquals($expected, CakePlugin::loaded());
  56. }
  57. /**
  58. * Tests unloading plugins
  59. *
  60. * @return void
  61. */
  62. public function testUnload() {
  63. CakePlugin::load('TestPlugin');
  64. $expected = array('TestPlugin');
  65. $this->assertEquals($expected, CakePlugin::loaded());
  66. CakePlugin::unload('TestPlugin');
  67. $this->assertEquals(array(), CakePlugin::loaded());
  68. CakePlugin::load('TestPlugin');
  69. $expected = array('TestPlugin');
  70. $this->assertEquals($expected, CakePlugin::loaded());
  71. CakePlugin::unload('TestFakePlugin');
  72. $this->assertEquals($expected, CakePlugin::loaded());
  73. }
  74. /**
  75. * Tests loading a plugin and its bootstrap file
  76. *
  77. * @return void
  78. */
  79. public function testLoadSingleWithBootstrap() {
  80. CakePlugin::load('TestPlugin', array('bootstrap' => true));
  81. $this->assertTrue(CakePlugin::loaded('TestPlugin'));
  82. $this->assertEquals('loaded plugin bootstrap', Configure::read('CakePluginTest.test_plugin.bootstrap'));
  83. }
  84. /**
  85. * Tests loading a plugin with bootstrap file and routes file
  86. *
  87. * @return void
  88. */
  89. public function testLoadSingleWithBootstrapAndRoutes() {
  90. CakePlugin::load('TestPlugin', array('bootstrap' => true, 'routes' => true));
  91. $this->assertTrue(CakePlugin::loaded('TestPlugin'));
  92. $this->assertEquals('loaded plugin bootstrap', Configure::read('CakePluginTest.test_plugin.bootstrap'));
  93. CakePlugin::routes();
  94. $this->assertEquals('loaded plugin routes', Configure::read('CakePluginTest.test_plugin.routes'));
  95. }
  96. /**
  97. * Tests loading multiple plugins at once
  98. *
  99. * @return void
  100. */
  101. public function testLoadMultiple() {
  102. CakePlugin::load(array('TestPlugin', 'TestPluginTwo'));
  103. $expected = array('TestPlugin', 'TestPluginTwo');
  104. $this->assertEquals($expected, CakePlugin::loaded());
  105. }
  106. /**
  107. * Tests loading multiple plugins and their bootstrap files
  108. *
  109. * @return void
  110. */
  111. public function testLoadMultipleWithDefaults() {
  112. CakePlugin::load(array('TestPlugin', 'TestPluginTwo'), array('bootstrap' => true, 'routes' => false));
  113. $expected = array('TestPlugin', 'TestPluginTwo');
  114. $this->assertEquals($expected, CakePlugin::loaded());
  115. $this->assertEquals('loaded plugin bootstrap', Configure::read('CakePluginTest.test_plugin.bootstrap'));
  116. $this->assertEquals('loaded plugin two bootstrap', Configure::read('CakePluginTest.test_plugin_two.bootstrap'));
  117. }
  118. /**
  119. * Tests loading multiple plugins with default loading params and some overrides
  120. *
  121. * @return void
  122. */
  123. public function testLoadMultipleWithDefaultsAndOverride() {
  124. CakePlugin::load(
  125. array('TestPlugin', 'TestPluginTwo' => array('routes' => false)),
  126. array('bootstrap' => true, 'routes' => true)
  127. );
  128. $expected = array('TestPlugin', 'TestPluginTwo');
  129. $this->assertEquals($expected, CakePlugin::loaded());
  130. $this->assertEquals('loaded plugin bootstrap', Configure::read('CakePluginTest.test_plugin.bootstrap'));
  131. $this->assertEquals(null, Configure::read('CakePluginTest.test_plugin_two.bootstrap'));
  132. }
  133. /**
  134. * Tests that it is possible to load multiple bootstrap files at once
  135. *
  136. * @return void
  137. */
  138. public function testMultipleBootstrapFiles() {
  139. CakePlugin::load('TestPlugin', array('bootstrap' => array('bootstrap', 'custom_config')));
  140. $this->assertTrue(CakePlugin::loaded('TestPlugin'));
  141. $this->assertEquals('loaded plugin bootstrap', Configure::read('CakePluginTest.test_plugin.bootstrap'));
  142. }
  143. /**
  144. * Tests that it is possible to load plugin bootstrap by calling a callback function
  145. *
  146. * @return void
  147. */
  148. public function testCallbackBootstrap() {
  149. CakePlugin::load('TestPlugin', array('bootstrap' => array($this, 'pluginBootstrap')));
  150. $this->assertTrue(CakePlugin::loaded('TestPlugin'));
  151. $this->assertEquals('called plugin bootstrap callback', Configure::read('CakePluginTest.test_plugin.bootstrap'));
  152. }
  153. /**
  154. * Tests that loading a missing routes file throws a warning
  155. *
  156. * @return void
  157. * @expectedException PHPUNIT_FRAMEWORK_ERROR_WARNING
  158. */
  159. public function testLoadMultipleWithDefaultsMissingFile() {
  160. CakePlugin::load(array('TestPlugin', 'TestPluginTwo'), array('bootstrap' => true, 'routes' => true));
  161. CakePlugin::routes();
  162. }
  163. /**
  164. * Tests that CakePlugin::load() throws an exception on unknown plugin
  165. *
  166. * @return void
  167. * @expectedException MissingPluginException
  168. */
  169. public function testLoadNotFound() {
  170. CakePlugin::load('MissingPlugin');
  171. }
  172. /**
  173. * Tests that CakePlugin::path() returns the correct path for the loaded plugins
  174. *
  175. * @return void
  176. */
  177. public function testPath() {
  178. CakePlugin::load(array('TestPlugin', 'TestPluginTwo'));
  179. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPlugin' . DS;
  180. $this->assertEquals(CakePlugin::path('TestPlugin'), $expected);
  181. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPluginTwo' . DS;
  182. $this->assertEquals(CakePlugin::path('TestPluginTwo'), $expected);
  183. }
  184. /**
  185. * Tests that CakePlugin::path() throws an exception on unknown plugin
  186. *
  187. * @return void
  188. * @expectedException MissingPluginException
  189. */
  190. public function testPathNotFound() {
  191. CakePlugin::path('TestPlugin');
  192. }
  193. /**
  194. * Tests that CakePlugin::loadAll() will load all plgins in the configured folder
  195. *
  196. * @return void
  197. */
  198. public function testLoadAll() {
  199. CakePlugin::loadAll();
  200. $expected = array('PluginJs', 'TestPlugin', 'TestPluginTwo');
  201. $this->assertEquals($expected, CakePlugin::loaded());
  202. }
  203. /**
  204. * Tests that CakePlugin::loadAll() will load all plgins in the configured folder with bootstrap loading
  205. *
  206. * @return void
  207. */
  208. public function testLoadAllWithDefaults() {
  209. $defaults = array('bootstrap' => true);
  210. CakePlugin::loadAll(array($defaults));
  211. $expected = array('PluginJs', 'TestPlugin', 'TestPluginTwo');
  212. $this->assertEquals($expected, CakePlugin::loaded());
  213. $this->assertEquals('loaded js plugin bootstrap', Configure::read('CakePluginTest.js_plugin.bootstrap'));
  214. $this->assertEquals('loaded plugin bootstrap', Configure::read('CakePluginTest.test_plugin.bootstrap'));
  215. $this->assertEquals('loaded plugin two bootstrap', Configure::read('CakePluginTest.test_plugin_two.bootstrap'));
  216. }
  217. /**
  218. * Tests that CakePlugin::loadAll() will load all plgins in the configured folder wit defaults
  219. * and overrides for a plugin
  220. *
  221. * @return void
  222. */
  223. public function testLoadAllWithDefaultsAndOverride() {
  224. CakePlugin::loadAll(array(array('bootstrap' => true), 'TestPlugin' => array('routes' => true)));
  225. CakePlugin::routes();
  226. $expected = array('PluginJs', 'TestPlugin', 'TestPluginTwo');
  227. $this->assertEquals($expected, CakePlugin::loaded());
  228. $this->assertEquals('loaded js plugin bootstrap', Configure::read('CakePluginTest.js_plugin.bootstrap'));
  229. $this->assertEquals('loaded plugin routes', Configure::read('CakePluginTest.test_plugin.routes'));
  230. $this->assertEquals(null, Configure::read('CakePluginTest.test_plugin.bootstrap'));
  231. $this->assertEquals('loaded plugin two bootstrap', Configure::read('CakePluginTest.test_plugin_two.bootstrap'));
  232. }
  233. /**
  234. * Auxiliary function to test plugin bootstrap callbacks
  235. *
  236. * @return void
  237. */
  238. public function pluginBootstrap() {
  239. Configure::write('CakePluginTest.test_plugin.bootstrap', 'called plugin bootstrap callback');
  240. }
  241. }