PluginTest.php 10 KB

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