Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/4.6'
Browse files Browse the repository at this point in the history
# Conflicts:
#	tests/lib/Content/Form/Provider/GroupedContentFormFieldsProviderTest.php
  • Loading branch information
Sztig committed Mar 6, 2025
2 parents efee933 + 3edd454 commit 63d4583
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 94 deletions.
4 changes: 4 additions & 0 deletions src/bundle/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,7 @@ services:
Ibexa\ContentForms\Content\Form\Provider\GroupedContentFormFieldsProvider:
arguments:
$fieldsGroupsList: '@Ibexa\Core\Helper\FieldsGroups\FieldsGroupsList'

Ibexa\ContentForms\Content\Form\Provider\IdentifiedGroupedContentFormFieldsProvider:
arguments:
$fieldsGroupsList: '@Ibexa\Core\Helper\FieldsGroups\FieldsGroupsList'
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?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\Content\Form\Provider;

use Ibexa\Contracts\ContentForms\Content\Form\Provider\GroupedContentFormFieldsProviderInterface;
use Ibexa\Core\Helper\FieldsGroups\FieldsGroupsList;
use JMS\TranslationBundle\Model\Message;
use JMS\TranslationBundle\Translation\TranslationContainerInterface;

abstract class AbstractGroupedContentFormFieldsProvider implements GroupedContentFormFieldsProviderInterface, TranslationContainerInterface
{
protected FieldsGroupsList $fieldsGroupsList;

public function __construct(FieldsGroupsList $fieldsGroupsList)
{
$this->fieldsGroupsList = $fieldsGroupsList;
}

public function getGroupedFields(array $fieldsDataForm): array
{
$groupedFields = [];

foreach ($fieldsDataForm as $fieldForm) {
/** @var \Ibexa\Contracts\ContentForms\Data\Content\FieldData $fieldData */
$fieldData = $fieldForm->getViewData();
$fieldGroupIdentifier = $this->fieldsGroupsList->getFieldGroup($fieldData->fieldDefinition);
$groupKey = $this->getGroupKey($fieldGroupIdentifier);

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

return $groupedFields;
}

abstract protected function getGroupKey(string $fieldGroupIdentifier): string;

public static function getTranslationMessages(): array
{
return [
Message::create('content', 'ibexa_fields_groups')->setDesc('Content'),
Message::create('metadata', 'ibexa_fields_groups')->setDesc('Metadata'),
];
}
}
42 changes: 8 additions & 34 deletions src/lib/Content/Form/Provider/GroupedContentFormFieldsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,17 @@

namespace Ibexa\ContentForms\Content\Form\Provider;

use Ibexa\Contracts\ContentForms\Content\Form\Provider\GroupedContentFormFieldsProviderInterface;
use Ibexa\Core\Helper\FieldsGroups\FieldsGroupsList;
use JMS\TranslationBundle\Model\Message;
use JMS\TranslationBundle\Translation\TranslationContainerInterface;

final class GroupedContentFormFieldsProvider implements GroupedContentFormFieldsProviderInterface, TranslationContainerInterface
/**
* @deprecated 4.6.17 The "GroupedContentFormFieldsProvider" class is deprecated.
*/
final class GroupedContentFormFieldsProvider extends AbstractGroupedContentFormFieldsProvider
{
/** @var \Ibexa\Core\Helper\FieldsGroups\FieldsGroupsList */
private $fieldsGroupsList;

public function __construct(FieldsGroupsList $fieldsGroupsList)
{
$this->fieldsGroupsList = $fieldsGroupsList;
}
protected array $groupContext;

public function getGroupedFields(array $fieldsDataForm): array
protected function getGroupKey(string $fieldGroupIdentifier): string
{
$fieldsGroups = $this->fieldsGroupsList->getGroups();
$groupedFields = [];
$this->groupContext ??= $this->fieldsGroupsList->getGroups();

foreach ($fieldsDataForm as $fieldForm) {
/** @var \Ibexa\Contracts\ContentForms\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;
}

public static function getTranslationMessages(): array
{
return [
Message::create('content', 'ibexa_fields_groups')->setDesc('Content'),
Message::create('metadata', 'ibexa_fields_groups')->setDesc('Metadata'),
];
return $this->groupContext[$fieldGroupIdentifier] ?? $this->fieldsGroupsList->getDefaultGroup();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?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\Content\Form\Provider;

final class IdentifiedGroupedContentFormFieldsProvider extends AbstractGroupedContentFormFieldsProvider
{
protected function getGroupKey(string $fieldGroupIdentifier): string
{
return $fieldGroupIdentifier;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?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\ContentForms\Content\Form\Provider;

use Ibexa\Contracts\ContentForms\Data\Content\FieldData;
use Ibexa\Contracts\Core\Repository\Values\Content\Field;
use Ibexa\Core\FieldType\TextLine\Value;
use Ibexa\Core\Helper\FieldsGroups\FieldsGroupsList;
use Ibexa\Core\Repository\Values\ContentType\FieldDefinition;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\FormInterface;

abstract class AbstractGroupedContentFormFieldsProviderTest extends TestCase
{
final protected function getFieldsGroupsListMock(): FieldsGroupsList
{
$mock = $this->createMock(FieldsGroupsList::class);
$matcher = self::exactly(3);
$expectedGroups = [1 => 'group_1', 2 => 'group_2', 3 => 'group_2'];

$mock
->expects($matcher)
->method('getFieldGroup')
->willReturnCallback(static function () use ($matcher, $expectedGroups) {
return $expectedGroups[$matcher->getInvocationCount()];
});

return $mock;
}

final protected function getFormMockWithFieldData(
string $fieldDefIdentifier,
string $fieldTypeIdentifier
): FormInterface {
$formMock = $this
->getMockBuilder(FormInterface::class)
->disableOriginalConstructor()
->getMock();

$formMock
->expects(self::once())
->method('getViewData')
->willReturn(new FieldData([
'field' => new Field(['fieldDefIdentifier' => $fieldDefIdentifier]),
'fieldDefinition' => new FieldDefinition(['fieldTypeIdentifier' => $fieldTypeIdentifier]),
'value' => new Value('value'),
]));

$formMock
->expects(self::once())
->method('getName')
->willReturn($fieldDefIdentifier);

return $formMock;
}

final protected function getTestForms(): array
{
return [
$this->getFormMockWithFieldData('first_field', 'first_field_type'),
$this->getFormMockWithFieldData('second_field', 'second_field_type'),
$this->getFormMockWithFieldData('third_field', 'third_field_type'),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,12 @@
namespace Ibexa\Tests\ContentForms\Content\Form\Provider;

use Ibexa\ContentForms\Content\Form\Provider\GroupedContentFormFieldsProvider;
use Ibexa\Contracts\ContentForms\Data\Content\FieldData;
use Ibexa\Contracts\Core\Repository\Values\Content\Field;
use Ibexa\Core\FieldType\TextLine\Value;
use Ibexa\Core\Helper\FieldsGroups\FieldsGroupsList;
use Ibexa\Core\Repository\Values\ContentType\FieldDefinition;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\FormInterface;

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

$fieldsGroupsListMock = $this->getFieldsGroupsListMock();
$fieldsGroupsListMock
->expects(self::once())
->method('getGroups')
Expand All @@ -37,23 +24,7 @@ public function testGetGroupedFields(): void
]);

$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]);
$result = $subject->getGroupedFields($this->getTestForms());

$expected = [
'Group 1' => [
Expand All @@ -65,33 +36,6 @@ public function testGetGroupedFields(): void
],
];

self::assertEquals($expected, $result);
}

/**
* @return \Symfony\Component\Form\FormInterface
*/
private function getFormMockWithFieldData(
string $fieldDefIdentifier,
string $fieldTypeIdentifier
) {
$formMock = $this
->getMockBuilder(FormInterface::class)
->disableOriginalConstructor()
->getMock();
$formMock
->expects(self::once())
->method('getViewData')
->willReturn(new FieldData([
'field' => new Field(['fieldDefIdentifier' => $fieldDefIdentifier]),
'fieldDefinition' => new FieldDefinition(['fieldTypeIdentifier' => $fieldTypeIdentifier]),
'value' => new Value('value'),
]));
$formMock
->expects(self::once())
->method('getName')
->willReturn($fieldDefIdentifier);

return $formMock;
$this->assertEquals($expected, $result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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\ContentForms\Content\Form\Provider;

use Ibexa\ContentForms\Content\Form\Provider\IdentifiedGroupedContentFormFieldsProvider;

final class IdentifiedGroupedContentFormFieldsProviderTest extends AbstractGroupedContentFormFieldsProviderTest
{
public function testGetGroupedFields(): void
{
$fieldsGroupsListMock = $this->getFieldsGroupsListMock();

$subject = new IdentifiedGroupedContentFormFieldsProvider($fieldsGroupsListMock);
$result = $subject->getGroupedFields($this->getTestForms());

$expected = [
'group_1' => [
0 => 'first_field',
],
'group_2' => [
0 => 'second_field',
1 => 'third_field',
],
];

$this->assertEquals($expected, $result);
}
}

0 comments on commit 63d4583

Please sign in to comment.