Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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