Skip to content

Commit

Permalink
updated the regex to handle more cases, improved tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sztig committed Feb 5, 2025
1 parent 2eb1790 commit cb81e71
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/lib/Repository/NameSchema/NameSchemaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
88 changes: 64 additions & 24 deletions tests/lib/Repository/NameSchema/NameSchemaServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
],
];

Expand All @@ -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 (<text1> - <text2>)
[
'eng-GB' => 'three (EZMETAGROUP_0) two',
'cro-HR' => ' - Dva (EZMETAGROUP_0) Dva',
],
],
];
}
Expand All @@ -119,27 +148,38 @@ public function testResolveNameSchema(
array $expectedNames
): void {
$content = $this->buildTestContentObject();
$nameSchema = '<text3|(<text1> - <text2>)> (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 = [
'<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],

Check failure on line 179 in tests/lib/Repository/NameSchema/NameSchemaServiceTest.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (7.4)

Offset 0|1|2|3 does not exist on array<string, string>.

Check failure on line 179 in tests/lib/Repository/NameSchema/NameSchemaServiceTest.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (8.0)

Offset 0|1|2|3 does not exist on array<string, string>.

Check failure on line 179 in tests/lib/Repository/NameSchema/NameSchemaServiceTest.php

View workflow job for this annotation

GitHub Actions / Unit tests & SQLite integration tests (8.1)

Offset 0|1|2|3 does not exist on array<string, string>.
$result
);
}
}

/**
Expand Down

0 comments on commit cb81e71

Please sign in to comment.