Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable26] fix(initial-state): Log an error when initial-state can not be JSON e… #37576

Merged
merged 1 commit into from
Apr 4, 2023
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
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