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

FIX Prevent invalid i18n sources from being collected #11603

Merged
merged 1 commit into from
Feb 12, 2025
Merged
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
8 changes: 6 additions & 2 deletions src/ORM/DataObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -4429,15 +4429,19 @@ public function provideI18nEntities()
$pluralName = $this->plural_name();
$singularName = $this->singular_name();
$conjunction = preg_match('/^[aeiou]/i', $singularName ?? '') ? 'An ' : 'A ';
return [
static::class . '.CLASS_DESCRIPTION' => $this->classDescription(),
$entities = [
static::class . '.SINGULARNAME' => $singularName,
static::class . '.PLURALNAME' => $pluralName,
static::class . '.PLURALS' => [
'one' => $conjunction . $singularName,
'other' => '{count} ' . $pluralName
]
];
$classDescription = $this->classDescription();
if ($classDescription) {
$entities[static::class . '.CLASS_DESCRIPTION'] = $classDescription;
}
return $entities;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/i18n/TextCollection/i18nTextCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,7 @@ public function collectFromCode($content, $fileName, Module $module)
$inTransFn = false;
$inConcat = false;
// Ensure key is valid before saving
if (!empty($currentEntity[0])) {
if (!empty($currentEntity[0]) && !str_ends_with($currentEntity[0], '.')) {
Copy link
Member

Choose a reason for hiding this comment

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

I assume this is for

In assets File.php, an invalid key ending with "." with a value of "generic" was being collected

  1. Shouldn't we have a PR in silverstripe/assets to fix that key?
  2. Should we throw a warning when we find these instead of just ignoring them?
  3. We should update our PHPStan rule to capture this scenario so it doesn't happen again

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think we should do an of those - here's the unit test that would trigger this before this PR https://github.com/silverstripe/silverstripe-framework/pull/11603/files#diff-7b72a58f2b9e604294d0342910391de6700b4c6fe15f99615bbaa729c196135dR1039 - it's a weird condition

There may be well other conditions in the future that mean we end up with a dot at the end of the key. I think having a catch-all "key ends with a period, it's clearly invalid, so just ignore it" is a pretty good real-world solution

Copy link
Member

Choose a reason for hiding this comment

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

Discussed offline. We'll catch in PHPStan with silverstripe/silverstripe-standards#16 later on

$key = $currentEntity[0];
$default = $currentEntity[1] ?? '';
$comment = $currentEntity[2] ?? '';
Expand Down
52 changes: 52 additions & 0 deletions tests/php/ORM/DataObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2840,4 +2840,56 @@ public function testExceptionForUniqueIndexViolation(array $fieldsRecordOne, arr
$this->expectExceptionMessage($expectedMessage);
$record2->write();
}

public static function provideProvideI18nEntities(): array
{
return [
'has-class-description' => [
'classDescription' => 'A fluffy cloud',
'expected' => true,
],
'no-class-description' => [
'classDescription' => null,
'expected' => false,
],
];
}

/**
* @dataProvider provideProvideI18nEntities
*/
public function testProvideI18nEntities(?string $classDescription, bool $expected): void
{
$obj = new class extends DataObject {
public $classDescription;
public function singular_name()
{
return 'Cloud';
}
public function plural_name()
{
return 'Clouds';
}
public function classDescription()
{
return $this->classDescription;
}
};
$obj->classDescription = $classDescription;
$entities = $obj->provideI18nEntities();
// Fix up anonymous class keys
foreach ($entities as $key => $entity) {
unset($entities[$key]);
$newKey = preg_replace('#^.+?\.([A-Z_]+)$#', '$1', $key);
$entities[$newKey] = $entity;
}
$this->assertSame('Cloud', $entities['SINGULARNAME']);
$this->assertSame('Clouds', $entities['PLURALNAME']);
$this->assertSame(['one' => 'A Cloud', 'other' => '{count} Clouds'], $entities['PLURALS']);
if ($expected) {
$this->assertSame('A fluffy cloud', $entities['CLASS_DESCRIPTION']);
} else {
$this->assertFalse(array_key_exists('CLASS_DESCRIPTION', $entities));
}
}
}
22 changes: 22 additions & 0 deletions tests/php/i18n/i18nTextCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1028,4 +1028,26 @@ public function testItCanUseVariableAsContext()
'TestEntity.REGULARCONTEXT' => "test {type}",
], $collectedTranslatables);
}

public function testDoesNotCollectInvalidKeys()
{
// From the code below this will previously collect `'.' => 'generic'`
// Code was added to i18nTextCollector::collectFromCode() to ignore keys
// that end with "."
$c = i18nTextCollector::create();
$mymodule = ModuleLoader::inst()->getManifest()->getModule('i18ntestmodule');
$php = <<<'PHP'
$data = [
$foo,
static::get_something($foo),
'generic'
];
_t(
__CLASS__ . '.' . ucfirst($foo) . 'Type',
$hello[$foo]
);
PHP;
$collectedTranslatables = $c->collectFromCode($php, null, $mymodule);
$this->assertEmpty($collectedTranslatables);
}
}
Loading