Base for a static organization website

HtmlToolbarHelperTest.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. <?php
  2. /**
  3. * Toolbar HTML Helper Test Case
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @since DebugKit 0.1
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('View', 'View');
  19. App::uses('Controller', 'Controller');
  20. App::uses('Router', 'Routing');
  21. App::uses('CakeResponse', 'Network');
  22. App::uses('ToolbarHelper', 'DebugKit.View/Helper');
  23. App::uses('HtmlToolbarHelper', 'DebugKit.View/Helper');
  24. App::uses('HtmlHelper', 'View/Helper');
  25. App::uses('FormHelper', 'View/Helper');
  26. /**
  27. * Class HtmlToolbarHelperTestCase
  28. *
  29. * @since DebugKit 0.1
  30. */
  31. class HtmlToolbarHelperTestCase extends CakeTestCase {
  32. /**
  33. * Setup Test Case
  34. */
  35. public static function setupBeforeClass() {
  36. App::build(array(
  37. 'View' => array(
  38. CAKE_CORE_INCLUDE_PATH . DS . 'Cake' . DS . 'Test' . DS . 'test_app' . DS . 'View' . DS,
  39. APP . 'Plugin' . DS . 'DebugKit' . DS . 'View' . DS,
  40. CAKE_CORE_INCLUDE_PATH . DS . 'Cake' . DS . 'View' . DS
  41. )
  42. ), true);
  43. }
  44. /**
  45. * Tear Down Test Case
  46. */
  47. public static function tearDownAfterClass() {
  48. App::build();
  49. }
  50. /**
  51. * Setup
  52. *
  53. * @return void
  54. */
  55. public function setUp() {
  56. parent::setUp();
  57. Router::connect('/:controller/:action');
  58. $request = new CakeRequest();
  59. $request->addParams(array('controller' => 'pages', 'action' => 'display'));
  60. $this->Controller = new Controller($request, new CakeResponse());
  61. $this->View = new View($this->Controller);
  62. $this->Toolbar = new ToolbarHelper($this->View, array('output' => 'DebugKit.HtmlToolbar'));
  63. $this->Toolbar->HtmlToolbar = new HtmlToolbarHelper($this->View);
  64. $this->Toolbar->HtmlToolbar->Html = new HtmlHelper($this->View);
  65. $this->Toolbar->HtmlToolbar->Form = new FormHelper($this->View);
  66. }
  67. /**
  68. * Tear Down
  69. *
  70. * @return void
  71. */
  72. public function tearDown() {
  73. parent::tearDown();
  74. unset($this->Toolbar, $this->Controller);
  75. }
  76. /**
  77. * Test makeNeatArray with basic types.
  78. *
  79. * @return void
  80. */
  81. public function testMakeNeatArrayBasic() {
  82. $in = false;
  83. $result = $this->Toolbar->makeNeatArray($in);
  84. $expected = array(
  85. 'ul' => array('class' => 'neat-array depth-0'),
  86. '<li', '<strong', '0' , '/strong', '(false)', '/li',
  87. '/ul'
  88. );
  89. $this->assertTags($result, $expected);
  90. $in = null;
  91. $result = $this->Toolbar->makeNeatArray($in);
  92. $expected = array(
  93. 'ul' => array('class' => 'neat-array depth-0'),
  94. '<li', '<strong', '0' , '/strong', '(null)', '/li',
  95. '/ul'
  96. );
  97. $this->assertTags($result, $expected);
  98. $in = true;
  99. $result = $this->Toolbar->makeNeatArray($in);
  100. $expected = array(
  101. 'ul' => array('class' => 'neat-array depth-0'),
  102. '<li', '<strong', '0' , '/strong', '(true)', '/li',
  103. '/ul'
  104. );
  105. $this->assertTags($result, $expected);
  106. $in = array();
  107. $result = $this->Toolbar->makeNeatArray($in);
  108. $expected = array(
  109. 'ul' => array('class' => 'neat-array depth-0'),
  110. '<li', '<strong', '0' , '/strong', '(empty)', '/li',
  111. '/ul'
  112. );
  113. $this->assertTags($result, $expected);
  114. }
  115. /**
  116. * Test that cyclic references can be printed.
  117. *
  118. * @return void
  119. */
  120. public function testMakeNeatArrayCyclicObjects() {
  121. $a = new StdClass;
  122. $b = new StdClass;
  123. $a->child = $b;
  124. $b->parent = $a;
  125. $in = array('obj' => $a);
  126. $result = $this->Toolbar->makeNeatArray($in);
  127. $expected = array(
  128. array('ul' => array('class' => 'neat-array depth-0')),
  129. '<li', '<strong', 'obj', '/strong', '(object)',
  130. array('ul' => array('class' => 'neat-array depth-1')),
  131. '<li', '<strong', 'child', '/strong', '(object)',
  132. array('ul' => array('class' => 'neat-array depth-2')),
  133. '<li', '<strong', 'parent', '/strong',
  134. '(object) - recursion',
  135. '/li',
  136. '/ul',
  137. '/li',
  138. '/ul',
  139. '/li',
  140. '/ul'
  141. );
  142. $this->assertTags($result, $expected);
  143. }
  144. /**
  145. * Test Neat Array formatting
  146. *
  147. * @return void
  148. */
  149. public function testMakeNeatArray() {
  150. $in = array('key' => 'value');
  151. $result = $this->Toolbar->makeNeatArray($in);
  152. $expected = array(
  153. 'ul' => array('class' => 'neat-array depth-0'),
  154. '<li', '<strong', 'key', '/strong', 'value', '/li',
  155. '/ul'
  156. );
  157. $this->assertTags($result, $expected);
  158. $in = array('key' => null);
  159. $result = $this->Toolbar->makeNeatArray($in);
  160. $expected = array(
  161. 'ul' => array('class' => 'neat-array depth-0'),
  162. '<li', '<strong', 'key', '/strong', '(null)', '/li',
  163. '/ul'
  164. );
  165. $this->assertTags($result, $expected);
  166. $in = array('key' => 'value', 'foo' => 'bar');
  167. $result = $this->Toolbar->makeNeatArray($in);
  168. $expected = array(
  169. 'ul' => array('class' => 'neat-array depth-0'),
  170. '<li', '<strong', 'key', '/strong', 'value', '/li',
  171. '<li', '<strong', 'foo', '/strong', 'bar', '/li',
  172. '/ul'
  173. );
  174. $this->assertTags($result, $expected);
  175. $in = array(
  176. 'key' => 'value',
  177. 'foo' => array(
  178. 'this' => 'deep',
  179. 'another' => 'value'
  180. )
  181. );
  182. $result = $this->Toolbar->makeNeatArray($in);
  183. $expected = array(
  184. 'ul' => array('class' => 'neat-array depth-0'),
  185. '<li', '<strong', 'key', '/strong', 'value', '/li',
  186. '<li', '<strong', 'foo', '/strong',
  187. '(array)',
  188. array('ul' => array('class' => 'neat-array depth-1')),
  189. '<li', '<strong', 'this', '/strong', 'deep', '/li',
  190. '<li', '<strong', 'another', '/strong', 'value', '/li',
  191. '/ul',
  192. '/li',
  193. '/ul'
  194. );
  195. $this->assertTags($result, $expected);
  196. $in = array(
  197. 'key' => 'value',
  198. 'foo' => array(
  199. 'this' => 'deep',
  200. 'another' => 'value'
  201. ),
  202. 'lotr' => array(
  203. 'gandalf' => 'wizard',
  204. 'bilbo' => 'hobbit'
  205. )
  206. );
  207. $result = $this->Toolbar->makeNeatArray($in, 1);
  208. $expected = array(
  209. 'ul' => array('class' => 'neat-array depth-0 expanded'),
  210. '<li', '<strong', 'key', '/strong', 'value', '/li',
  211. '<li', '<strong', 'foo', '/strong',
  212. '(array)',
  213. array('ul' => array('class' => 'neat-array depth-1')),
  214. '<li', '<strong', 'this', '/strong', 'deep', '/li',
  215. '<li', '<strong', 'another', '/strong', 'value', '/li',
  216. '/ul',
  217. '/li',
  218. '<li', '<strong', 'lotr', '/strong',
  219. '(array)',
  220. array('ul' => array('class' => 'neat-array depth-1')),
  221. '<li', '<strong', 'gandalf', '/strong', 'wizard', '/li',
  222. '<li', '<strong', 'bilbo', '/strong', 'hobbit', '/li',
  223. '/ul',
  224. '/li',
  225. '/ul'
  226. );
  227. $this->assertTags($result, $expected);
  228. $result = $this->Toolbar->makeNeatArray($in, 2);
  229. $expected = array(
  230. 'ul' => array('class' => 'neat-array depth-0 expanded'),
  231. '<li', '<strong', 'key', '/strong', 'value', '/li',
  232. '<li', '<strong', 'foo', '/strong',
  233. '(array)',
  234. array('ul' => array('class' => 'neat-array depth-1 expanded')),
  235. '<li', '<strong', 'this', '/strong', 'deep', '/li',
  236. '<li', '<strong', 'another', '/strong', 'value', '/li',
  237. '/ul',
  238. '/li',
  239. '<li', '<strong', 'lotr', '/strong',
  240. '(array)',
  241. array('ul' => array('class' => 'neat-array depth-1 expanded')),
  242. '<li', '<strong', 'gandalf', '/strong', 'wizard', '/li',
  243. '<li', '<strong', 'bilbo', '/strong', 'hobbit', '/li',
  244. '/ul',
  245. '/li',
  246. '/ul'
  247. );
  248. $this->assertTags($result, $expected);
  249. $in = array('key' => 'value', 'array' => array());
  250. $result = $this->Toolbar->makeNeatArray($in);
  251. $expected = array(
  252. 'ul' => array('class' => 'neat-array depth-0'),
  253. '<li', '<strong', 'key', '/strong', 'value', '/li',
  254. '<li', '<strong', 'array', '/strong', '(empty)', '/li',
  255. '/ul'
  256. );
  257. $this->assertTags($result, $expected);
  258. }
  259. /**
  260. * Test makeNeatArray with object inputs.
  261. *
  262. * @return void
  263. */
  264. public function testMakeNeatArrayObjects() {
  265. $in = new StdClass();
  266. $in->key = 'value';
  267. $in->nested = new StdClass();
  268. $in->nested->name = 'mark';
  269. $result = $this->Toolbar->makeNeatArray($in);
  270. $expected = array(
  271. array('ul' => array('class' => 'neat-array depth-0')),
  272. '<li', '<strong', 'key', '/strong', 'value', '/li',
  273. '<li', '<strong', 'nested', '/strong',
  274. '(object)',
  275. array('ul' => array('class' => 'neat-array depth-1')),
  276. '<li', '<strong', 'name', '/strong', 'mark', '/li',
  277. '/ul',
  278. '/li',
  279. '/ul'
  280. );
  281. $this->assertTags($result, $expected);
  282. }
  283. /**
  284. * Test injection of toolbar
  285. *
  286. * @return void
  287. */
  288. public function testInjectToolbar() {
  289. $this->Controller->viewPath = 'Posts';
  290. $request = new CakeRequest('/posts/index');
  291. $request->addParams(Router::parse($request->url));
  292. $request->addPaths(array(
  293. 'webroot' => '/',
  294. 'base' => '/',
  295. 'here' => '/posts/index',
  296. ));
  297. $this->Controller->setRequest($request);
  298. $this->Controller->helpers = array('Html', 'Js', 'Session', 'DebugKit.Toolbar');
  299. $this->Controller->layout = 'default';
  300. $this->Controller->uses = null;
  301. $this->Controller->components = array('DebugKit.Toolbar');
  302. $this->Controller->constructClasses();
  303. $this->Controller->Components->trigger('startup', array($this->Controller));
  304. $this->Controller->Components->trigger('beforeRender', array($this->Controller));
  305. $result = $this->Controller->render();
  306. $result = str_replace(array("\n", "\r"), '', $result);
  307. $this->assertPattern('#<div id\="debug-kit-toolbar">.+</div>.*</body>#', $result);
  308. }
  309. /**
  310. * test injection of javascript
  311. *
  312. * @return void
  313. */
  314. public function testJavascriptInjection() {
  315. $this->Controller->viewPath = 'Posts';
  316. $this->Controller->uses = null;
  317. $request = new CakeRequest('/posts/index');
  318. $request->addParams(Router::parse($request->url));
  319. $request->addPaths(array(
  320. 'webroot' => '/',
  321. 'base' => '/',
  322. 'here' => '/posts/index',
  323. ));
  324. $this->Controller->setRequest($request);
  325. $this->Controller->helpers = array('Js', 'Html', 'Session');
  326. $this->Controller->components = array('DebugKit.Toolbar');
  327. $this->Controller->layout = 'default';
  328. $this->Controller->constructClasses();
  329. $this->Controller->Components->trigger('startup', array($this->Controller));
  330. $this->Controller->Components->trigger('beforeRender', array($this->Controller));
  331. $result = $this->Controller->render();
  332. $result = str_replace(array("\n", "\r"), '', $result);
  333. $this->assertPattern('#<script\s*type="text/javascript"\s*src="/debug_kit/js/js_debug_toolbar.js(?:\?\d*?)?"\s*>\s?</script>#', $result);
  334. }
  335. /**
  336. * test message creation
  337. *
  338. * @return void
  339. */
  340. public function testMessage() {
  341. $result = $this->Toolbar->message('test', 'one, two');
  342. $expected = array(
  343. '<p',
  344. '<strong', 'test', '/strong',
  345. ' one, two',
  346. '/p',
  347. );
  348. $this->assertTags($result, $expected);
  349. }
  350. /**
  351. * Test Table generation
  352. *
  353. * @return void
  354. */
  355. public function testTable() {
  356. $rows = array(
  357. array(1,2),
  358. array(3,4),
  359. );
  360. $result = $this->Toolbar->table($rows);
  361. $expected = array(
  362. 'table' => array('class' => 'debug-table'),
  363. array('tr' => array('class' => 'odd')),
  364. '<td', '1', '/td',
  365. '<td', '2', '/td',
  366. '/tr',
  367. array('tr' => array('class' => 'even')),
  368. '<td', '3', '/td',
  369. '<td', '4', '/td',
  370. '/tr',
  371. '/table'
  372. );
  373. $this->assertTags($result, $expected);
  374. }
  375. /**
  376. * test starting a panel
  377. *
  378. * @return void
  379. */
  380. public function testStartPanel() {
  381. $result = $this->Toolbar->panelStart('My Panel', 'my_panel');
  382. $expected = array(
  383. 'a' => array('href' => '#my_panel'),
  384. 'My Panel',
  385. '/a'
  386. );
  387. $this->assertTags($result, $expected);
  388. }
  389. /**
  390. * test ending a panel
  391. *
  392. * @return void
  393. */
  394. public function testPanelEnd() {
  395. $result = $this->Toolbar->panelEnd();
  396. $this->assertNull($result);
  397. }
  398. /**
  399. * Test generating links for query explains.
  400. *
  401. * @return void
  402. */
  403. public function testExplainLink() {
  404. $sql = 'SELECT * FROM tasks';
  405. $result = $this->Toolbar->explainLink($sql, 'default');
  406. $expected = array(
  407. 'form' => array('action' => '/debug_kit/toolbar_access/sql_explain', 'method' => 'post',
  408. 'accept-charset' => 'utf-8', 'id'),
  409. array('div' => array('style' => 'display:none;')),
  410. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  411. '/div',
  412. array('input' => array('type' => 'hidden', 'id', 'name' => 'data[log][ds]', 'value' => 'default')),
  413. array('input' => array('type' => 'hidden', 'id', 'name' => 'data[log][sql]', 'value' => $sql)),
  414. array('input' => array('type' => 'hidden', 'id', 'name' => 'data[log][hash]', 'value')),
  415. array('input' => array('class' => 'sql-explain-link', 'type' => 'submit', 'value' => 'Explain')),
  416. '/form',
  417. );
  418. $this->assertTags($result, $expected);
  419. }
  420. }