diff --git a/lib/FieldType/DataTransformer/BinaryFileValueTransformer.php b/lib/FieldType/DataTransformer/BinaryFileValueTransformer.php index 5626285ff..6def996e2 100644 --- a/lib/FieldType/DataTransformer/BinaryFileValueTransformer.php +++ b/lib/FieldType/DataTransformer/BinaryFileValueTransformer.php @@ -43,6 +43,13 @@ public function transform($value) */ public function reverseTransform($value) { - return $this->getReverseTransformedValue($value); + /** @var Value $valueObject */ + $valueObject = $this->getReverseTransformedValue($value); + + if ($this->fieldType->isEmptyValue($valueObject)) { + return $valueObject; + } + + return $valueObject; } } diff --git a/lib/Form/Processor/ContentFormProcessor.php b/lib/Form/Processor/ContentFormProcessor.php index 3d834341d..db055d64b 100644 --- a/lib/Form/Processor/ContentFormProcessor.php +++ b/lib/Form/Processor/ContentFormProcessor.php @@ -37,11 +37,6 @@ class ContentFormProcessor implements EventSubscriberInterface /** @var \Symfony\Component\Routing\RouterInterface */ private $router; - /** - * @param \eZ\Publish\API\Repository\ContentService $contentService - * @param \eZ\Publish\API\Repository\LocationService $locationService - * @param \Symfony\Component\Routing\RouterInterface $router - */ public function __construct( ContentService $contentService, LocationService $locationService, @@ -52,10 +47,7 @@ public function __construct( $this->router = $router; } - /** - * @return array - */ - public static function getSubscribedEvents(): array + public static function getSubscribedEvents() { return [ RepositoryFormEvents::CONTENT_PUBLISH => ['processPublish', 10], @@ -65,18 +57,7 @@ public static function getSubscribedEvents(): array ]; } - /** - * @param \EzSystems\RepositoryForms\Event\FormActionEvent $event - * - * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException - * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException - * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException - * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException - * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException - * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException - */ - public function processSaveDraft(FormActionEvent $event): void + public function processSaveDraft(FormActionEvent $event) { /** @var \EzSystems\RepositoryForms\Data\Content\ContentCreateData|\EzSystems\RepositoryForms\Data\Content\ContentUpdateData $data */ $data = $event->getData(); @@ -84,7 +65,6 @@ public function processSaveDraft(FormActionEvent $event): void $formConfig = $form->getConfig(); $languageCode = $formConfig->getOption('languageCode'); - $draft = $this->saveDraft($data, $languageCode); $referrerLocation = $event->getOption('referrerLocation'); $contentLocation = $this->resolveLocation($draft, $referrerLocation, $data); @@ -98,24 +78,13 @@ public function processSaveDraft(FormActionEvent $event): void $event->setResponse(new RedirectResponse($formConfig->getAction() ?: $defaultUrl)); } - /** - * @param \EzSystems\RepositoryForms\Event\FormActionEvent $event - * - * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException - * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException - * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException - * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException - * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException - * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException - */ - public function processPublish(FormActionEvent $event): void + public function processPublish(FormActionEvent $event) { /** @var \EzSystems\RepositoryForms\Data\Content\ContentCreateData|\EzSystems\RepositoryForms\Data\Content\ContentUpdateData $data */ $data = $event->getData(); $form = $event->getForm(); $draft = $this->saveDraft($data, $form->getConfig()->getOption('languageCode')); - $content = $this->contentService->publishVersion($draft->versionInfo); // Redirect to the provided URL. Defaults to URLAlias of the published content. @@ -127,13 +96,7 @@ public function processPublish(FormActionEvent $event): void $event->setResponse(new RedirectResponse($redirectUrl)); } - /** - * @param \EzSystems\RepositoryForms\Event\FormActionEvent $event - * - * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException - * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException - */ - public function processCancel(FormActionEvent $event): void + public function processCancel(FormActionEvent $event) { /** @var \EzSystems\RepositoryForms\Data\Content\ContentCreateData|\EzSystems\RepositoryForms\Data\Content\ContentUpdateData $data */ $data = $event->getData(); @@ -170,13 +133,7 @@ public function processCancel(FormActionEvent $event): void $event->setResponse(new RedirectResponse($url)); } - /** - * @param \EzSystems\RepositoryForms\Event\FormActionEvent $event - * - * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException - */ - public function processCreateDraft(FormActionEvent $event): void + public function processCreateDraft(FormActionEvent $event) { /** @var $createContentDraft \EzSystems\RepositoryForms\Data\Content\CreateContentDraftData */ $createContentDraft = $event->getData(); @@ -201,17 +158,9 @@ public function processCreateDraft(FormActionEvent $event): void * * @param ContentStruct|\EzSystems\RepositoryForms\Data\Content\ContentCreateData|\EzSystems\RepositoryForms\Data\Content\ContentUpdateData $data * @param $languageCode - * * @return \eZ\Publish\API\Repository\Values\Content\Content - * - * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException - * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException - * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException - * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException - * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException - * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException */ - private function saveDraft(ContentStruct $data, $languageCode): Content + private function saveDraft(ContentStruct $data, $languageCode) { $mainLanguageCode = $this->resolveMainLanguageCode($data); foreach ($data->fieldsData as $fieldDefIdentifier => $fieldData) { @@ -219,11 +168,9 @@ private function saveDraft(ContentStruct $data, $languageCode): Content continue; } - /* comparing value, not references */ - if ($fieldData->value != $fieldData->field->value) { - $data->setField($fieldDefIdentifier, $fieldData->value, $languageCode); - } + $data->setField($fieldDefIdentifier, $fieldData->value, $languageCode); } + if ($data->isNew()) { $contentDraft = $this->contentService->createContent($data, $data->getLocationStructs()); } else { @@ -260,9 +207,6 @@ private function resolveMainLanguageCode($data): string * @param \EzSystems\RepositoryForms\Data\NewnessCheckable $data * * @return \eZ\Publish\API\Repository\Values\Content\Location|null - * - * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException */ private function resolveLocation(Content $content, ?Location $referrerLocation, NewnessCheckable $data): ?Location { diff --git a/tests/RepositoryForms/Form/Processor/ContentFormProcessorTest.php b/tests/RepositoryForms/Form/Processor/ContentFormProcessorTest.php deleted file mode 100644 index ec302657e..000000000 --- a/tests/RepositoryForms/Form/Processor/ContentFormProcessorTest.php +++ /dev/null @@ -1,271 +0,0 @@ -contentService = $this->createMock(ContentService::class); - $this->locationService = $this->createMock(LocationService::class); - $this->router = $this->createMock(RouterInterface::class); - $this->processor = new ContentFormProcessor($this->contentService, $this->locationService, $this->router); - $this->versionInfo = new VersionInfo([ - 'contentInfo' => new ContentInfo([ - 'id' => self::CONTENT_ID, - 'mainLanguageCode' => self::LANGUAGE_CODE, - ]), - ]); - $this->contentDraft = new Content([ - 'versionInfo' => $this->versionInfo, - ]); - } - - public function testGetSubscribedEvents(): void - { - self::assertSame( - [ - RepositoryFormEvents::CONTENT_PUBLISH => ['processPublish', 10], - RepositoryFormEvents::CONTENT_CANCEL => ['processCancel', 10], - RepositoryFormEvents::CONTENT_SAVE_DRAFT => ['processSaveDraft', 10], - RepositoryFormEvents::CONTENT_CREATE_DRAFT => ['processCreateDraft', 10], - ], - ContentFormProcessor::getSubscribedEvents() - ); - } - - public function testProcessSaveDraftWithTwoUpdatedFields(): void - { - $form = $this->getSaveDraftForm(); - - $field1 = $this->getField(self::FIELD_1_IDENTIFIER, self::LANGUAGE_CODE, 'field_1_value'); - $field2 = $this->getField(self::FIELD_2_IDENTIFIER, self::LANGUAGE_CODE, 'field_2_value'); - - $data = new ContentUpdateData([ - 'contentDraft' => $this->contentDraft, - 'fields' => [], - 'fieldsData' => [ - $field1->fieldDefIdentifier => new FieldData([ - 'field' => $field1, - 'fieldDefinition' => $this->getFieldDefinition($field1->fieldDefIdentifier), - 'value' => 'new_field_1_value', - ]), - $field2->fieldDefIdentifier => new FieldData([ - 'field' => $field2, - 'fieldDefinition' => $this->getFieldDefinition($field2->fieldDefIdentifier), - 'value' => 'new_field_2_value', - ]), - ], - ]); - - $updateContentData = new ContentUpdateData([ - 'contentDraft' => $this->contentDraft, - 'fields' => [ - $this->getField(self::FIELD_1_IDENTIFIER, self::LANGUAGE_CODE, 'new_field_1_value'), - $this->getField(self::FIELD_1_IDENTIFIER, self::LANGUAGE_CODE, 'new_field_2_value'), - ], - 'fieldsData' => [ - $field1->fieldDefIdentifier => new FieldData([ - 'field' => $field1, - 'fieldDefinition' => $this->getFieldDefinition($field1->fieldDefIdentifier), - 'value' => 'new_field_1_value', - ]), - $field2->fieldDefIdentifier => new FieldData([ - 'field' => $field2, - 'fieldDefinition' => $this->getFieldDefinition($field2->fieldDefIdentifier), - 'value' => 'new_field_2_value', - ]), - ], - ]); - - $event = new FormActionEvent($form, $data, 'foo'); - - $this->contentService - ->expects($this->once()) - ->method('updateContent') - ->withConsecutive($this->versionInfo, $updateContentData) - ->willReturn($this->contentDraft); - - $this->processor->processSaveDraft($event); - } - - public function testProcessPublishWithOneUpdatedField(): void - { - $form = $this->getPublishForm(); - - $field1 = $this->getField(self::FIELD_1_IDENTIFIER, self::LANGUAGE_CODE, 'field_1_value'); - $field2 = $this->getField(self::FIELD_2_IDENTIFIER, self::LANGUAGE_CODE, 'field_2_value'); - - $data = new ContentUpdateData([ - 'contentDraft' => $this->contentDraft, - 'fields' => [], - 'fieldsData' => [ - $field1->fieldDefIdentifier => new FieldData([ - 'field' => $field1, - 'fieldDefinition' => $this->getFieldDefinition($field1->fieldDefIdentifier), - 'value' => 'new_field_1_value', - ]), - $field2->fieldDefIdentifier => new FieldData([ - 'field' => $field2, - 'fieldDefinition' => $this->getFieldDefinition($field2->fieldDefIdentifier), - 'value' => 'field_2_value', - ]), - ], - ]); - - $updateContentData = new ContentUpdateData([ - 'contentDraft' => $this->contentDraft, - 'fields' => [$this->getField(self::FIELD_1_IDENTIFIER, self::LANGUAGE_CODE, 'new_field_1_value')], - 'fieldsData' => [ - $field1->fieldDefIdentifier => new FieldData([ - 'field' => $field1, - 'fieldDefinition' => $this->getFieldDefinition($field1->fieldDefIdentifier), - 'value' => 'new_field_1_value', - ]), - $field2->fieldDefIdentifier => new FieldData([ - 'field' => $field2, - 'fieldDefinition' => $this->getFieldDefinition($field2->fieldDefIdentifier), - 'value' => 'field_2_value', - ]), - ], - ]); - - $event = new FormActionEvent($form, $data, 'foo'); - - $this->contentService - ->expects($this->once()) - ->method('updateContent') - ->with($this->versionInfo, $updateContentData) - ->willReturn($this->contentDraft); - - $this->processor->processPublish($event); - } - - /** - * @param string $fieldDefIdentifier - * @param string $languageCode - * @param string $value - * - * @return Field - */ - private function getField($fieldDefIdentifier = 'identifier', $languageCode = self::LANGUAGE_CODE, $value = 'string_value'): Field - { - return new Field([ - 'fieldDefIdentifier' => $fieldDefIdentifier, - 'languageCode' => $languageCode, - 'value' => $value, - ]); - } - - /** - * @param string $identifier - * - * @return FieldDefinition - */ - private function getFieldDefinition(string $identifier = 'identifier'): FieldDefinition - { - return new FieldDefinition([ - 'identifier' => $identifier, - 'defaultValue' => 'default_value', - ]); - } - - /** - * @return \PHPUnit\Framework\MockObject\MockObject|\Symfony\Component\Form\FormInterface - */ - private function getPublishForm() - { - $formConfigInterface = $this->createMock(FormConfigInterface::class); - $formConfigInterface - ->method('getOption') - ->with('languageCode') - ->willReturn(self::LANGUAGE_CODE); - - $formRedirectUrlAfterPublish = $this->createMock(FormInterface::class); - $formRedirectUrlAfterPublish - ->method('getData') - ->willReturn('url'); - - $form = $this->createMock(FormInterface::class); - $form - ->method('getConfig') - ->willReturn($formConfigInterface); - $form - ->method('offsetGet') - ->with('redirectUrlAfterPublish') - ->willReturn($formRedirectUrlAfterPublish); - - return $form; - } - - /** - * @return \PHPUnit\Framework\MockObject\MockObject|\Symfony\Component\Form\FormInterface - */ - private function getSaveDraftForm() - { - $formConfigInterface = $this->createMock(FormConfigInterface::class); - $formConfigInterface - ->method('getOption') - ->with('languageCode') - ->willReturn(self::LANGUAGE_CODE); - - $formConfigInterface - ->method('getAction') - ->willReturn('action-url'); - - $form = $this->createMock(FormInterface::class); - $form - ->method('getConfig') - ->willReturn($formConfigInterface); - - return $form; - } -}