Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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