Skip to content

Commit c6590fc

Browse files
authored
Merge pull request #899 from cakephp/fix-request-params
Fix request panel data saving with authentication
2 parents ebbbf09 + 1132740 commit c6590fc

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

src/Panel/RequestPanel.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
use Cake\Event\EventInterface;
1818
use DebugKit\DebugPanel;
19+
use Exception;
1920

2021
/**
2122
* Provides debug information on the Current request params.
@@ -33,8 +34,19 @@ public function shutdown(EventInterface $event)
3334
/** @var \Cake\Controller\Controller $controller */
3435
$controller = $event->getSubject();
3536
$request = $controller->getRequest();
37+
38+
$attributes = [];
39+
foreach ($request->getAttributes() as $attr => $value) {
40+
try {
41+
serialize($value);
42+
} catch (Exception $e) {
43+
$value = "Could not serialize `{$attr}`. It failed with {$e->getMessage()}";
44+
}
45+
$attributes[$attr] = $value;
46+
}
47+
3648
$this->_data = [
37-
'attributes' => $request->getAttributes(),
49+
'attributes' => $attributes,
3850
'query' => $request->getQueryParams(),
3951
'data' => $request->getData(),
4052
'cookie' => $request->getCookieParams(),
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
declare(strict_types=1);
3+
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+
* @license http://www.opensource.org/licenses/mit-license.php MIT License
14+
**/
15+
namespace DebugKit\Test\TestCase\Panel;
16+
17+
use Cake\Controller\Controller;
18+
use Cake\Event\Event;
19+
use Cake\Http\ServerRequest;
20+
use Cake\TestSuite\TestCase;
21+
use DebugKit\Panel\RequestPanel;
22+
23+
/**
24+
* Class RequestPanelTest
25+
*/
26+
class RequestPanelTest extends TestCase
27+
{
28+
/**
29+
* @var RequestPanel
30+
*/
31+
protected $panel;
32+
33+
/**
34+
* set up
35+
*
36+
* @return void
37+
*/
38+
public function setUp(): void
39+
{
40+
parent::setUp();
41+
$this->panel = new RequestPanel();
42+
}
43+
44+
/**
45+
* Test that shutdown will skip unserializable attributes.
46+
*
47+
* @return void
48+
*/
49+
public function testShutdownSkipAttributes()
50+
{
51+
$request = new ServerRequest([
52+
'url' => '/',
53+
'post' => ['name' => 'bob'],
54+
'query' => ['page' => 1],
55+
]);
56+
$request = $request
57+
->withAttribute('ok', 'string')
58+
->withAttribute('closure', function () {
59+
});
60+
61+
$controller = new Controller($request);
62+
$event = new Event('Controller.shutdown', $controller);
63+
$this->panel->shutdown($event);
64+
65+
$data = $this->panel->data();
66+
$this->assertArrayHasKey('attributes', $data);
67+
$this->assertEquals('string', $data['attributes']['ok']);
68+
$this->assertStringContainsString('Could not serialize `closure`', $data['attributes']['closure']);
69+
}
70+
}

0 commit comments

Comments
 (0)