-
Notifications
You must be signed in to change notification settings - Fork 2
IBX-9277: Fixed error when translating User content with non-translatable fields #114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mateuszdebinski
wants to merge
4
commits into
4.6
Choose a base branch
from
IBX-9277-Error-when-translate-User-content
base: 4.6
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
80 changes: 80 additions & 0 deletions
80
src/lib/Form/Processor/User/UserTranslationSecureListener.php
This file contains hidden or 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,80 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
| * @license For full copyright and license information view LICENSE file distributed with this source code. | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Ibexa\ContentForms\Form\Processor\User; | ||
|
|
||
| use Ibexa\ContentForms\Event\ContentFormEvents; | ||
| use Ibexa\ContentForms\Event\FormActionEvent; | ||
| use Ibexa\Contracts\Core\Repository\ContentService; | ||
| use Ibexa\Contracts\Core\Repository\UserService; | ||
| use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
|
||
| /** | ||
| * Re-applies the User data after publishing a non-main-language translation. | ||
| * | ||
| * The "user_account" field (ezuser) is non-translatable and its form is disabled while | ||
| * translating (see UserAccountFieldValueFormMapper::mapFieldValueForm()), so it is never | ||
| * submitted as part of the translation's content update. Without this listener re-running | ||
| * UserService::updateUser() for the translated language after publish, translating User | ||
| * content throws a content field validation error / leaves the user_account field broken | ||
| * for that language. | ||
| * | ||
| * It also prevents data loss on translation removal: without re-persisting the user_account | ||
| * field for the translation's language here, ContentService::deleteTranslation() on that | ||
| * language would delete the User itself (login, password, etc.), even though the underlying | ||
| * content object survives. | ||
| */ | ||
| final class UserTranslationSecureListener implements EventSubscriberInterface | ||
| { | ||
| private UserService $userService; | ||
|
|
||
| private ContentService $contentService; | ||
|
|
||
| public function __construct( | ||
| UserService $userService, | ||
| ContentService $contentService | ||
| ) { | ||
| $this->userService = $userService; | ||
| $this->contentService = $contentService; | ||
| } | ||
|
|
||
| public static function getSubscribedEvents(): array | ||
| { | ||
| return [ | ||
| ContentFormEvents::CONTENT_PUBLISH => ['onPublish', 5], | ||
| ContentFormEvents::CONTENT_PUBLISH_AND_EDIT => ['onPublish', 5], | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException | ||
| * @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException | ||
| * @throws \Ibexa\Contracts\Core\Repository\Exceptions\ContentValidationException | ||
| * @throws \Ibexa\Contracts\Core\Repository\Exceptions\ContentFieldValidationException | ||
| * @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException | ||
| */ | ||
| public function onPublish(FormActionEvent $event): void | ||
| { | ||
| $content = $event->getPayload('content'); | ||
| if (null === $content || !$this->userService->isUser($content)) { | ||
| return; | ||
| } | ||
|
|
||
| $languageCode = $event->getForm()->getConfig()->getOption('languageCode'); | ||
| if ($languageCode === $content->contentInfo->mainLanguageCode) { | ||
| return; | ||
| } | ||
|
|
||
| $user = $this->userService->loadUser($content->id, [$languageCode]); | ||
| $userStruct = $this->userService->newUserUpdateStruct(); | ||
| $userStruct->contentUpdateStruct = $this->contentService->newContentUpdateStruct(); | ||
| $userStruct->contentUpdateStruct->initialLanguageCode = $languageCode; | ||
|
|
||
| $this->userService->updateUser($user, $userStruct); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
110 changes: 110 additions & 0 deletions
110
tests/integration/UserTranslationSecureListenerTest.php
This file contains hidden or 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,110 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
| * @license For full copyright and license information view LICENSE file distributed with this source code. | ||
| */ | ||
| declare(strict_types=1); | ||
|
|
||
| namespace Ibexa\Tests\Integration\ContentForms; | ||
|
|
||
| use Ibexa\ContentForms\Event\ContentFormEvents; | ||
| use Ibexa\ContentForms\Event\FormActionEvent; | ||
| use Ibexa\ContentForms\Form\Processor\User\UserTranslationSecureListener; | ||
| use Ibexa\Tests\Integration\Core\RepositoryTestCase; | ||
| use Symfony\Component\EventDispatcher\EventDispatcher; | ||
| use Symfony\Component\Form\Form; | ||
| use Symfony\Component\Form\FormConfigBuilder; | ||
|
|
||
| final class UserTranslationSecureListenerTest extends RepositoryTestCase | ||
| { | ||
| private const NEW_LANGUAGE = 'ger-DE'; | ||
|
|
||
| /** | ||
| * @dataProvider providePublishEventNames | ||
| */ | ||
| public function testTranslationFlowDoesNotRemoveUserData(string $eventName, string $loginSuffix): void | ||
| { | ||
| $login = 'jdoe_' . $loginSuffix; | ||
| $email = $login . '@mail.invalid'; | ||
|
|
||
| $ibexaTestCore = $this->getIbexaTestCore(); | ||
| $userService = $ibexaTestCore->getUserService(); | ||
| $contentService = $ibexaTestCore->getContentService(); | ||
| $contentTypeService = $ibexaTestCore->getContentTypeService(); | ||
|
|
||
| $user = $this->createUser($login, 'John', 'Doe'); | ||
| $mainLanguageCode = $user->contentInfo->mainLanguageCode; | ||
|
|
||
| $userContentType = $contentTypeService->loadContentTypeByIdentifier('user'); | ||
| $translatableFieldDefinitions = []; | ||
| foreach ($userContentType->fieldDefinitions as $fieldDefinition) { | ||
| if ( | ||
| $fieldDefinition->identifier !== 'user_account' | ||
| && $fieldDefinition->isTranslatable | ||
| && \in_array($fieldDefinition->fieldTypeIdentifier, ['ezstring', 'eztext'], true) | ||
| ) { | ||
| $translatableFieldDefinitions[] = $fieldDefinition; | ||
| } | ||
| } | ||
| self::assertNotEmpty($translatableFieldDefinitions, 'Expected the "user" content type to have translatable fields besides "user_account".'); | ||
|
|
||
| $draft = $contentService->createContentDraft($user->contentInfo); | ||
| $updateStruct = $contentService->newContentUpdateStruct(); | ||
| $updateStruct->initialLanguageCode = self::NEW_LANGUAGE; | ||
| foreach ($translatableFieldDefinitions as $fieldDefinition) { | ||
| $updateStruct->setField($fieldDefinition->identifier, 'German value', self::NEW_LANGUAGE); | ||
| } | ||
| $updatedDraft = $contentService->updateContent($draft->versionInfo, $updateStruct); | ||
| $publishedContent = $contentService->publishVersion($updatedDraft->versionInfo); | ||
|
|
||
| $form = new Form( | ||
| (new FormConfigBuilder('form', null, new EventDispatcher(), [ | ||
| 'languageCode' => self::NEW_LANGUAGE, | ||
| ]))->getFormConfig() | ||
| ); | ||
|
|
||
| $eventDispatcher = new EventDispatcher(); | ||
| $eventDispatcher->addSubscriber(new UserTranslationSecureListener($userService, $contentService)); | ||
| $eventDispatcher->dispatch( | ||
| new FormActionEvent( | ||
| $form, | ||
| null, | ||
| 'publish', | ||
| [], | ||
| ['content' => $publishedContent] | ||
| ), | ||
| $eventName | ||
| ); | ||
|
|
||
| $userAfterTranslation = $userService->loadUserByLogin($login); | ||
| self::assertSame($login, $userAfterTranslation->login); | ||
| self::assertSame($email, $userAfterTranslation->email); | ||
| self::assertTrue($userAfterTranslation->enabled); | ||
|
|
||
| $contentService->deleteTranslation($publishedContent->contentInfo, self::NEW_LANGUAGE); | ||
| $userAfterDeletion = $userService->loadUserByLogin($login); | ||
|
|
||
| self::assertSame($login, $userAfterDeletion->login); | ||
| self::assertSame($email, $userAfterDeletion->email); | ||
| self::assertTrue($userAfterDeletion->enabled); | ||
| self::assertSame($userAfterTranslation->passwordHash, $userAfterDeletion->passwordHash); | ||
|
|
||
| $contentAfterDeletion = $contentService->loadContent($publishedContent->id, [$mainLanguageCode]); | ||
| self::assertSame( | ||
| [$mainLanguageCode], | ||
| $contentAfterDeletion->versionInfo->languageCodes | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @return iterable<string, array{0: string}> | ||
| */ | ||
| public static function providePublishEventNames(): iterable | ||
| { | ||
| return [ | ||
| 'publish' => [ContentFormEvents::CONTENT_PUBLISH, 'publish'], | ||
| 'publish and edit' => [ContentFormEvents::CONTENT_PUBLISH_AND_EDIT, 'publish_and_edit'], | ||
| ]; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.