Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350<?php
declare(strict_types=1);
namespace Wotnak\Atproto\Tests\Unit\Schema;
use PHPUnit\Framework\Attributes\DataProvider;use PHPUnit\Framework\TestCase;use Wotnak\Atproto\Lexicon\RecordValidator;use Wotnak\Atproto\Lexicon\Schema\StringSchema;use Wotnak\Atproto\Lexicon\ValidationOptions;
/** * Duplicate byte-length and grapheme vectors remain asserted. PHP strings * cannot represent lone UTF-16 surrogates as valid UTF-8, and StringSchema * rejects their byte representation as invalid_utf8. */final class StringSchemaTest extends TestCase {
#[DataProvider('nonStringProvider')] public function testRejectsNonStrings(mixed $value): void { static::assertFalse( new StringSchema() ->safeValidate($value) ->isValid(), ); }
/** @return array<string, array{mixed}> */ public static function nonStringProvider(): array { return [ 'null' => [NULL], 'integer' => [123], 'boolean' => [TRUE], 'list' => [['hello']], 'object' => [['value' => 'hello']], ]; }
public function testByteLengthConstraintsIncludeUtf8Bytes(): void { $schema = new StringSchema(minLength: 3, maxLength: 10);
static::assertTrue($schema->safeValidate('abc')->isValid()); static::assertTrue($schema->safeValidate('1234567890')->isValid()); static::assertFalse($schema->safeValidate('hi')->isValid()); static::assertFalse($schema->safeValidate('hello world!')->isValid()); static::assertTrue( new StringSchema(maxLength: 4) ->safeValidate('😀') ->isValid(), ); static::assertFalse( new StringSchema(maxLength: 3) ->safeValidate('😀') ->isValid(), ); }
public function testCombinedGraphemeConstraints(): void { $schema = new StringSchema(minGraphemes: 2, maxGraphemes: 5);
static::assertTrue($schema->safeValidate('hi')->isValid()); static::assertTrue($schema->safeValidate('hello')->isValid()); static::assertFalse($schema->safeValidate('a')->isValid()); static::assertFalse($schema->safeValidate('hello!')->isValid()); static::assertTrue( new StringSchema(minGraphemes: 3) ->safeValidate('😀😀😀') ->isValid(), ); }
public function testDefaultEnumAndConstConstraints(): void { $default = new StringSchema(default: 'default value'); static::assertSame('default value', $default->safeParse(NULL)->value); static::assertFalse($default->safeValidate(NULL)->isValid());
$invalidDefault = new StringSchema(minLength: 5, default: 'hi'); static::assertFalse($invalidDefault->safeParse(NULL)->isValid());
$enum = new StringSchema(enum: ['active', 'inactive']); static::assertTrue($enum->safeValidate('active')->isValid()); static::assertFalse($enum->safeValidate('unknown')->isValid());
$const = new StringSchema(const: 'self'); static::assertTrue($const->safeValidate('self')->isValid()); static::assertFalse($const->safeValidate('other')->isValid()); }
public function testStringsMustContainValidUtf8(): void { $schemaResult = new StringSchema()->safeValidate("\xFF"); static::assertFalse($schemaResult->isValid()); static::assertSame(['invalid_utf8'], \array_column($schemaResult->violations, 'code'));
$recordResult = new RecordValidator()->validateRecord( 'com.example.post', [ '$type' => 'com.example.post', 'text' => 'Hello', 'futureField' => "\xFF", ], self::recordSchema(), ); static::assertFalse($recordResult->isValid()); static::assertSame(['$.futureField'], \array_column($recordResult->violations, 'path')); static::assertSame(['invalid_utf8'], \array_column($recordResult->violations, 'code'));
$invalidKeyResult = new RecordValidator()->validateRecord( 'com.example.post', [ '$type' => 'com.example.post', 'text' => 'Hello', "\xFF" => TRUE, ], self::recordSchema(), ); static::assertFalse($invalidKeyResult->isValid()); static::assertSame(['invalid_lex_key'], \array_column($invalidKeyResult->violations, 'code')); }
public function testUriFormatAcceptsNonHttpUrisAndRejectsMalformedEscapes(): void { $schema = new StringSchema(format: 'uri');
static::assertTrue($schema->safeValidate('did:plc:abc123')->isValid()); static::assertTrue($schema->safeValidate('urn:ietf:params:oauth:request_uri:abc')->isValid()); static::assertTrue($schema->safeValidate('https://[2001:db8::1]/path')->isValid()); static::assertFalse($schema->safeValidate('https://example.com/%zz')->isValid()); static::assertFalse($schema->safeValidate('https://example.com:abc')->isValid()); static::assertFalse($schema->safeValidate('foo:[bar]')->isValid()); static::assertFalse($schema->safeValidate('not a uri')->isValid()); }
public function testUnsupportedFormatNeverSilentlyAcceptsValues(): void { $schema = new StringSchema(format: 'datatime');
static::assertFalse($schema->safeValidate('anything')->isValid()); }
#[DataProvider('languageTagProvider')] public function testLanguageFormatFollowsBcp47(string $tag, bool $valid): void { $schema = new StringSchema(format: 'language');
static::assertSame($valid, $schema->safeValidate($tag)->isValid(), $tag); }
/** * @return array<string, array{string, bool}> */ public static function languageTagProvider(): array { return [ 'language' => ['en', TRUE], 'region' => ['en-US', TRUE], 'script and region' => ['zh-Hant-CN', TRUE], 'extension' => ['en-u-ca-gregory', TRUE], 'private use' => ['x-private', TRUE], 'grandfathered' => ['i-klingon', TRUE], 'one-character extension value' => ['en-a', FALSE], 'one-character language' => ['a-value', FALSE], 'empty subtag' => ['en--US', FALSE], ]; }
public function testStrictDatetimeValidatesCalendarAndTimeComponents(): void { $schema = new StringSchema(format: 'datetime');
static::assertTrue($schema->safeValidate('2024-02-29T23:59:59.123Z')->isValid()); static::assertTrue($schema->safeValidate('0000-01-01T00:00:00.000Z')->isValid()); static::assertFalse($schema->safeValidate('2023-02-29T12:00:00Z')->isValid()); static::assertFalse($schema->safeValidate('2024-02-29 23:59:59Z')->isValid()); static::assertFalse($schema->safeValidate('2024-02-29T23:59:59z')->isValid()); static::assertFalse($schema->safeValidate('2024-01-01T24:00:00Z')->isValid()); static::assertFalse($schema->safeValidate('2024-01-01T12:00:00+24:00')->isValid()); static::assertFalse($schema->safeValidate('2024-01-01T12:00:00-00:00')->isValid()); static::assertFalse($schema->safeValidate('0000-01-01T00:00:00+01:00')->isValid()); }
public function testLooseDatetimeAllowsCommonNonCanonicalForms(): void { $schema = new StringSchema(format: 'datetime'); $loose = ValidationOptions::validate(FALSE);
static::assertTrue($schema->safeValidate('2023-12-25T12:00:00', $loose)->isValid()); static::assertTrue($schema->safeValidate('2023-12-25', $loose)->isValid()); static::assertTrue($schema->safeValidate('2023-12-25T12:00:00+05:30', $loose)->isValid()); }
#[DataProvider('formatProvider')] public function testLexiconStringFormats(string $format, string $value, bool $valid): void { static::assertSame( $valid, new StringSchema(format: $format) ->safeValidate($value) ->isValid(), ); }
/** @return array<string, array{string, string, bool}> */ public static function formatProvider(): array { return [ 'AT URI' => ['at-uri', 'at://did:plc:abc123/app.bsky.feed.post/xyz', TRUE], 'AT URI rejects HTTP' => ['at-uri', 'https://example.com', FALSE], 'PLC DID' => ['did', 'did:plc:abc123', TRUE], 'web DID' => ['did', 'did:web:example.com', TRUE], 'invalid DID' => ['did', 'did:', FALSE], 'handle' => ['handle', 'user.bsky.social', TRUE], 'subdomain handle' => ['handle', 'alice.test.example.com', TRUE], 'invalid handle' => ['handle', 'invalid handle!', FALSE], 'AT identifier DID' => ['at-identifier', 'did:plc:abc123', TRUE], 'AT identifier handle' => ['at-identifier', 'user.bsky.social', TRUE], 'invalid AT identifier' => ['at-identifier', 'invalid!', FALSE], 'NSID' => ['nsid', 'app.bsky.feed.post', TRUE], 'long NSID' => ['nsid', 'com.example.app.feature.action', TRUE], 'invalid NSID' => ['nsid', 'app.bsky.feed!', FALSE], 'CID' => ['cid', 'bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi', TRUE], 'invalid CID' => ['cid', 'not-a-cid', FALSE], 'TID' => ['tid', '3jzfcijpj2z2a', TRUE], 'invalid TID' => ['tid', '3jzfcijpj2z2!', FALSE], 'record key TID' => ['record-key', '3jzfcijpj2z2a', TRUE], 'record key text' => ['record-key', 'myRecordKey123', TRUE], 'record key slash' => ['record-key', 'invalid/key', FALSE], 'record key space' => ['record-key', 'invalid key', FALSE], ]; }
public function testStrictLanguageTagsRejectDuplicateVariantsAndSingletons(): void { $schema = new StringSchema(format: 'language');
static::assertFalse($schema->safeValidate('de-DE-1901-1901')->isValid()); static::assertFalse($schema->safeValidate('en-a-foo-a-bar')->isValid()); static::assertTrue( $schema ->safeValidate( 'de-DE-1901-1901', ValidationOptions::validate(FALSE), ) ->isValid(), ); static::assertTrue( $schema ->safeValidate( 'en-a-foo-a-bar', ValidationOptions::validate(FALSE), ) ->isValid(), ); }
public function testGraphemeLimitsCountExtendedGraphemeClusters(): void { $schema = new StringSchema(maxGraphemes: 1);
static::assertTrue($schema->safeValidate('👨👩👧👦')->isValid()); static::assertFalse($schema->safeValidate('ab')->isValid()); }
#[DataProvider('utf8ByteLengthVectorProvider')] public function testByteLengthMatchesUtf8Vectors( string $implementation, string $value, int $length, ): void { static::assertNotSame('', $implementation); static::assertTrue( new StringSchema(minLength: $length, maxLength: $length) ->safeValidate($value) ->isValid(), ); static::assertFalse( new StringSchema(maxLength: $length - 1) ->safeValidate($value) ->isValid(), ); }
/** @return iterable<string, array{string, string, int}> */ public static function utf8ByteLengthVectorProvider(): iterable { $values = [ 'ASCII letter' => ['a', 1], 'ASCII symbol' => ['~', 1], 'o diaeresis' => ['ö', 2], 'n tilde' => ['ñ', 2], 'copyright' => ['©', 2], 'CJK radical' => ['⽘', 3], 'telephone' => ['☎', 3], 'Egyptian hieroglyph' => ['𓋓', 4], 'emoji' => ['😀', 4], 'family emoji' => ['👨👩👧👧', 25], ]; foreach (['node', 'compute'] as $implementation) { foreach ($values as $name => [$value, $length]) { yield $implementation . ' ' . $name => [$implementation, $value, $length]; } } }
#[DataProvider('graphemeLengthVectorProvider')] public function testGraphemeLengthMatchesVectors( string $implementation, string $value, int $length, ): void { static::assertNotSame('', $implementation); static::assertTrue( new StringSchema(minGraphemes: $length, maxGraphemes: $length) ->safeValidate($value) ->isValid(), ); static::assertFalse( new StringSchema(maxGraphemes: $length - 1) ->safeValidate($value) ->isValid(), ); }
/** @return iterable<string, array{string, string, int}> */ public static function graphemeLengthVectorProvider(): iterable { $values = [ 'ASCII letter' => ['a', 1], 'ASCII symbol' => ['~', 1], 'o diaeresis' => ['ö', 1], 'n tilde' => ['ñ', 1], 'copyright' => ['©', 1], 'CJK radical' => ['⽘', 1], 'telephone' => ['☎', 1], 'Egyptian hieroglyph' => ['𓋓', 1], 'emoji' => ['😀', 1], 'family emoji' => ['👨👩👧👧', 1], 'combined string' => ['a~öñ©⽘☎𓋓😀👨👩👧👧', 10], 'Hindi issue 4321' => ['नमस्ते', 3], ]; foreach (['native', 'ponyfill'] as $implementation) { foreach ($values as $name => [$value, $length]) { yield $implementation . ' ' . $name => [$implementation, $value, $length]; } } }
/** * @return array<string, mixed> */ private static function recordSchema(): array { return [ 'type' => 'object', 'required' => ['text'], 'properties' => [ 'text' => ['type' => 'string'], ], ]; }
}