-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EZP-31825: Fixed crashes when ezcontentquery field parameters are inv…
…alid (#48)
- Loading branch information
Showing
5 changed files
with
142 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) eZ Systems AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
namespace spec\EzSystems\EzPlatformQueryFieldType\API; | ||
|
||
use eZ\Publish\Core\Repository\Values\Content\Content; | ||
use EzSystems\EzPlatformQueryFieldType\API\ExceptionSafeQueryFieldService; | ||
use EzSystems\EzPlatformQueryFieldType\API\QueryFieldServiceInterface; | ||
use PhpSpec\ObjectBehavior; | ||
use Prophecy\Argument; | ||
|
||
class ExceptionSafeQueryFieldServiceSpec extends ObjectBehavior | ||
{ | ||
function let(QueryFieldServiceInterface $queryFieldService) | ||
{ | ||
$arguments = [ | ||
Argument::type(Content::class), | ||
Argument::type('string'), | ||
]; | ||
$queryFieldService->countContentItems(...$arguments)->willThrow('Exception'); | ||
$queryFieldService->loadContentItems(...$arguments)->willThrow('Exception'); | ||
|
||
$arguments[] = Argument::type('int'); | ||
$arguments[] = Argument::type('int'); | ||
$queryFieldService->loadContentItemsSlice(...$arguments)->willThrow('Exception'); | ||
|
||
$this->beConstructedWith($queryFieldService); | ||
} | ||
|
||
function it_is_initializable() | ||
{ | ||
$this->shouldHaveType(ExceptionSafeQueryFieldService::class); | ||
} | ||
|
||
function it_should_return_empty_results_on_count_content_items() | ||
{ | ||
$result = $this->countContentItems(new Content([]), 'any'); | ||
$result->shouldBe(0); | ||
} | ||
|
||
function it_should_return_empty_results_on_load_content_items() | ||
{ | ||
$result = $this->loadContentItems(new Content([]), 'any'); | ||
$result->shouldBe([]); | ||
} | ||
|
||
function it_should_return_empty_results_on_load_content_items_slice() | ||
{ | ||
$result = $this->loadContentItemsSlice(new Content([]), 'any', 0, 5); | ||
$result->shouldBe([]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) eZ Systems AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
namespace EzSystems\EzPlatformQueryFieldType\API; | ||
|
||
use eZ\Publish\API\Repository\Values\Content\Content; | ||
use Psr\Log\LoggerAwareInterface; | ||
use Psr\Log\LoggerAwareTrait; | ||
use Psr\Log\LoggerInterface; | ||
use Psr\Log\NullLogger; | ||
|
||
/** | ||
* Silences exceptions when they occur in query field service, for example due to field type misconfigurations. | ||
*/ | ||
final class ExceptionSafeQueryFieldService implements QueryFieldServiceInterface, LoggerAwareInterface | ||
{ | ||
use LoggerAwareTrait; | ||
|
||
/** @var QueryFieldServiceInterface */ | ||
private $inner; | ||
|
||
public function __construct(QueryFieldServiceInterface $inner, ?LoggerInterface $logger = null) | ||
{ | ||
$this->inner = $inner; | ||
$this->logger = $logger ?: new NullLogger(); | ||
} | ||
|
||
public function loadContentItems(Content $content, string $fieldDefinitionIdentifier): iterable | ||
{ | ||
try { | ||
return $this->inner->loadContentItems($content, $fieldDefinitionIdentifier); | ||
} catch (\Throwable $e) { | ||
$this->logger->error($e->getMessage(), [ | ||
'exception' => $e, | ||
]); | ||
|
||
return []; | ||
} | ||
} | ||
|
||
public function countContentItems(Content $content, string $fieldDefinitionIdentifier): int | ||
{ | ||
try { | ||
return $this->inner->countContentItems($content, $fieldDefinitionIdentifier); | ||
} catch (\Throwable $e) { | ||
$this->logger->error($e->getMessage(), [ | ||
'exception' => $e, | ||
]); | ||
|
||
return 0; | ||
} | ||
} | ||
|
||
public function loadContentItemsSlice(Content $content, string $fieldDefinitionIdentifier, int $offset, int $limit): iterable | ||
{ | ||
try { | ||
return $this->inner->loadContentItemsSlice($content, $fieldDefinitionIdentifier, $offset, $limit); | ||
} catch (\Throwable $e) { | ||
$this->logger->error($e->getMessage(), [ | ||
'exception' => $e, | ||
]); | ||
|
||
return []; | ||
} | ||
} | ||
|
||
public function getPaginationConfiguration(Content $content, string $fieldDefinitionIdentifier): int | ||
{ | ||
return $this->inner->getPaginationConfiguration($content, $fieldDefinitionIdentifier); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
services: | ||
_defaults: | ||
autoconfigure: true | ||
autowire: true | ||
public: true | ||
|
||
EzSystems\EzPlatformQueryFieldType\API\ExceptionSafeQueryFieldService: | ||
decorates: 'EzSystems\EzPlatformQueryFieldType\API\QueryFieldServiceInterface' |