Add two modes for short URLs

This commit is contained in:
Alejandro Celaya
2023-01-25 20:33:07 +01:00
parent 87007677ed
commit 05acd4ae88
15 changed files with 68 additions and 17 deletions

View File

@@ -11,13 +11,16 @@ use Shlinkio\Shlink\Core\Model\DeviceType;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlCreation;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlEdition;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlMode;
use Shlinkio\Shlink\Core\ShortUrl\Model\Validation\ShortUrlInputFilter;
use Shlinkio\Shlink\Importer\Model\ImportedShlinkUrl;
use Shlinkio\Shlink\Importer\Sources\ImportSource;
use function Functional\every;
use function Functional\map;
use function range;
use function strlen;
use function strtolower;
use const Shlinkio\Shlink\DEFAULT_SHORT_CODES_LENGTH;
@@ -34,7 +37,7 @@ class ShortUrlTest extends TestCase
$this->expectException(ShortCodeCannotBeRegeneratedException::class);
$this->expectExceptionMessage($expectedMessage);
$shortUrl->regenerateShortCode();
$shortUrl->regenerateShortCode(ShortUrlMode::STRICT);
}
public function provideInvalidShortUrls(): iterable
@@ -58,7 +61,7 @@ class ShortUrlTest extends TestCase
): void {
$firstShortCode = $shortUrl->getShortCode();
$shortUrl->regenerateShortCode();
$shortUrl->regenerateShortCode(ShortUrlMode::STRICT);
$secondShortCode = $shortUrl->getShortCode();
self::assertNotEquals($firstShortCode, $secondShortCode);
@@ -133,4 +136,22 @@ class ShortUrlTest extends TestCase
DeviceType::DESKTOP->value => 'desktop',
], $shortUrl->deviceLongUrls());
}
/** @test */
public function generatesLowercaseOnlyShortCodesInLooselyMode(): void
{
$range = range(1, 1000); // Use a "big" number to reduce false negatives
$allFor = static fn (ShortUrlMode $mode): bool => every($range, static function () use ($mode): bool {
$shortUrl = ShortUrl::create(ShortUrlCreation::fromRawData(
[ShortUrlInputFilter::LONG_URL => 'foo'],
$mode,
));
$shortCode = $shortUrl->getShortCode();
return $shortCode === strtolower($shortCode);
});
self::assertTrue($allFor(ShortUrlMode::LOOSELY));
self::assertFalse($allFor(ShortUrlMode::STRICT));
}
}

View File

@@ -8,6 +8,7 @@ use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Core\Domain\Entity\Domain;
use Shlinkio\Shlink\Core\Options\UrlShortenerOptions;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortCodeUniquenessHelper;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier;
@@ -22,7 +23,7 @@ class ShortCodeUniquenessHelperTest extends TestCase
protected function setUp(): void
{
$this->em = $this->createMock(EntityManagerInterface::class);
$this->helper = new ShortCodeUniquenessHelper($this->em);
$this->helper = new ShortCodeUniquenessHelper($this->em, new UrlShortenerOptions());
$this->shortUrl = $this->createMock(ShortUrl::class);
$this->shortUrl->method('getShortCode')->willReturn('abc123');