Skip to content

Commit

Permalink
Merge pull request #899 from cakephp/fix-request-params
Browse files Browse the repository at this point in the history
Fix request panel data saving with authentication
  • Loading branch information
LordSimal authored Nov 26, 2022
2 parents ebbbf09 + 1132740 commit c6590fc
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/Panel/RequestPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use Cake\Event\EventInterface;
use DebugKit\DebugPanel;
use Exception;

/**
* Provides debug information on the Current request params.
Expand All @@ -33,8 +34,19 @@ public function shutdown(EventInterface $event)
/** @var \Cake\Controller\Controller $controller */
$controller = $event->getSubject();
$request = $controller->getRequest();

$attributes = [];
foreach ($request->getAttributes() as $attr => $value) {
try {
serialize($value);
} catch (Exception $e) {
$value = "Could not serialize `{$attr}`. It failed with {$e->getMessage()}";
}
$attributes[$attr] = $value;
}

$this->_data = [
'attributes' => $request->getAttributes(),
'attributes' => $attributes,
'query' => $request->getQueryParams(),
'data' => $request->getData(),
'cookie' => $request->getCookieParams(),
Expand Down
70 changes: 70 additions & 0 deletions tests/TestCase/Panel/RequestPanelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
declare(strict_types=1);

/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @license http://www.opensource.org/licenses/mit-license.php MIT License
**/
namespace DebugKit\Test\TestCase\Panel;

use Cake\Controller\Controller;
use Cake\Event\Event;
use Cake\Http\ServerRequest;
use Cake\TestSuite\TestCase;
use DebugKit\Panel\RequestPanel;

/**
* Class RequestPanelTest
*/
class RequestPanelTest extends TestCase
{
/**
* @var RequestPanel
*/
protected $panel;

/**
* set up
*
* @return void
*/
public function setUp(): void
{
parent::setUp();
$this->panel = new RequestPanel();
}

/**
* Test that shutdown will skip unserializable attributes.
*
* @return void
*/
public function testShutdownSkipAttributes()
{
$request = new ServerRequest([
'url' => '/',
'post' => ['name' => 'bob'],
'query' => ['page' => 1],
]);
$request = $request
->withAttribute('ok', 'string')
->withAttribute('closure', function () {
});

$controller = new Controller($request);
$event = new Event('Controller.shutdown', $controller);
$this->panel->shutdown($event);

$data = $this->panel->data();
$this->assertArrayHasKey('attributes', $data);
$this->assertEquals('string', $data['attributes']['ok']);
$this->assertStringContainsString('Could not serialize `closure`', $data['attributes']['closure']);
}
}

0 comments on commit c6590fc

Please sign in to comment.