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-8897: Fixed incorrect extrapolation of parentheses in the name schema pattern #449

Open
wants to merge 3 commits into
base: 4.6
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion src/lib/Repository/NameSchema/NameSchemaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,17 @@ protected function tokenParts(string $token): array
protected function filterNameSchema(string $nameSchema): array
{
$retNamePattern = $nameSchema;
$foundGroups = preg_match_all('/\((.+)\)/U', $nameSchema, $groupArray);
$foundGroups = preg_match_all('/<.*\((.*<.+>.*)\).*>/U', $nameSchema, $groupArray);
$groupLookupTable = [];

if ($foundGroups) {
$i = 0;
foreach ($groupArray[1] as $group) {
// Skip the group if it has no fields to parse
if (!preg_match('/<.*>/', $group)) {
continue;
}
Comment on lines +254 to +257
Copy link
Member

Choose a reason for hiding this comment

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

After looking into this we could probably do more complex regex in L248:

// match groups "( )" containing fields "< >" - curly brackets without any fields are not a group, but a literal string
$foundGroups = preg_match_all('/\((.*<.+>.*)\)/U', $nameSchema, $groupArray);

But as we discussed internally, I'm not sure which solution would be better for @ibexa/php-dev. The current one is maybe more readable.

POV ping @ibexa/php-dev

Copy link
Contributor

Choose a reason for hiding this comment

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

If we want to proceed with the above, then tests related to this functionality should be expanded (which would be welcome by itself anyway, by the way).

This would ensure that any changes to the name schema processing, including regexp changes to improve performance, are not breaking anything - especially not any nested patterns.

Copy link
Author

Choose a reason for hiding this comment

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

I've already modified a test to cover the "new" part of this functionality, is it sufficient of should i cover more cases like so?


// Create meta-token for group
$metaToken = self::META_STRING . $i;

Expand Down
120 changes: 80 additions & 40 deletions tests/lib/Repository/NameSchema/NameSchemaServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,35 @@ public function testResolveUrlAliasSchemaFallbackToNameSchema(): void
* 0: array<int|string, array<string, \Ibexa\Contracts\Core\FieldType\Value>>,
* 1: array<string, array<string, string>>,
* 2: array<string>,
* 3: array<string, string>
* 3: array<int, array<string, string>>
* }>
*/
public static function getDataForTestResolveNameSchema(): iterable
{
yield 'Default: Field Map and Languages taken from Content Version' => [
[],
[
'eng-GB' => ['text2' => 'two'],
'cro-HR' => ['text2' => 'dva'],
'eng-GB' => ['text1' => 'one', 'text2' => 'two'],
'cro-HR' => ['text1' => 'jedan', 'text2' => 'dva'],
],
[],
[
'eng-GB' => 'two',
'cro-HR' => 'dva',
[
'eng-GB' => 'one (text)',
'cro-HR' => 'jedan (text)',
],
[
'eng-GB' => 'one - two',
'cro-HR' => 'jedan - dva',
],
[
'eng-GB' => 'one - two (two) two (text2)',
'cro-HR' => 'jedan - dva (dva) dva (text2)',
],
[
'eng-GB' => 'one - two (EZMETAGROUP_0) two',
'cro-HR' => 'jedan - dva (EZMETAGROUP_0) dva',
],
],
];

Expand All @@ -98,8 +112,23 @@ public static function getDataForTestResolveNameSchema(): iterable
],
['eng-GB', 'cro-HR'],
[
'eng-GB' => 'three',
'cro-HR' => 'Dva',
[
'eng-GB' => ' (text)',
'cro-HR' => ' (text)',
],
[
'eng-GB' => 'three',
'cro-HR' => ' - Dva',
],
[
'eng-GB' => 'three (two) two (text2)',
'cro-HR' => ' - Dva (Dva) Dva (text2)',
],
//known incorrect behavior - using the same group that was in two different statements (<text1> - <text2>)
[
'eng-GB' => 'three (EZMETAGROUP_0) two',
'cro-HR' => ' - Dva (EZMETAGROUP_0) Dva',
],
],
];
}
Expand All @@ -110,7 +139,7 @@ public static function getDataForTestResolveNameSchema(): iterable
* @param array<int|string, array<string, \Ibexa\Contracts\Core\FieldType\Value>> $fieldMap
* @param array<string, array<string, string>> $tokenValues
* @param array<string> $languageCodes
* @param array<string, string> $expectedNames
* @param array<int, array<string, string>> $expectedNames
*/
public function testResolveNameSchema(
array $fieldMap,
Expand All @@ -119,27 +148,38 @@ public function testResolveNameSchema(
array $expectedNames
): void {
$content = $this->buildTestContentObject();
$nameSchema = '<text3|text2>';
$contentType = $this->buildTestContentTypeStub($nameSchema, $nameSchema);
$event = new ResolveContentNameSchemaEvent(
$content,
['field' => ['text3', 'text2']],
$contentType,
$fieldMap,
$languageCodes
);
$event->setTokenValues($tokenValues);

$nameSchemaService = $this->buildNameSchemaService(
$event
);

$result = $nameSchemaService->resolveContentNameSchema($content, $fieldMap, $languageCodes, $contentType);
$schemas = [
'<text1> (text)',
'<text3|(<text1> - <text2>)>',
'<text3|(<text1> - <text2>)> (<text2>) <text2> (text2)',
'<text3|(<text1> - <text2>)> (<text1> - <text2>) <text2>',
];

self::assertEquals(
$expectedNames,
$result
);
foreach ($schemas as $index => $nameSchema) {
$contentType = $this->buildTestContentTypeStub($nameSchema, $nameSchema);
$event = new ResolveContentNameSchemaEvent(
$content,
['field' => ['text3', 'text2', 'text1']],
$contentType,
$fieldMap,
$languageCodes
);
$event->setTokenValues($tokenValues);

$nameSchemaService = $this->buildNameSchemaService($event);

$result = $nameSchemaService->resolveContentNameSchema(
$content,
$fieldMap,
$languageCodes,
$contentType
);

self::assertEquals(
$expectedNames[$index],
$result
);
}
}

/**
Expand All @@ -163,10 +203,10 @@ public static function getDataForTestResolve(): array
['field' => ['text1']],
'<text1>',
[
'text1' => ['cro-HR' => new TextLineValue('jedan'), 'eng-GB' => new TextLineValue('one')],
'text2' => ['cro-HR' => new TextLineValue('Dva'), 'eng-GB' => new TextLineValue('two')],
'text3' => ['eng-GB' => new TextLineValue('three')],
],
'text1' => ['cro-HR' => new TextLineValue('jedan'), 'eng-GB' => new TextLineValue('one')],
'text2' => ['cro-HR' => new TextLineValue('Dva'), 'eng-GB' => new TextLineValue('two')],
'text3' => ['eng-GB' => new TextLineValue('three')],
],
[
'eng-GB' => 'one',
'cro-HR' => 'jedan',
Expand All @@ -180,10 +220,10 @@ public static function getDataForTestResolve(): array
['field' => ['text2']],
'<text2>',
[
'text1' => ['cro-HR' => new TextLineValue('jedan'), 'eng-GB' => new TextLineValue('one')],
'text2' => ['cro-HR' => new TextLineValue('Dva'), 'eng-GB' => new TextLineValue('two')],
'text3' => ['eng-GB' => new TextLineValue('three')],
],
'text1' => ['cro-HR' => new TextLineValue('jedan'), 'eng-GB' => new TextLineValue('one')],
'text2' => ['cro-HR' => new TextLineValue('Dva'), 'eng-GB' => new TextLineValue('two')],
'text3' => ['eng-GB' => new TextLineValue('three')],
],
[
'eng-GB' => 'two',
'cro-HR' => 'dva',
Expand All @@ -197,10 +237,10 @@ public static function getDataForTestResolve(): array
['field' => ['text2', 'text2']],
'Hello, <text1> and <text2> and then goodbye and hello again',
[
'text1' => ['cro-HR' => new TextLineValue('jedan'), 'eng-GB' => new TextLineValue('one')],
'text2' => ['cro-HR' => new TextLineValue('Dva'), 'eng-GB' => new TextLineValue('two')],
'text3' => ['eng-GB' => new TextLineValue('three')],
],
'text1' => ['cro-HR' => new TextLineValue('jedan'), 'eng-GB' => new TextLineValue('one')],
'text2' => ['cro-HR' => new TextLineValue('Dva'), 'eng-GB' => new TextLineValue('two')],
'text3' => ['eng-GB' => new TextLineValue('three')],
],
[
'eng-GB' => 'Hello, one and two and then goodbye...',
'cro-HR' => 'Hello, jedan and dva and then goodb...',
Expand Down
Loading