Ensured empty string is ignored as the domain during short URL creation

This commit is contained in:
Alejandro Celaya 2022-12-07 19:06:05 +01:00
parent 54bc169525
commit f41d947cf7
3 changed files with 30 additions and 1 deletions

View File

@ -78,6 +78,12 @@ function getOptionalBoolFromInputFilter(InputFilter $inputFilter, string $fieldN
return $value !== null ? (bool) $value : null;
}
function getNonEmptyOptionalValueFromInputFilter(InputFilter $inputFilter, string $fieldName): mixed
{
$value = $inputFilter->getValue($fieldName);
return empty($value) ? null : $value;
}
function arrayToString(array $array, int $indentSize = 4): string
{
$indent = str_repeat(' ', $indentSize);

View File

@ -10,6 +10,7 @@ use Shlinkio\Shlink\Core\ShortUrl\Helper\TitleResolutionModelInterface;
use Shlinkio\Shlink\Core\ShortUrl\Model\Validation\ShortUrlInputFilter;
use Shlinkio\Shlink\Rest\Entity\ApiKey;
use function Shlinkio\Shlink\Core\getNonEmptyOptionalValueFromInputFilter;
use function Shlinkio\Shlink\Core\getOptionalBoolFromInputFilter;
use function Shlinkio\Shlink\Core\getOptionalIntFromInputFilter;
use function Shlinkio\Shlink\Core\normalizeOptionalDate;
@ -74,7 +75,7 @@ final class ShortUrlCreation implements TitleResolutionModelInterface
$this->maxVisits = getOptionalIntFromInputFilter($inputFilter, ShortUrlInputFilter::MAX_VISITS);
$this->findIfExists = $inputFilter->getValue(ShortUrlInputFilter::FIND_IF_EXISTS);
$this->validateUrl = getOptionalBoolFromInputFilter($inputFilter, ShortUrlInputFilter::VALIDATE_URL) ?? false;
$this->domain = $inputFilter->getValue(ShortUrlInputFilter::DOMAIN);
$this->domain = getNonEmptyOptionalValueFromInputFilter($inputFilter, ShortUrlInputFilter::DOMAIN);
$this->shortCodeLength = getOptionalIntFromInputFilter(
$inputFilter,
ShortUrlInputFilter::SHORT_CODE_LENGTH,

View File

@ -146,4 +146,26 @@ class ShortUrlCreationTest extends TestCase
yield [str_pad('', 600, 'd'), str_pad('', 512, 'd')];
yield [str_pad('', 800, 'e'), str_pad('', 512, 'e')];
}
/**
* @test
* @dataProvider provideDomains
*/
public function emptyDomainIsDiscarded(?string $domain, ?string $expectedDomain): void
{
$meta = ShortUrlCreation::fromRawData([
'domain' => $domain,
'longUrl' => '',
]);
self::assertSame($expectedDomain, $meta->getDomain());
}
public function provideDomains(): iterable
{
yield 'null domain' => [null, null];
yield 'empty domain' => ['', null];
yield 'trimmable domain' => [' ', null];
yield 'valid domain' => ['doma.in', 'doma.in'];
}
}