CakePluginTest.php 7.7 KB

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