diff --git a/src/lib/Repository/NameSchema/NameSchemaService.php b/src/lib/Repository/NameSchema/NameSchemaService.php index 782a154191..8645a5622c 100644 --- a/src/lib/Repository/NameSchema/NameSchemaService.php +++ b/src/lib/Repository/NameSchema/NameSchemaService.php @@ -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; + } + // Create meta-token for group $metaToken = self::META_STRING . $i; diff --git a/tests/lib/Repository/NameSchema/NameSchemaServiceTest.php b/tests/lib/Repository/NameSchema/NameSchemaServiceTest.php index 95c9d6a820..99a0aec3ec 100644 --- a/tests/lib/Repository/NameSchema/NameSchemaServiceTest.php +++ b/tests/lib/Repository/NameSchema/NameSchemaServiceTest.php @@ -68,7 +68,7 @@ public function testResolveUrlAliasSchemaFallbackToNameSchema(): void * 0: array>, * 1: array>, * 2: array, - * 3: array + * 3: array> * }> */ public static function getDataForTestResolveNameSchema(): iterable @@ -76,13 +76,27 @@ 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', + ], ], ]; @@ -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 ( - ) + [ + 'eng-GB' => 'three (EZMETAGROUP_0) two', + 'cro-HR' => ' - Dva (EZMETAGROUP_0) Dva', + ], ], ]; } @@ -110,7 +139,7 @@ public static function getDataForTestResolveNameSchema(): iterable * @param array> $fieldMap * @param array> $tokenValues * @param array $languageCodes - * @param array $expectedNames + * @param array> $expectedNames */ public function testResolveNameSchema( array $fieldMap, @@ -119,27 +148,38 @@ public function testResolveNameSchema( array $expectedNames ): void { $content = $this->buildTestContentObject(); - $nameSchema = ''; - $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 = [ + ' (text)', + ' - )>', + ' - )> () (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 + ); + } } /** @@ -163,10 +203,10 @@ public static function getDataForTestResolve(): array ['field' => ['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', @@ -180,10 +220,10 @@ public static function getDataForTestResolve(): array ['field' => ['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', @@ -197,10 +237,10 @@ public static function getDataForTestResolve(): array ['field' => ['text2', 'text2']], 'Hello, and 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...',