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