Skip to content

Commit 92cbfa9

Browse files
authored
Merge pull request #967 from cakephp/4.x
merge 4.x => 4.next
2 parents 4779a0a + 4ffcdd2 commit 92cbfa9

File tree

10 files changed

+92
-34
lines changed

10 files changed

+92
-34
lines changed

.github/workflows/ci.yml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ on:
77
pull_request:
88
branches:
99
- '*'
10+
workflow_dispatch:
1011

1112
permissions:
1213
contents: read
@@ -21,6 +22,8 @@ jobs:
2122
db-type: [mysql, pgsql, sqlite]
2223
prefer-lowest: ['']
2324
include:
25+
- php-version: '8.2'
26+
db-type: 'sqlite'
2427
- php-version: '8.1'
2528
db-type: 'sqlite'
2629
- php-version: '7.4'
@@ -36,7 +39,7 @@ jobs:
3639
POSTGRES_PASSWORD: postgres
3740

3841
steps:
39-
- uses: actions/checkout@v3
42+
- uses: actions/checkout@v4
4043

4144
- name: Setup Service
4245
if: matrix.db-type == 'mysql'
@@ -69,8 +72,6 @@ jobs:
6972
run: |
7073
if ${{ matrix.prefer-lowest == 'prefer-lowest' }}; then
7174
composer update --prefer-lowest --prefer-stable
72-
elif ${{ matrix.php-version == '8.1' }}; then
73-
composer update --ignore-platform-reqs
7475
else
7576
composer update
7677
fi
@@ -86,22 +87,22 @@ jobs:
8687
if [[ ${{ matrix.db-type }} == 'pgsql' ]]; then
8788
export DB_URL='postgres://postgres:[email protected]/postgres'
8889
fi
89-
if [[ ${{ matrix.php-version }} == '7.4' && ${{ matrix.db-type }} == 'mysql' ]]; then
90+
if [[ ${{ matrix.php-version }} == '8.1' && ${{ matrix.db-type }} == 'mysql' ]]; then
9091
vendor/bin/phpunit --coverage-clover=coverage.xml
9192
else
9293
vendor/bin/phpunit
9394
fi
9495
9596
- name: Code Coverage Report
96-
if: success() && matrix.php-version == '7.4' && matrix.db-type == 'mysql'
97+
if: success() && matrix.php-version == '8.1' && matrix.db-type == 'mysql'
9798
uses: codecov/codecov-action@v3
9899

99100
cs-stan:
100101
name: Coding Standard & Static Analysis
101102
runs-on: ubuntu-22.04
102103

103104
steps:
104-
- uses: actions/checkout@v3
105+
- uses: actions/checkout@v4
105106

106107
- name: Setup PHP
107108
uses: shivammathur/setup-php@v2

.github/workflows/deploy_docs_4x.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ on:
55
push:
66
branches:
77
- 4.x
8+
workflow_dispatch:
89

910
jobs:
1011
deploy:
1112
runs-on: ubuntu-latest
1213
steps:
1314
- name: Cloning repo
14-
uses: actions/checkout@v3
15+
uses: actions/checkout@v4
1516
with:
1617
fetch-depth: 0
1718

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@ $this->addPlugin('DebugKit');
3838

3939
If you don't see a CakePHP icon on the bottom right of your page DebugKit is not be
4040
working correctly. Some common problems are:
41-
41+
4242
1. Your PHP environment doesn't have SQLite installed. Check your application
4343
logs to confirm if this happening. You can either configure DebugKit to use
4444
a different database, or install the PDO SQLite 3 extension.
4545
2. Your hostname needs to be added to the `DebugKit.safeTld`. If your local
4646
domain isn't a known development environment name, DebugKit will disable
4747
itself to protect a potentially non-development environment.
48+
3. If you are using the [Authorization Plugin](https://github.com/cakephp/authorization)
49+
you need to set `DebugKit.ignoreAuthorization` to `true` in your config.
4850

4951
## Reporting Issues
5052

@@ -61,5 +63,5 @@ requests](https://help.github.com/articles/using-pull-requests) or open
6163

6264
## Documentation
6365

64-
Documentation for DebugKit can be found in the
66+
Documentation for DebugKit can be found in the
6567
[CakePHP documentation](https://book.cakephp.org/debugkit/4/en/index.html).

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "cakephp/debug_kit",
33
"description": "CakePHP Debug Kit",
44
"type": "cakephp-plugin",
5-
"keywords": ["cakephp", "debug", "kit"],
5+
"keywords": ["cakephp", "debug", "kit", "dev"],
66
"homepage": "https://github.com/cakephp/debug_kit",
77
"license": "MIT",
88
"authors": [

docs/en/index.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ Configuration
5050
// Before loading DebugKit
5151
Configure::write('DebugKit.forceEnable', true);
5252

53+
You can also provide a callable::
54+
55+
Configure::write('DebugKit.forceEnable', function() {
56+
return $_SERVER['REMOTE_ADDR'] === '192.168.2.182';
57+
});
58+
5359
* ``DebugKit.ignorePathsPattern`` - Regex pattern (including delimiter) to ignore paths.
5460
DebugKit won't save data for request URLs that match this regex. Defaults to ``null``::
5561

src/Panel/CachePanel.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,17 @@ public function initialize()
5555
if (isset($config['className']) && $config['className'] instanceof DebugEngine) {
5656
$instance = $config['className'];
5757
} elseif (isset($config['className'])) {
58-
Cache::drop($name);
59-
$instance = new DebugEngine($config, $name, $this->logger);
58+
/** @var \Cake\Cache\CacheEngine $engine */
59+
$engine = Cache::pool($name);
60+
// Unload from the cache registry so that subsequence calls to
61+
// Cache::pool($name) use the new config with DebugEngine instance set below.
62+
Cache::getRegistry()->unload($name);
63+
64+
$instance = new DebugEngine($engine, $name, $this->logger);
65+
$instance->init();
6066
$config['className'] = $instance;
67+
68+
Cache::drop($name);
6169
Cache::setConfig($name, $config);
6270
}
6371
if (isset($instance)) {

src/ToolbarService.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,13 +357,21 @@ public function injectScripts($row, ResponseInterface $response)
357357
if ($pos === false) {
358358
return $response;
359359
}
360+
// Use Router to get the request so that we can see the
361+
// state after other middleware have been applied.
362+
$request = Router::getRequest();
363+
$nonce = '';
364+
if ($request && $request->getAttribute('cspScriptNonce')) {
365+
$nonce = sprintf(' nonce="%s"', $request->getAttribute('cspScriptNonce'));
366+
}
360367

361368
$url = Router::url('/', true);
362369
$script = sprintf(
363-
'<script id="__debug_kit_script" data-id="%s" data-url="%s" type="module" src="%s"></script>',
370+
'<script id="__debug_kit_script" data-id="%s" data-url="%s" type="module" src="%s"%s></script>',
364371
$row->id,
365372
$url,
366-
Router::url($this->getToolbarUrl())
373+
Router::url($this->getToolbarUrl()),
374+
$nonce
367375
);
368376
$contents = substr($contents, 0, $pos) . $script . substr($contents, $pos);
369377
$body->rewind();

templates/MailPreview/index.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,13 @@ class UserMailer extends Mailer
4747
{
4848
public function welcome($user)
4949
{
50-
return $this // Returning the chain is a good idea :)
51-
->to($user->email)
52-
->subject(sprintf("Welcome %s", $user->name))
53-
->template("welcome_mail") // By default template with same name as method name is used.
54-
->layout("custom")
55-
->set(["user" => $user]);
50+
$mailer = $this->setTo($user->email)
51+
->setSubject(sprintf("Welcome %s", $user->name))
52+
->setViewVars(["user" => $user]);
53+
$mailer->viewBuilder()
54+
->setTemplate("welcome_mail") // By default template with same name as method name is used.
55+
->setLayout("custom");
56+
return $mailer;
5657
}
5758
}';
5859
highlight_string($code);
@@ -75,9 +76,10 @@ public function welcome()
7576
{
7677
$this->loadModel("Users");
7778
$user = $this->Users->find()->first();
79+
7880
return $this->getMailer("User")
7981
->welcome($user)
80-
->set(["activationToken" => "dummy-token"]);
82+
->setViewVars(["activationToken" => "dummy-token"]);
8183
}
8284
}';
8385
highlight_string($code);

tests/TestCase/Middleware/DebugKitMiddlewareTest.php

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Cake\Http\CallbackStream;
2222
use Cake\Http\Response;
2323
use Cake\Http\ServerRequest;
24+
use Cake\Routing\Router;
2425
use Cake\TestSuite\TestCase;
2526
use DebugKit\Middleware\DebugKitMiddleware;
2627
use Psr\Http\Server\RequestHandlerInterface;
@@ -50,7 +51,7 @@ public function setUp(): void
5051
parent::setUp();
5152

5253
$connection = ConnectionManager::get('test');
53-
$this->skipIf($connection->getDriver() instanceof Sqlite, 'Schema insertion/removal breaks SQLite');
54+
$this->skipIf($connection->getDriver() instanceof Sqlite, 'This test fails in CI with sqlite');
5455
$this->oldConfig = Configure::read('DebugKit');
5556
$this->restore = $GLOBALS['__PHPUNIT_BOOTSTRAP'];
5657
unset($GLOBALS['__PHPUNIT_BOOTSTRAP']);
@@ -122,7 +123,7 @@ public function testInvokeSaveData()
122123
$this->assertNotNull($result->panels[11]->summary);
123124
$this->assertSame('Sql Log', $result->panels[11]->title);
124125

125-
$timeStamp = filemtime(Plugin::path('DebugKit') . 'webroot' . DS . 'js' . DS . 'main.js');
126+
$timeStamp = filemtime(Plugin::path('DebugKit') . 'webroot' . DS . 'js' . DS . 'inject-iframe.js');
126127

127128
$expected = '<html><title>test</title><body><p>some text</p>' .
128129
'<script id="__debug_kit_script" data-id="' . $result->id . '" ' .
@@ -132,6 +133,39 @@ public function testInvokeSaveData()
132133
$this->assertTextEquals($expected, $body);
133134
}
134135

136+
/**
137+
* Ensure data is saved for HTML requests
138+
*
139+
* @return void
140+
*/
141+
public function testInvokeInjectCspNonce()
142+
{
143+
$request = new ServerRequest([
144+
'url' => '/articles',
145+
'environment' => ['REQUEST_METHOD' => 'GET'],
146+
]);
147+
$request = $request->withAttribute('cspScriptNonce', 'csp-nonce');
148+
Router::setRequest($request);
149+
150+
$response = new Response([
151+
'statusCode' => 200,
152+
'type' => 'text/html',
153+
'body' => '<html><title>test</title><body><p>some text</p></body>',
154+
]);
155+
156+
$handler = $this->handler();
157+
$handler->expects($this->once())
158+
->method('handle')
159+
->willReturn($response);
160+
161+
$middleware = new DebugKitMiddleware();
162+
$response = $middleware->process($request, $handler);
163+
$this->assertInstanceOf(Response::class, $response, 'Should return the response');
164+
165+
$body = (string)$response->getBody();
166+
$this->assertStringContainsString('nonce="csp-nonce"', $body);
167+
}
168+
135169
/**
136170
* Ensure that streaming results are tracked, but not modified.
137171
*

tests/TestCase/ToolbarServiceTest.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Cake\Http\Response;
2323
use Cake\Http\ServerRequest as Request;
2424
use Cake\Log\Log;
25+
use Cake\Routing\Router;
2526
use Cake\TestSuite\TestCase;
2627
use DebugKit\Model\Entity\Request as RequestEntity;
2728
use DebugKit\ToolbarService;
@@ -294,6 +295,7 @@ public function testInjectScriptsLastBodyTag()
294295
'url' => '/articles',
295296
'environment' => ['REQUEST_METHOD' => 'GET'],
296297
]);
298+
Router::setRequest($request);
297299
$response = new Response([
298300
'statusCode' => 200,
299301
'type' => 'text/html',
@@ -305,7 +307,7 @@ public function testInjectScriptsLastBodyTag()
305307
$row = $bar->saveData($request, $response);
306308
$response = $bar->injectScripts($row, $response);
307309

308-
$timeStamp = filemtime(Plugin::path('DebugKit') . 'webroot' . DS . 'js' . DS . 'main.js');
310+
$timeStamp = filemtime(Plugin::path('DebugKit') . 'webroot' . DS . 'js' . DS . 'inject-iframe.js');
309311

310312
$expected = '<html><title>test</title><body><p>some text</p>' .
311313
'<script id="__debug_kit_script" data-id="' . $row->id . '" ' .
@@ -322,10 +324,6 @@ public function testInjectScriptsLastBodyTag()
322324
*/
323325
public function testInjectScriptsFileBodies()
324326
{
325-
$request = new Request([
326-
'url' => '/articles',
327-
'params' => ['plugin' => null],
328-
]);
329327
$response = new Response([
330328
'statusCode' => 200,
331329
'type' => 'text/html',
@@ -348,10 +346,6 @@ public function testInjectScriptsFileBodies()
348346
*/
349347
public function testInjectScriptsStreamBodies()
350348
{
351-
$request = new Request([
352-
'url' => '/articles',
353-
'params' => ['plugin' => null],
354-
]);
355349
$response = new Response([
356350
'statusCode' => 200,
357351
'type' => 'text/html',
@@ -373,8 +367,10 @@ public function testInjectScriptsStreamBodies()
373367
*/
374368
public function testInjectScriptsNoModifyResponse()
375369
{
376-
$request = new Request(['url' => '/articles']);
377-
370+
$request = new Request([
371+
'url' => '/articles/view/123',
372+
'params' => [],
373+
]);
378374
$response = new Response([
379375
'statusCode' => 200,
380376
'type' => 'application/json',

0 commit comments

Comments
 (0)