-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/4.6'
# Conflicts: # tests/lib/Content/Form/Provider/GroupedContentFormFieldsProviderTest.php
- Loading branch information
Showing
7 changed files
with
188 additions
and
94 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
50 changes: 50 additions & 0 deletions
50
src/lib/Content/Form/Provider/AbstractGroupedContentFormFieldsProvider.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,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'), | ||
]; | ||
} | ||
} |
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
17 changes: 17 additions & 0 deletions
17
src/lib/Content/Form/Provider/IdentifiedGroupedContentFormFieldsProvider.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,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; | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
tests/lib/Content/Form/Provider/AbstractGroupedContentFormFieldsProviderTest.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,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'), | ||
]; | ||
} | ||
} |
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
34 changes: 34 additions & 0 deletions
34
tests/lib/Content/Form/Provider/IdentifiedGroupedContentFormFieldsProviderTest.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,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); | ||
} | ||
} |