-
Notifications
You must be signed in to change notification settings - Fork 19
IBX-11687: Fixed ContentTypeDomainMapper to not unset the default field value #755
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
Sztig
wants to merge
3
commits into
4.6
Choose a base branch
from
IBX-11687-content-type-field-default-value-fix
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
3 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2440,6 +2440,30 @@ public function testUpdateFieldDefinitionWithEmptyStruct() | |
| ); | ||
| } | ||
|
|
||
| public function testUpdateFieldDefinitionPreservesDefaultValueWhenNotInUpdateStruct(): void | ||
| { | ||
| $repository = $this->getRepository(); | ||
| $contentTypeService = $repository->getContentTypeService(); | ||
|
|
||
| $contentTypeDraft = $this->createContentTypeDraft(); | ||
| $fieldDefinition = $contentTypeDraft->getFieldDefinition('body'); | ||
|
|
||
| self::assertNotNull($fieldDefinition); | ||
| self::assertNotNull($fieldDefinition->defaultValue); | ||
| self::assertSame(2, $fieldDefinition->position); | ||
|
|
||
| $updateStruct = $contentTypeService->newFieldDefinitionUpdateStruct(); | ||
| $updateStruct->position = 100; | ||
|
|
||
| $contentTypeService->updateFieldDefinition($contentTypeDraft, $fieldDefinition, $updateStruct); | ||
| $contentTypeDraft = $contentTypeService->loadContentTypeDraft($contentTypeDraft->id); | ||
| $updatedFieldDefinition = $contentTypeDraft->getFieldDefinition('body'); | ||
|
|
||
| self::assertNotNull($updatedFieldDefinition); | ||
| self::assertEquals($fieldDefinition->defaultValue, $updatedFieldDefinition->defaultValue); | ||
| self::assertSame(100, $updatedFieldDefinition->position); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be good to check |
||
| } | ||
|
|
||
| /** | ||
| * Test for the updateFieldDefinition() method with already defined field identifier. | ||
| * | ||
|
|
||
117 changes: 117 additions & 0 deletions
117
tests/lib/Repository/Mapper/ContentTypeDomainMapperTest.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,117 @@ | ||
| <?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\Core\Repository\Mapper; | ||
|
|
||
| use Ibexa\Contracts\Core\FieldType\FieldType as FieldTypeInterface; | ||
| use Ibexa\Contracts\Core\Persistence\Content\FieldValue; | ||
| use Ibexa\Contracts\Core\Persistence\Content\Language\Handler as SPILanguageHandler; | ||
| use Ibexa\Contracts\Core\Persistence\Content\Type\Handler as SPITypeHandler; | ||
| use Ibexa\Contracts\Core\Repository\Values\ContentType\FieldDefinitionUpdateStruct; | ||
| use Ibexa\Core\FieldType\FieldTypeRegistry; | ||
| use Ibexa\Core\FieldType\TextLine\Value as TextLineValue; | ||
| use Ibexa\Core\Repository\Mapper\ContentTypeDomainMapper; | ||
| use Ibexa\Core\Repository\Values\ContentType\FieldDefinition; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| /** | ||
| * @covers \Ibexa\Core\Repository\Mapper\ContentTypeDomainMapper | ||
| */ | ||
| final class ContentTypeDomainMapperTest extends TestCase | ||
| { | ||
| private ContentTypeDomainMapper $mapper; | ||
|
|
||
| /** @var \Ibexa\Core\FieldType\FieldTypeRegistry&\PHPUnit\Framework\MockObject\MockObject */ | ||
| private FieldTypeRegistry $fieldTypeRegistry; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| $this->fieldTypeRegistry = $this->createMock(FieldTypeRegistry::class); | ||
|
|
||
| $this->mapper = new ContentTypeDomainMapper( | ||
| $this->createMock(SPITypeHandler::class), | ||
| $this->createMock(SPILanguageHandler::class), | ||
| $this->fieldTypeRegistry, | ||
| ); | ||
| } | ||
|
|
||
| public function testBuildSPIFieldDefinitionFromUpdateStructPreservesDefaultValueWhenNotSet(): void | ||
| { | ||
| $existingDefaultValue = new TextLineValue('Foo'); | ||
| $persistedValue = new FieldValue(['data' => 'Foo']); | ||
|
|
||
| $this->configureFieldTypeRegistry($existingDefaultValue, $persistedValue); | ||
|
|
||
| $updateStruct = new FieldDefinitionUpdateStruct(); | ||
| $updateStruct->position = 100; | ||
|
|
||
| $spiFieldDefinition = $this->mapper->buildSPIFieldDefinitionFromUpdateStruct( | ||
| $updateStruct, | ||
| $this->buildFieldDefinition($existingDefaultValue), | ||
| 'eng-GB' | ||
| ); | ||
|
|
||
| self::assertSame($persistedValue, $spiFieldDefinition->defaultValue); | ||
| self::assertSame(100, $spiFieldDefinition->position); | ||
| } | ||
|
|
||
| public function testBuildSPIFieldDefinitionFromUpdateStructOverridesDefaultValueWhenExplicitlySet(): void | ||
| { | ||
| $newDefaultValue = new TextLineValue('Bar'); | ||
| $persistedValue = new FieldValue(['data' => 'Bar']); | ||
|
|
||
| $this->configureFieldTypeRegistry($newDefaultValue, $persistedValue); | ||
|
|
||
| $updateStruct = new FieldDefinitionUpdateStruct(); | ||
| $updateStruct->defaultValue = $newDefaultValue; | ||
|
|
||
| $spiFieldDefinition = $this->mapper->buildSPIFieldDefinitionFromUpdateStruct( | ||
| $updateStruct, | ||
| $this->buildFieldDefinition(new TextLineValue('Foo')), | ||
| 'eng-GB' | ||
| ); | ||
|
|
||
| self::assertSame($persistedValue, $spiFieldDefinition->defaultValue); | ||
| } | ||
|
|
||
| private function buildFieldDefinition(TextLineValue $defaultValue): FieldDefinition | ||
| { | ||
| return new FieldDefinition([ | ||
| 'id' => 1, | ||
| 'identifier' => 'my_name', | ||
| 'fieldTypeIdentifier' => 'ezstring', | ||
| 'defaultValue' => $defaultValue, | ||
| 'isTranslatable' => false, | ||
| 'isRequired' => false, | ||
| 'isInfoCollector' => false, | ||
| 'isThumbnail' => false, | ||
| 'isSearchable' => true, | ||
| 'position' => 1, | ||
| ]); | ||
| } | ||
|
|
||
| private function configureFieldTypeRegistry(TextLineValue $expectedInput, FieldValue $persistedValue): void | ||
| { | ||
| $fieldType = $this->createMock(FieldTypeInterface::class); | ||
| $fieldType->method('validateValidatorConfiguration')->willReturn([]); | ||
| $fieldType->method('validateFieldSettings')->willReturn([]); | ||
| $fieldType->method('isSearchable')->willReturn(true); | ||
| $fieldType | ||
| ->expects(self::once()) | ||
| ->method('acceptValue') | ||
| ->with($expectedInput) | ||
| ->willReturn($expectedInput); | ||
| $fieldType | ||
| ->expects(self::once()) | ||
| ->method('toPersistenceValue') | ||
| ->with($expectedInput) | ||
| ->willReturn($persistedValue); | ||
|
|
||
| $this->fieldTypeRegistry->method('getFieldType')->with('ezstring')->willReturn($fieldType); | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One thing that troubles me here is that the
defaultValuein update struct could be set tonulland it would be overriden by field definition's one because of??There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the intention is to have the field empty then the field value should be set to an empty string instead of null IMO. Entire struct here is set up in this way where null means that it was not updated.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is true for values sent from the Back Office - it's an empty string.
However, we also have an API. If one browses through contracts, one spots:
one would think that setting it to
nullalso sets the db column tonull, but it's not the case, it skips the current value ofFieldDefinitionCreateStruct. Maybe it would be enough to update description for this field in contracts? Open for discussionThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can see your point, this would probably require some dedicated flag just for
defaultValueI guess.If clearing to null is a valid use case then it is kinda a separate API design problem and how this
defaultValuein particular differs from the rest of the struct.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, this is an API problem, however, imo in this PR we are fixing one issue with unitialized value that has a default but we are introducing a new one because the behavior is now changed for an initialized field with
nullvalue.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should populate
defaultValuewith current default at creation then?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we handle this without
??? Right now, both "not provided" and "explicitly set to null" end up with the current default value, so API clients cannot intentionally clear an existing default value.