-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IBX-902: Refactored GroupedFields as a separate service.
- Loading branch information
Showing
9 changed files
with
220 additions
and
48 deletions.
There are no files selected for viewing
This file contains 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 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 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
20 changes: 20 additions & 0 deletions
20
src/contracts/Content/Form/Provider/GroupedContentFormFieldsProviderInterface.php
This file contains 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,20 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) eZ Systems 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\Contracts\ContentForms\Content\Form\Provider; | ||
|
||
interface GroupedContentFormFieldsProviderInterface | ||
{ | ||
/** | ||
* @param \Symfony\Component\Form\FormInterface[] $fieldsDataForm | ||
* @psalm-return array<string, array<int, string>> Array of fieldGroupIdentifier grouped by fieldGroupName. | ||
*/ | ||
public function getGroupedFields(array $fieldsDataForm): array; | ||
} | ||
|
||
class_alias(GroupedContentFormFieldsProviderInterface::class, 'EzSystems\EzPlatformContentForms\Content\Form\Provider\GroupedContentFormFieldsProviderInterface'); |
61 changes: 61 additions & 0 deletions
61
src/lib/Content/Form/Provider/GroupedContentFormFieldsProvider.php
This file contains 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,61 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) eZ Systems 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\Content\Form\Provider; | ||
|
||
use eZ\Publish\Core\Helper\FieldsGroups\FieldsGroupsList; | ||
use Ibexa\Contracts\ContentForms\Content\Form\Provider\GroupedContentFormFieldsProviderInterface; | ||
|
||
final class GroupedContentFormFieldsProvider implements GroupedContentFormFieldsProviderInterface | ||
{ | ||
/** @var \eZ\Publish\Core\Helper\FieldsGroups\FieldsGroupsList */ | ||
private $fieldsGroupsList; | ||
|
||
public function __construct(FieldsGroupsList $fieldsGroupsList) | ||
{ | ||
$this->fieldsGroupsList = $fieldsGroupsList; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getGroupedFields(array $fieldsDataForm): array | ||
{ | ||
$groupedFields = []; | ||
|
||
foreach ($fieldsDataForm as $fieldForm) { | ||
/** @var \EzSystems\EzPlatformContentForms\Data\Content\FieldData $fieldData */ | ||
$fieldData = $fieldForm->getViewData(); | ||
|
||
$fieldGroup = $this->fieldsGroupsList->getFieldGroup( | ||
$fieldData->fieldDefinition | ||
); | ||
|
||
$groupedFields[$fieldGroup][] = $fieldForm->getName(); | ||
} | ||
|
||
return $this->sortGroupedFields($groupedFields); | ||
} | ||
|
||
/** | ||
* Makes sure fields groups order in the same like in YAML definition. | ||
*/ | ||
private function sortGroupedFields(array $groupedFields): array | ||
{ | ||
$groupedFieldsList = []; | ||
|
||
$fieldsGroups = $this->fieldsGroupsList->getGroups(); | ||
foreach ($fieldsGroups as $fieldGroupIdentifier => $fieldGroupName) { | ||
if (array_key_exists($fieldGroupIdentifier, $groupedFields)) { | ||
$groupedFieldsList[$fieldGroupName] = $groupedFields[$fieldGroupIdentifier]; | ||
} | ||
} | ||
|
||
return $groupedFieldsList; | ||
} | ||
} |
This file contains 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 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 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
111 changes: 111 additions & 0 deletions
111
tests/lib/Content/Form/Provider/GroupedContentFormFieldsProviderTest.php
This file contains 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,111 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) eZ Systems 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\ContentForms\Content\Form\Provider; | ||
|
||
use eZ\Publish\API\Repository\Values\Content\Field; | ||
use eZ\Publish\Core\FieldType\TextLine\Value; | ||
use eZ\Publish\Core\Helper\FieldsGroups\FieldsGroupsList; | ||
use eZ\Publish\Core\Repository\Values\ContentType\FieldDefinition; | ||
use EzSystems\EzPlatformContentForms\Data\Content\FieldData; | ||
use Ibexa\ContentForms\Content\Form\Provider\GroupedContentFormFieldsProvider; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Form\FormInterface; | ||
|
||
class GroupedContentFormFieldsProviderTest extends TestCase | ||
{ | ||
public function testGetGroupedFields() | ||
{ | ||
$fieldsGroupsListMock = $this->getMockBuilder(FieldsGroupsList::class)->getMock(); | ||
$fieldsGroupsListMock | ||
->expects($this->at(0)) | ||
->method('getFieldGroup') | ||
->withAnyParameters() | ||
->willReturn('group_1'); | ||
|
||
$fieldsGroupsListMock | ||
->expects($this->at(1)) | ||
->method('getFieldGroup') | ||
->withAnyParameters() | ||
->willReturn('group_2'); | ||
|
||
$fieldsGroupsListMock | ||
->expects($this->at(2)) | ||
->method('getFieldGroup') | ||
->withAnyParameters() | ||
->willReturn('group_2'); | ||
|
||
$fieldsGroupsListMock | ||
->expects($this->once()) | ||
->method('getGroups') | ||
->withAnyParameters() | ||
->willReturn([ | ||
'group_1' => 'Group 1', | ||
'group_2' => 'Group 2', | ||
]); | ||
|
||
$subject = new GroupedContentFormFieldsProvider($fieldsGroupsListMock); | ||
|
||
|
||
$form1 = $this->getFormMockWithFieldData( | ||
'first_field', | ||
'first_field_type', | ||
); | ||
|
||
$form2 = $this->getFormMockWithFieldData( | ||
'second_field', | ||
'second_field_type', | ||
); | ||
|
||
$form3 = $this->getFormMockWithFieldData( | ||
'third_field', | ||
'third_field_type', | ||
); | ||
|
||
$result = $subject->getGroupedFields([$form1, $form2, $form3]); | ||
|
||
$expected = [ | ||
"Group 1" => [ | ||
0 => "first_field", | ||
], | ||
"Group 2" => [ | ||
0 => "second_field", | ||
1 => "third_field", | ||
] | ||
]; | ||
|
||
$this->assertEquals($expected, $result); | ||
} | ||
|
||
/** | ||
* @return mixed|\PHPUnit\Framework\MockObject\MockObject|\Symfony\Component\Form\FormInterface | ||
*/ | ||
protected function getFormMockWithFieldData( | ||
string $fieldDefIdentifier, | ||
string $fieldTypeIdentifier | ||
) { | ||
$formMock = $this | ||
->getMockBuilder(FormInterface::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$formMock | ||
->expects($this->any()) | ||
->method('getViewData') | ||
->willReturn(new FieldData([ | ||
'field' => new Field(['fieldDefIdentifier' => $fieldDefIdentifier]), | ||
'fieldDefinition' => new FieldDefinition(['fieldTypeIdentifier' => $fieldTypeIdentifier]), | ||
'value' => new Value('value'), | ||
])); | ||
$formMock | ||
->expects($this->any()) | ||
->method('getName') | ||
->willReturn($fieldDefIdentifier); | ||
|
||
return $formMock; | ||
} | ||
} |