Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix(initial-state): Log an error when initial-state can not be JSON e…
…ncoded

Signed-off-by: Joas Schilling <[email protected]>
  • Loading branch information
nickvergessen authored and backportbot-nextcloud[bot] committed Apr 4, 2023
commit cbec7528ded3a3c830b098aaebfcb0fd84846247
6 changes: 5 additions & 1 deletion lib/private/InitialStateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ public function provideInitialState(string $appName, string $key, $data): void {
if (!isset($this->states[$appName])) {
$this->states[$appName] = [];
}
$this->states[$appName][$key] = json_encode($data);
try {
$this->states[$appName][$key] = json_encode($data, JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
$this->logger->error('Invalid '. $key . ' data provided to provideInitialState by ' . $appName, ['exception' => $e]);
}
return;
}

Expand Down
31 changes: 26 additions & 5 deletions tests/lib/InitialStateServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

use OC\AppFramework\Bootstrap\Coordinator;
use OCP\IServerContainer;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use function json_encode;
use JsonSerializable;
Expand All @@ -36,18 +37,22 @@
class InitialStateServiceTest extends TestCase {
/** @var InitialStateService */
private $service;
/** @var MockObject|LoggerInterface|(LoggerInterface&MockObject) */
protected $logger;

protected function setUp(): void {
parent::setUp();

$this->logger = $this->createMock(LoggerInterface::class);

$this->service = new InitialStateService(
$this->createMock(LoggerInterface::class),
$this->logger,
$this->createMock(Coordinator::class),
$this->createMock(IServerContainer::class)
);
}

public function staticData() {
public function staticData(): array {
return [
['string'],
[23],
Expand All @@ -63,7 +68,7 @@ public function jsonSerialize(): int {
/**
* @dataProvider staticData
*/
public function testStaticData($value) {
public function testStaticData(mixed $value): void {
$this->service->provideInitialState('test', 'key', $value);
$data = $this->service->getInitialStates();

Expand All @@ -73,7 +78,23 @@ public function testStaticData($value) {
);
}

public function testStaticButInvalidData() {
public function testValidDataButFailsToJSONEncode(): void {
$this->logger->expects($this->once())
->method('error');

$this->service->provideInitialState('test', 'key', ['upload' => INF]);
$data = $this->service->getInitialStates();

$this->assertEquals(
[],
$data
);
}

public function testStaticButInvalidData(): void {
$this->logger->expects($this->once())
->method('warning');

$this->service->provideInitialState('test', 'key', new stdClass());
$data = $this->service->getInitialStates();

Expand All @@ -86,7 +107,7 @@ public function testStaticButInvalidData() {
/**
* @dataProvider staticData
*/
public function testLazyData($value) {
public function testLazyData(mixed $value): void {
$this->service->provideLazyInitialState('test', 'key', function () use ($value) {
return $value;
});
Expand Down