From e093480a5b9ce8c300062a9688e1d02f0d798f54 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Fri, 26 Feb 2021 20:24:57 +0100 Subject: [PATCH] Fixed API tests --- module/Core/src/Repository/TagRepository.php | 7 ++++--- module/Core/src/Repository/TagRepositoryInterface.php | 2 +- module/Core/src/ShortUrl/Spec/BelongsToApiKey.php | 2 +- module/Core/src/ShortUrl/Spec/BelongsToDomain.php | 2 +- module/Core/src/Tag/TagService.php | 2 +- module/Core/test/Service/Tag/TagServiceTest.php | 2 +- module/Rest/src/ApiKey/Role.php | 8 +++++--- .../Rest/src/ApiKey/Spec/WithApiKeySpecsEnsuringJoin.php | 2 +- module/Rest/src/Entity/ApiKey.php | 4 ++-- 9 files changed, 17 insertions(+), 14 deletions(-) diff --git a/module/Core/src/Repository/TagRepository.php b/module/Core/src/Repository/TagRepository.php index d5a33c8b..d21122d0 100644 --- a/module/Core/src/Repository/TagRepository.php +++ b/module/Core/src/Repository/TagRepository.php @@ -6,7 +6,6 @@ namespace Shlinkio\Shlink\Core\Repository; use Happyr\DoctrineSpecification\Repository\EntitySpecificationRepository; use Happyr\DoctrineSpecification\Spec; -use Happyr\DoctrineSpecification\Specification\Specification; use Shlinkio\Shlink\Core\Entity\Tag; use Shlinkio\Shlink\Core\Tag\Model\TagInfo; use Shlinkio\Shlink\Core\Tag\Spec\CountTagsWithName; @@ -33,7 +32,7 @@ class TagRepository extends EntitySpecificationRepository implements TagReposito /** * @return TagInfo[] */ - public function findTagsWithInfo(?Specification $spec = null): array + public function findTagsWithInfo(?ApiKey $apiKey = null): array { $qb = $this->createQueryBuilder('t'); $qb->select('t AS tag', 'COUNT(DISTINCT s.id) AS shortUrlsCount', 'COUNT(DISTINCT v.id) AS visitsCount') @@ -42,7 +41,9 @@ class TagRepository extends EntitySpecificationRepository implements TagReposito ->groupBy('t') ->orderBy('t.name', 'ASC'); - $this->applySpecification($qb, $spec, 't'); + if ($apiKey !== null) { + $this->applySpecification($qb, $apiKey->spec(false, 'shortUrls'), 't'); + } $query = $qb->getQuery(); diff --git a/module/Core/src/Repository/TagRepositoryInterface.php b/module/Core/src/Repository/TagRepositoryInterface.php index 97c308df..9833eea1 100644 --- a/module/Core/src/Repository/TagRepositoryInterface.php +++ b/module/Core/src/Repository/TagRepositoryInterface.php @@ -17,7 +17,7 @@ interface TagRepositoryInterface extends ObjectRepository, EntitySpecificationRe /** * @return TagInfo[] */ - public function findTagsWithInfo(?Specification $spec = null): array; + public function findTagsWithInfo(?ApiKey $apiKey = null): array; public function tagExists(string $tag, ?ApiKey $apiKey = null): bool; } diff --git a/module/Core/src/ShortUrl/Spec/BelongsToApiKey.php b/module/Core/src/ShortUrl/Spec/BelongsToApiKey.php index ac20d0db..4aa3579f 100644 --- a/module/Core/src/ShortUrl/Spec/BelongsToApiKey.php +++ b/module/Core/src/ShortUrl/Spec/BelongsToApiKey.php @@ -18,7 +18,7 @@ class BelongsToApiKey extends BaseSpecification { $this->apiKey = $apiKey; $this->dqlAlias = $dqlAlias; - parent::__construct($this->dqlAlias); + parent::__construct(); } protected function getSpec(): Filter diff --git a/module/Core/src/ShortUrl/Spec/BelongsToDomain.php b/module/Core/src/ShortUrl/Spec/BelongsToDomain.php index 59898476..7745ff27 100644 --- a/module/Core/src/ShortUrl/Spec/BelongsToDomain.php +++ b/module/Core/src/ShortUrl/Spec/BelongsToDomain.php @@ -17,7 +17,7 @@ class BelongsToDomain extends BaseSpecification { $this->domainId = $domainId; $this->dqlAlias = $dqlAlias; - parent::__construct($this->dqlAlias); + parent::__construct(); } protected function getSpec(): Filter diff --git a/module/Core/src/Tag/TagService.php b/module/Core/src/Tag/TagService.php index ae46a312..4619bd9d 100644 --- a/module/Core/src/Tag/TagService.php +++ b/module/Core/src/Tag/TagService.php @@ -52,7 +52,7 @@ class TagService implements TagServiceInterface { /** @var TagRepositoryInterface $repo */ $repo = $this->em->getRepository(Tag::class); - return $repo->findTagsWithInfo($apiKey !== null ? $apiKey->spec() : null); + return $repo->findTagsWithInfo($apiKey); } /** diff --git a/module/Core/test/Service/Tag/TagServiceTest.php b/module/Core/test/Service/Tag/TagServiceTest.php index 5f518184..9b484791 100644 --- a/module/Core/test/Service/Tag/TagServiceTest.php +++ b/module/Core/test/Service/Tag/TagServiceTest.php @@ -60,7 +60,7 @@ class TagServiceTest extends TestCase { $expected = [new TagInfo(new Tag('foo'), 1, 1), new TagInfo(new Tag('bar'), 3, 10)]; - $find = $this->repo->findTagsWithInfo($apiKey === null ? null : $apiKey->spec())->willReturn($expected); + $find = $this->repo->findTagsWithInfo($apiKey)->willReturn($expected); $result = $this->service->tagsInfo($apiKey); diff --git a/module/Rest/src/ApiKey/Role.php b/module/Rest/src/ApiKey/Role.php index 93ac717a..c3677029 100644 --- a/module/Rest/src/ApiKey/Role.php +++ b/module/Rest/src/ApiKey/Role.php @@ -21,16 +21,18 @@ class Role self::DOMAIN_SPECIFIC => 'Domain only', ]; - public static function toSpec(ApiKeyRole $role, bool $inlined): Specification + public static function toSpec(ApiKeyRole $role, bool $inlined, ?string $context = null): Specification { if ($role->name() === self::AUTHORED_SHORT_URLS) { $apiKey = $role->apiKey(); - return $inlined ? Spec::andX(new BelongsToApiKeyInlined($apiKey)) : new BelongsToApiKey($apiKey); + return $inlined ? Spec::andX(new BelongsToApiKeyInlined($apiKey)) : new BelongsToApiKey($apiKey, $context); } if ($role->name() === self::DOMAIN_SPECIFIC) { $domainId = self::domainIdFromMeta($role->meta()); - return $inlined ? Spec::andX(new BelongsToDomainInlined($domainId)) : new BelongsToDomain($domainId); + return $inlined + ? Spec::andX(new BelongsToDomainInlined($domainId)) + : new BelongsToDomain($domainId, $context); } return Spec::andX(); diff --git a/module/Rest/src/ApiKey/Spec/WithApiKeySpecsEnsuringJoin.php b/module/Rest/src/ApiKey/Spec/WithApiKeySpecsEnsuringJoin.php index e05dca41..a1f9b361 100644 --- a/module/Rest/src/ApiKey/Spec/WithApiKeySpecsEnsuringJoin.php +++ b/module/Rest/src/ApiKey/Spec/WithApiKeySpecsEnsuringJoin.php @@ -25,7 +25,7 @@ class WithApiKeySpecsEnsuringJoin extends BaseSpecification { return $this->apiKey === null || $this->apiKey->isAdmin() ? Spec::andX() : Spec::andX( Spec::join($this->fieldToJoin, 's'), - $this->apiKey->spec(), + $this->apiKey->spec(false, $this->fieldToJoin), ); } } diff --git a/module/Rest/src/Entity/ApiKey.php b/module/Rest/src/Entity/ApiKey.php index 62729031..d153b7f1 100644 --- a/module/Rest/src/Entity/ApiKey.php +++ b/module/Rest/src/Entity/ApiKey.php @@ -92,9 +92,9 @@ class ApiKey extends AbstractEntity return $this->key; } - public function spec(bool $inlined = false): Specification + public function spec(bool $inlined = false, ?string $context = null): Specification { - $specs = $this->roles->map(fn (ApiKeyRole $role) => Role::toSpec($role, $inlined))->getValues(); + $specs = $this->roles->map(fn (ApiKeyRole $role) => Role::toSpec($role, $inlined, $context))->getValues(); return Spec::andX(...$specs); }