PluginTest.php 9.6 KB

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