From c50751cf5097baa725e2cb7f3795feaf1e09eae6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edi=20Modri=C4=87?= Date: Wed, 6 Sep 2023 11:30:43 +0200 Subject: [PATCH] Immediately return null when null is provided in content and location providers --- .../ValueObjectProvider/ContentProvider.php | 4 ++++ .../ValueObjectProvider/LocationProvider.php | 4 ++++ .../ValueObjectProvider/ContentProviderTest.php | 12 ++++++++++++ .../ValueObjectProvider/LocationProviderTest.php | 12 ++++++++++++ 4 files changed, 32 insertions(+) diff --git a/lib/Parameters/ValueObjectProvider/ContentProvider.php b/lib/Parameters/ValueObjectProvider/ContentProvider.php index 7d71bcc8..4f52407d 100644 --- a/lib/Parameters/ValueObjectProvider/ContentProvider.php +++ b/lib/Parameters/ValueObjectProvider/ContentProvider.php @@ -24,6 +24,10 @@ public function __construct(Repository $repository, ErrorHandlerInterface $error public function getValueObject($value): ?Content { + if ($value === null) { + return null; + } + try { /** @var \eZ\Publish\API\Repository\Values\Content\Content $content */ $content = $this->repository->sudo( diff --git a/lib/Parameters/ValueObjectProvider/LocationProvider.php b/lib/Parameters/ValueObjectProvider/LocationProvider.php index 078e0abd..4b2f4b84 100644 --- a/lib/Parameters/ValueObjectProvider/LocationProvider.php +++ b/lib/Parameters/ValueObjectProvider/LocationProvider.php @@ -24,6 +24,10 @@ public function __construct(Repository $repository, ErrorHandlerInterface $error public function getValueObject($value): ?Location { + if ($value === null) { + return null; + } + try { return $this->repository->sudo( static fn (Repository $repository): Location => $repository->getLocationService()->loadLocation((int) $value), diff --git a/tests/lib/Parameters/ValueObjectProvider/ContentProviderTest.php b/tests/lib/Parameters/ValueObjectProvider/ContentProviderTest.php index 398cfa35..5c4a0380 100644 --- a/tests/lib/Parameters/ValueObjectProvider/ContentProviderTest.php +++ b/tests/lib/Parameters/ValueObjectProvider/ContentProviderTest.php @@ -69,6 +69,18 @@ public function testGetValueObject(): void self::assertSame($content, $this->valueObjectProvider->getValueObject(42)); } + /** + * @covers \Netgen\Layouts\Ez\Parameters\ValueObjectProvider\ContentProvider::getValueObject + */ + public function testGetValueObjectWithNullValue(): void + { + $this->contentServiceMock + ->expects(self::never()) + ->method('loadContent'); + + self::assertNull($this->valueObjectProvider->getValueObject(null)); + } + /** * @covers \Netgen\Layouts\Ez\Parameters\ValueObjectProvider\ContentProvider::getValueObject */ diff --git a/tests/lib/Parameters/ValueObjectProvider/LocationProviderTest.php b/tests/lib/Parameters/ValueObjectProvider/LocationProviderTest.php index 6b3909b6..a7492338 100644 --- a/tests/lib/Parameters/ValueObjectProvider/LocationProviderTest.php +++ b/tests/lib/Parameters/ValueObjectProvider/LocationProviderTest.php @@ -59,6 +59,18 @@ public function testGetValueObject(): void self::assertSame($location, $this->valueObjectProvider->getValueObject(42)); } + /** + * @covers \Netgen\Layouts\Ez\Parameters\ValueObjectProvider\LocationProvider::getValueObject + */ + public function testGetValueObjectWithNullValue(): void + { + $this->locationServiceMock + ->expects(self::never()) + ->method('loadLocation'); + + self::assertNull($this->valueObjectProvider->getValueObject(null)); + } + /** * @covers \Netgen\Layouts\Ez\Parameters\ValueObjectProvider\LocationProvider::getValueObject */