Skip to content

Commit

Permalink
isBlockedId tests + bug fix in int regex
Browse files Browse the repository at this point in the history
  • Loading branch information
4kimov committed Jan 3, 2025
1 parent 9390a85 commit a818ed4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Sqids.php
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ protected function isBlockedId(string $id): bool
if ($id == $word) {
return true;
}
} elseif (preg_match('/~[0-9]+~/', (string) $word)) {
} elseif (preg_match('/[0-9]+/', (string) $word)) {
if (str_starts_with($id, (string) $word) || strrpos($id, (string) $word) === strlen($id) - strlen((string) $word)) {
return true;
}
Expand Down
24 changes: 24 additions & 0 deletions tests/SqidsBlocklistTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,28 @@ public function testMaxEncodingAttempts()
$this->expectException(InvalidArgumentException::class);
$sqids->encode([0]);
}

public function testSpecificIsBlockedIdScenarios()
{
$sqids = new Sqids('', 0, ['hey']);
$this->assertSame('86u', $sqids->encode([100]));

$sqids = new Sqids('', 0, ['86u']);
$this->assertSame('sec', $sqids->encode([100]));

$sqids = new Sqids('', 0, ['vFo']);
$this->assertSame('gMvFo', $sqids->encode([1000000]));

$sqids = new Sqids('', 0, ['lP3i']);
$this->assertSame('oDqljxrokxRt', $sqids->encode([100, 202, 303, 404]));

$sqids = new Sqids('', 0, ['1HkYs']);
$this->assertSame('oDqljxrokxRt', $sqids->encode([100, 202, 303, 404]));

$sqids = new Sqids('', 0, ['0hfxX']);
$this->assertSame('862REt0hfxXVdsLG8vGWD', $sqids->encode([101, 202, 303, 404, 505, 606, 707]));

$sqids = new Sqids('', 0, ['hfxX']);
$this->assertSame('seu8n1jO9C4KQQDxdOxsK', $sqids->encode([101, 202, 303, 404, 505, 606, 707]));
}
}

0 comments on commit a818ed4

Please sign in to comment.