diff --git a/config/autoload/url-shortener.global.php b/config/autoload/url-shortener.global.php index 3fe6714a..dd268cb7 100644 --- a/config/autoload/url-shortener.global.php +++ b/config/autoload/url-shortener.global.php @@ -12,6 +12,7 @@ return [ 'hostname' => env('SHORTENED_URL_HOSTNAME'), ], 'shortcode_chars' => env('SHORTCODE_CHARS', UrlShortener::DEFAULT_CHARS), + 'validate_url' => true, ], ]; diff --git a/module/CLI/src/Install/Plugin/UrlShortenerConfigCustomizerPlugin.php b/module/CLI/src/Install/Plugin/UrlShortenerConfigCustomizerPlugin.php index 82c96ff3..3adcfc30 100644 --- a/module/CLI/src/Install/Plugin/UrlShortenerConfigCustomizerPlugin.php +++ b/module/CLI/src/Install/Plugin/UrlShortenerConfigCustomizerPlugin.php @@ -45,6 +45,13 @@ class UrlShortenerConfigCustomizerPlugin extends AbstractConfigCustomizerPlugin null, true ) ?: str_shuffle(UrlShortener::DEFAULT_CHARS), + 'VALIDATE_URL' => $this->questionHelper->ask( + $input, + $output, + new ConfirmationQuestion( + 'Do you want to validate long urls by 200 HTTP status code on response (Y/n):' + ) + ), ]); } } diff --git a/module/CLI/src/Model/CustomizableAppConfig.php b/module/CLI/src/Model/CustomizableAppConfig.php index 3db3f94d..5ef42d1f 100644 --- a/module/CLI/src/Model/CustomizableAppConfig.php +++ b/module/CLI/src/Model/CustomizableAppConfig.php @@ -191,6 +191,7 @@ final class CustomizableAppConfig implements ArraySerializableInterface 'SCHEMA' => $urlShortener['domain']['schema'], 'HOSTNAME' => $urlShortener['domain']['hostname'], 'CHARS' => $urlShortener['shortcode_chars'], + 'VALIDATE_URL' => $urlShortener['validate_url'], ]); } } @@ -242,6 +243,7 @@ final class CustomizableAppConfig implements ArraySerializableInterface 'hostname' => $this->urlShortener['HOSTNAME'], ], 'shortcode_chars' => $this->urlShortener['CHARS'], + 'validate_url' => $this->urlShortener['VALIDATE_URL'], ], ]; diff --git a/module/CLI/test/Install/Plugin/UrlShortenerConfigCustomizerPluginTest.php b/module/CLI/test/Install/Plugin/UrlShortenerConfigCustomizerPluginTest.php index dc2a9f6e..2c396db2 100644 --- a/module/CLI/test/Install/Plugin/UrlShortenerConfigCustomizerPluginTest.php +++ b/module/CLI/test/Install/Plugin/UrlShortenerConfigCustomizerPluginTest.php @@ -47,8 +47,9 @@ class UrlShortenerConfigCustomizerPluginTest extends TestCase 'SCHEMA' => 'something', 'HOSTNAME' => 'something', 'CHARS' => 'something', + 'VALIDATE_URL' => 'something', ], $config->getUrlShortener()); - $askSecret->shouldHaveBeenCalledTimes(3); + $askSecret->shouldHaveBeenCalledTimes(4); } /** @@ -66,6 +67,7 @@ class UrlShortenerConfigCustomizerPluginTest extends TestCase 'SCHEMA' => 'bar', 'HOSTNAME' => 'bar', 'CHARS' => 'bar', + 'VALIDATE_URL' => 'bar', ]); $this->plugin->process(new ArrayInput([]), new NullOutput(), $config); @@ -74,8 +76,9 @@ class UrlShortenerConfigCustomizerPluginTest extends TestCase 'SCHEMA' => 'foo', 'HOSTNAME' => 'foo', 'CHARS' => 'foo', + 'VALIDATE_URL' => false, ], $config->getUrlShortener()); - $ask->shouldHaveBeenCalledTimes(4); + $ask->shouldHaveBeenCalledTimes(5); } /** @@ -91,6 +94,7 @@ class UrlShortenerConfigCustomizerPluginTest extends TestCase 'SCHEMA' => 'foo', 'HOSTNAME' => 'foo', 'CHARS' => 'foo', + 'VALIDATE_URL' => 'foo', ]); $this->plugin->process(new ArrayInput([]), new NullOutput(), $config); @@ -99,6 +103,7 @@ class UrlShortenerConfigCustomizerPluginTest extends TestCase 'SCHEMA' => 'foo', 'HOSTNAME' => 'foo', 'CHARS' => 'foo', + 'VALIDATE_URL' => 'foo', ], $config->getUrlShortener()); $ask->shouldHaveBeenCalledTimes(1); } diff --git a/module/Common/test/Factory/CacheFactoryTest.php b/module/Common/test/Factory/CacheFactoryTest.php index 53d7291a..0da1b694 100644 --- a/module/Common/test/Factory/CacheFactoryTest.php +++ b/module/Common/test/Factory/CacheFactoryTest.php @@ -75,7 +75,7 @@ class CacheFactoryTest extends TestCase */ public function filesystemCacheAdaptersReadDirOption() { - $dir = sys_get_temp_dir(); + $dir = realpath(sys_get_temp_dir()); /** @var FilesystemCache $instance */ $instance = $this->factory->__invoke($this->createSM(FilesystemCache::class, ['dir' => $dir]), ''); $this->assertInstanceOf(FilesystemCache::class, $instance); diff --git a/module/Core/config/dependencies.config.php b/module/Core/config/dependencies.config.php index 9fbd2766..31a4619a 100644 --- a/module/Core/config/dependencies.config.php +++ b/module/Core/config/dependencies.config.php @@ -42,7 +42,13 @@ return [ NotFoundDelegate::class => [TemplateRendererInterface::class], // Services - Service\UrlShortener::class => ['httpClient', 'em', Cache::class, 'config.url_shortener.shortcode_chars'], + Service\UrlShortener::class => [ + 'httpClient', + 'em', + Cache::class, + 'config.url_shortener.validate_url', + 'config.url_shortener.shortcode_chars', + ], Service\VisitsTracker::class => ['em'], Service\ShortUrlService::class => ['em'], Service\VisitService::class => ['em'], diff --git a/module/Core/src/Service/UrlShortener.php b/module/Core/src/Service/UrlShortener.php index e294fe70..cd282035 100644 --- a/module/Core/src/Service/UrlShortener.php +++ b/module/Core/src/Service/UrlShortener.php @@ -46,18 +46,24 @@ class UrlShortener implements UrlShortenerInterface * @var SlugifyInterface */ private $slugger; + /** + * @var bool + */ + private $urlValidationEnabled; public function __construct( ClientInterface $httpClient, EntityManagerInterface $em, Cache $cache, + $urlValidationEnabled, $chars = self::DEFAULT_CHARS, SlugifyInterface $slugger = null ) { $this->httpClient = $httpClient; $this->em = $em; - $this->chars = empty($chars) ? self::DEFAULT_CHARS : $chars; $this->cache = $cache; + $this->urlValidationEnabled = $urlValidationEnabled; + $this->chars = empty($chars) ? self::DEFAULT_CHARS : $chars; $this->slugger = $slugger ?: new Slugify(); } @@ -91,8 +97,11 @@ class UrlShortener implements UrlShortenerInterface return $shortUrl->getShortCode(); } - // Check that the URL exists - $this->checkUrlExists($url); + // Check if the validation of url is enabled in the config + if (true === $this->urlValidationEnabled) { + // Check that the URL exists + $this->checkUrlExists($url); + } $customSlug = $this->processCustomSlug($customSlug); // Transactionally insert the short url, then generate the short code and finally update the short code diff --git a/module/Core/test/Service/UrlShortenerTest.php b/module/Core/test/Service/UrlShortenerTest.php index 7a1c93c6..5b32c49d 100644 --- a/module/Core/test/Service/UrlShortenerTest.php +++ b/module/Core/test/Service/UrlShortenerTest.php @@ -69,10 +69,19 @@ class UrlShortenerTest extends TestCase $this->cache = new ArrayCache(); $this->slugger = $this->prophesize(SlugifyInterface::class); + $this->setUrlShortener(false); + } + + /** + * @param bool $urlValidationEnabled + */ + public function setUrlShortener($urlValidationEnabled) + { $this->urlShortener = new UrlShortener( $this->httpClient->reveal(), $this->em->reveal(), $this->cache, + $urlValidationEnabled, UrlShortener::DEFAULT_CHARS, $this->slugger->reveal() ); @@ -110,6 +119,8 @@ class UrlShortenerTest extends TestCase */ public function exceptionIsThrownWhenUrlDoesNotExist() { + $this->setUrlShortener(true); + $this->httpClient->request(Argument::cetera())->willThrow( new ClientException('', $this->prophesize(Request::class)->reveal()) );