Skip to content
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

IBX-902: Refactored GroupedFields as a separate service #46

Merged
merged 4 commits into from
Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,17 @@
"psr-4": {
"EzSystems\\EzPlatformContentFormsBundle\\": "src/bundle/",
"EzSystems\\EzPlatformContentForms\\": "src/lib/",
"EzSystems\\EzPlatformContentForms\\Features\\": "features/"
"EzSystems\\EzPlatformContentForms\\Features\\": "features/",
"Ibexa\\ContentForms\\": "src/lib/",
"Ibexa\\Bundle\\ContentForms\\": "src/bundle/",
"Ibexa\\Contracts\\ContentForms\\": "src/contracts/"
}
},
"autoload-dev": {
"psr-4": {
"EzSystems\\EzPlatformContentForms\\Tests\\": "src/lib/Tests",
"EzSystems\\EzPlatformContentFormsBundle\\Tests\\": "src/bundle/Tests"
"EzSystems\\EzPlatformContentFormsBundle\\Tests\\": "src/bundle/Tests",
"Ibexa\\Tests\\ContentForms\\": "tests/lib/"
}
},
"extra": {
Expand Down
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
<testsuite name="ContentFormsBundle tests">
<directory suffix="Test.php">./src/bundle/Tests</directory>
</testsuite>
<testsuite name="Ibexa ContentForms tests">
<directory suffix="Test.php">./tests/</directory>
</testsuite>
</testsuites>

<filter>
Expand Down
7 changes: 7 additions & 0 deletions src/bundle/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,10 @@ services:
- { name: kernel.event_subscriber }

EzSystems\EzPlatformContentForms\ConfigResolver\MaxUploadSize: ~

Ibexa\Contracts\ContentForms\Content\Form\Provider\GroupedContentFormFieldsProviderInterface:
'@Ibexa\ContentForms\Content\Form\Provider\GroupedContentFormFieldsProvider'

Ibexa\ContentForms\Content\Form\Provider\GroupedContentFormFieldsProvider:
arguments:
$fieldsGroupsList: '@ezpublish.fields_groups.list'
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?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
* @phpstan-return array<string, array<int, string>> Array of fieldGroupIdentifier grouped by fieldGroupName.
*/
public function getGroupedFields(array $fieldsDataForm): array;
}
40 changes: 40 additions & 0 deletions src/lib/Content/Form/Provider/GroupedContentFormFieldsProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?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;
}

public function getGroupedFields(array $fieldsDataForm): array
{
$fieldsGroups = $this->fieldsGroupsList->getGroups();
$groupedFields = [];

foreach ($fieldsDataForm as $fieldForm) {
/** @var \EzSystems\EzPlatformContentForms\Data\Content\FieldData $fieldData */
$fieldData = $fieldForm->getViewData();
$fieldGroupIdentifier = $this->fieldsGroupsList->getFieldGroup($fieldData->fieldDefinition);
$fieldGroupName = $fieldsGroups[$fieldGroupIdentifier] ?? $this->fieldsGroupsList->getDefaultGroup();

$groupedFields[$fieldGroupName][] = $fieldForm->getName();
}

return $groupedFields;
}
}
48 changes: 5 additions & 43 deletions src/lib/Content/View/Builder/AbstractContentViewBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@
use eZ\Publish\API\Repository\Values\Content\Language;
use eZ\Publish\API\Repository\Values\Content\Location;
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
use eZ\Publish\Core\Helper\FieldsGroups\FieldsGroupsList;
use eZ\Publish\Core\MVC\ConfigResolverInterface;
use eZ\Publish\Core\MVC\Symfony\Locale\UserLanguagePreferenceProviderInterface;
use eZ\Publish\Core\MVC\Symfony\View\Configurator;
use eZ\Publish\Core\MVC\Symfony\View\ParametersInjector;
use EzSystems\EzPlatformContentForms\Form\ActionDispatcher\ActionDispatcherInterface;
use Symfony\Component\Form\Form;
use Ibexa\Contracts\ContentForms\Content\Form\Provider\GroupedContentFormFieldsProviderInterface;

/*
* @internal
Expand All @@ -44,8 +43,8 @@ abstract class AbstractContentViewBuilder
/** @var \eZ\Publish\Core\MVC\ConfigResolverInterface */
protected $configResolver;

/** @var \eZ\Publish\Core\Helper\FieldsGroups\FieldsGroupsList */
private $fieldsGroupsList;
/** @var \Ibexa\Contracts\ContentForms\Content\Form\Provider\GroupedContentFormFieldsProviderInterface */
protected $groupedContentFormFieldsProvider;

/** @var \eZ\Publish\API\Repository\ContentService */
protected $contentService;
Expand All @@ -57,7 +56,7 @@ public function __construct(
ActionDispatcherInterface $contentActionDispatcher,
UserLanguagePreferenceProviderInterface $languagePreferenceProvider,
ConfigResolverInterface $configResolver,
FieldsGroupsList $fieldsGroupsList,
GroupedContentFormFieldsProviderInterface $groupedContentFormFieldsProvider,
ContentService $contentService
) {
$this->repository = $repository;
Expand All @@ -66,7 +65,7 @@ public function __construct(
$this->contentActionDispatcher = $contentActionDispatcher;
$this->languagePreferenceProvider = $languagePreferenceProvider;
$this->configResolver = $configResolver;
$this->fieldsGroupsList = $fieldsGroupsList;
$this->groupedContentFormFieldsProvider = $groupedContentFormFieldsProvider;
$this->contentService = $contentService;
}

Expand Down Expand Up @@ -111,41 +110,4 @@ protected function resolveLanguage(array $parameters): Language
throw new InvalidArgumentException('Language',
'No language information provided. Are you missing language or languageCode parameters?');
}

protected function getGroupedFields(Form $form): array
{
$fieldsDataForm = $form->get('fieldsData');
$groupedFields = [];

/** @var \Symfony\Component\Form\Form $fieldForm */
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;
}
}
4 changes: 3 additions & 1 deletion src/lib/Content/View/Builder/ContentCreateViewBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ public function buildView(array $parameters)
'language' => $language,
'parent_location' => $location,
'form' => $form->createView(),
'grouped_fields' => $this->getGroupedFields($form),
'grouped_fields' => $this->groupedContentFormFieldsProvider->getGroupedFields(
$form->get('fieldsData')->all()
),
]);

$this->viewParametersInjector->injectViewParameters($view, $parameters);
Expand Down
4 changes: 3 additions & 1 deletion src/lib/Content/View/Builder/ContentEditViewBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ public function buildView(array $parameters)
'language' => $language,
'content_type' => $contentType,
'form' => $form->createView(),
'grouped_fields' => $this->getGroupedFields($form),
'grouped_fields' => $this->groupedContentFormFieldsProvider->getGroupedFields(
$form->get('fieldsData')->all()
),
Comment on lines +120 to +122
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH while it's a change to initial implementation, I believe that probably AbstractType::buildView() from the Form component should have been used instead for grouping data for the form. After all, how fields are grouped should be part of the form itself, not part of a view.

Just a note though. Optional.

]);

$this->viewParametersInjector->injectViewParameters($view, $parameters);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?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;

final class GroupedContentFormFieldsProviderTest extends TestCase
{
public function testGetGroupedFields(): void
{
$fieldsGroupsListMock = $this->createMock(FieldsGroupsList::class);
$fieldsGroupsListMock
->expects($this->exactly(3))
->method('getFieldGroup')
->withConsecutive()
->willReturnOnConsecutiveCalls('group_1', 'group_2', 'group_2');

$fieldsGroupsListMock
->expects($this->once())
->method('getGroups')
->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 \Symfony\Component\Form\FormInterface
*/
private function getFormMockWithFieldData(
string $fieldDefIdentifier,
string $fieldTypeIdentifier
) {
damianz5 marked this conversation as resolved.
Show resolved Hide resolved
$formMock = $this
->getMockBuilder(FormInterface::class)
->disableOriginalConstructor()
->getMock();
$formMock
->expects($this->once())
->method('getViewData')
->willReturn(new FieldData([
'field' => new Field(['fieldDefIdentifier' => $fieldDefIdentifier]),
'fieldDefinition' => new FieldDefinition(['fieldTypeIdentifier' => $fieldTypeIdentifier]),
'value' => new Value('value'),
]));
$formMock
->expects($this->once())
->method('getName')
->willReturn($fieldDefIdentifier);

return $formMock;
}
}