From 7eec749c98698e150dc9c6d22a4b534045a3c553 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20W=C3=B3js?= Date: Mon, 18 Dec 2017 15:06:05 +0100 Subject: [PATCH] EZP-28544: Added proper NotFoundException handling in limitation value mappers (#196) --- bundle/Resources/config/role.yml | 14 +++++++ .../Mapper/ContentTypeLimitationMapper.php | 12 +++++- lib/Limitation/Mapper/LanguageLimitationMapper.php | 12 +++++- .../Mapper/ObjectStateLimitationMapper.php | 16 +++++-- lib/Limitation/Mapper/SectionLimitationMapper.php | 12 +++++- .../Mapper/ContentTypeLimitationMapperTest.php | 48 +++++++++++++++++++-- .../Mapper/LanguageTypeLimitationMapperTest.php | 48 +++++++++++++++++++-- .../Mapper/ObjectStateLimitationMapperTest.php | 48 +++++++++++++++++++-- .../Mapper/SectionLimitationMapperTest.php | 49 +++++++++++++++++++--- 9 files changed, 236 insertions(+), 23 deletions(-) diff --git a/bundle/Resources/config/role.yml b/bundle/Resources/config/role.yml index 89bec9342..5b386c871 100644 --- a/bundle/Resources/config/role.yml +++ b/bundle/Resources/config/role.yml @@ -106,6 +106,8 @@ services: parent: ezrepoforms.limitation.form_mapper.multiple_selection class: "%ezrepoforms.limitation.form_mapper.contenttype.class%" arguments: ["@ezpublish.api.service.content_type"] + calls: + - [setLogger, ['@?logger']] tags: - { name: ez.limitation.formMapper, limitationType: Class } - { name: ez.limitation.valueMapper, limitationType: Class } @@ -114,6 +116,8 @@ services: parent: ezrepoforms.limitation.form_mapper.multiple_selection class: "%ezrepoforms.limitation.form_mapper.contenttype.class%" arguments: ["@ezpublish.api.service.content_type"] + calls: + - [setLogger, ['@?logger']] tags: - { name: ez.limitation.formMapper, limitationType: ParentClass } - { name: ez.limitation.valueMapper, limitationType: ParentClass } @@ -122,6 +126,8 @@ services: parent: ezrepoforms.limitation.form_mapper.multiple_selection class: "%ezrepoforms.limitation.form_mapper.section.class%" arguments: ["@ezpublish.api.service.section"] + calls: + - [setLogger, ['@?logger']] tags: - { name: ez.limitation.formMapper, limitationType: Section } - { name: ez.limitation.valueMapper, limitationType: Section } @@ -130,6 +136,8 @@ services: parent: ezrepoforms.limitation.form_mapper.multiple_selection class: "%ezrepoforms.limitation.form_mapper.section.class%" arguments: ["@ezpublish.api.service.section"] + calls: + - [setLogger, ['@?logger']] tags: - { name: ez.limitation.formMapper, limitationType: NewSection } - { name: ez.limitation.valueMapper, limitationType: NewSection } @@ -138,6 +146,8 @@ services: parent: ezrepoforms.limitation.form_mapper.multiple_selection class: "%ezrepoforms.limitation.form_mapper.objectstate.class%" arguments: ["@ezpublish.api.service.object_state"] + calls: + - [setLogger, ['@?logger']] tags: - { name: ez.limitation.formMapper, limitationType: State } - { name: ez.limitation.valueMapper, limitationType: State } @@ -146,6 +156,8 @@ services: parent: ezrepoforms.limitation.form_mapper.multiple_selection class: "%ezrepoforms.limitation.form_mapper.objectstate.class%" arguments: ["@ezpublish.api.service.object_state"] + calls: + - [setLogger, ['@?logger']] tags: - { name: ez.limitation.formMapper, limitationType: NewState } - { name: ez.limitation.valueMapper, limitationType: NewState } @@ -154,6 +166,8 @@ services: parent: ezrepoforms.limitation.form_mapper.multiple_selection class: "%ezrepoforms.limitation.form_mapper.language.class%" arguments: ["@ezpublish.api.service.language"] + calls: + - [setLogger, ['@?logger']] tags: - { name: ez.limitation.formMapper, limitationType: Language } - { name: ez.limitation.valueMapper, limitationType: Language } diff --git a/lib/Limitation/Mapper/ContentTypeLimitationMapper.php b/lib/Limitation/Mapper/ContentTypeLimitationMapper.php index 996ad4cd7..aefaa9503 100644 --- a/lib/Limitation/Mapper/ContentTypeLimitationMapper.php +++ b/lib/Limitation/Mapper/ContentTypeLimitationMapper.php @@ -9,11 +9,16 @@ namespace EzSystems\RepositoryForms\Limitation\Mapper; use eZ\Publish\API\Repository\ContentTypeService; +use eZ\Publish\API\Repository\Exceptions\NotFoundException; use eZ\Publish\API\Repository\Values\User\Limitation; use EzSystems\RepositoryForms\Limitation\LimitationValueMapperInterface; +use Psr\Log\LoggerAwareTrait; +use Psr\Log\NullLogger; class ContentTypeLimitationMapper extends MultipleSelectionBasedMapper implements LimitationValueMapperInterface { + use LoggerAwareTrait; + /** * @var ContentTypeService */ @@ -22,6 +27,7 @@ class ContentTypeLimitationMapper extends MultipleSelectionBasedMapper implement public function __construct(ContentTypeService $contentTypeService) { $this->contentTypeService = $contentTypeService; + $this->logger = new NullLogger(); } protected function getSelectionChoices() @@ -40,7 +46,11 @@ public function mapLimitationValue(Limitation $limitation) { $values = []; foreach ($limitation->limitationValues as $contentTypeId) { - $values[] = $this->contentTypeService->loadContentType($contentTypeId); + try { + $values[] = $this->contentTypeService->loadContentType($contentTypeId); + } catch (NotFoundException $e) { + $this->logger->error(sprintf('Could not map limitation value: Content Type with id = %s not found', $contentTypeId)); + } } return $values; diff --git a/lib/Limitation/Mapper/LanguageLimitationMapper.php b/lib/Limitation/Mapper/LanguageLimitationMapper.php index d50332500..38b569f87 100644 --- a/lib/Limitation/Mapper/LanguageLimitationMapper.php +++ b/lib/Limitation/Mapper/LanguageLimitationMapper.php @@ -8,12 +8,17 @@ */ namespace EzSystems\RepositoryForms\Limitation\Mapper; +use eZ\Publish\API\Repository\Exceptions\NotFoundException; use eZ\Publish\API\Repository\LanguageService; use eZ\Publish\API\Repository\Values\User\Limitation; use EzSystems\RepositoryForms\Limitation\LimitationValueMapperInterface; +use Psr\Log\LoggerAwareTrait; +use Psr\Log\NullLogger; class LanguageLimitationMapper extends MultipleSelectionBasedMapper implements LimitationValueMapperInterface { + use LoggerAwareTrait; + /** * @var LanguageService */ @@ -22,6 +27,7 @@ class LanguageLimitationMapper extends MultipleSelectionBasedMapper implements L public function __construct(LanguageService $languageService) { $this->languageService = $languageService; + $this->logger = new NullLogger(); } protected function getSelectionChoices() @@ -39,7 +45,11 @@ public function mapLimitationValue(Limitation $limitation) $values = []; foreach ($limitation->limitationValues as $languageCode) { - $values[] = $this->languageService->loadLanguage($languageCode); + try { + $values[] = $this->languageService->loadLanguage($languageCode); + } catch (NotFoundException $e) { + $this->logger->error(sprintf('Could not map limitation value: Language with code = %s not found', $languageCode)); + } } return $values; diff --git a/lib/Limitation/Mapper/ObjectStateLimitationMapper.php b/lib/Limitation/Mapper/ObjectStateLimitationMapper.php index 933d29fdb..9c30b59d0 100644 --- a/lib/Limitation/Mapper/ObjectStateLimitationMapper.php +++ b/lib/Limitation/Mapper/ObjectStateLimitationMapper.php @@ -8,13 +8,18 @@ */ namespace EzSystems\RepositoryForms\Limitation\Mapper; +use eZ\Publish\API\Repository\Exceptions\NotFoundException; use eZ\Publish\API\Repository\ObjectStateService; use eZ\Publish\API\Repository\Values\ObjectState\ObjectState; use eZ\Publish\API\Repository\Values\User\Limitation; use EzSystems\RepositoryForms\Limitation\LimitationValueMapperInterface; +use Psr\Log\LoggerAwareTrait; +use Psr\Log\NullLogger; class ObjectStateLimitationMapper extends MultipleSelectionBasedMapper implements LimitationValueMapperInterface { + use LoggerAwareTrait; + /** * @var ObjectStateService */ @@ -23,6 +28,7 @@ class ObjectStateLimitationMapper extends MultipleSelectionBasedMapper implement public function __construct(ObjectStateService $objectStateService) { $this->objectStateService = $objectStateService; + $this->logger = new NullLogger(); } protected function getSelectionChoices() @@ -42,9 +48,13 @@ public function mapLimitationValue(Limitation $limitation) $values = []; foreach ($limitation->limitationValues as $stateId) { - $values[] = $this->getObjectStateLabel( - $this->objectStateService->loadObjectState($stateId) - ); + try { + $values[] = $this->getObjectStateLabel( + $this->objectStateService->loadObjectState($stateId) + ); + } catch (NotFoundException $e) { + $this->logger->error(sprintf('Could not map limitation value: ObjectState with id = %s not found', $stateId)); + } } return $values; diff --git a/lib/Limitation/Mapper/SectionLimitationMapper.php b/lib/Limitation/Mapper/SectionLimitationMapper.php index 08df49e5c..2a2f17f95 100644 --- a/lib/Limitation/Mapper/SectionLimitationMapper.php +++ b/lib/Limitation/Mapper/SectionLimitationMapper.php @@ -10,10 +10,15 @@ use eZ\Publish\API\Repository\SectionService; use eZ\Publish\API\Repository\Values\User\Limitation; +use eZ\Publish\API\Repository\Exceptions\NotFoundException; use EzSystems\RepositoryForms\Limitation\LimitationValueMapperInterface; +use Psr\Log\LoggerAwareTrait; +use Psr\Log\NullLogger; class SectionLimitationMapper extends MultipleSelectionBasedMapper implements LimitationValueMapperInterface { + use LoggerAwareTrait; + /** * @var SectionService */ @@ -22,6 +27,7 @@ class SectionLimitationMapper extends MultipleSelectionBasedMapper implements Li public function __construct(SectionService $sectionService) { $this->sectionService = $sectionService; + $this->logger = new NullLogger(); } protected function getSelectionChoices() @@ -38,7 +44,11 @@ public function mapLimitationValue(Limitation $limitation) { $values = []; foreach ($limitation->limitationValues as $sectionId) { - $values[] = $this->sectionService->loadSection($sectionId); + try { + $values[] = $this->sectionService->loadSection($sectionId); + } catch (NotFoundException $e) { + $this->logger->error(sprintf('Could not map limitation value: Section with id = %s not found', $sectionId)); + } } return $values; diff --git a/tests/RepositoryForms/Limitation/Mapper/ContentTypeLimitationMapperTest.php b/tests/RepositoryForms/Limitation/Mapper/ContentTypeLimitationMapperTest.php index 2790c8b45..a74077e4b 100644 --- a/tests/RepositoryForms/Limitation/Mapper/ContentTypeLimitationMapperTest.php +++ b/tests/RepositoryForms/Limitation/Mapper/ContentTypeLimitationMapperTest.php @@ -9,31 +9,71 @@ namespace EzSystems\RepositoryForms\Tests\Limitation\Mapper; use eZ\Publish\API\Repository\ContentTypeService; +use eZ\Publish\API\Repository\Exceptions\NotFoundException; use eZ\Publish\API\Repository\Values\User\Limitation\ContentTypeLimitation; use EzSystems\RepositoryForms\Limitation\Mapper\ContentTypeLimitationMapper; use PHPUnit\Framework\TestCase; +use Psr\Log\LoggerInterface; class ContentTypeLimitationMapperTest extends TestCase { + /** @var ContentTypeService|\PHPUnit_Framework_MockObject_MockObject */ + private $contentTypeService; + + /** @var LoggerInterface|\PHPUnit_Framework_MockObject_MockObject */ + private $logger; + + /** @var ContentTypeLimitationMapper */ + private $mapper; + + public function setUp() + { + $this->contentTypeService = $this->createMock(ContentTypeService::class); + $this->logger = $this->createMock(LoggerInterface::class); + + $this->mapper = new ContentTypeLimitationMapper($this->contentTypeService); + $this->mapper->setLogger($this->logger); + } + public function testMapLimitationValue() { $values = ['foo', 'bar', 'baz']; - $contentTypeServiceMock = $this->createMock(ContentTypeService::class); foreach ($values as $i => $value) { - $contentTypeServiceMock + $this->contentTypeService ->expects($this->at($i)) ->method('loadContentType') ->with($value) ->willReturn($value); } - $mapper = new ContentTypeLimitationMapper($contentTypeServiceMock); - $result = $mapper->mapLimitationValue(new ContentTypeLimitation([ + $result = $this->mapper->mapLimitationValue(new ContentTypeLimitation([ 'limitationValues' => $values, ])); $this->assertEquals($values, $result); $this->assertCount(3, $result); } + + public function testMapLimitationValueWithNotExistingContentType() + { + $values = ['foo']; + + $this->contentTypeService + ->expects($this->once()) + ->method('loadContentType') + ->with($values[0]) + ->willThrowException($this->createMock(NotFoundException::class)); + + $this->logger + ->expects($this->once()) + ->method('error') + ->with('Could not map limitation value: Content Type with id = foo not found'); + + $actual = $this->mapper->mapLimitationValue(new ContentTypeLimitation([ + 'limitationValues' => $values, + ])); + + $this->assertEmpty($actual); + } } diff --git a/tests/RepositoryForms/Limitation/Mapper/LanguageTypeLimitationMapperTest.php b/tests/RepositoryForms/Limitation/Mapper/LanguageTypeLimitationMapperTest.php index 44ed1cb95..c49b2bf94 100644 --- a/tests/RepositoryForms/Limitation/Mapper/LanguageTypeLimitationMapperTest.php +++ b/tests/RepositoryForms/Limitation/Mapper/LanguageTypeLimitationMapperTest.php @@ -8,31 +8,71 @@ */ namespace EzSystems\RepositoryForms\Tests\Limitation\Mapper; +use eZ\Publish\API\Repository\Exceptions\NotFoundException; use eZ\Publish\API\Repository\LanguageService; use eZ\Publish\API\Repository\Values\User\Limitation\LanguageLimitation; use EzSystems\RepositoryForms\Limitation\Mapper\LanguageLimitationMapper; use PHPUnit\Framework\TestCase; +use Psr\Log\LoggerInterface; class LanguageTypeLimitationMapperTest extends TestCase { + /** @var LanguageService|\PHPUnit_Framework_MockObject_MockObject */ + private $languageService; + + /** @var LoggerInterface|\PHPUnit_Framework_MockObject_MockObject */ + private $logger; + + /** @var LanguageLimitationMapper */ + private $mapper; + + protected function setUp() + { + $this->languageService = $this->createMock(LanguageService::class); + $this->logger = $this->createMock(LoggerInterface::class); + + $this->mapper = new LanguageLimitationMapper($this->languageService); + $this->mapper->setLogger($this->logger); + } + public function testMapLimitationValue() { $values = ['en_GB', 'en_US', 'pl_PL']; - $languageServiceMock = $this->createMock(LanguageService::class); foreach ($values as $i => $value) { - $languageServiceMock + $this->languageService ->expects($this->at($i)) ->method('loadLanguage') ->with($value) ->willReturnArgument(0); } - $mapper = new LanguageLimitationMapper($languageServiceMock); - $result = $mapper->mapLimitationValue(new LanguageLimitation([ + $result = $this->mapper->mapLimitationValue(new LanguageLimitation([ 'limitationValues' => $values, ])); $this->assertEquals($values, $result); } + + public function testMapLimitationValueWithNotExistingContentType() + { + $values = ['foo']; + + $this->languageService + ->expects($this->once()) + ->method('loadLanguage') + ->with($values[0]) + ->willThrowException($this->createMock(NotFoundException::class)); + + $this->logger + ->expects($this->once()) + ->method('error') + ->with('Could not map limitation value: Language with code = foo not found'); + + $actual = $this->mapper->mapLimitationValue(new LanguageLimitation([ + 'limitationValues' => $values, + ])); + + $this->assertEmpty($actual); + } } diff --git a/tests/RepositoryForms/Limitation/Mapper/ObjectStateLimitationMapperTest.php b/tests/RepositoryForms/Limitation/Mapper/ObjectStateLimitationMapperTest.php index 0e63f33f0..9cc6a86a7 100644 --- a/tests/RepositoryForms/Limitation/Mapper/ObjectStateLimitationMapperTest.php +++ b/tests/RepositoryForms/Limitation/Mapper/ObjectStateLimitationMapperTest.php @@ -8,32 +8,50 @@ */ namespace EzSystems\RepositoryForms\Tests\Limitation\Mapper; +use eZ\Publish\API\Repository\Exceptions\NotFoundException; use eZ\Publish\API\Repository\ObjectStateService; use eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup; use eZ\Publish\API\Repository\Values\User\Limitation\ObjectStateLimitation; use eZ\Publish\Core\Repository\Values\ObjectState\ObjectState; use EzSystems\RepositoryForms\Limitation\Mapper\ObjectStateLimitationMapper; use PHPUnit\Framework\TestCase; +use Psr\Log\LoggerInterface; class ObjectStateLimitationMapperTest extends TestCase { + /** @var ObjectStateService|\PHPUnit_Framework_MockObject_MockObject */ + private $objectStateService; + + /** @var LoggerInterface|\PHPUnit_Framework_MockObject_MockObject */ + private $logger; + + /** @var ObjectStateLimitationMapper */ + private $mapper; + + protected function setUp() + { + $this->objectStateService = $this->createMock(ObjectStateService::class); + $this->logger = $this->createMock(LoggerInterface::class); + + $this->mapper = new ObjectStateLimitationMapper($this->objectStateService); + $this->mapper->setLogger($this->logger); + } + public function testMapLimitationValue() { $values = ['foo', 'bar', 'baz']; - $objectStateServiceMock = $this->createMock(ObjectStateService::class); foreach ($values as $i => $value) { $stateMock = $this->createStateMock($value); - $objectStateServiceMock + $this->objectStateService ->expects($this->at($i)) ->method('loadObjectState') ->with($value) ->willReturn($stateMock); } - $mapper = new ObjectStateLimitationMapper($objectStateServiceMock); - $result = $mapper->mapLimitationValue(new ObjectStateLimitation([ + $result = $this->mapper->mapLimitationValue(new ObjectStateLimitation([ 'limitationValues' => $values, ])); @@ -42,6 +60,28 @@ public function testMapLimitationValue() ], $result); } + public function testMapLimitationValueWithNotExistingObjectState() + { + $values = ['foo']; + + $this->objectStateService + ->expects($this->once()) + ->method('loadObjectState') + ->with($values[0]) + ->willThrowException($this->createMock(NotFoundException::class)); + + $this->logger + ->expects($this->once()) + ->method('error') + ->with('Could not map limitation value: ObjectState with id = foo not found'); + + $actual = $this->mapper->mapLimitationValue(new ObjectStateLimitation([ + 'limitationValues' => $values, + ])); + + $this->assertEmpty($actual); + } + private function createStateMock($value) { $stateGroupMock = $this->createMock(ObjectStateGroup::class); diff --git a/tests/RepositoryForms/Limitation/Mapper/SectionLimitationMapperTest.php b/tests/RepositoryForms/Limitation/Mapper/SectionLimitationMapperTest.php index 3a5ea11fa..079da4629 100644 --- a/tests/RepositoryForms/Limitation/Mapper/SectionLimitationMapperTest.php +++ b/tests/RepositoryForms/Limitation/Mapper/SectionLimitationMapperTest.php @@ -8,38 +8,77 @@ */ namespace EzSystems\RepositoryForms\Tests\Limitation\Mapper; +use eZ\Publish\API\Repository\Exceptions\NotFoundException; use eZ\Publish\API\Repository\SectionService; use eZ\Publish\API\Repository\Values\Content\Section; use eZ\Publish\API\Repository\Values\User\Limitation\SectionLimitation; use EzSystems\RepositoryForms\Limitation\Mapper\SectionLimitationMapper; use PHPUnit\Framework\TestCase; +use Psr\Log\LoggerInterface; class SectionLimitationMapperTest extends TestCase { + /** @var SectionService|\PHPUnit_Framework_MockObject_MockObject */ + private $sectionServiceMock; + + /** @var LoggerInterface|\PHPUnit_Framework_MockObject_MockObject */ + private $logger; + + /** @var SectionLimitationMapper */ + private $mapper; + + protected function setUp() + { + $this->sectionServiceMock = $this->createMock(SectionService::class); + $this->logger = $this->createMock(LoggerInterface::class); + + $this->mapper = new SectionLimitationMapper($this->sectionServiceMock); + $this->mapper->setLogger($this->logger); + } + public function testMapLimitationValue() { $values = ['3', '5', '7']; - $sectionServiceMock = $this->createMock(SectionService::class); - $expected = []; foreach ($values as $i => $value) { $expected[$i] = new Section([ 'id' => $value, ]); - $sectionServiceMock + $this->sectionServiceMock ->expects($this->at($i)) ->method('loadSection') ->with($value) ->willReturn($expected[$i]); } - $mapper = new SectionLimitationMapper($sectionServiceMock); - $result = $mapper->mapLimitationValue(new SectionLimitation([ + $result = $this->mapper->mapLimitationValue(new SectionLimitation([ 'limitationValues' => $values, ])); $this->assertEquals($expected, $result); } + + public function testMapLimitationValueWithNotExistingContentType() + { + $values = ['foo']; + + $this->sectionServiceMock + ->expects($this->once()) + ->method('loadSection') + ->with($values[0]) + ->willThrowException($this->createMock(NotFoundException::class)); + + $this->logger + ->expects($this->once()) + ->method('error') + ->with('Could not map limitation value: Section with id = foo not found'); + + $actual = $this->mapper->mapLimitationValue(new SectionLimitation([ + 'limitationValues' => $values, + ])); + + $this->assertEmpty($actual); + } }