From d5c26b9b975253974e98b63ecb752876178d2e01 Mon Sep 17 00:00:00 2001 From: Bartek Wajda Date: Tue, 1 Aug 2023 15:22:28 +0200 Subject: [PATCH 1/3] IBX-4031: Forced published non-translatable field to be shown in current field --- .../View/Filter/ContentEditViewFilter.php | 22 ++-- src/lib/Data/Mapper/ContentUpdateMapper.php | 10 +- .../Data/Mapper/ContentUpdateMapperTest.php | 108 ++++++++++++++++++ 3 files changed, 130 insertions(+), 10 deletions(-) create mode 100644 tests/lib/Data/Mapper/ContentUpdateMapperTest.php diff --git a/src/lib/Content/View/Filter/ContentEditViewFilter.php b/src/lib/Content/View/Filter/ContentEditViewFilter.php index b884322e..b191c7d2 100644 --- a/src/lib/Content/View/Filter/ContentEditViewFilter.php +++ b/src/lib/Content/View/Filter/ContentEditViewFilter.php @@ -74,18 +74,26 @@ public function handleContentEditForm(FilterViewBuilderParametersEvent $event) $request = $event->getRequest(); $languageCode = $request->attributes->get('language'); + $contentId = $request->attributes->getInt('contentId'); $contentDraft = $this->contentService->loadContent( - $request->attributes->getInt('contentId'), + $contentId, [$languageCode], // @todo: rename to languageCode in 3.0 $request->attributes->getInt('versionNo') ); + $currentContent = $this->contentService->loadContent($contentId); + $currentFields = $currentContent->getFields(); $contentType = $this->contentTypeService->loadContentType( $contentDraft->contentInfo->contentTypeId, $this->languagePreferenceProvider->getPreferredLanguages() ); - $contentUpdate = $this->resolveContentEditData($contentDraft, $languageCode, $contentType); + $contentUpdate = $this->resolveContentEditData( + $contentDraft, + $languageCode, + $contentType, + $currentFields, + ); $form = $this->resolveContentEditForm( $contentUpdate, $languageCode, @@ -99,22 +107,20 @@ public function handleContentEditForm(FilterViewBuilderParametersEvent $event) } /** - * @param \eZ\Publish\API\Repository\Values\Content\Content $content - * @param string $languageCode - * @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType - * - * @return \EzSystems\EzPlatformContentForms\Data\Content\ContentUpdateData + * @param array<\eZ\Publish\API\Repository\Values\Content\Field> $currentFields */ private function resolveContentEditData( Content $content, string $languageCode, - ContentType $contentType + ContentType $contentType, + array $currentFields ): ContentUpdateData { $contentUpdateMapper = new ContentUpdateMapper(); return $contentUpdateMapper->mapToFormData($content, [ 'languageCode' => $languageCode, 'contentType' => $contentType, + 'currentFields' => $currentFields, ]); } diff --git a/src/lib/Data/Mapper/ContentUpdateMapper.php b/src/lib/Data/Mapper/ContentUpdateMapper.php index 6864a007..93d226fe 100644 --- a/src/lib/Data/Mapper/ContentUpdateMapper.php +++ b/src/lib/Data/Mapper/ContentUpdateMapper.php @@ -31,17 +31,23 @@ public function mapToFormData(ValueObject $contentDraft, array $params = []) $params = $optionsResolver->resolve($params); $languageCode = $params['languageCode']; + $currentFields = $params['currentFields']; + $mappedCurrentFields = array_column($currentFields, null, 'fieldDefIdentifier'); $data = new ContentUpdateData(['contentDraft' => $contentDraft]); $data->initialLanguageCode = $languageCode; $fields = $contentDraft->getFieldsByLanguage($languageCode); + foreach ($params['contentType']->fieldDefinitions as $fieldDef) { + $isNonTranslatable = $fieldDef->isTranslatable === false; $field = $fields[$fieldDef->identifier]; $data->addFieldData(new FieldData([ 'fieldDefinition' => $fieldDef, 'field' => $field, - 'value' => $field->value, + 'value' => $isNonTranslatable + ? $mappedCurrentFields[$fieldDef->identifier]->value + : $field->value, ])); } @@ -51,7 +57,7 @@ public function mapToFormData(ValueObject $contentDraft, array $params = []) private function configureOptions(OptionsResolver $optionsResolver) { $optionsResolver - ->setRequired(['languageCode', 'contentType']) + ->setRequired(['languageCode', 'contentType', 'currentFields']) ->setAllowedTypes('contentType', ContentType::class); } } diff --git a/tests/lib/Data/Mapper/ContentUpdateMapperTest.php b/tests/lib/Data/Mapper/ContentUpdateMapperTest.php new file mode 100644 index 00000000..eb0d937a --- /dev/null +++ b/tests/lib/Data/Mapper/ContentUpdateMapperTest.php @@ -0,0 +1,108 @@ + 'name', + 'fieldTypeIdentifier' => 'ezstring', + 'languageCode' => 'eng-GB', + 'value' => 'Name', + ]), + new APIField([ + 'fieldDefIdentifier' => 'short_name', + 'fieldTypeIdentifier' => 'ezstring', + 'languageCode' => 'eng-DE', + 'value' => $expectedShortName = 'Nontranslateable short name', + ]), + ]; + $content = new Content([ + 'versionInfo' => new VersionInfo([ + 'contentInfo' => new ContentInfo([ + 'remoteId' => 'foo', + 'mainLanguage' => new Language([ + 'languageCode' => 'eng-GB', + ]), + 'alwaysAvailable' => true, + 'sectionId' => 2, + 'section' => new Section([ + 'id' => 2, + 'identifier' => 'foo_section_identifier', + ]), + 'publishedDate' => new \DateTime('2020-10-30T11:40:09+00:00'), + 'modificationDate' => new \DateTime('2020-10-30T11:40:09+00:00'), + 'mainLocation' => new APILocation([ + 'parentLocationId' => 1, + 'hidden' => true, + 'sortField' => 9, + 'sortOrder' => 0, + 'priority' => 1, + ]), + ]), + ]), + 'contentType' => $contentType = new ContentType([ + 'identifier' => 'folder', + 'fieldDefinitions' => new FieldDefinitionCollection([ + new FieldDefinition([ + 'identifier' => 'name', + 'isTranslatable' => true, + 'defaultValue' => '', + ]), + new FieldDefinition([ + 'identifier' => 'short_name', + 'isTranslatable' => false, + 'defaultValue' => '', + ]), + ]), + ]), + 'internalFields' => [ + new APIField([ + 'fieldDefIdentifier' => 'name', + 'fieldTypeIdentifier' => 'ezstring', + 'languageCode' => 'ger-DE', + 'value' => $expectedName = 'GER name', + ]), + new APIField([ + 'fieldDefIdentifier' => 'short_name', + 'fieldTypeIdentifier' => 'ezstring', + 'languageCode' => 'ger-DE', + 'value' => '', + ]), + ], + ]); + + $data = (new ContentUpdateMapper())->mapToFormData($content, [ + 'languageCode' => 'ger-DE', + 'contentType' => $contentType, + 'currentFields' => $currentFields, + ]); + + $fieldsData = $data->fieldsData; + + self::assertSame($expectedName, $fieldsData['name']->value); + self::assertSame($expectedShortName, $fieldsData['short_name']->value); + } +} From 0394dab8d7e76ffb39c3a0c96f6dc46d80b19fb4 Mon Sep 17 00:00:00 2001 From: Bartek Wajda Date: Tue, 1 Aug 2023 15:39:21 +0200 Subject: [PATCH 2/3] IBX-4031: Fixed Behat test --- src/lib/Behat/Context/UserRegistrationContext.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/Behat/Context/UserRegistrationContext.php b/src/lib/Behat/Context/UserRegistrationContext.php index 0ec48caa..557747d9 100644 --- a/src/lib/Behat/Context/UserRegistrationContext.php +++ b/src/lib/Behat/Context/UserRegistrationContext.php @@ -305,8 +305,8 @@ public function addUserRegistrationConfiguration(PyStringNode $extraConfiguratio { $extraConfigurationString = str_replace( '', - $this->customUserGroup->id, - $extraConfigurationString + (string)$this->customUserGroup->id, + $extraConfigurationString->getRaw() ); $this->yamlConfigurationContext->addConfiguration(Yaml::parse($extraConfigurationString)); From a2f51cb98e30526592147cd281d9cd25acc4f5ae Mon Sep 17 00:00:00 2001 From: Bartek Wajda Date: Mon, 7 Aug 2023 15:19:26 +0200 Subject: [PATCH 3/3] IBX-4031: Made `ContentUpdateMapperTest` final --- tests/lib/Data/Mapper/ContentUpdateMapperTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/lib/Data/Mapper/ContentUpdateMapperTest.php b/tests/lib/Data/Mapper/ContentUpdateMapperTest.php index eb0d937a..8c583210 100644 --- a/tests/lib/Data/Mapper/ContentUpdateMapperTest.php +++ b/tests/lib/Data/Mapper/ContentUpdateMapperTest.php @@ -21,7 +21,7 @@ use EzSystems\EzPlatformContentForms\Data\Mapper\ContentUpdateMapper; use PHPUnit\Framework\TestCase; -class ContentUpdateMapperTest extends TestCase +final class ContentUpdateMapperTest extends TestCase { public function testMapToFormData(): void {