From 2eb17909b98746fd7e1e86ce8053437cbf50168c Mon Sep 17 00:00:00 2001 From: Sztig Date: Mon, 4 Nov 2024 14:52:45 +0100 Subject: [PATCH 1/3] fixed filterNameSchema method, updated the test --- .../Repository/NameSchema/NameSchemaService.php | 5 +++++ .../NameSchema/NameSchemaServiceTest.php | 16 ++++++++-------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/lib/Repository/NameSchema/NameSchemaService.php b/src/lib/Repository/NameSchema/NameSchemaService.php index 782a154191..f1d9459638 100644 --- a/src/lib/Repository/NameSchema/NameSchemaService.php +++ b/src/lib/Repository/NameSchema/NameSchemaService.php @@ -251,6 +251,11 @@ protected function filterNameSchema(string $nameSchema): array 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..1f98c65aa5 100644 --- a/tests/lib/Repository/NameSchema/NameSchemaServiceTest.php +++ b/tests/lib/Repository/NameSchema/NameSchemaServiceTest.php @@ -76,13 +76,13 @@ 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 - two (testString)', + 'cro-HR' => 'jedan - dva (testString)', ], ]; @@ -98,8 +98,8 @@ public static function getDataForTestResolveNameSchema(): iterable ], ['eng-GB', 'cro-HR'], [ - 'eng-GB' => 'three', - 'cro-HR' => 'Dva', + 'eng-GB' => 'three (testString)', + 'cro-HR' => ' - Dva (testString)', ], ]; } @@ -119,11 +119,11 @@ public function testResolveNameSchema( array $expectedNames ): void { $content = $this->buildTestContentObject(); - $nameSchema = ''; + $nameSchema = ' - )> (testString)'; $contentType = $this->buildTestContentTypeStub($nameSchema, $nameSchema); $event = new ResolveContentNameSchemaEvent( $content, - ['field' => ['text3', 'text2']], + ['field' => ['text3', 'text2', 'text1']], $contentType, $fieldMap, $languageCodes From cb81e718c0eb6fef499a1cf130a38b925b7b1738 Mon Sep 17 00:00:00 2001 From: Sztig Date: Wed, 5 Feb 2025 17:14:29 +0100 Subject: [PATCH 2/3] updated the regex to handle more cases, improved tests --- .../NameSchema/NameSchemaService.php | 2 +- .../NameSchema/NameSchemaServiceTest.php | 88 ++++++++++++++----- 2 files changed, 65 insertions(+), 25 deletions(-) diff --git a/src/lib/Repository/NameSchema/NameSchemaService.php b/src/lib/Repository/NameSchema/NameSchemaService.php index f1d9459638..8645a5622c 100644 --- a/src/lib/Repository/NameSchema/NameSchemaService.php +++ b/src/lib/Repository/NameSchema/NameSchemaService.php @@ -245,7 +245,7 @@ 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) { diff --git a/tests/lib/Repository/NameSchema/NameSchemaServiceTest.php b/tests/lib/Repository/NameSchema/NameSchemaServiceTest.php index 1f98c65aa5..e206824a75 100644 --- a/tests/lib/Repository/NameSchema/NameSchemaServiceTest.php +++ b/tests/lib/Repository/NameSchema/NameSchemaServiceTest.php @@ -81,8 +81,22 @@ public static function getDataForTestResolveNameSchema(): iterable ], [], [ - 'eng-GB' => 'one - two (testString)', - 'cro-HR' => 'jedan - dva (testString)', + [ + '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 (testString)', - 'cro-HR' => ' - Dva (testString)', + [ + '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', + ], ], ]; } @@ -119,27 +148,38 @@ public function testResolveNameSchema( array $expectedNames ): void { $content = $this->buildTestContentObject(); - $nameSchema = ' - )> (testString)'; - $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); + $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 + ); + } } /** From e435deda96c4885a327b9eedb2fbd51abbe2472d Mon Sep 17 00:00:00 2001 From: Sztig Date: Wed, 5 Feb 2025 17:27:22 +0100 Subject: [PATCH 3/3] fixed typing for arrays in test --- .../NameSchema/NameSchemaServiceTest.php | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/tests/lib/Repository/NameSchema/NameSchemaServiceTest.php b/tests/lib/Repository/NameSchema/NameSchemaServiceTest.php index e206824a75..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 @@ -139,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, @@ -203,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', @@ -220,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', @@ -237,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...',