refactor: FontNameRules::check() ohne toten Reserviert-Zweig; Kollision bleibt taken()/409
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
bfe6623fe8
commit
15cffedb9f
@@ -160,9 +160,7 @@ final class DiviFontsUploadAbility implements Ability, RequiresDiviFeatures
|
||||
}
|
||||
$name = is_string($input['name'] ?? null) ? $input['name'] : '';
|
||||
$taken = FontNameRules::taken($name, $this->catalog->reserved());
|
||||
$nameProblem = $taken === null
|
||||
? FontNameRules::check($name, [], $this->installer->nameMaxLength())
|
||||
: null;
|
||||
$nameProblem = FontNameRules::check($name, $this->installer->nameMaxLength());
|
||||
if ($nameProblem !== null) {
|
||||
$errors['name'] = $nameProblem;
|
||||
}
|
||||
|
||||
@@ -23,10 +23,11 @@ final class FontNameRules
|
||||
private const DIVI_STRIPS = '/[^A-Za-z0-9\s\_-]/';
|
||||
|
||||
/**
|
||||
* @param list<string> $reserved Names of Google, web-safe and uploaded fonts (FontCatalog::reserved()).
|
||||
* The form only; whether the name is taken is taken().
|
||||
*
|
||||
* @return string|null null = valid, else the problem (German).
|
||||
*/
|
||||
public static function check(string $name, array $reserved, int $maxLength = self::MAX_LENGTH): ?string
|
||||
public static function check(string $name, int $maxLength = self::MAX_LENGTH): ?string
|
||||
{
|
||||
if (trim($name) === '') {
|
||||
return sprintf('Name fehlt (1–%d Zeichen)', $maxLength);
|
||||
@@ -47,17 +48,11 @@ final class FontNameRules
|
||||
return sprintf('höchstens %d Zeichen (%d angegeben)', $maxLength, strlen($name));
|
||||
}
|
||||
|
||||
$taken = self::taken($name, $reserved);
|
||||
|
||||
return $taken === null ? null : sprintf(
|
||||
'„%s“ gibt es schon als Google-, Web-Safe- oder hochgeladene Schrift (ohne Beachtung der '
|
||||
. 'Groß-/Kleinschreibung)',
|
||||
$taken
|
||||
);
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<string> $reserved
|
||||
* @param list<string> $reserved Names of Google, web-safe and uploaded fonts (FontCatalog::reserved()).
|
||||
* @return string|null The existing name $name collides with (case-insensitive), null when it is free.
|
||||
*/
|
||||
public static function taken(string $name, array $reserved): ?string
|
||||
|
||||
@@ -308,6 +308,14 @@ final class DiviFontsUploadAbilityTest extends TestCase
|
||||
self::assertSame([], $this->fetches);
|
||||
}
|
||||
|
||||
public function testAMalformedNameIsAFieldErrorNotAConflict(): void
|
||||
{
|
||||
$result = $this->ability(catalog: self::catalog(['Open Sans']))->execute(self::input(['name' => 'Open Sans!']));
|
||||
|
||||
self::assertToolError($result, 'validation_failed');
|
||||
self::assertArrayHasKey('name', self::errorFields($result));
|
||||
}
|
||||
|
||||
public function testTheDryRunDownloadsAndChecksEveryFileAndAsksForConfirmation(): void
|
||||
{
|
||||
$installer = self::installer();
|
||||
|
||||
@@ -13,40 +13,40 @@ final class FontNameRulesTest extends TestCase
|
||||
|
||||
public function testLettersDigitsSpacesUnderscoresAndHyphensAreAllowed(): void
|
||||
{
|
||||
self::assertNull(FontNameRules::check('Inter Display_2-Bold', self::RESERVED));
|
||||
self::assertNull(FontNameRules::check('X', self::RESERVED));
|
||||
self::assertNull(FontNameRules::check('Inter Display_2-Bold'));
|
||||
self::assertNull(FontNameRules::check('X'));
|
||||
}
|
||||
|
||||
public function testOtherCharactersAreRefusedWithTheNameDiviWouldStore(): void
|
||||
{
|
||||
$problem = (string) FontNameRules::check('Grotesk (Pro)!', self::RESERVED);
|
||||
$problem = (string) FontNameRules::check('Grotesk (Pro)!');
|
||||
self::assertStringContainsString('nur Buchstaben', $problem);
|
||||
self::assertStringContainsString('„Grotesk Pro“', $problem);
|
||||
|
||||
self::assertStringContainsString('„Mller“', (string) FontNameRules::check('Müller', self::RESERVED));
|
||||
self::assertStringContainsString('nur Buchstaben', (string) FontNameRules::check('a.b', self::RESERVED));
|
||||
self::assertStringContainsString('nur Buchstaben', (string) FontNameRules::check("Tab\tName", self::RESERVED));
|
||||
self::assertStringContainsString('nichts übrig', (string) FontNameRules::check('«»', self::RESERVED));
|
||||
self::assertStringContainsString('„Mller“', (string) FontNameRules::check('Müller'));
|
||||
self::assertStringContainsString('nur Buchstaben', (string) FontNameRules::check('a.b'));
|
||||
self::assertStringContainsString('nur Buchstaben', (string) FontNameRules::check("Tab\tName"));
|
||||
self::assertStringContainsString('nichts übrig', (string) FontNameRules::check('«»'));
|
||||
}
|
||||
|
||||
public function testEmptyAndPaddedNamesAreRefused(): void
|
||||
{
|
||||
self::assertStringContainsString('1–100 Zeichen', (string) FontNameRules::check('', self::RESERVED));
|
||||
self::assertStringContainsString('1–100 Zeichen', (string) FontNameRules::check(' ', self::RESERVED));
|
||||
self::assertStringContainsString('Anfang oder Ende', (string) FontNameRules::check(' Inter', self::RESERVED));
|
||||
self::assertStringContainsString('Anfang oder Ende', (string) FontNameRules::check('Inter ', self::RESERVED));
|
||||
self::assertStringContainsString('1–100 Zeichen', (string) FontNameRules::check(''));
|
||||
self::assertStringContainsString('1–100 Zeichen', (string) FontNameRules::check(' '));
|
||||
self::assertStringContainsString('Anfang oder Ende', (string) FontNameRules::check(' Inter'));
|
||||
self::assertStringContainsString('Anfang oder Ende', (string) FontNameRules::check('Inter '));
|
||||
}
|
||||
|
||||
public function testTheLengthLimitIsOneHundredCharactersOrDivisBound(): void
|
||||
{
|
||||
self::assertNull(FontNameRules::check(str_repeat('a', 100), self::RESERVED));
|
||||
self::assertNull(FontNameRules::check(str_repeat('a', 100)));
|
||||
self::assertStringContainsString(
|
||||
'höchstens 100 Zeichen (101 angegeben)',
|
||||
(string) FontNameRules::check(str_repeat('a', 101), self::RESERVED)
|
||||
(string) FontNameRules::check(str_repeat('a', 101))
|
||||
);
|
||||
self::assertStringContainsString(
|
||||
'höchstens 50 Zeichen',
|
||||
(string) FontNameRules::check(str_repeat('a', 51), self::RESERVED, 50)
|
||||
(string) FontNameRules::check(str_repeat('a', 51), 50)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -57,8 +57,6 @@ final class FontNameRulesTest extends TestCase
|
||||
self::assertSame('Hausschrift', FontNameRules::taken('Hausschrift', self::RESERVED));
|
||||
self::assertNull(FontNameRules::taken('Open Sans Pro', self::RESERVED));
|
||||
self::assertNull(FontNameRules::taken('Roboto', []));
|
||||
|
||||
$problem = (string) FontNameRules::check('times new roman', self::RESERVED);
|
||||
self::assertStringContainsString('„Times New Roman“ gibt es schon', $problem);
|
||||
self::assertNull(FontNameRules::check('times new roman'), 'the form is fine; the collision is taken()');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user