From 977058d219fc5d5a73e1946c6cf3a6f3cf5a8eaf Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 31 Jan 2021 12:12:21 +0100 Subject: [PATCH] Updated short URL edition so that it supports editing tags --- module/Core/config/dependencies.config.php | 7 ++++++- module/Core/src/Entity/ShortUrl.php | 10 ++++++++-- module/Core/src/Service/ShortUrlService.php | 8 ++++++-- .../Core/test/Service/ShortUrlServiceTest.php | 2 ++ .../test-api/Action/EditShortUrlTagsTest.php | 19 +++++++++++++++++++ 5 files changed, 41 insertions(+), 5 deletions(-) diff --git a/module/Core/config/dependencies.config.php b/module/Core/config/dependencies.config.php index a843a0a2..0eaa7a8e 100644 --- a/module/Core/config/dependencies.config.php +++ b/module/Core/config/dependencies.config.php @@ -77,7 +77,12 @@ return [ EventDispatcherInterface::class, 'config.url_shortener.anonymize_remote_addr', ], - Service\ShortUrlService::class => ['em', Service\ShortUrl\ShortUrlResolver::class, Util\UrlValidator::class], + Service\ShortUrlService::class => [ + 'em', + Service\ShortUrl\ShortUrlResolver::class, + Util\UrlValidator::class, + ShortUrl\Resolver\PersistenceShortUrlRelationResolver::class, + ], Visit\VisitLocator::class => ['em'], Visit\VisitsStatsHelper::class => ['em'], Tag\TagService::class => ['em'], diff --git a/module/Core/src/Entity/ShortUrl.php b/module/Core/src/Entity/ShortUrl.php index 975028f8..bf01e7a5 100644 --- a/module/Core/src/Entity/ShortUrl.php +++ b/module/Core/src/Entity/ShortUrl.php @@ -138,8 +138,10 @@ class ShortUrl extends AbstractEntity return $this; } - public function update(ShortUrlEdit $shortUrlEdit): void - { + public function update( + ShortUrlEdit $shortUrlEdit, + ?ShortUrlRelationResolverInterface $relationResolver = null + ): void { if ($shortUrlEdit->hasValidSince()) { $this->validSince = $shortUrlEdit->validSince(); } @@ -152,6 +154,10 @@ class ShortUrl extends AbstractEntity if ($shortUrlEdit->hasLongUrl()) { $this->longUrl = $shortUrlEdit->longUrl(); } + if ($shortUrlEdit->hasTags()) { + $relationResolver = $relationResolver ?? new SimpleShortUrlRelationResolver(); + $this->tags = $relationResolver->resolveTags($shortUrlEdit->tags()); + } } /** diff --git a/module/Core/src/Service/ShortUrlService.php b/module/Core/src/Service/ShortUrlService.php index 8b5a6362..44de59a1 100644 --- a/module/Core/src/Service/ShortUrlService.php +++ b/module/Core/src/Service/ShortUrlService.php @@ -15,6 +15,7 @@ use Shlinkio\Shlink\Core\Model\ShortUrlsParams; use Shlinkio\Shlink\Core\Paginator\Adapter\ShortUrlRepositoryAdapter; use Shlinkio\Shlink\Core\Repository\ShortUrlRepository; use Shlinkio\Shlink\Core\Service\ShortUrl\ShortUrlResolverInterface; +use Shlinkio\Shlink\Core\ShortUrl\Resolver\ShortUrlRelationResolverInterface; use Shlinkio\Shlink\Core\Util\TagManagerTrait; use Shlinkio\Shlink\Core\Util\UrlValidatorInterface; use Shlinkio\Shlink\Rest\Entity\ApiKey; @@ -26,15 +27,18 @@ class ShortUrlService implements ShortUrlServiceInterface private ORM\EntityManagerInterface $em; private ShortUrlResolverInterface $urlResolver; private UrlValidatorInterface $urlValidator; + private ShortUrlRelationResolverInterface $relationResolver; public function __construct( ORM\EntityManagerInterface $em, ShortUrlResolverInterface $urlResolver, - UrlValidatorInterface $urlValidator + UrlValidatorInterface $urlValidator, + ShortUrlRelationResolverInterface $relationResolver ) { $this->em = $em; $this->urlResolver = $urlResolver; $this->urlValidator = $urlValidator; + $this->relationResolver = $relationResolver; } /** @@ -80,7 +84,7 @@ class ShortUrlService implements ShortUrlServiceInterface } $shortUrl = $this->urlResolver->resolveShortUrl($identifier, $apiKey); - $shortUrl->update($shortUrlEdit); + $shortUrl->update($shortUrlEdit, $this->relationResolver); $this->em->flush(); diff --git a/module/Core/test/Service/ShortUrlServiceTest.php b/module/Core/test/Service/ShortUrlServiceTest.php index be0389e6..5d873ee9 100644 --- a/module/Core/test/Service/ShortUrlServiceTest.php +++ b/module/Core/test/Service/ShortUrlServiceTest.php @@ -19,6 +19,7 @@ use Shlinkio\Shlink\Core\Model\ShortUrlsParams; use Shlinkio\Shlink\Core\Repository\ShortUrlRepository; use Shlinkio\Shlink\Core\Service\ShortUrl\ShortUrlResolverInterface; use Shlinkio\Shlink\Core\Service\ShortUrlService; +use Shlinkio\Shlink\Core\ShortUrl\Resolver\SimpleShortUrlRelationResolver; use Shlinkio\Shlink\Core\Util\UrlValidatorInterface; use Shlinkio\Shlink\Rest\Entity\ApiKey; use ShlinkioTest\Shlink\Core\Util\ApiKeyHelpersTrait; @@ -48,6 +49,7 @@ class ShortUrlServiceTest extends TestCase $this->em->reveal(), $this->urlResolver->reveal(), $this->urlValidator->reveal(), + new SimpleShortUrlRelationResolver(), ); } diff --git a/module/Rest/test-api/Action/EditShortUrlTagsTest.php b/module/Rest/test-api/Action/EditShortUrlTagsTest.php index f016882b..18f6f3b0 100644 --- a/module/Rest/test-api/Action/EditShortUrlTagsTest.php +++ b/module/Rest/test-api/Action/EditShortUrlTagsTest.php @@ -52,6 +52,25 @@ class EditShortUrlTagsTest extends ApiTestCase self::assertEquals($domain, $payload['domain'] ?? null); } + /** @test */ + public function allowsEditingTagsWithTwoEndpoints(): void + { + $getUrlTagsFromApi = fn () => $this->getJsonResponsePayload( + $this->callApiWithKey(self::METHOD_GET, '/short-urls/abc123'), + )['tags'] ?? null; + self::assertEquals(['foo'], $getUrlTagsFromApi()); + + $this->callApiWithKey(self::METHOD_PUT, '/short-urls/abc123/tags', [RequestOptions::JSON => [ + 'tags' => ['a', 'e'], + ]]); + self::assertEquals(['a', 'e'], $getUrlTagsFromApi()); + + $this->callApiWithKey(self::METHOD_PATCH, '/short-urls/abc123', [RequestOptions::JSON => [ + 'tags' => ['i', 'o', 'u'], + ]]); + self::assertEquals(['i', 'o', 'u'], $getUrlTagsFromApi()); + } + /** @test */ public function tagsAreSetOnProperShortUrlBasedOnProvidedDomain(): void {