2020-09-27 11:11:41 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioTest\Shlink\Core\Domain;
|
|
|
|
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2022-10-21 20:01:41 +03:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2020-09-27 11:11:41 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2021-12-09 14:32:02 +03:00
|
|
|
use Shlinkio\Shlink\Core\Config\EmptyNotFoundRedirectConfig;
|
2021-07-22 18:49:37 +03:00
|
|
|
use Shlinkio\Shlink\Core\Config\NotFoundRedirects;
|
2020-09-27 11:11:41 +03:00
|
|
|
use Shlinkio\Shlink\Core\Domain\DomainService;
|
2022-09-23 20:03:32 +03:00
|
|
|
use Shlinkio\Shlink\Core\Domain\Entity\Domain;
|
2021-01-04 17:02:37 +03:00
|
|
|
use Shlinkio\Shlink\Core\Domain\Model\DomainItem;
|
2020-09-27 11:11:41 +03:00
|
|
|
use Shlinkio\Shlink\Core\Domain\Repository\DomainRepositoryInterface;
|
2021-01-04 22:15:42 +03:00
|
|
|
use Shlinkio\Shlink\Core\Exception\DomainNotFoundException;
|
2021-03-14 11:59:35 +03:00
|
|
|
use Shlinkio\Shlink\Rest\ApiKey\Model\ApiKeyMeta;
|
2021-01-09 14:38:06 +03:00
|
|
|
use Shlinkio\Shlink\Rest\ApiKey\Model\RoleDefinition;
|
|
|
|
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
2020-09-27 11:11:41 +03:00
|
|
|
|
|
|
|
class DomainServiceTest extends TestCase
|
|
|
|
{
|
|
|
|
private DomainService $domainService;
|
2022-10-24 20:53:13 +03:00
|
|
|
private MockObject & EntityManagerInterface $em;
|
2020-09-27 11:11:41 +03:00
|
|
|
|
2022-09-11 13:02:49 +03:00
|
|
|
protected function setUp(): void
|
2020-09-27 11:11:41 +03:00
|
|
|
{
|
2022-10-21 20:01:41 +03:00
|
|
|
$this->em = $this->createMock(EntityManagerInterface::class);
|
|
|
|
$this->domainService = new DomainService($this->em, 'default.com');
|
2020-09-27 11:11:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideExcludedDomains
|
|
|
|
*/
|
2021-01-09 14:38:06 +03:00
|
|
|
public function listDomainsDelegatesIntoRepository(array $domains, array $expectedResult, ?ApiKey $apiKey): void
|
2020-09-27 11:11:41 +03:00
|
|
|
{
|
2022-10-21 20:01:41 +03:00
|
|
|
$repo = $this->createMock(DomainRepositoryInterface::class);
|
2022-10-23 19:15:57 +03:00
|
|
|
$repo->expects($this->once())->method('findDomains')->with($apiKey)->willReturn($domains);
|
|
|
|
$this->em->expects($this->once())->method('getRepository')->with(Domain::class)->willReturn($repo);
|
2020-09-27 11:11:41 +03:00
|
|
|
|
2021-01-09 14:38:06 +03:00
|
|
|
$result = $this->domainService->listDomains($apiKey);
|
2020-09-27 11:11:41 +03:00
|
|
|
|
|
|
|
self::assertEquals($expectedResult, $result);
|
|
|
|
}
|
|
|
|
|
2023-02-09 11:32:38 +03:00
|
|
|
public static function provideExcludedDomains(): iterable
|
2020-09-27 11:11:41 +03:00
|
|
|
{
|
2021-12-09 14:32:02 +03:00
|
|
|
$default = DomainItem::forDefaultDomain('default.com', new EmptyNotFoundRedirectConfig());
|
2021-03-14 11:59:35 +03:00
|
|
|
$adminApiKey = ApiKey::create();
|
2022-10-24 21:11:25 +03:00
|
|
|
$domain = Domain::withAuthority('');
|
|
|
|
$domain->setId('123');
|
2021-03-14 11:59:35 +03:00
|
|
|
$domainSpecificApiKey = ApiKey::fromMeta(
|
2022-10-24 21:11:25 +03:00
|
|
|
ApiKeyMeta::withRoles(RoleDefinition::forDomain($domain)),
|
2021-03-14 11:59:35 +03:00
|
|
|
);
|
2021-01-09 14:38:06 +03:00
|
|
|
|
|
|
|
yield 'empty list without API key' => [[], [$default], null];
|
|
|
|
yield 'one item without API key' => [
|
2021-07-22 21:48:58 +03:00
|
|
|
[Domain::withAuthority('bar.com')],
|
2021-12-09 14:32:02 +03:00
|
|
|
[$default, DomainItem::forNonDefaultDomain(Domain::withAuthority('bar.com'))],
|
2021-01-09 14:38:06 +03:00
|
|
|
null,
|
|
|
|
];
|
|
|
|
yield 'multiple items without API key' => [
|
2021-07-22 21:48:58 +03:00
|
|
|
[Domain::withAuthority('foo.com'), Domain::withAuthority('bar.com')],
|
2021-07-21 22:09:33 +03:00
|
|
|
[
|
|
|
|
$default,
|
2021-12-09 14:32:02 +03:00
|
|
|
DomainItem::forNonDefaultDomain(Domain::withAuthority('foo.com')),
|
|
|
|
DomainItem::forNonDefaultDomain(Domain::withAuthority('bar.com')),
|
2021-07-21 22:09:33 +03:00
|
|
|
],
|
2021-01-09 14:38:06 +03:00
|
|
|
null,
|
|
|
|
];
|
2021-01-04 17:02:37 +03:00
|
|
|
|
2021-01-09 14:38:06 +03:00
|
|
|
yield 'empty list with admin API key' => [[], [$default], $adminApiKey];
|
|
|
|
yield 'one item with admin API key' => [
|
2021-07-22 21:48:58 +03:00
|
|
|
[Domain::withAuthority('bar.com')],
|
2021-12-09 14:32:02 +03:00
|
|
|
[$default, DomainItem::forNonDefaultDomain(Domain::withAuthority('bar.com'))],
|
2021-01-09 14:38:06 +03:00
|
|
|
$adminApiKey,
|
|
|
|
];
|
|
|
|
yield 'multiple items with admin API key' => [
|
2021-07-22 21:48:58 +03:00
|
|
|
[Domain::withAuthority('foo.com'), Domain::withAuthority('bar.com')],
|
2021-07-21 22:09:33 +03:00
|
|
|
[
|
|
|
|
$default,
|
2021-12-09 14:32:02 +03:00
|
|
|
DomainItem::forNonDefaultDomain(Domain::withAuthority('foo.com')),
|
|
|
|
DomainItem::forNonDefaultDomain(Domain::withAuthority('bar.com')),
|
2021-07-21 22:09:33 +03:00
|
|
|
],
|
2021-01-09 14:38:06 +03:00
|
|
|
$adminApiKey,
|
|
|
|
];
|
|
|
|
|
|
|
|
yield 'empty list with domain-specific API key' => [[], [], $domainSpecificApiKey];
|
|
|
|
yield 'one item with domain-specific API key' => [
|
2021-07-22 21:48:58 +03:00
|
|
|
[Domain::withAuthority('bar.com')],
|
2021-12-09 14:32:02 +03:00
|
|
|
[DomainItem::forNonDefaultDomain(Domain::withAuthority('bar.com'))],
|
2021-01-09 14:38:06 +03:00
|
|
|
$domainSpecificApiKey,
|
|
|
|
];
|
|
|
|
yield 'multiple items with domain-specific API key' => [
|
2021-07-22 21:48:58 +03:00
|
|
|
[Domain::withAuthority('foo.com'), Domain::withAuthority('bar.com')],
|
2021-07-21 22:09:33 +03:00
|
|
|
[
|
2021-12-09 14:32:02 +03:00
|
|
|
DomainItem::forNonDefaultDomain(Domain::withAuthority('foo.com')),
|
|
|
|
DomainItem::forNonDefaultDomain(Domain::withAuthority('bar.com')),
|
2021-07-21 22:09:33 +03:00
|
|
|
],
|
2021-01-09 14:38:06 +03:00
|
|
|
$domainSpecificApiKey,
|
2021-01-04 17:02:37 +03:00
|
|
|
];
|
2020-09-27 11:11:41 +03:00
|
|
|
}
|
2021-01-04 22:15:42 +03:00
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function getDomainThrowsExceptionWhenDomainIsNotFound(): void
|
|
|
|
{
|
2022-10-23 19:15:57 +03:00
|
|
|
$this->em->expects($this->once())->method('find')->with(Domain::class, '123')->willReturn(null);
|
2021-01-04 22:15:42 +03:00
|
|
|
|
|
|
|
$this->expectException(DomainNotFoundException::class);
|
|
|
|
|
|
|
|
$this->domainService->getDomain('123');
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function getDomainReturnsEntityWhenFound(): void
|
|
|
|
{
|
2021-07-22 21:48:58 +03:00
|
|
|
$domain = Domain::withAuthority('');
|
2022-10-23 19:15:57 +03:00
|
|
|
$this->em->expects($this->once())->method('find')->with(Domain::class, '123')->willReturn($domain);
|
2021-01-04 22:15:42 +03:00
|
|
|
|
|
|
|
$result = $this->domainService->getDomain('123');
|
|
|
|
|
|
|
|
self::assertSame($domain, $result);
|
|
|
|
}
|
2021-01-10 22:05:14 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideFoundDomains
|
|
|
|
*/
|
2021-07-29 20:08:29 +03:00
|
|
|
public function getOrCreateAlwaysPersistsDomain(?Domain $foundDomain, ?ApiKey $apiKey): void
|
2021-01-10 22:05:14 +03:00
|
|
|
{
|
|
|
|
$authority = 'example.com';
|
2022-10-21 20:01:41 +03:00
|
|
|
$repo = $this->createMock(DomainRepositoryInterface::class);
|
2022-10-23 19:15:57 +03:00
|
|
|
$repo->method('findOneByAuthority')->with($authority, $apiKey)->willReturn(
|
2022-10-21 20:01:41 +03:00
|
|
|
$foundDomain,
|
|
|
|
);
|
2022-10-23 19:15:57 +03:00
|
|
|
$this->em->expects($this->once())->method('getRepository')->with(Domain::class)->willReturn($repo);
|
|
|
|
$this->em->expects($this->once())->method('persist')->with($foundDomain ?? $this->isInstanceOf(Domain::class));
|
2022-10-21 20:01:41 +03:00
|
|
|
$this->em->expects($this->once())->method('flush');
|
2021-01-10 22:05:14 +03:00
|
|
|
|
2021-07-29 20:08:29 +03:00
|
|
|
$result = $this->domainService->getOrCreate($authority, $apiKey);
|
2021-01-10 22:05:14 +03:00
|
|
|
|
|
|
|
if ($foundDomain !== null) {
|
|
|
|
self::assertSame($result, $foundDomain);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-29 20:08:29 +03:00
|
|
|
/** @test */
|
|
|
|
public function getOrCreateThrowsExceptionForApiKeysWithDomainRole(): void
|
|
|
|
{
|
|
|
|
$authority = 'example.com';
|
2022-10-24 21:11:25 +03:00
|
|
|
$domain = Domain::withAuthority($authority);
|
|
|
|
$domain->setId('1');
|
2021-07-29 20:08:29 +03:00
|
|
|
$apiKey = ApiKey::fromMeta(ApiKeyMeta::withRoles(RoleDefinition::forDomain($domain)));
|
2022-10-21 20:01:41 +03:00
|
|
|
$repo = $this->createMock(DomainRepositoryInterface::class);
|
2022-10-23 19:15:57 +03:00
|
|
|
$repo->method('findOneByAuthority')->with($authority, $apiKey)->willReturn(null);
|
|
|
|
$this->em->expects($this->once())->method('getRepository')->with(Domain::class)->willReturn($repo);
|
2022-10-21 20:01:41 +03:00
|
|
|
$this->em->expects($this->never())->method('persist');
|
|
|
|
$this->em->expects($this->never())->method('flush');
|
2021-07-29 20:08:29 +03:00
|
|
|
|
|
|
|
$this->expectException(DomainNotFoundException::class);
|
|
|
|
|
|
|
|
$this->domainService->getOrCreate($authority, $apiKey);
|
|
|
|
}
|
|
|
|
|
2021-07-22 18:49:37 +03:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideFoundDomains
|
|
|
|
*/
|
2021-07-29 20:08:29 +03:00
|
|
|
public function configureNotFoundRedirectsConfiguresFetchedDomain(?Domain $foundDomain, ?ApiKey $apiKey): void
|
2021-07-22 18:49:37 +03:00
|
|
|
{
|
|
|
|
$authority = 'example.com';
|
2022-10-21 20:01:41 +03:00
|
|
|
$repo = $this->createMock(DomainRepositoryInterface::class);
|
2022-10-23 19:15:57 +03:00
|
|
|
$repo->method('findOneByAuthority')->with($authority, $apiKey)->willReturn($foundDomain);
|
|
|
|
$this->em->expects($this->once())->method('getRepository')->with(Domain::class)->willReturn($repo);
|
|
|
|
$this->em->expects($this->once())->method('persist')->with($foundDomain ?? $this->isInstanceOf(Domain::class));
|
2022-10-21 20:01:41 +03:00
|
|
|
$this->em->expects($this->once())->method('flush');
|
2021-07-22 18:49:37 +03:00
|
|
|
|
2021-08-03 15:08:36 +03:00
|
|
|
$result = $this->domainService->configureNotFoundRedirects($authority, NotFoundRedirects::withRedirects(
|
2021-07-22 18:49:37 +03:00
|
|
|
'foo.com',
|
|
|
|
'bar.com',
|
|
|
|
'baz.com',
|
2021-07-29 20:08:29 +03:00
|
|
|
), $apiKey);
|
2021-07-22 18:49:37 +03:00
|
|
|
|
|
|
|
if ($foundDomain !== null) {
|
|
|
|
self::assertSame($result, $foundDomain);
|
|
|
|
}
|
|
|
|
self::assertEquals('foo.com', $result->baseUrlRedirect());
|
|
|
|
self::assertEquals('bar.com', $result->regular404Redirect());
|
|
|
|
self::assertEquals('baz.com', $result->invalidShortUrlRedirect());
|
|
|
|
}
|
|
|
|
|
2023-02-09 11:32:38 +03:00
|
|
|
public static function provideFoundDomains(): iterable
|
2021-01-10 22:05:14 +03:00
|
|
|
{
|
2021-07-29 20:08:29 +03:00
|
|
|
$domain = Domain::withAuthority('');
|
|
|
|
$adminApiKey = ApiKey::create();
|
|
|
|
$authorApiKey = ApiKey::fromMeta(ApiKeyMeta::withRoles(RoleDefinition::forAuthoredShortUrls()));
|
|
|
|
|
|
|
|
yield 'domain not found and no API key' => [null, null];
|
|
|
|
yield 'domain found and no API key' => [$domain, null];
|
|
|
|
yield 'domain not found and admin API key' => [null, $adminApiKey];
|
|
|
|
yield 'domain found and admin API key' => [$domain, $adminApiKey];
|
|
|
|
yield 'domain not found and author API key' => [null, $authorApiKey];
|
|
|
|
yield 'domain found and author API key' => [$domain, $authorApiKey];
|
2021-01-10 22:05:14 +03:00
|
|
|
}
|
2020-09-27 11:11:41 +03:00
|
|
|
}
|