diff --git a/src/DebugBar/DebugBar.php b/src/DebugBar/DebugBar.php index 42ba2e7..a8ecb79 100644 --- a/src/DebugBar/DebugBar.php +++ b/src/DebugBar/DebugBar.php @@ -490,7 +490,7 @@ class DebugBar implements ArrayAccess */ public static function hasDataHasher() : bool { - return static::$dataHasher !== null; + return isset(static::$dataHasher); } /** @@ -500,7 +500,7 @@ class DebugBar implements ArrayAccess */ public static function getDataHasher() : DataHasher { - if (static::$dataHasher === null) { + if (!isset(static::$dataHasher)) { throw new DebugBarException('DataHasher is not set'); } return static::$dataHasher; diff --git a/src/DebugBar/OpenHandler.php b/src/DebugBar/OpenHandler.php index 5b48d5a..4347dab 100644 --- a/src/DebugBar/OpenHandler.php +++ b/src/DebugBar/OpenHandler.php @@ -129,7 +129,7 @@ class OpenHandler protected function execute($request) { if (!isset($request['collector']) || !isset($request['action']) || !isset($request['signature'])) { - throw new DebugBarException("Missing 'collector' and/or 'action' parameter in 'execute' operation"); + throw new DebugBarException("Missing 'collector', 'action' and/or 'signature' parameter in 'execute' operation"); } if (!DebugBar::hasDataHasher()) { diff --git a/tests/DebugBar/Tests/DataCollector/MockActionCollector.php b/tests/DebugBar/Tests/DataCollector/MockActionCollector.php new file mode 100644 index 0000000..0866941 --- /dev/null +++ b/tests/DebugBar/Tests/DataCollector/MockActionCollector.php @@ -0,0 +1,14 @@ + 'done']; + } +} diff --git a/tests/DebugBar/Tests/OpenHandlerTest.php b/tests/DebugBar/Tests/OpenHandlerTest.php index cb0ea3b..a105012 100644 --- a/tests/DebugBar/Tests/OpenHandlerTest.php +++ b/tests/DebugBar/Tests/OpenHandlerTest.php @@ -2,9 +2,12 @@ namespace DebugBar\Tests; +use DebugBar\DataHasher; use DebugBar\DebugBar; use DebugBar\DebugBarException; use DebugBar\OpenHandler; +use DebugBar\Tests\DataCollector\MockActionCollector; +use DebugBar\Tests\DataCollector\MockCollector; use DebugBar\Tests\Storage\MockStorage; class OpenHandlerTest extends DebugBarTestCase @@ -47,4 +50,40 @@ class OpenHandlerTest extends DebugBarTestCase $result = $this->openHandler->handle(array('op' => 'clear'), false, false); $this->assertJsonPropertyEquals($result, 'success', true); } + + public function testExecute() + { + $this->debugbar->addCollector(new MockActionCollector([], 'mock-action')); + $dataHasher = new DataHasher('secret'); + DebugBar::setDataHasher($dataHasher); + + $data = [ + 'op' => 'execute', + 'collector' => 'mock-action', + 'action' => 'run', + ]; + + $data['signature'] = $dataHasher->sign($data); + + $result = $this->openHandler->handle($data, false, false); + $this->assertJsonPropertyEquals($result, 'result', 'done'); + } + + public function testExecuteWrongSignature() + { + $this->debugbar->addCollector(new MockActionCollector([], 'mock-action')); + $dataHasher = new DataHasher('secret'); + DebugBar::setDataHasher($dataHasher); + + $data = [ + 'op' => 'execute', + 'collector' => 'mock-action', + 'action' => 'run', + 'signature' => 'invalid', + ]; + + $this->expectExceptionMessage("Signature does not match in 'execute' operation"); + + $this->openHandler->handle($data, false, false); + } }