PluginTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. * Test the plugin namespace
  44. *
  45. * @return void
  46. */
  47. public function testGetNamespace() {
  48. Plugin::load('TestPlugin');
  49. $this->assertEquals('TestPlugin', Plugin::getNamespace('TestPlugin'));
  50. $this->assertEquals('TestPluginTwo', Plugin::getNamespace('TestPluginTwo'));
  51. Plugin::load('TestPluginThree', array('namespace' => 'Company\TestPluginThree'));
  52. $this->assertEquals('Company\TestPluginThree', Plugin::getNamespace('TestPluginThree'));
  53. Plugin::load('CustomPlugin', array('namespace' => 'Company\TestPluginThree'));
  54. $this->assertEquals('Company\TestPluginThree', Plugin::getNamespace('CustomPlugin'));
  55. }
  56. /**
  57. * Tests loading a single plugin
  58. *
  59. * @return void
  60. */
  61. public function testLoadSingle() {
  62. Plugin::unload();
  63. Plugin::load('TestPlugin');
  64. $expected = array('TestPlugin');
  65. $this->assertEquals($expected, Plugin::loaded());
  66. }
  67. /**
  68. * Tests unloading plugins
  69. *
  70. * @return void
  71. */
  72. public function testUnload() {
  73. Plugin::load('TestPlugin');
  74. $expected = array('TestPlugin');
  75. $this->assertEquals($expected, Plugin::loaded());
  76. Plugin::unload('TestPlugin');
  77. $this->assertEquals(array(), Plugin::loaded());
  78. Plugin::load('TestPlugin');
  79. $expected = array('TestPlugin');
  80. $this->assertEquals($expected, Plugin::loaded());
  81. Plugin::unload('TestFakePlugin');
  82. $this->assertEquals($expected, Plugin::loaded());
  83. }
  84. /**
  85. * Test load() with the autoload option.
  86. *
  87. * @return void
  88. */
  89. public function testLoadSingleWithAutoload() {
  90. $this->assertFalse(class_exists('Company\TestPluginThree\Utility\Hello'));
  91. Plugin::load('TestPluginThree', [
  92. 'namespace' => 'Company\TestPluginThree',
  93. 'path' => TEST_APP . 'Plugin/Company/TestPluginThree',
  94. 'autoload' => true,
  95. ]);
  96. $this->assertTrue(
  97. class_exists('Company\TestPluginThree\Utility\Hello'),
  98. 'Class should be loaded'
  99. );
  100. }
  101. /**
  102. * Tests loading a plugin and its bootstrap file
  103. *
  104. * @return void
  105. */
  106. public function testLoadSingleWithBootstrap() {
  107. Plugin::load('TestPlugin', array('bootstrap' => true));
  108. $this->assertTrue(Plugin::loaded('TestPlugin'));
  109. $this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
  110. }
  111. /**
  112. * Tests loading a plugin with bootstrap file and routes file
  113. *
  114. * @return void
  115. */
  116. public function testLoadSingleWithBootstrapAndRoutes() {
  117. Plugin::load('TestPlugin', array('bootstrap' => true, 'routes' => true));
  118. $this->assertTrue(Plugin::loaded('TestPlugin'));
  119. $this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
  120. Plugin::routes();
  121. $this->assertEquals('loaded plugin routes', Configure::read('PluginTest.test_plugin.routes'));
  122. }
  123. /**
  124. * Tests loading multiple plugins at once
  125. *
  126. * @return void
  127. */
  128. public function testLoadMultiple() {
  129. Plugin::load(array('TestPlugin', 'TestPluginTwo'));
  130. $expected = array('TestPlugin', 'TestPluginTwo');
  131. $this->assertEquals($expected, Plugin::loaded());
  132. }
  133. /**
  134. * Tests loading multiple plugins and their bootstrap files
  135. *
  136. * @return void
  137. */
  138. public function testLoadMultipleWithDefaults() {
  139. Plugin::load(array('TestPlugin', 'TestPluginTwo'), array('bootstrap' => true, 'routes' => false));
  140. $expected = array('TestPlugin', 'TestPluginTwo');
  141. $this->assertEquals($expected, Plugin::loaded());
  142. $this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
  143. $this->assertEquals('loaded plugin two bootstrap', Configure::read('PluginTest.test_plugin_two.bootstrap'));
  144. }
  145. /**
  146. * Tests loading multiple plugins with default loading params and some overrides
  147. *
  148. * @return void
  149. */
  150. public function testLoadMultipleWithDefaultsAndOverride() {
  151. Plugin::load(
  152. array('TestPlugin', 'TestPluginTwo' => array('routes' => false)),
  153. array('bootstrap' => true, 'routes' => true)
  154. );
  155. $expected = array('TestPlugin', 'TestPluginTwo');
  156. $this->assertEquals($expected, Plugin::loaded());
  157. $this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
  158. $this->assertEquals(null, Configure::read('PluginTest.test_plugin_two.bootstrap'));
  159. }
  160. /**
  161. * Tests that loading a missing routes file throws a warning
  162. *
  163. * @return void
  164. * @expectedException \PHPUNIT_FRAMEWORK_ERROR_WARNING
  165. */
  166. public function testLoadMultipleWithDefaultsMissingFile() {
  167. Plugin::load(array('TestPlugin', 'TestPluginTwo'), array('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. Plugin::loadAll(array(array(
  177. 'bootstrap' => true,
  178. 'routes' => true,
  179. 'ignoreMissing' => true
  180. )));
  181. Plugin::routes();
  182. }
  183. /**
  184. * Tests that Plugin::load() throws an exception on unknown plugin
  185. *
  186. * @return void
  187. * @expectedException \Cake\Core\Error\MissingPluginException
  188. */
  189. public function testLoadNotFound() {
  190. Plugin::load('MissingPlugin');
  191. }
  192. /**
  193. * Tests that Plugin::path() returns the correct path for the loaded plugins
  194. *
  195. * @return void
  196. */
  197. public function testPath() {
  198. Plugin::load(array('TestPlugin', 'TestPluginTwo'));
  199. $expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS;
  200. $this->assertPathEquals(Plugin::path('TestPlugin'), $expected);
  201. $expected = TEST_APP . 'Plugin' . DS . 'TestPluginTwo' . DS;
  202. $this->assertPathEquals(Plugin::path('TestPluginTwo'), $expected);
  203. }
  204. /**
  205. * Tests that Plugin::path() throws an exception on unknown plugin
  206. *
  207. * @return void
  208. * @expectedException \Cake\Core\Error\MissingPluginException
  209. */
  210. public function testPathNotFound() {
  211. Plugin::path('TestPlugin');
  212. }
  213. /**
  214. * Tests that Plugin::loadAll() will load all plgins in the configured folder
  215. *
  216. * @return void
  217. */
  218. public function testLoadAll() {
  219. Plugin::loadAll();
  220. $expected = ['Company', 'PluginJs', 'TestPlugin', 'TestPluginTwo', 'TestTheme'];
  221. $this->assertEquals($expected, Plugin::loaded());
  222. }
  223. /**
  224. * Test that plugins don't reload using loadAll();
  225. *
  226. * @return void
  227. */
  228. public function testLoadAllWithPluginAlreadyLoaded() {
  229. Plugin::load('PluginJs', ['namespace' => 'Company\TestPluginJs']);
  230. Plugin::loadAll();
  231. $this->assertEquals('Company\TestPluginJs', Plugin::getNamespace('PluginJs'));
  232. }
  233. /**
  234. * Tests that Plugin::loadAll() will load all plgins in the configured folder with bootstrap loading
  235. *
  236. * @return void
  237. */
  238. public function testLoadAllWithDefaults() {
  239. $defaults = array('bootstrap' => true, 'ignoreMissing' => true);
  240. Plugin::loadAll(array($defaults));
  241. $expected = ['Company', 'PluginJs', 'TestPlugin', 'TestPluginTwo', 'TestTheme'];
  242. $this->assertEquals($expected, Plugin::loaded());
  243. $this->assertEquals('loaded js plugin bootstrap', Configure::read('PluginTest.js_plugin.bootstrap'));
  244. $this->assertEquals('loaded plugin bootstrap', Configure::read('PluginTest.test_plugin.bootstrap'));
  245. $this->assertEquals('loaded plugin two bootstrap', Configure::read('PluginTest.test_plugin_two.bootstrap'));
  246. }
  247. /**
  248. * Tests that Plugin::loadAll() will load all plgins in the configured folder wit defaults
  249. * and overrides for a plugin
  250. *
  251. * @return void
  252. */
  253. public function testLoadAllWithDefaultsAndOverride() {
  254. Plugin::loadAll(array(
  255. array('bootstrap' => true, 'ignoreMissing' => true),
  256. 'TestPlugin' => array('routes' => true)
  257. ));
  258. Plugin::routes();
  259. $expected = ['Company', 'PluginJs', 'TestPlugin', 'TestPluginTwo', 'TestTheme'];
  260. $this->assertEquals($expected, Plugin::loaded());
  261. $this->assertEquals('loaded js plugin bootstrap', Configure::read('PluginTest.js_plugin.bootstrap'));
  262. $this->assertEquals('loaded plugin routes', Configure::read('PluginTest.test_plugin.routes'));
  263. $this->assertEquals(null, Configure::read('PluginTest.test_plugin.bootstrap'));
  264. $this->assertEquals('loaded plugin two bootstrap', Configure::read('PluginTest.test_plugin_two.bootstrap'));
  265. }
  266. }