Skip to content

Commit 74c289c

Browse files
authored
Merge pull request #593 from cakephp/master-dashboard
Add basic dashboard.
2 parents 5de7f3b + 8fed506 commit 74c289c

File tree

11 files changed

+231
-60
lines changed

11 files changed

+231
-60
lines changed

config/routes.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
Router::plugin('DebugKit', ['path' => '/debug-kit'], function (RouteBuilder $routes) {
77
$routes->setExtensions('json');
88
$routes->setRouteClass(DashedRoute::class);
9+
910
$routes->connect(
1011
'/toolbar/clear-cache',
1112
['controller' => 'Toolbar', 'action' => 'clearCache']
@@ -38,4 +39,8 @@ function (RouteBuilder $routes) {
3839
$routes->connect('/sent/:panel/:id', ['action' => 'sent'], ['pass' => ['panel', 'id']]);
3940
}
4041
);
42+
43+
$routes->get('/', ['controller' => 'Dashboard', 'action' => 'index']);
44+
$routes->get('/dashboard', ['controller' => 'Dashboard', 'action' => 'index']);
45+
$routes->post('/dashboard/reset', ['controller' => 'Dashboard', 'action' => 'reset']);
4146
});
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
* @license http://www.opensource.org/licenses/mit-license.php MIT License
12+
*/
13+
namespace DebugKit\Controller;
14+
15+
use Cake\Controller\Controller;
16+
use Cake\Core\Configure;
17+
use Cake\Event\Event;
18+
use Cake\Http\Exception\NotFoundException;
19+
20+
/**
21+
* Dashboard and common DebugKit backend.
22+
*/
23+
class DashboardController extends Controller
24+
{
25+
/**
26+
* Before filter handler.
27+
*
28+
* @param \Cake\Event\Event $event The event.
29+
* @return void
30+
* @throws \Cake\Http\Exception\NotFoundException
31+
*/
32+
public function beforeFilter(Event $event)
33+
{
34+
// TODO add config override.
35+
if (!Configure::read('debug')) {
36+
throw new NotFoundException('Not available without debug mode on.');
37+
}
38+
39+
$this->viewBuilder()->setLayout('dashboard');
40+
}
41+
42+
/**
43+
* Dashboard.
44+
*
45+
* @return void
46+
* @throws \Cake\Network\Exception\NotFoundException
47+
*/
48+
public function index()
49+
{
50+
$this->loadModel('DebugKit.Requests');
51+
52+
$data = [
53+
'driver' => get_class($this->Requests->getConnection()->getDriver()),
54+
'rows' => $this->Requests->find()->count(),
55+
];
56+
57+
$this->set('connection', $data);
58+
}
59+
60+
/**
61+
* Reset SQLite DB.
62+
*
63+
* @return \Cake\Http\Response
64+
*/
65+
public function reset()
66+
{
67+
$this->request->allowMethod('post');
68+
$this->loadModel('DebugKit.Requests');
69+
70+
$this->Requests->Panels->deleteAll('1=1');
71+
$this->Requests->deleteAll('1=1');
72+
73+
return $this->redirect(['action' => 'index']);
74+
}
75+
}

src/Template/Dashboard/index.ctp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* @var \App\View\AppView $this
4+
*/
5+
?>
6+
<h1><?= __d('debug_kit', 'Debug Kit Dashboard') ?></h1>
7+
8+
<h2><?= __d('debug_kit', 'Database') ?></h2>
9+
<ul>
10+
<li><?= __d('debug_kit', 'Driver') ?>: <?= h($connection['driver']); ?></li>
11+
<?php if (isset($connection['rows'])): ?>
12+
<li><?= __d('debug_kit', 'Requests') ?>: <?= $this->Number->format($connection['rows']) ?></li>
13+
<?php endif; ?>
14+
</ul>
15+
<?php if (!empty($connection['rows'])): ?>
16+
<?= $this->Form->postLink(
17+
__d('debug_kit', 'Reset database'),
18+
['_method' => 'POST', 'action' => 'reset'],
19+
['confirm' => 'Are you sure?']
20+
); ?>
21+
<?php endif; ?>
22+
23+
<h3>Actions</h3>
24+
<ul>
25+
<li><?= $this->Html->link(__d('debug_kit', 'Mail Preview'), ['controller' => 'MailPreview']); ?></li>
26+
</ul>

src/Template/Layout/dashboard.ctp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php $this->extend('toolbar') ?>
2+
3+
<div style="height:calc(100vh);overflow-y:scroll">
4+
<?php if (empty($noHeader)) : ?>
5+
<h2 class="panel-title">
6+
<?= isset($title) ? h($title) : __d('debug_kit', 'DebugKit Dashboard') ?>
7+
</h2>
8+
<?php endif ?>
9+
10+
<div class="panel-content">
11+
<?= $this->fetch('content'); ?>
12+
</div>
13+
</div>

tests/TestCase/Controller/ComposerControllerTest.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,8 @@
1313
*/
1414
namespace DebugKit\Test\TestCase\Controller;
1515

16-
use Cake\Database\Driver\Sqlite;
17-
use Cake\Datasource\ConnectionManager;
18-
use Cake\Routing\RouteBuilder;
19-
use Cake\Routing\Router;
2016
use Cake\TestSuite\IntegrationTestCase;
17+
use DebugKit\TestApp\Application;
2118

2219
/**
2320
* Composer controller test.
@@ -32,9 +29,7 @@ class ComposerControllerTest extends IntegrationTestCase
3229
public function setUp()
3330
{
3431
parent::setUp();
35-
Router::plugin('DebugKit', function (RouteBuilder $routes) {
36-
$routes->connect('/composer/:action', ['controller' => 'Composer']);
37-
});
32+
$this->configApplication(Application::class, []);
3833
$this->useHttpServer(true);
3934
}
4035

@@ -50,7 +45,7 @@ public function testCheckDependencies()
5045
'accept' => 'application/json, text/javascript, */*; q=0.01',
5146
]
5247
]);
53-
$this->post('/debug_kit/composer/checkDependencies');
48+
$this->post('/debug-kit/composer/check-dependencies');
5449
$this->assertResponseOk();
5550
$this->assertContentType('application/json');
5651
$data = json_decode((string)$this->_response->getBody(), true);
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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 3.19.1
12+
* @license http://www.opensource.org/licenses/mit-license.php MIT License
13+
*/
14+
namespace DebugKit\Test\TestCase\Controller;
15+
16+
use Cake\ORM\TableRegistry;
17+
use Cake\TestSuite\IntegrationTestCase;
18+
use DebugKit\TestApp\Application;
19+
20+
/**
21+
* Dashboard controller test.
22+
*/
23+
class DashboardControllerTest extends IntegrationTestCase
24+
{
25+
public $fixtures = [
26+
'plugin.DebugKit.Requests',
27+
'plugin.DebugKit.Panels',
28+
];
29+
30+
/**
31+
* Setup method.
32+
*
33+
* @return void
34+
*/
35+
public function setUp()
36+
{
37+
parent::setUp();
38+
$this->configApplication(Application::class, []);
39+
$this->useHttpServer(true);
40+
}
41+
42+
public function testIndexNoRequests()
43+
{
44+
$requests = TableRegistry::get('DebugKit.Requests');
45+
$requests->Panels->deleteAll('1=1');
46+
$requests->deleteAll('1=1');
47+
48+
$this->get('/debug-kit/dashboard');
49+
50+
$this->assertResponseOk();
51+
$this->assertResponseContains('Database');
52+
$this->assertResponseNotContains('Reset database');
53+
}
54+
55+
public function testIndexWithRequests()
56+
{
57+
$requests = TableRegistry::get('DebugKit.Requests');
58+
$request = $requests->newEntity(['url' => '/example']);
59+
$requests->save($request);
60+
61+
$this->get('/debug-kit/dashboard');
62+
63+
$this->assertResponseOk();
64+
$this->assertResponseContains('Database');
65+
$this->assertResponseContains('Reset database');
66+
}
67+
68+
public function testReset()
69+
{
70+
$requests = TableRegistry::get('DebugKit.Requests');
71+
$this->assertGreaterThan(0, $requests->find()->count(), 'precondition failed');
72+
73+
$this->post('/debug-kit/dashboard/reset');
74+
75+
$this->assertRedirect('/debug-kit');
76+
$this->assertEquals(0, $requests->find()->count());
77+
}
78+
}

tests/TestCase/Controller/MailPreviewControllerTest.php

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@
1313
*/
1414
namespace DebugKit\Test\TestCase\Controller;
1515

16-
use Cake\Core\Plugin;
1716
use Cake\ORM\TableRegistry;
18-
use Cake\Routing\RouteBuilder;
1917
use Cake\Routing\Router;
2018
use Cake\TestSuite\IntegrationTestCase;
19+
use DebugKit\TestApp\Application;
2120

2221
/**
2322
* Mail preview controller test
@@ -43,22 +42,10 @@ class MailPreviewControllerTest extends IntegrationTestCase
4342
public function setUp()
4443
{
4544
parent::setUp();
46-
47-
Plugin::getCollection()->add(new \Debugkit\Plugin());
4845
Router::scope('/', function ($routes) {
4946
$routes->connect('/users/:action/*', ['controller' => 'Users']);
5047
});
51-
52-
Router::plugin('DebugKit', function (RouteBuilder $routes) {
53-
$routes->scope(
54-
'/mail_preview',
55-
['controller' => 'MailPreview'],
56-
function ($routes) {
57-
$routes->connect('/sent/*', ['action' => 'sent']);
58-
$routes->connect('/preview/*', ['action' => 'email']);
59-
}
60-
);
61-
});
48+
$this->configApplication(Application::class, []);
6249
$this->useHttpServer(true);
6350
}
6451

@@ -69,7 +56,7 @@ function ($routes) {
6956
*/
7057
public function testEmailPluginPassedToView()
7158
{
72-
$this->get('/debug_kit/mail_preview/preview/TestMailerPreview/test_email?plugin=DebugkitTestPlugin');
59+
$this->get('/debug-kit/mail-preview/preview/TestMailerPreview/test_email?plugin=DebugkitTestPlugin');
7360

7461
$this->assertResponseOk();
7562
$this->assertResponseContains('src="?part=text&plugin=DebugkitTestPlugin');
@@ -81,7 +68,7 @@ public function testEmailPluginPassedToView()
8168
*/
8269
public function testEmailPartTextContent()
8370
{
84-
$this->get('/debug_kit/mail_preview/preview/TestMailerPreview/test_email?part=text&plugin=DebugkitTestPlugin');
71+
$this->get('/debug-kit/mail-preview/preview/TestMailerPreview/test_email?part=text&plugin=DebugkitTestPlugin');
8572

8673
$this->assertResponseOk();
8774
$this->assertResponseContains('Testing email action.');
@@ -95,7 +82,7 @@ public function testEmailPartTextContent()
9582
*/
9683
public function testOnChangeJsPluginPassedToview()
9784
{
98-
$this->get('/debug_kit/mail_preview/preview/TestMailerPreview/test_email?plugin=DebugkitTestPlugin');
85+
$this->get('/debug-kit/mail-preview/preview/TestMailerPreview/test_email?plugin=DebugkitTestPlugin');
9986

10087
$this->assertResponseContains("iframe.contentWindow.location.replace('?part=' + part_name + '&plugin=DebugkitTestPlugin');");
10188
}
@@ -107,7 +94,7 @@ public function testOnChangeJsPluginPassedToview()
10794
*/
10895
public function testSentInvalidData()
10996
{
110-
$this->get('/debug_kit/mail_preview/sent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa/0');
97+
$this->get('/debug-kit/mail-preview/sent/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa/0');
11198
$this->assertResponseCode(404);
11299
}
113100

@@ -131,7 +118,7 @@ public function testSentValidData()
131118
$panel->content = serialize($data);
132119
$panels->save($panel);
133120

134-
$this->get("/debug_kit/mail_preview/sent/{$panel->id}/0");
121+
$this->get("/debug-kit/mail-preview/sent/{$panel->id}/0");
135122
$this->assertResponseCode(200);
136123
$this->assertResponseContains('[email protected]');
137124
$this->assertResponseContains('<iframe');
@@ -157,7 +144,7 @@ public function testSentValidDataRenderPart()
157144
$panel->content = serialize($data);
158145
$panels->save($panel);
159146

160-
$this->get("/debug_kit/mail_preview/sent/{$panel->id}/0?part=html");
147+
$this->get("/debug-kit/mail-preview/sent/{$panel->id}/0?part=html");
161148
$this->assertResponseCode(200);
162149
$this->assertResponseContains('<h1>Hi</h1>');
163150
}

tests/TestCase/Controller/PanelsControllerTest.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,8 @@
1313
*/
1414
namespace DebugKit\Test\TestCase\Controller;
1515

16-
use Cake\Database\Driver\Sqlite;
17-
use Cake\Datasource\ConnectionManager;
18-
use Cake\Routing\RouteBuilder;
19-
use Cake\Routing\Router;
2016
use Cake\TestSuite\IntegrationTestCase;
17+
use DebugKit\TestApp\Application;
2118

2219
/**
2320
* Panel controller test.
@@ -43,9 +40,7 @@ class PanelsControllerTest extends IntegrationTestCase
4340
public function setUp()
4441
{
4542
parent::setUp();
46-
Router::plugin('DebugKit', function (RouteBuilder $routes) {
47-
$routes->connect('/panels/:action/*', ['controller' => 'Panels']);
48-
});
43+
$this->configApplication(Application::class, []);
4944
$this->useHttpServer(true);
5045
}
5146

@@ -62,7 +57,7 @@ public function testIndex()
6257
]
6358
]);
6459

65-
$this->get('/debug_kit/panels/index/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa');
60+
$this->get('/debug-kit/panels/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa');
6661

6762
$this->assertResponseOk();
6863
$this->assertContentType('application/json');
@@ -75,7 +70,7 @@ public function testIndex()
7570
*/
7671
public function testView()
7772
{
78-
$this->get('/debug_kit/panels/view/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa');
73+
$this->get('/debug-kit/panels/view/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa');
7974

8075
$this->assertResponseOk();
8176
$this->assertResponseContains('Request</h2>');
@@ -89,7 +84,7 @@ public function testView()
8984
*/
9085
public function testViewNotExists()
9186
{
92-
$this->get('/debug_kit/panels/view/aaaaaaaa-ffff-ffff-ffff-aaaaaaaaaaaa');
87+
$this->get('/debug-kit/panels/view/aaaaaaaa-ffff-ffff-ffff-aaaaaaaaaaaa');
9388
$this->assertResponseError();
9489
$this->assertResponseContains('Error page');
9590
}

0 commit comments

Comments
 (0)