Base for a static organization website

ToolbarComponentTest.php 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. <?php
  2. /**
  3. * PHP 5
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  12. * @link http://cakephp.org CakePHP(tm) Project
  13. * @since DebugKit 2.1
  14. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  15. */
  16. App::uses('Router', 'Routing');
  17. App::uses('Controller', 'Controller');
  18. App::uses('AppController', 'Controller');
  19. App::uses('Component', 'Controller');
  20. App::uses('ToolbarComponent', 'DebugKit.Controller/Component');
  21. App::uses('DebugMemory', 'DebugKit.Lib');
  22. App::uses('DebugTimer', 'DebugKit.Lib');
  23. /**
  24. * Class TestToolbarComponent
  25. *
  26. * @since DebugKit 2.1
  27. */
  28. class TestToolbarComponent extends ToolbarComponent {
  29. /**
  30. * Load Panels of Toolbar
  31. *
  32. * @param $panels
  33. * @param array $settings
  34. */
  35. public function loadPanels($panels, $settings = array()) {
  36. $this->_loadPanels($panels, $settings);
  37. }
  38. }
  39. /**
  40. * ToolbarComponentTestCase Test case
  41. *
  42. */
  43. class ToolbarComponentTestCase extends CakeTestCase {
  44. /**
  45. * fixtures
  46. *
  47. * @var array
  48. */
  49. public $fixtures = array('core.article');
  50. /**
  51. * url for test
  52. *
  53. * @var string
  54. */
  55. public $url;
  56. /**
  57. * Start test callback
  58. *
  59. * @return void
  60. */
  61. public function setUp() {
  62. parent::setUp();
  63. Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
  64. $this->_server = $_SERVER;
  65. $this->_get = $_GET;
  66. $this->_paths = array();
  67. $this->_paths['plugins'] = App::path('plugins');
  68. $this->_paths['views'] = App::path('views');
  69. $this->_paths['vendors'] = App::path('vendors');
  70. $this->_paths['controllers'] = App::path('controllers');
  71. Configure::write('Cache.disable', false);
  72. $this->url = '/';
  73. }
  74. /**
  75. * endTest
  76. *
  77. * @return void
  78. */
  79. public function tearDown() {
  80. $_SERVER = $this->_server;
  81. $_GET = $this->_get;
  82. parent::tearDown();
  83. App::build(array(
  84. 'plugins' => $this->_paths['plugins'],
  85. 'views' => $this->_paths['views'],
  86. 'controllers' => $this->_paths['controllers'],
  87. 'vendors' => $this->_paths['vendors']
  88. ), true);
  89. Configure::write('Cache.disable', true);
  90. unset($this->Controller);
  91. ClassRegistry::flush();
  92. if (class_exists('DebugMemory')) {
  93. DebugMemory::clear();
  94. }
  95. if (class_exists('DebugTimer')) {
  96. DebugTimer::clear();
  97. }
  98. Router::reload();
  99. }
  100. /**
  101. * loading test controller
  102. *
  103. * @param array $settings
  104. * @return Controller
  105. */
  106. protected function _loadController($settings = array()) {
  107. $request = new CakeRequest($this->url);
  108. $request->addParams(Router::parse($this->url));
  109. $this->Controller = new Controller($request);
  110. $this->Controller->uses = null;
  111. $this->Controller->components = array('Toolbar' => $settings + array('className' => 'TestToolbar'));
  112. $this->Controller->constructClasses();
  113. $this->Controller->Components->trigger('initialize', array($this->Controller));
  114. return $this->Controller;
  115. }
  116. /**
  117. * test Loading of panel classes
  118. *
  119. * @return void
  120. */
  121. public function testLoadPanels() {
  122. $this->_loadController();
  123. $this->Controller->Toolbar->loadPanels(array('session', 'request'));
  124. $this->assertInstanceOf('SessionPanel', $this->Controller->Toolbar->panels['session']);
  125. $this->assertInstanceOf('RequestPanel', $this->Controller->Toolbar->panels['request']);
  126. $this->Controller->Toolbar->loadPanels(array('history'), array('history' => 10));
  127. $this->assertEquals($this->Controller->Toolbar->panels['history']->history, 10);
  128. }
  129. /**
  130. * Test exceptions on bad panel names
  131. *
  132. * @expectedException PHPUnit_Framework_Error
  133. * @return void
  134. */
  135. public function testLoadPanelsError() {
  136. $this->Controller->Toolbar->loadPanels(array('randomNonExisting', 'request'));
  137. }
  138. /**
  139. * test Loading of panel classes from a plugin
  140. *
  141. * @return void
  142. */
  143. public function testLoadPluginPanels() {
  144. $debugKitPath = App::pluginPath('DebugKit');
  145. $noDir = (empty($debugKitPath) || !file_exists($debugKitPath));
  146. if ($noDir) {
  147. $this->markTestAsSkipped('Could not find DebugKit in plugin paths');
  148. }
  149. App::build(array(
  150. 'Plugin' => array($debugKitPath . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  151. ));
  152. CakePlugin::load('DebugkitTestPlugin');
  153. $this->_loadController();
  154. $this->Controller->Toolbar->loadPanels(array('DebugkitTestPlugin.PluginTest'));
  155. $this->assertInstanceOf(
  156. 'PluginTestPanel',
  157. $this->Controller->Toolbar->panels['plugin_test']
  158. );
  159. }
  160. /**
  161. * test loading of vendor panels from test_app folder
  162. *
  163. * @return void
  164. */
  165. public function testLibPanels() {
  166. $debugKitPath = App::pluginPath('DebugKit');
  167. $noDir = (empty($debugKitPath) || !file_exists($debugKitPath));
  168. if ($noDir) {
  169. $this->markTestAsSkipped('Could not find DebugKit in plugin paths');
  170. }
  171. App::build(array(
  172. 'Lib' => array($debugKitPath . 'Test' . DS . 'test_app' . DS . 'Lib' . DS)
  173. ));
  174. $this->_loadController(array(
  175. 'panels' => array('test'),
  176. 'className' => 'DebugKit.Toolbar',
  177. ));
  178. $this->assertTrue(isset($this->Controller->Toolbar->panels['test']));
  179. $this->assertInstanceOf('TestPanel', $this->Controller->Toolbar->panels['test']);
  180. }
  181. /**
  182. * test construct
  183. *
  184. * @return void
  185. */
  186. public function testConstruct() {
  187. $this->_loadController();
  188. $this->assertFalse(empty($this->Controller->Toolbar->panels));
  189. $memory = DebugMemory::getAll();
  190. $this->assertTrue(isset($memory['Component initialization']));
  191. $events = $this->Controller->getEventManager();
  192. $this->assertNotEmpty($events->listeners('Controller.initialize'));
  193. $this->assertNotEmpty($events->listeners('Controller.startup'));
  194. $this->assertNotEmpty($events->listeners('Controller.beforeRender'));
  195. $this->assertNotEmpty($events->listeners('Controller.shutdown'));
  196. $this->assertNotEmpty($events->listeners('View.beforeRender'));
  197. $this->assertNotEmpty($events->listeners('View.afterRender'));
  198. $this->assertNotEmpty($events->listeners('View.beforeLayout'));
  199. $this->assertNotEmpty($events->listeners('View.afterLayout'));
  200. }
  201. /**
  202. * test initialize w/ custom panels and defaults
  203. *
  204. * @return void
  205. */
  206. public function testInitializeCustomPanelsWithDefaults() {
  207. $this->_loadController(array(
  208. 'panels' => array('test'),
  209. ));
  210. $expected = array(
  211. 'history', 'session', 'request', 'sql_log', 'timer',
  212. 'log', 'variables', 'environment', 'include', 'test'
  213. );
  214. $this->assertEquals($expected, array_keys($this->Controller->Toolbar->panels));
  215. }
  216. /**
  217. * test syntax for removing panels
  218. *
  219. * @return void
  220. */
  221. public function testInitializeRemovingPanels() {
  222. $this->_loadController(array(
  223. 'panels' => array(
  224. 'session' => false,
  225. 'history' => false,
  226. )
  227. ));
  228. $expected = array('request', 'sql_log', 'timer', 'log', 'variables', 'environment', 'include');
  229. $this->assertEquals($expected, array_keys($this->Controller->Toolbar->panels));
  230. }
  231. /**
  232. * ensure that Toolbar is not enabled when debug == 0 on initialize
  233. *
  234. * @return void
  235. */
  236. public function testDebugDisableOnInitialize() {
  237. $_debug = Configure::read('debug');
  238. Configure::write('debug', 0);
  239. $this->_loadController();
  240. Configure::write('debug', $_debug);
  241. $this->assertFalse($this->Controller->Components->enabled('Toolbar'));
  242. }
  243. /**
  244. * test that passing in forceEnable will enable the toolbar even if debug = 0
  245. *
  246. * @return void
  247. */
  248. public function testForceEnable() {
  249. $_debug = Configure::read('debug');
  250. Configure::write('debug', 0);
  251. $this->_loadController(array(
  252. 'forceEnable' => true,
  253. ));
  254. Configure::write('debug', $_debug);
  255. $this->assertTrue($this->Controller->Components->enabled('Toolbar'));
  256. }
  257. /**
  258. * Test disabling autoRunning of toolbar
  259. *
  260. * @return void
  261. */
  262. public function testAutoRunSettingFalse() {
  263. $this->_loadController(array(
  264. 'autoRun' => false,
  265. ));
  266. $this->assertFalse($this->Controller->Components->enabled('Toolbar'));
  267. }
  268. /**
  269. * test autorun = false with query string param
  270. *
  271. * @return void
  272. */
  273. public function testAutoRunSettingWithQueryString() {
  274. $this->url = '/?debug=1';
  275. $_GET['debug'] = 1;
  276. $this->_loadController(array(
  277. 'autoRun' => false,
  278. ));
  279. $this->assertTrue($this->Controller->Components->enabled('Toolbar'));
  280. }
  281. /**
  282. * test startup
  283. *
  284. * @return void
  285. */
  286. public function testStartup() {
  287. $this->_loadController(array(
  288. 'panels' => array('timer'),
  289. ));
  290. $MockPanel = $this->getMock('DebugPanel');
  291. $MockPanel->expects($this->once())->method('startup');
  292. $this->Controller->Toolbar->panels['timer'] = $MockPanel;
  293. $this->Controller->Toolbar->startup($this->Controller);
  294. $timers = DebugTimer::getAll();
  295. $this->assertTrue(isset($timers['controllerAction']));
  296. $memory = DebugMemory::getAll();
  297. $this->assertTrue(isset($memory['Controller action start']));
  298. }
  299. /**
  300. * Test that cache config generation works.
  301. *
  302. * @return void
  303. */
  304. public function testCacheConfigGeneration() {
  305. $this->_loadController();
  306. $this->Controller->Components->trigger('startup', array($this->Controller));
  307. $results = Cache::config('debug_kit');
  308. $this->assertTrue(is_array($results));
  309. }
  310. /**
  311. * test state saving of toolbar
  312. *
  313. * @return void
  314. */
  315. public function testStateSaving() {
  316. $this->_loadController();
  317. $configName = 'debug_kit';
  318. $this->Controller->Toolbar->cacheKey = 'toolbar_history';
  319. $this->Controller->Components->trigger('startup', array($this->Controller));
  320. $this->Controller->set('test', 'testing');
  321. $this->Controller->Components->trigger('beforeRender', array($this->Controller));
  322. $result = Cache::read('toolbar_history', $configName);
  323. $this->assertEquals($result[0]['variables']['content']['test'], 'testing');
  324. Cache::delete('toolbar_history', $configName);
  325. }
  326. /**
  327. * Test Before Render callback
  328. *
  329. * @return void
  330. */
  331. public function testBeforeRender() {
  332. $this->_loadController(array(
  333. 'panels' => array('timer', 'session'),
  334. ));
  335. $MockPanel = $this->getMock('DebugPanel');
  336. $MockPanel->expects($this->once())->method('beforeRender');
  337. $this->Controller->Toolbar->panels['timer'] = $MockPanel;
  338. $this->Controller->Toolbar->beforeRender($this->Controller);
  339. $this->assertTrue(isset($this->Controller->helpers['DebugKit.Toolbar']));
  340. $this->assertEquals($this->Controller->helpers['DebugKit.Toolbar']['output'], 'DebugKit.HtmlToolbar');
  341. $this->assertEquals($this->Controller->helpers['DebugKit.Toolbar']['cacheConfig'], 'debug_kit');
  342. $this->assertTrue(isset($this->Controller->helpers['DebugKit.Toolbar']['cacheKey']));
  343. $this->assertTrue(isset($this->Controller->viewVars['debugToolbarPanels']));
  344. $vars = $this->Controller->viewVars['debugToolbarPanels'];
  345. $expected = array(
  346. 'plugin' => 'DebugKit',
  347. 'elementName' => 'session_panel',
  348. 'content' => $this->Controller->Toolbar->Session->read(),
  349. 'disableTimer' => true,
  350. 'title' => ''
  351. );
  352. $this->assertEquals($expected, $vars['session']);
  353. $memory = DebugMemory::getAll();
  354. $this->assertTrue(isset($memory['Controller render start']));
  355. }
  356. /**
  357. * test that vars are gathered and state is saved on beforeRedirect
  358. *
  359. * @return void
  360. */
  361. public function testBeforeRedirect() {
  362. $this->_loadController(array(
  363. 'panels' => array('session', 'history'),
  364. ));
  365. $configName = 'debug_kit';
  366. $this->Controller->Toolbar->cacheKey = 'toolbar_history';
  367. Cache::delete('toolbar_history', $configName);
  368. DebugTimer::start('controllerAction', 'testing beforeRedirect');
  369. $MockPanel = $this->getMock('DebugPanel');
  370. $MockPanel->expects($this->once())->method('beforeRender');
  371. $this->Controller->Toolbar->panels['session'] = $MockPanel;
  372. $this->Controller->Toolbar->beforeRedirect($this->Controller, '/another/url');
  373. $result = Cache::read('toolbar_history', $configName);
  374. $this->assertTrue(isset($result[0]['session']));
  375. $this->assertFalse(isset($result[0]['history']));
  376. $timers = DebugTimer::getAll();
  377. $this->assertTrue(isset($timers['controllerAction']));
  378. }
  379. /**
  380. * test that loading state (accessing cache) works.
  381. *
  382. * @return void
  383. */
  384. public function testLoadState() {
  385. $this->_loadController();
  386. $this->Controller->Toolbar->cacheKey = 'toolbar_history';
  387. $data = array(0 => array('my data'));
  388. Cache::write('toolbar_history', $data, 'debug_kit');
  389. $result = $this->Controller->Toolbar->loadState(0);
  390. $this->assertEquals($result, $data[0]);
  391. }
  392. /**
  393. * Test that history state urls set prefix = null and admin = null so generated urls do not
  394. * adopt these params.
  395. *
  396. * @return void
  397. */
  398. public function testHistoryUrlGenerationWithPrefixes() {
  399. $this->url = '/debugkit_url_with_prefixes_test';
  400. Router::connect($this->url, array(
  401. 'controller' => 'posts',
  402. 'action' => 'edit',
  403. 'admin' => 1,
  404. 'prefix' => 'admin',
  405. 'plugin' => 'cms',
  406. ));
  407. $this->_loadController();
  408. $this->Controller->Toolbar->cacheKey = 'url_test';
  409. $this->Controller->Components->trigger('startup', array($this->Controller));
  410. $this->Controller->Components->trigger('beforeRender', array($this->Controller));
  411. $result = $this->Controller->Toolbar->panels['history']->beforeRender($this->Controller);
  412. $expected = array(
  413. 'plugin' => 'debug_kit',
  414. 'controller' => 'toolbar_access',
  415. 'action' => 'history_state',
  416. 0 => 1,
  417. 'admin' => false,
  418. );
  419. $this->assertEquals($result[0]['url'], $expected);
  420. Cache::delete('url_test', 'debug_kit');
  421. }
  422. /**
  423. * Test that the FireCake toolbar is used on AJAX requests
  424. *
  425. * @return void
  426. */
  427. public function testAjaxToolbar() {
  428. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  429. $this->_loadController();
  430. $this->Controller->Components->trigger('startup', array($this->Controller));
  431. $this->Controller->Components->trigger('beforeRender', array($this->Controller));
  432. $this->assertEquals($this->Controller->helpers['DebugKit.Toolbar']['output'], 'DebugKit.FirePhpToolbar');
  433. }
  434. /**
  435. * Test that the toolbar does not interfere with requestAction
  436. *
  437. * @return void
  438. */
  439. public function testNoRequestActionInterference() {
  440. $debugKitPath = App::pluginPath('DebugKit');
  441. $noDir = (empty($debugKitPath) || !file_exists($debugKitPath));
  442. if ($noDir) {
  443. $this->markTestAsSkipped('Could not find DebugKit in plugin paths');
  444. }
  445. App::build(array(
  446. 'Controller' => $debugKitPath . 'Test' . DS . 'test_app' . DS . 'Controller' . DS,
  447. 'View' => array(
  448. $debugKitPath . 'Test' . DS . 'test_app' . DS . 'View' . DS,
  449. CAKE_CORE_INCLUDE_PATH . DS . 'Cake' . DS . 'View' . DS
  450. ),
  451. 'plugins' => $this->_paths['plugins']
  452. ));
  453. Router::reload();
  454. $this->_loadController();
  455. $result = $this->Controller->requestAction('/debug_kit_test/request_action_return', array('return'));
  456. $this->assertEquals($result, 'I am some value from requestAction.');
  457. $result = $this->Controller->requestAction('/debug_kit_test/request_action_render', array('return'));
  458. $this->assertEquals($result, 'I have been rendered.');
  459. }
  460. }