From 405369824bf5fe5dcdfa9808d809d04d188b3652 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Thu, 24 Sep 2020 21:54:03 +0200 Subject: [PATCH] Added hability to override URL validation from the CLI --- .../ShortUrl/GenerateShortUrlCommand.php | 29 +++++++++++++++++ .../ShortUrl/GenerateShortUrlCommandTest.php | 31 +++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/module/CLI/src/Command/ShortUrl/GenerateShortUrlCommand.php b/module/CLI/src/Command/ShortUrl/GenerateShortUrlCommand.php index 06cdd274..97907fea 100644 --- a/module/CLI/src/Command/ShortUrl/GenerateShortUrlCommand.php +++ b/module/CLI/src/Command/ShortUrl/GenerateShortUrlCommand.php @@ -22,6 +22,7 @@ use function Functional\curry; use function Functional\flatten; use function Functional\unique; use function sprintf; +use function strpos; class GenerateShortUrlCommand extends Command { @@ -94,6 +95,18 @@ class GenerateShortUrlCommand extends Command 'l', InputOption::VALUE_REQUIRED, 'The length for generated short code (it will be ignored if --customSlug was provided).', + ) + ->addOption( + 'validate-url', + null, + InputOption::VALUE_NONE, + 'Forces the long URL to be validated, regardless what is globally configured.', + ) + ->addOption( + 'no-validate-url', + null, + InputOption::VALUE_NONE, + 'Forces the long URL to not be validated, regardless what is globally configured.', ); } @@ -125,6 +138,7 @@ class GenerateShortUrlCommand extends Command $customSlug = $input->getOption('customSlug'); $maxVisits = $input->getOption('maxVisits'); $shortCodeLength = $input->getOption('shortCodeLength') ?? $this->defaultShortCodeLength; + $doValidateUrl = $this->doValidateUrl($input); try { $shortUrl = $this->urlShortener->urlToShortCode($longUrl, $tags, ShortUrlMeta::fromRawData([ @@ -135,6 +149,7 @@ class GenerateShortUrlCommand extends Command ShortUrlMetaInputFilter::FIND_IF_EXISTS => $input->getOption('findIfExists'), ShortUrlMetaInputFilter::DOMAIN => $input->getOption('domain'), ShortUrlMetaInputFilter::SHORT_CODE_LENGTH => $shortCodeLength, + ShortUrlMetaInputFilter::VALIDATE_URL => $doValidateUrl, ])); $io->writeln([ @@ -147,4 +162,18 @@ class GenerateShortUrlCommand extends Command return ExitCodes::EXIT_FAILURE; } } + + private function doValidateUrl(InputInterface $input): ?bool + { + $rawInput = (string) $input; + + if (strpos($rawInput, '--no-validate-url') !== false) { + return false; + } + if (strpos($rawInput, '--validate-url') !== false) { + return true; + } + + return null; + } } diff --git a/module/CLI/test/Command/ShortUrl/GenerateShortUrlCommandTest.php b/module/CLI/test/Command/ShortUrl/GenerateShortUrlCommandTest.php index 689a5e7c..eee57b81 100644 --- a/module/CLI/test/Command/ShortUrl/GenerateShortUrlCommandTest.php +++ b/module/CLI/test/Command/ShortUrl/GenerateShortUrlCommandTest.php @@ -13,6 +13,7 @@ use Shlinkio\Shlink\CLI\Util\ExitCodes; use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\Exception\InvalidUrlException; use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException; +use Shlinkio\Shlink\Core\Model\ShortUrlMeta; use Shlinkio\Shlink\Core\Service\UrlShortener; use Symfony\Component\Console\Application; use Symfony\Component\Console\Tester\CommandTester; @@ -105,4 +106,34 @@ class GenerateShortUrlCommandTest extends TestCase $this->assertStringContainsString($shortUrl->toString(self::DOMAIN_CONFIG), $output); $urlToShortCode->shouldHaveBeenCalledOnce(); } + + /** + * @test + * @dataProvider provideFlags + */ + public function urlValidationHasExpectedValueBasedOnProvidedTags(array $options, ?bool $expectedValidateUrl): void + { + $shortUrl = new ShortUrl(''); + $urlToShortCode = $this->urlShortener->urlToShortCode( + Argument::type('string'), + Argument::type('array'), + Argument::that(function (ShortUrlMeta $meta) use ($expectedValidateUrl) { + Assert::assertEquals($expectedValidateUrl, $meta->doValidateUrl()); + return $meta; + }), + )->willReturn($shortUrl); + + $options['longUrl'] = 'http://domain.com/foo/bar'; + $this->commandTester->execute($options); + + $urlToShortCode->shouldHaveBeenCalledOnce(); + } + + public function provideFlags(): iterable + { + yield 'no flags' => [[], null]; + yield 'no-validate-url only' => [['--no-validate-url' => true], false]; + yield 'validate-url' => [['--validate-url' => true], true]; + yield 'both flags' => [['--validate-url' => true, '--no-validate-url' => true], false]; + } }