mirror of
https://github.com/shlinkio/shlink.git
synced 2025-03-14 04:00:57 +03:00
Merge pull request #1124 from acelaya-forks/feature/domain-specific-redirects
Feature/domain specific redirects
This commit is contained in:
commit
dbc50b6d4f
40 changed files with 1038 additions and 184 deletions
|
@ -17,6 +17,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com), and this
|
|||
|
||||
This behavior needs to be actively opted in, via installer config options or env vars.
|
||||
|
||||
* [#943](https://github.com/shlinkio/shlink/issues/943) Added support to define different "not-found" redirects for every domain handled by Shlink.
|
||||
|
||||
Shlink will continue to allow defining the default values via env vars or config, but afterwards, you can use the `domain:redirects` command to define specific values for every single domain.
|
||||
|
||||
### Changed
|
||||
* [#1118](https://github.com/shlinkio/shlink/issues/1118) Increased phpstan required level to 8.
|
||||
|
||||
|
|
|
@ -2,13 +2,16 @@
|
|||
|
||||
declare(strict_types=1);
|
||||
|
||||
$isSwoole = extension_loaded('swoole');
|
||||
|
||||
return [
|
||||
|
||||
'url_shortener' => [
|
||||
'domain' => [
|
||||
'schema' => 'http',
|
||||
'hostname' => 'localhost:8080',
|
||||
'hostname' => sprintf('localhost:%s', $isSwoole ? '8080' : '8000'),
|
||||
],
|
||||
'auto_resolve_titles' => true,
|
||||
],
|
||||
|
||||
];
|
||||
|
|
41
data/migrations/Version20210720143824.php
Normal file
41
data/migrations/Version20210720143824.php
Normal file
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ShlinkMigrations;
|
||||
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Doctrine\DBAL\Schema\Table;
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\Migrations\AbstractMigration;
|
||||
|
||||
final class Version20210720143824 extends AbstractMigration
|
||||
{
|
||||
public function up(Schema $schema): void
|
||||
{
|
||||
$domainsTable = $schema->getTable('domains');
|
||||
$this->skipIf($domainsTable->hasColumn('base_url_redirect'));
|
||||
|
||||
$this->createRedirectColumn($domainsTable, 'base_url_redirect');
|
||||
$this->createRedirectColumn($domainsTable, 'regular_not_found_redirect');
|
||||
$this->createRedirectColumn($domainsTable, 'invalid_short_url_redirect');
|
||||
}
|
||||
|
||||
private function createRedirectColumn(Table $table, string $columnName): void
|
||||
{
|
||||
$table->addColumn($columnName, Types::STRING, [
|
||||
'notnull' => false,
|
||||
'default' => null,
|
||||
]);
|
||||
}
|
||||
|
||||
public function down(Schema $schema): void
|
||||
{
|
||||
$domainsTable = $schema->getTable('domains');
|
||||
$this->skipIf(! $domainsTable->hasColumn('base_url_redirect'));
|
||||
|
||||
$domainsTable->dropColumn('base_url_redirect');
|
||||
$domainsTable->dropColumn('regular_not_found_redirect');
|
||||
$domainsTable->dropColumn('invalid_short_url_redirect');
|
||||
}
|
||||
}
|
|
@ -27,6 +27,7 @@ return [
|
|||
Command\Tag\DeleteTagsCommand::NAME => Command\Tag\DeleteTagsCommand::class,
|
||||
|
||||
Command\Domain\ListDomainsCommand::NAME => Command\Domain\ListDomainsCommand::class,
|
||||
Command\Domain\DomainRedirectsCommand::NAME => Command\Domain\DomainRedirectsCommand::class,
|
||||
|
||||
Command\Db\CreateDatabaseCommand::NAME => Command\Db\CreateDatabaseCommand::class,
|
||||
Command\Db\MigrateDatabaseCommand::NAME => Command\Db\MigrateDatabaseCommand::class,
|
||||
|
|
|
@ -61,6 +61,7 @@ return [
|
|||
Command\Db\MigrateDatabaseCommand::class => ConfigAbstractFactory::class,
|
||||
|
||||
Command\Domain\ListDomainsCommand::class => ConfigAbstractFactory::class,
|
||||
Command\Domain\DomainRedirectsCommand::class => ConfigAbstractFactory::class,
|
||||
],
|
||||
],
|
||||
|
||||
|
@ -104,6 +105,7 @@ return [
|
|||
Command\Tag\DeleteTagsCommand::class => [TagService::class],
|
||||
|
||||
Command\Domain\ListDomainsCommand::class => [DomainService::class],
|
||||
Command\Domain\DomainRedirectsCommand::class => [DomainService::class],
|
||||
|
||||
Command\Db\CreateDatabaseCommand::class => [
|
||||
LockFactory::class,
|
||||
|
|
114
module/CLI/src/Command/Domain/DomainRedirectsCommand.php
Normal file
114
module/CLI/src/Command/Domain/DomainRedirectsCommand.php
Normal file
|
@ -0,0 +1,114 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\CLI\Command\Domain;
|
||||
|
||||
use Shlinkio\Shlink\CLI\Util\ExitCodes;
|
||||
use Shlinkio\Shlink\Core\Config\NotFoundRedirects;
|
||||
use Shlinkio\Shlink\Core\Domain\DomainServiceInterface;
|
||||
use Shlinkio\Shlink\Core\Domain\Model\DomainItem;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
|
||||
use function Functional\filter;
|
||||
use function Functional\invoke;
|
||||
use function sprintf;
|
||||
use function str_contains;
|
||||
|
||||
class DomainRedirectsCommand extends Command
|
||||
{
|
||||
public const NAME = 'domain:redirects';
|
||||
|
||||
public function __construct(private DomainServiceInterface $domainService)
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure(): void
|
||||
{
|
||||
$this
|
||||
->setName(self::NAME)
|
||||
->setDescription('Set specific "not found" redirects for individual domains.')
|
||||
->addArgument(
|
||||
'domain',
|
||||
InputArgument::REQUIRED,
|
||||
'The domain authority to which you want to set the specific redirects',
|
||||
);
|
||||
}
|
||||
|
||||
protected function interact(InputInterface $input, OutputInterface $output): void
|
||||
{
|
||||
/** @var string|null $domain */
|
||||
$domain = $input->getArgument('domain');
|
||||
if ($domain !== null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
$askNewDomain = static fn () => $io->ask('Domain authority for which you want to set specific redirects');
|
||||
|
||||
/** @var string[] $availableDomains */
|
||||
$availableDomains = invoke(
|
||||
filter($this->domainService->listDomains(), static fn (DomainItem $item) => ! $item->isDefault()),
|
||||
'toString',
|
||||
);
|
||||
if (empty($availableDomains)) {
|
||||
$input->setArgument('domain', $askNewDomain());
|
||||
return;
|
||||
}
|
||||
|
||||
$selectedOption = $io->choice(
|
||||
'Select the domain to configure',
|
||||
[...$availableDomains, '<options=bold>New domain</>'],
|
||||
);
|
||||
$input->setArgument('domain', str_contains($selectedOption, 'New domain') ? $askNewDomain() : $selectedOption);
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): ?int
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
$domainAuthority = $input->getArgument('domain');
|
||||
$domain = $this->domainService->findByAuthority($domainAuthority);
|
||||
|
||||
$ask = static function (string $message, ?string $current) use ($io): ?string {
|
||||
if ($current === null) {
|
||||
return $io->ask(sprintf('%s (Leave empty for no redirect)', $message));
|
||||
}
|
||||
|
||||
$choice = $io->choice($message, [
|
||||
sprintf('Keep current one: [%s]', $current),
|
||||
'Set new redirect URL',
|
||||
'Remove redirect',
|
||||
]);
|
||||
|
||||
return match ($choice) {
|
||||
'Set new redirect URL' => $io->ask('New redirect URL'),
|
||||
'Remove redirect' => null,
|
||||
default => $current,
|
||||
};
|
||||
};
|
||||
|
||||
$this->domainService->configureNotFoundRedirects($domainAuthority, new NotFoundRedirects(
|
||||
$ask(
|
||||
'URL to redirect to when a user hits this domain\'s base URL',
|
||||
$domain?->baseUrlRedirect(),
|
||||
),
|
||||
$ask(
|
||||
'URL to redirect to when a user hits a not found URL other than an invalid short URL',
|
||||
$domain?->regular404Redirect(),
|
||||
),
|
||||
$ask(
|
||||
'URL to redirect to when a user hits an invalid short URL',
|
||||
$domain?->invalidShortUrlRedirect(),
|
||||
),
|
||||
));
|
||||
|
||||
$io->success(sprintf('"Not found" redirects properly set for "%s"', $domainAuthority));
|
||||
|
||||
return ExitCodes::EXIT_SUCCESS;
|
||||
}
|
||||
}
|
|
@ -6,10 +6,12 @@ namespace Shlinkio\Shlink\CLI\Command\Domain;
|
|||
|
||||
use Shlinkio\Shlink\CLI\Util\ExitCodes;
|
||||
use Shlinkio\Shlink\CLI\Util\ShlinkTable;
|
||||
use Shlinkio\Shlink\Core\Config\NotFoundRedirectConfigInterface;
|
||||
use Shlinkio\Shlink\Core\Domain\DomainServiceInterface;
|
||||
use Shlinkio\Shlink\Core\Domain\Model\DomainItem;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
use function Functional\map;
|
||||
|
@ -27,18 +29,48 @@ class ListDomainsCommand extends Command
|
|||
{
|
||||
$this
|
||||
->setName(self::NAME)
|
||||
->setDescription('List all domains that have been ever used for some short URL');
|
||||
->setDescription('List all domains that have been ever used for some short URL')
|
||||
->addOption(
|
||||
'show-redirects',
|
||||
'r',
|
||||
InputOption::VALUE_NONE,
|
||||
'Will display an extra column with the information of the "not found" redirects for every domain.',
|
||||
);
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): ?int
|
||||
{
|
||||
$domains = $this->domainService->listDomains();
|
||||
$showRedirects = $input->getOption('show-redirects');
|
||||
$commonFields = ['Domain', 'Is default'];
|
||||
|
||||
ShlinkTable::fromOutput($output)->render(
|
||||
['Domain', 'Is default'],
|
||||
map($domains, fn (DomainItem $domain) => [$domain->toString(), $domain->isDefault() ? 'Yes' : 'No']),
|
||||
$showRedirects ? [...$commonFields, '"Not found" redirects'] : $commonFields,
|
||||
map($domains, function (DomainItem $domain) use ($showRedirects) {
|
||||
$commonValues = [$domain->toString(), $domain->isDefault() ? 'Yes' : 'No'];
|
||||
|
||||
return $showRedirects
|
||||
? [
|
||||
...$commonValues,
|
||||
$this->notFoundRedirectsToString($domain->notFoundRedirectConfig()),
|
||||
]
|
||||
: $commonValues;
|
||||
}),
|
||||
);
|
||||
|
||||
return ExitCodes::EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
private function notFoundRedirectsToString(NotFoundRedirectConfigInterface $config): string
|
||||
{
|
||||
$baseUrl = $config->baseUrlRedirect() ?? 'N/A';
|
||||
$regular404 = $config->regular404Redirect() ?? 'N/A';
|
||||
$invalidShortUrl = $config->invalidShortUrlRedirect() ?? 'N/A';
|
||||
|
||||
return <<<EOL
|
||||
* Base URL: {$baseUrl}
|
||||
* Regular 404: {$regular404}
|
||||
* Invalid short URL: {$invalidShortUrl}
|
||||
EOL;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,7 +36,7 @@ class RoleResolverTest extends TestCase
|
|||
int $expectedDomainCalls,
|
||||
): void {
|
||||
$getDomain = $this->domainService->getOrCreate('example.com')->willReturn(
|
||||
(new Domain('example.com'))->setId('1'),
|
||||
Domain::withAuthority('example.com')->setId('1'),
|
||||
);
|
||||
|
||||
$result = $this->resolver->determineRoles($input);
|
||||
|
@ -47,7 +47,7 @@ class RoleResolverTest extends TestCase
|
|||
|
||||
public function provideRoles(): iterable
|
||||
{
|
||||
$domain = (new Domain('example.com'))->setId('1');
|
||||
$domain = Domain::withAuthority('example.com')->setId('1');
|
||||
$buildInput = function (array $definition): InputInterface {
|
||||
$input = $this->prophesize(InputInterface::class);
|
||||
|
||||
|
|
|
@ -76,11 +76,13 @@ class ListKeysCommandTest extends TestCase
|
|||
[
|
||||
$apiKey1 = ApiKey::create(),
|
||||
$apiKey2 = $this->apiKeyWithRoles([RoleDefinition::forAuthoredShortUrls()]),
|
||||
$apiKey3 = $this->apiKeyWithRoles([RoleDefinition::forDomain((new Domain('example.com'))->setId('1'))]),
|
||||
$apiKey3 = $this->apiKeyWithRoles(
|
||||
[RoleDefinition::forDomain(Domain::withAuthority('example.com')->setId('1'))],
|
||||
),
|
||||
$apiKey4 = ApiKey::create(),
|
||||
$apiKey5 = $this->apiKeyWithRoles([
|
||||
RoleDefinition::forAuthoredShortUrls(),
|
||||
RoleDefinition::forDomain((new Domain('example.com'))->setId('1')),
|
||||
RoleDefinition::forDomain(Domain::withAuthority('example.com')->setId('1')),
|
||||
]),
|
||||
$apiKey6 = ApiKey::create(),
|
||||
],
|
||||
|
|
180
module/CLI/test/Command/Domain/DomainRedirectsCommandTest.php
Normal file
180
module/CLI/test/Command/Domain/DomainRedirectsCommandTest.php
Normal file
|
@ -0,0 +1,180 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioTest\Shlink\CLI\Command\Domain;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Prophecy\Prophecy\ObjectProphecy;
|
||||
use Shlinkio\Shlink\CLI\Command\Domain\DomainRedirectsCommand;
|
||||
use Shlinkio\Shlink\Core\Config\NotFoundRedirects;
|
||||
use Shlinkio\Shlink\Core\Domain\DomainServiceInterface;
|
||||
use Shlinkio\Shlink\Core\Domain\Model\DomainItem;
|
||||
use Shlinkio\Shlink\Core\Entity\Domain;
|
||||
use Shlinkio\Shlink\Core\Options\NotFoundRedirectOptions;
|
||||
use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait;
|
||||
use Symfony\Component\Console\Tester\CommandTester;
|
||||
|
||||
use function substr_count;
|
||||
|
||||
class DomainRedirectsCommandTest extends TestCase
|
||||
{
|
||||
use CliTestUtilsTrait;
|
||||
|
||||
private CommandTester $commandTester;
|
||||
private ObjectProphecy $domainService;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->domainService = $this->prophesize(DomainServiceInterface::class);
|
||||
$this->commandTester = $this->testerForCommand(new DomainRedirectsCommand($this->domainService->reveal()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideDomains
|
||||
*/
|
||||
public function onlyPlainQuestionsAreAskedForNewDomainsAndDomainsWithNoRedirects(?Domain $domain): void
|
||||
{
|
||||
$domainAuthority = 'my-domain.com';
|
||||
$findDomain = $this->domainService->findByAuthority($domainAuthority)->willReturn($domain);
|
||||
$configureRedirects = $this->domainService->configureNotFoundRedirects(
|
||||
$domainAuthority,
|
||||
new NotFoundRedirects('foo.com', null, 'baz.com'),
|
||||
)->willReturn(Domain::withAuthority(''));
|
||||
|
||||
$this->commandTester->setInputs(['foo.com', '', 'baz.com']);
|
||||
$this->commandTester->execute(['domain' => $domainAuthority]);
|
||||
$output = $this->commandTester->getDisplay();
|
||||
|
||||
self::assertStringContainsString('[OK] "Not found" redirects properly set for "my-domain.com"', $output);
|
||||
self::assertStringContainsString('URL to redirect to when a user hits this domain\'s base URL', $output);
|
||||
self::assertStringContainsString(
|
||||
'URL to redirect to when a user hits a not found URL other than an invalid short URL',
|
||||
$output,
|
||||
);
|
||||
self::assertStringContainsString('URL to redirect to when a user hits an invalid short URL', $output);
|
||||
self::assertEquals(3, substr_count($output, '(Leave empty for no redirect)'));
|
||||
$findDomain->shouldHaveBeenCalledOnce();
|
||||
$configureRedirects->shouldHaveBeenCalledOnce();
|
||||
$this->domainService->listDomains()->shouldNotHaveBeenCalled();
|
||||
}
|
||||
|
||||
public function provideDomains(): iterable
|
||||
{
|
||||
yield 'no domain' => [null];
|
||||
yield 'domain without redirects' => [Domain::withAuthority('')];
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function offersNewOptionsForDomainsWithExistingRedirects(): void
|
||||
{
|
||||
$domainAuthority = 'example.com';
|
||||
$domain = Domain::withAuthority($domainAuthority);
|
||||
$domain->configureNotFoundRedirects(new NotFoundRedirects('foo.com', 'bar.com', 'baz.com'));
|
||||
|
||||
$findDomain = $this->domainService->findByAuthority($domainAuthority)->willReturn($domain);
|
||||
$configureRedirects = $this->domainService->configureNotFoundRedirects(
|
||||
$domainAuthority,
|
||||
new NotFoundRedirects(null, 'edited.com', 'baz.com'),
|
||||
)->willReturn($domain);
|
||||
|
||||
$this->commandTester->setInputs(['2', '1', 'edited.com', '0']);
|
||||
$this->commandTester->execute(['domain' => $domainAuthority]);
|
||||
$output = $this->commandTester->getDisplay();
|
||||
|
||||
self::assertStringContainsString('[OK] "Not found" redirects properly set for "example.com"', $output);
|
||||
self::assertStringContainsString('Keep current one: [bar.com]', $output);
|
||||
self::assertStringContainsString('Keep current one: [baz.com]', $output);
|
||||
self::assertStringContainsString('Keep current one: [baz.com]', $output);
|
||||
self::assertStringNotContainsStringIgnoringCase('(Leave empty for no redirect)', $output);
|
||||
self::assertEquals(3, substr_count($output, 'Set new redirect URL'));
|
||||
self::assertEquals(3, substr_count($output, 'Remove redirect'));
|
||||
$findDomain->shouldHaveBeenCalledOnce();
|
||||
$configureRedirects->shouldHaveBeenCalledOnce();
|
||||
$this->domainService->listDomains()->shouldNotHaveBeenCalled();
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function authorityIsRequestedWhenNotProvidedAndNoOtherDomainsExist(): void
|
||||
{
|
||||
$domainAuthority = 'example.com';
|
||||
$domain = Domain::withAuthority($domainAuthority);
|
||||
|
||||
$listDomains = $this->domainService->listDomains()->willReturn([]);
|
||||
$findDomain = $this->domainService->findByAuthority($domainAuthority)->willReturn($domain);
|
||||
$configureRedirects = $this->domainService->configureNotFoundRedirects(
|
||||
$domainAuthority,
|
||||
new NotFoundRedirects(),
|
||||
)->willReturn($domain);
|
||||
|
||||
$this->commandTester->setInputs([$domainAuthority, '', '', '']);
|
||||
$this->commandTester->execute([]);
|
||||
$output = $this->commandTester->getDisplay();
|
||||
|
||||
self::assertStringContainsString('Domain authority for which you want to set specific redirects', $output);
|
||||
$listDomains->shouldHaveBeenCalledOnce();
|
||||
$findDomain->shouldHaveBeenCalledOnce();
|
||||
$configureRedirects->shouldHaveBeenCalledOnce();
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function oneOfTheExistingDomainsCanBeSelected(): void
|
||||
{
|
||||
$domainAuthority = 'existing-two.com';
|
||||
$domain = Domain::withAuthority($domainAuthority);
|
||||
|
||||
$listDomains = $this->domainService->listDomains()->willReturn([
|
||||
DomainItem::forDefaultDomain('default-domain.com', new NotFoundRedirectOptions()),
|
||||
DomainItem::forExistingDomain(Domain::withAuthority('existing-one.com')),
|
||||
DomainItem::forExistingDomain(Domain::withAuthority($domainAuthority)),
|
||||
]);
|
||||
$findDomain = $this->domainService->findByAuthority($domainAuthority)->willReturn($domain);
|
||||
$configureRedirects = $this->domainService->configureNotFoundRedirects(
|
||||
$domainAuthority,
|
||||
new NotFoundRedirects(),
|
||||
)->willReturn($domain);
|
||||
|
||||
$this->commandTester->setInputs(['1', '', '', '']);
|
||||
$this->commandTester->execute([]);
|
||||
$output = $this->commandTester->getDisplay();
|
||||
|
||||
self::assertStringNotContainsString('Domain authority for which you want to set specific redirects', $output);
|
||||
self::assertStringNotContainsString('default-domain.com', $output);
|
||||
self::assertStringContainsString('existing-one.com', $output);
|
||||
self::assertStringContainsString($domainAuthority, $output);
|
||||
$listDomains->shouldHaveBeenCalledOnce();
|
||||
$findDomain->shouldHaveBeenCalledOnce();
|
||||
$configureRedirects->shouldHaveBeenCalledOnce();
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function aNewDomainCanBeCreatedEvenIfOthersAlreadyExist(): void
|
||||
{
|
||||
$domainAuthority = 'new-domain.com';
|
||||
$domain = Domain::withAuthority($domainAuthority);
|
||||
|
||||
$listDomains = $this->domainService->listDomains()->willReturn([
|
||||
DomainItem::forDefaultDomain('default-domain.com', new NotFoundRedirectOptions()),
|
||||
DomainItem::forExistingDomain(Domain::withAuthority('existing-one.com')),
|
||||
DomainItem::forExistingDomain(Domain::withAuthority('existing-two.com')),
|
||||
]);
|
||||
$findDomain = $this->domainService->findByAuthority($domainAuthority)->willReturn($domain);
|
||||
$configureRedirects = $this->domainService->configureNotFoundRedirects(
|
||||
$domainAuthority,
|
||||
new NotFoundRedirects(),
|
||||
)->willReturn($domain);
|
||||
|
||||
$this->commandTester->setInputs(['2', $domainAuthority, '', '', '']);
|
||||
$this->commandTester->execute([]);
|
||||
$output = $this->commandTester->getDisplay();
|
||||
|
||||
self::assertStringContainsString('Domain authority for which you want to set specific redirects', $output);
|
||||
self::assertStringNotContainsString('default-domain.com', $output);
|
||||
self::assertStringContainsString('existing-one.com', $output);
|
||||
self::assertStringContainsString('existing-two.com', $output);
|
||||
$listDomains->shouldHaveBeenCalledOnce();
|
||||
$findDomain->shouldHaveBeenCalledOnce();
|
||||
$configureRedirects->shouldHaveBeenCalledOnce();
|
||||
}
|
||||
}
|
|
@ -8,8 +8,11 @@ use PHPUnit\Framework\TestCase;
|
|||
use Prophecy\Prophecy\ObjectProphecy;
|
||||
use Shlinkio\Shlink\CLI\Command\Domain\ListDomainsCommand;
|
||||
use Shlinkio\Shlink\CLI\Util\ExitCodes;
|
||||
use Shlinkio\Shlink\Core\Config\NotFoundRedirects;
|
||||
use Shlinkio\Shlink\Core\Domain\DomainServiceInterface;
|
||||
use Shlinkio\Shlink\Core\Domain\Model\DomainItem;
|
||||
use Shlinkio\Shlink\Core\Entity\Domain;
|
||||
use Shlinkio\Shlink\Core\Options\NotFoundRedirectOptions;
|
||||
use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait;
|
||||
use Symfony\Component\Console\Tester\CommandTester;
|
||||
|
||||
|
@ -26,10 +29,38 @@ class ListDomainsCommandTest extends TestCase
|
|||
$this->commandTester = $this->testerForCommand(new ListDomainsCommand($this->domainService->reveal()));
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function allDomainsAreProperlyPrinted(): void
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideInputsAndOutputs
|
||||
*/
|
||||
public function allDomainsAreProperlyPrinted(array $input, string $expectedOutput): void
|
||||
{
|
||||
$expectedOutput = <<<OUTPUT
|
||||
$bazDomain = Domain::withAuthority('baz.com');
|
||||
$bazDomain->configureNotFoundRedirects(new NotFoundRedirects(
|
||||
null,
|
||||
'https://foo.com/baz-domain/regular',
|
||||
'https://foo.com/baz-domain/invalid',
|
||||
));
|
||||
|
||||
$listDomains = $this->domainService->listDomains()->willReturn([
|
||||
DomainItem::forDefaultDomain('foo.com', new NotFoundRedirectOptions([
|
||||
'base_url' => 'https://foo.com/default/base',
|
||||
'invalid_short_url' => 'https://foo.com/default/invalid',
|
||||
])),
|
||||
DomainItem::forExistingDomain(Domain::withAuthority('bar.com')),
|
||||
DomainItem::forExistingDomain($bazDomain),
|
||||
]);
|
||||
|
||||
$this->commandTester->execute($input);
|
||||
|
||||
self::assertEquals($expectedOutput, $this->commandTester->getDisplay());
|
||||
self::assertEquals(ExitCodes::EXIT_SUCCESS, $this->commandTester->getStatusCode());
|
||||
$listDomains->shouldHaveBeenCalledOnce();
|
||||
}
|
||||
|
||||
public function provideInputsAndOutputs(): iterable
|
||||
{
|
||||
$withoutRedirectsOutput = <<<OUTPUT
|
||||
+---------+------------+
|
||||
| Domain | Is default |
|
||||
+---------+------------+
|
||||
|
@ -39,16 +70,25 @@ class ListDomainsCommandTest extends TestCase
|
|||
+---------+------------+
|
||||
|
||||
OUTPUT;
|
||||
$listDomains = $this->domainService->listDomains()->willReturn([
|
||||
new DomainItem('foo.com', true),
|
||||
new DomainItem('bar.com', false),
|
||||
new DomainItem('baz.com', false),
|
||||
]);
|
||||
$withRedirectsOutput = <<<OUTPUT
|
||||
+---------+------------+---------------------------------------------------------+
|
||||
| Domain | Is default | "Not found" redirects |
|
||||
+---------+------------+---------------------------------------------------------+
|
||||
| foo.com | Yes | * Base URL: https://foo.com/default/base |
|
||||
| | | * Regular 404: N/A |
|
||||
| | | * Invalid short URL: https://foo.com/default/invalid |
|
||||
| bar.com | No | * Base URL: N/A |
|
||||
| | | * Regular 404: N/A |
|
||||
| | | * Invalid short URL: N/A |
|
||||
| baz.com | No | * Base URL: N/A |
|
||||
| | | * Regular 404: https://foo.com/baz-domain/regular |
|
||||
| | | * Invalid short URL: https://foo.com/baz-domain/invalid |
|
||||
+---------+------------+---------------------------------------------------------+
|
||||
|
||||
$this->commandTester->execute([]);
|
||||
OUTPUT;
|
||||
|
||||
self::assertEquals($expectedOutput, $this->commandTester->getDisplay());
|
||||
self::assertEquals(ExitCodes::EXIT_SUCCESS, $this->commandTester->getStatusCode());
|
||||
$listDomains->shouldHaveBeenCalledOnce();
|
||||
yield 'no args' => [[], $withoutRedirectsOutput];
|
||||
yield 'no show redirects' => [['--show-redirects' => false], $withoutRedirectsOutput];
|
||||
yield 'show redirects' => [['--show-redirects' => true], $withRedirectsOutput];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -46,6 +46,8 @@ return [
|
|||
Util\DoctrineBatchHelper::class => ConfigAbstractFactory::class,
|
||||
Util\RedirectResponseHelper::class => ConfigAbstractFactory::class,
|
||||
|
||||
Config\NotFoundRedirectResolver::class => ConfigAbstractFactory::class,
|
||||
|
||||
Action\RedirectAction::class => ConfigAbstractFactory::class,
|
||||
Action\PixelAction::class => ConfigAbstractFactory::class,
|
||||
Action\QrCodeAction::class => ConfigAbstractFactory::class,
|
||||
|
@ -75,7 +77,8 @@ return [
|
|||
ErrorHandler\NotFoundTrackerMiddleware::class => [Visit\RequestTracker::class],
|
||||
ErrorHandler\NotFoundRedirectHandler::class => [
|
||||
NotFoundRedirectOptions::class,
|
||||
Util\RedirectResponseHelper::class,
|
||||
Config\NotFoundRedirectResolver::class,
|
||||
Domain\DomainService::class,
|
||||
],
|
||||
|
||||
Options\AppOptions::class => ['config.app_options'],
|
||||
|
@ -112,12 +115,18 @@ return [
|
|||
],
|
||||
Service\ShortUrl\ShortUrlResolver::class => ['em'],
|
||||
Service\ShortUrl\ShortCodeHelper::class => ['em'],
|
||||
Domain\DomainService::class => ['em', 'config.url_shortener.domain.hostname'],
|
||||
Domain\DomainService::class => [
|
||||
'em',
|
||||
'config.url_shortener.domain.hostname',
|
||||
Options\NotFoundRedirectOptions::class,
|
||||
],
|
||||
|
||||
Util\UrlValidator::class => ['httpClient', Options\UrlShortenerOptions::class],
|
||||
Util\DoctrineBatchHelper::class => ['em'],
|
||||
Util\RedirectResponseHelper::class => [Options\UrlShortenerOptions::class],
|
||||
|
||||
Config\NotFoundRedirectResolver::class => [Util\RedirectResponseHelper::class],
|
||||
|
||||
Action\RedirectAction::class => [
|
||||
Service\ShortUrl\ShortUrlResolver::class,
|
||||
Visit\RequestTracker::class,
|
||||
|
|
|
@ -24,4 +24,19 @@ return static function (ClassMetadata $metadata, array $emConfig): void {
|
|||
$builder->createField('authority', Types::STRING)
|
||||
->unique()
|
||||
->build();
|
||||
|
||||
$builder->createField('baseUrlRedirect', Types::STRING)
|
||||
->columnName('base_url_redirect')
|
||||
->nullable()
|
||||
->build();
|
||||
|
||||
$builder->createField('regular404Redirect', Types::STRING)
|
||||
->columnName('regular_not_found_redirect')
|
||||
->nullable()
|
||||
->build();
|
||||
|
||||
$builder->createField('invalidShortUrlRedirect', Types::STRING)
|
||||
->columnName('invalid_short_url_redirect')
|
||||
->nullable()
|
||||
->build();
|
||||
};
|
||||
|
|
20
module/Core/src/Config/NotFoundRedirectConfigInterface.php
Normal file
20
module/Core/src/Config/NotFoundRedirectConfigInterface.php
Normal file
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Config;
|
||||
|
||||
interface NotFoundRedirectConfigInterface
|
||||
{
|
||||
public function invalidShortUrlRedirect(): ?string;
|
||||
|
||||
public function hasInvalidShortUrlRedirect(): bool;
|
||||
|
||||
public function regular404Redirect(): ?string;
|
||||
|
||||
public function hasRegular404Redirect(): bool;
|
||||
|
||||
public function baseUrlRedirect(): ?string;
|
||||
|
||||
public function hasBaseUrlRedirect(): bool;
|
||||
}
|
34
module/Core/src/Config/NotFoundRedirectResolver.php
Normal file
34
module/Core/src/Config/NotFoundRedirectResolver.php
Normal file
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Config;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Shlinkio\Shlink\Core\ErrorHandler\Model\NotFoundType;
|
||||
use Shlinkio\Shlink\Core\Util\RedirectResponseHelperInterface;
|
||||
|
||||
class NotFoundRedirectResolver implements NotFoundRedirectResolverInterface
|
||||
{
|
||||
public function __construct(private RedirectResponseHelperInterface $redirectResponseHelper)
|
||||
{
|
||||
}
|
||||
|
||||
public function resolveRedirectResponse(
|
||||
NotFoundType $notFoundType,
|
||||
NotFoundRedirectConfigInterface $config
|
||||
): ?ResponseInterface {
|
||||
return match (true) {
|
||||
$notFoundType->isBaseUrl() && $config->hasBaseUrlRedirect() =>
|
||||
// @phpstan-ignore-next-line Create custom PHPStan rule
|
||||
$this->redirectResponseHelper->buildRedirectResponse($config->baseUrlRedirect()),
|
||||
$notFoundType->isRegularNotFound() && $config->hasRegular404Redirect() =>
|
||||
// @phpstan-ignore-next-line Create custom PHPStan rule
|
||||
$this->redirectResponseHelper->buildRedirectResponse($config->regular404Redirect()),
|
||||
$notFoundType->isInvalidShortUrl() && $config->hasInvalidShortUrlRedirect() =>
|
||||
// @phpstan-ignore-next-line Create custom PHPStan rule
|
||||
$this->redirectResponseHelper->buildRedirectResponse($config->invalidShortUrlRedirect()),
|
||||
default => null,
|
||||
};
|
||||
}
|
||||
}
|
16
module/Core/src/Config/NotFoundRedirectResolverInterface.php
Normal file
16
module/Core/src/Config/NotFoundRedirectResolverInterface.php
Normal file
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Config;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Shlinkio\Shlink\Core\ErrorHandler\Model\NotFoundType;
|
||||
|
||||
interface NotFoundRedirectResolverInterface
|
||||
{
|
||||
public function resolveRedirectResponse(
|
||||
NotFoundType $notFoundType,
|
||||
NotFoundRedirectConfigInterface $config
|
||||
): ?ResponseInterface;
|
||||
}
|
30
module/Core/src/Config/NotFoundRedirects.php
Normal file
30
module/Core/src/Config/NotFoundRedirects.php
Normal file
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Config;
|
||||
|
||||
final class NotFoundRedirects
|
||||
{
|
||||
public function __construct(
|
||||
private ?string $baseUrlRedirect = null,
|
||||
private ?string $regular404Redirect = null,
|
||||
private ?string $invalidShortUrlRedirect = null,
|
||||
) {
|
||||
}
|
||||
|
||||
public function baseUrlRedirect(): ?string
|
||||
{
|
||||
return $this->baseUrlRedirect;
|
||||
}
|
||||
|
||||
public function regular404Redirect(): ?string
|
||||
{
|
||||
return $this->regular404Redirect;
|
||||
}
|
||||
|
||||
public function invalidShortUrlRedirect(): ?string
|
||||
{
|
||||
return $this->invalidShortUrlRedirect;
|
||||
}
|
||||
}
|
|
@ -5,10 +5,12 @@ declare(strict_types=1);
|
|||
namespace Shlinkio\Shlink\Core\Domain;
|
||||
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Shlinkio\Shlink\Core\Config\NotFoundRedirects;
|
||||
use Shlinkio\Shlink\Core\Domain\Model\DomainItem;
|
||||
use Shlinkio\Shlink\Core\Domain\Repository\DomainRepositoryInterface;
|
||||
use Shlinkio\Shlink\Core\Entity\Domain;
|
||||
use Shlinkio\Shlink\Core\Exception\DomainNotFoundException;
|
||||
use Shlinkio\Shlink\Core\Options\NotFoundRedirectOptions;
|
||||
use Shlinkio\Shlink\Rest\ApiKey\Role;
|
||||
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
||||
|
||||
|
@ -16,8 +18,11 @@ use function Functional\map;
|
|||
|
||||
class DomainService implements DomainServiceInterface
|
||||
{
|
||||
public function __construct(private EntityManagerInterface $em, private string $defaultDomain)
|
||||
{
|
||||
public function __construct(
|
||||
private EntityManagerInterface $em,
|
||||
private string $defaultDomain,
|
||||
private NotFoundRedirectOptions $redirectOptions,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -28,14 +33,14 @@ class DomainService implements DomainServiceInterface
|
|||
/** @var DomainRepositoryInterface $repo */
|
||||
$repo = $this->em->getRepository(Domain::class);
|
||||
$domains = $repo->findDomainsWithout($this->defaultDomain, $apiKey);
|
||||
$mappedDomains = map($domains, fn (Domain $domain) => new DomainItem($domain->getAuthority(), false));
|
||||
$mappedDomains = map($domains, fn (Domain $domain) => DomainItem::forExistingDomain($domain));
|
||||
|
||||
if ($apiKey?->hasRole(Role::DOMAIN_SPECIFIC)) {
|
||||
return $mappedDomains;
|
||||
}
|
||||
|
||||
return [
|
||||
new DomainItem($this->defaultDomain, true),
|
||||
DomainItem::forDefaultDomain($this->defaultDomain, $this->redirectOptions),
|
||||
...$mappedDomains,
|
||||
];
|
||||
}
|
||||
|
@ -54,14 +59,29 @@ class DomainService implements DomainServiceInterface
|
|||
return $domain;
|
||||
}
|
||||
|
||||
public function getOrCreate(string $authority): Domain
|
||||
public function findByAuthority(string $authority): ?Domain
|
||||
{
|
||||
$repo = $this->em->getRepository(Domain::class);
|
||||
$domain = $repo->findOneBy(['authority' => $authority]) ?? new Domain($authority);
|
||||
return $repo->findOneBy(['authority' => $authority]);
|
||||
}
|
||||
|
||||
public function getOrCreate(string $authority): Domain
|
||||
{
|
||||
$domain = $this->findByAuthority($authority) ?? Domain::withAuthority($authority);
|
||||
|
||||
$this->em->persist($domain);
|
||||
$this->em->flush();
|
||||
|
||||
return $domain;
|
||||
}
|
||||
|
||||
public function configureNotFoundRedirects(string $authority, NotFoundRedirects $notFoundRedirects): Domain
|
||||
{
|
||||
$domain = $this->getOrCreate($authority);
|
||||
$domain->configureNotFoundRedirects($notFoundRedirects);
|
||||
|
||||
$this->em->flush();
|
||||
|
||||
return $domain;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace Shlinkio\Shlink\Core\Domain;
|
||||
|
||||
use Shlinkio\Shlink\Core\Config\NotFoundRedirects;
|
||||
use Shlinkio\Shlink\Core\Domain\Model\DomainItem;
|
||||
use Shlinkio\Shlink\Core\Entity\Domain;
|
||||
use Shlinkio\Shlink\Core\Exception\DomainNotFoundException;
|
||||
|
@ -22,4 +23,8 @@ interface DomainServiceInterface
|
|||
public function getDomain(string $domainId): Domain;
|
||||
|
||||
public function getOrCreate(string $authority): Domain;
|
||||
|
||||
public function findByAuthority(string $authority): ?Domain;
|
||||
|
||||
public function configureNotFoundRedirects(string $authority, NotFoundRedirects $notFoundRedirects): Domain;
|
||||
}
|
||||
|
|
|
@ -5,28 +5,48 @@ declare(strict_types=1);
|
|||
namespace Shlinkio\Shlink\Core\Domain\Model;
|
||||
|
||||
use JsonSerializable;
|
||||
use Shlinkio\Shlink\Core\Config\NotFoundRedirectConfigInterface;
|
||||
use Shlinkio\Shlink\Core\Entity\Domain;
|
||||
|
||||
final class DomainItem implements JsonSerializable
|
||||
{
|
||||
public function __construct(private string $domain, private bool $isDefault)
|
||||
private function __construct(
|
||||
private string $authority,
|
||||
private NotFoundRedirectConfigInterface $notFoundRedirectConfig,
|
||||
private bool $isDefault
|
||||
) {
|
||||
}
|
||||
|
||||
public static function forExistingDomain(Domain $domain): self
|
||||
{
|
||||
return new self($domain->getAuthority(), $domain, false);
|
||||
}
|
||||
|
||||
public static function forDefaultDomain(string $authority, NotFoundRedirectConfigInterface $config): self
|
||||
{
|
||||
return new self($authority, $config, true);
|
||||
}
|
||||
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
return [
|
||||
'domain' => $this->domain,
|
||||
'domain' => $this->authority,
|
||||
'isDefault' => $this->isDefault,
|
||||
];
|
||||
}
|
||||
|
||||
public function toString(): string
|
||||
{
|
||||
return $this->domain;
|
||||
return $this->authority;
|
||||
}
|
||||
|
||||
public function isDefault(): bool
|
||||
{
|
||||
return $this->isDefault;
|
||||
}
|
||||
|
||||
public function notFoundRedirectConfig(): NotFoundRedirectConfigInterface
|
||||
{
|
||||
return $this->notFoundRedirectConfig;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,8 +18,13 @@ class DomainRepository extends EntitySpecificationRepository implements DomainRe
|
|||
public function findDomainsWithout(?string $excludedAuthority, ?ApiKey $apiKey = null): array
|
||||
{
|
||||
$qb = $this->createQueryBuilder('d');
|
||||
$qb->join(ShortUrl::class, 's', Join::WITH, 's.domain = d')
|
||||
->orderBy('d.authority', 'ASC');
|
||||
$qb->leftJoin(ShortUrl::class, 's', Join::WITH, 's.domain = d')
|
||||
->orderBy('d.authority', 'ASC')
|
||||
->groupBy('d')
|
||||
->having($qb->expr()->gt('COUNT(s.id)', '0'))
|
||||
->orHaving($qb->expr()->isNotNull('d.baseUrlRedirect'))
|
||||
->orHaving($qb->expr()->isNotNull('d.regular404Redirect'))
|
||||
->orHaving($qb->expr()->isNotNull('d.invalidShortUrlRedirect'));
|
||||
|
||||
if ($excludedAuthority !== null) {
|
||||
$qb->where($qb->expr()->neq('d.authority', ':excludedAuthority'))
|
||||
|
|
|
@ -6,13 +6,24 @@ namespace Shlinkio\Shlink\Core\Entity;
|
|||
|
||||
use JsonSerializable;
|
||||
use Shlinkio\Shlink\Common\Entity\AbstractEntity;
|
||||
use Shlinkio\Shlink\Core\Config\NotFoundRedirectConfigInterface;
|
||||
use Shlinkio\Shlink\Core\Config\NotFoundRedirects;
|
||||
|
||||
class Domain extends AbstractEntity implements JsonSerializable
|
||||
class Domain extends AbstractEntity implements JsonSerializable, NotFoundRedirectConfigInterface
|
||||
{
|
||||
public function __construct(private string $authority)
|
||||
private ?string $baseUrlRedirect = null;
|
||||
private ?string $regular404Redirect = null;
|
||||
private ?string $invalidShortUrlRedirect = null;
|
||||
|
||||
private function __construct(private string $authority)
|
||||
{
|
||||
}
|
||||
|
||||
public static function withAuthority(string $authority): self
|
||||
{
|
||||
return new self($authority);
|
||||
}
|
||||
|
||||
public function getAuthority(): string
|
||||
{
|
||||
return $this->authority;
|
||||
|
@ -22,4 +33,41 @@ class Domain extends AbstractEntity implements JsonSerializable
|
|||
{
|
||||
return $this->getAuthority();
|
||||
}
|
||||
|
||||
public function invalidShortUrlRedirect(): ?string
|
||||
{
|
||||
return $this->invalidShortUrlRedirect;
|
||||
}
|
||||
|
||||
public function hasInvalidShortUrlRedirect(): bool
|
||||
{
|
||||
return $this->invalidShortUrlRedirect !== null;
|
||||
}
|
||||
|
||||
public function regular404Redirect(): ?string
|
||||
{
|
||||
return $this->regular404Redirect;
|
||||
}
|
||||
|
||||
public function hasRegular404Redirect(): bool
|
||||
{
|
||||
return $this->regular404Redirect !== null;
|
||||
}
|
||||
|
||||
public function baseUrlRedirect(): ?string
|
||||
{
|
||||
return $this->baseUrlRedirect;
|
||||
}
|
||||
|
||||
public function hasBaseUrlRedirect(): bool
|
||||
{
|
||||
return $this->baseUrlRedirect !== null;
|
||||
}
|
||||
|
||||
public function configureNotFoundRedirects(NotFoundRedirects $redirects): void
|
||||
{
|
||||
$this->baseUrlRedirect = $redirects->baseUrlRedirect();
|
||||
$this->regular404Redirect = $redirects->regular404Redirect();
|
||||
$this->invalidShortUrlRedirect = $redirects->invalidShortUrlRedirect();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,15 +8,17 @@ use Psr\Http\Message\ResponseInterface;
|
|||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\MiddlewareInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use Shlinkio\Shlink\Core\Config\NotFoundRedirectResolverInterface;
|
||||
use Shlinkio\Shlink\Core\Domain\DomainServiceInterface;
|
||||
use Shlinkio\Shlink\Core\ErrorHandler\Model\NotFoundType;
|
||||
use Shlinkio\Shlink\Core\Options;
|
||||
use Shlinkio\Shlink\Core\Util\RedirectResponseHelperInterface;
|
||||
|
||||
class NotFoundRedirectHandler implements MiddlewareInterface
|
||||
{
|
||||
public function __construct(
|
||||
private Options\NotFoundRedirectOptions $redirectOptions,
|
||||
private RedirectResponseHelperInterface $redirectResponseHelper
|
||||
private NotFoundRedirectResolverInterface $redirectResolver,
|
||||
private DomainServiceInterface $domainService,
|
||||
) {
|
||||
}
|
||||
|
||||
|
@ -24,26 +26,17 @@ class NotFoundRedirectHandler implements MiddlewareInterface
|
|||
{
|
||||
/** @var NotFoundType $notFoundType */
|
||||
$notFoundType = $request->getAttribute(NotFoundType::class);
|
||||
$authority = $request->getUri()->getAuthority();
|
||||
$domainSpecificRedirect = $this->resolveDomainSpecificRedirect($authority, $notFoundType);
|
||||
|
||||
if ($notFoundType->isBaseUrl() && $this->redirectOptions->hasBaseUrlRedirect()) {
|
||||
// @phpstan-ignore-next-line Create custom PHPStan rule
|
||||
return $this->redirectResponseHelper->buildRedirectResponse($this->redirectOptions->getBaseUrlRedirect());
|
||||
}
|
||||
return $domainSpecificRedirect
|
||||
?? $this->redirectResolver->resolveRedirectResponse($notFoundType, $this->redirectOptions)
|
||||
?? $handler->handle($request);
|
||||
}
|
||||
|
||||
if ($notFoundType->isRegularNotFound() && $this->redirectOptions->hasRegular404Redirect()) {
|
||||
return $this->redirectResponseHelper->buildRedirectResponse(
|
||||
// @phpstan-ignore-next-line Create custom PHPStan rule
|
||||
$this->redirectOptions->getRegular404Redirect(),
|
||||
);
|
||||
}
|
||||
|
||||
if ($notFoundType->isInvalidShortUrl() && $this->redirectOptions->hasInvalidShortUrlRedirect()) {
|
||||
return $this->redirectResponseHelper->buildRedirectResponse(
|
||||
// @phpstan-ignore-next-line Create custom PHPStan rule
|
||||
$this->redirectOptions->getInvalidShortUrlRedirect(),
|
||||
);
|
||||
}
|
||||
|
||||
return $handler->handle($request);
|
||||
private function resolveDomainSpecificRedirect(string $authority, NotFoundType $notFoundType): ?ResponseInterface
|
||||
{
|
||||
$domain = $this->domainService->findByAuthority($authority);
|
||||
return $domain === null ? null : $this->redirectResolver->resolveRedirectResponse($notFoundType, $domain);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,14 +5,15 @@ declare(strict_types=1);
|
|||
namespace Shlinkio\Shlink\Core\Options;
|
||||
|
||||
use Laminas\Stdlib\AbstractOptions;
|
||||
use Shlinkio\Shlink\Core\Config\NotFoundRedirectConfigInterface;
|
||||
|
||||
class NotFoundRedirectOptions extends AbstractOptions
|
||||
class NotFoundRedirectOptions extends AbstractOptions implements NotFoundRedirectConfigInterface
|
||||
{
|
||||
private ?string $invalidShortUrl = null;
|
||||
private ?string $regular404 = null;
|
||||
private ?string $baseUrl = null;
|
||||
|
||||
public function getInvalidShortUrlRedirect(): ?string
|
||||
public function invalidShortUrlRedirect(): ?string
|
||||
{
|
||||
return $this->invalidShortUrl;
|
||||
}
|
||||
|
@ -28,7 +29,7 @@ class NotFoundRedirectOptions extends AbstractOptions
|
|||
return $this;
|
||||
}
|
||||
|
||||
public function getRegular404Redirect(): ?string
|
||||
public function regular404Redirect(): ?string
|
||||
{
|
||||
return $this->regular404;
|
||||
}
|
||||
|
@ -44,7 +45,7 @@ class NotFoundRedirectOptions extends AbstractOptions
|
|||
return $this;
|
||||
}
|
||||
|
||||
public function getBaseUrlRedirect(): ?string
|
||||
public function baseUrlRedirect(): ?string
|
||||
{
|
||||
return $this->baseUrl;
|
||||
}
|
||||
|
|
|
@ -41,7 +41,9 @@ class PersistenceShortUrlRelationResolver implements ShortUrlRelationResolverInt
|
|||
|
||||
private function memoizeNewDomain(string $domain): Domain
|
||||
{
|
||||
return $this->memoizedNewDomains[$domain] = $this->memoizedNewDomains[$domain] ?? new Domain($domain);
|
||||
return $this->memoizedNewDomains[$domain] = $this->memoizedNewDomains[$domain] ?? Domain::withAuthority(
|
||||
$domain,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -15,7 +15,7 @@ class SimpleShortUrlRelationResolver implements ShortUrlRelationResolverInterfac
|
|||
{
|
||||
public function resolveDomain(?string $domain): ?Domain
|
||||
{
|
||||
return $domain !== null ? new Domain($domain) : null;
|
||||
return $domain !== null ? Domain::withAuthority($domain) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -6,6 +6,7 @@ namespace ShlinkioTest\Shlink\Core\Domain\Repository;
|
|||
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Shlinkio\Shlink\Core\Config\NotFoundRedirects;
|
||||
use Shlinkio\Shlink\Core\Domain\Repository\DomainRepository;
|
||||
use Shlinkio\Shlink\Core\Entity\Domain;
|
||||
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
||||
|
@ -28,27 +29,47 @@ class DomainRepositoryTest extends DatabaseTestCase
|
|||
/** @test */
|
||||
public function findDomainsReturnsExpectedResult(): void
|
||||
{
|
||||
$fooDomain = new Domain('foo.com');
|
||||
$fooDomain = Domain::withAuthority('foo.com');
|
||||
$this->getEntityManager()->persist($fooDomain);
|
||||
$this->getEntityManager()->persist($this->createShortUrl($fooDomain));
|
||||
|
||||
$barDomain = new Domain('bar.com');
|
||||
$barDomain = Domain::withAuthority('bar.com');
|
||||
$this->getEntityManager()->persist($barDomain);
|
||||
$this->getEntityManager()->persist($this->createShortUrl($barDomain));
|
||||
|
||||
$bazDomain = new Domain('baz.com');
|
||||
$bazDomain = Domain::withAuthority('baz.com');
|
||||
$this->getEntityManager()->persist($bazDomain);
|
||||
$this->getEntityManager()->persist($this->createShortUrl($bazDomain));
|
||||
|
||||
$detachedDomain = new Domain('detached.com');
|
||||
$detachedDomain = Domain::withAuthority('detached.com');
|
||||
$this->getEntityManager()->persist($detachedDomain);
|
||||
|
||||
$detachedWithRedirects = Domain::withAuthority('detached-with-redirects.com');
|
||||
$detachedWithRedirects->configureNotFoundRedirects(new NotFoundRedirects('foo.com', 'bar.com'));
|
||||
$this->getEntityManager()->persist($detachedWithRedirects);
|
||||
|
||||
$this->getEntityManager()->flush();
|
||||
|
||||
self::assertEquals([$barDomain, $bazDomain, $fooDomain], $this->repo->findDomainsWithout(null));
|
||||
self::assertEquals([$barDomain, $bazDomain], $this->repo->findDomainsWithout('foo.com'));
|
||||
self::assertEquals([$bazDomain, $fooDomain], $this->repo->findDomainsWithout('bar.com'));
|
||||
self::assertEquals([$barDomain, $fooDomain], $this->repo->findDomainsWithout('baz.com'));
|
||||
self::assertEquals(
|
||||
[$barDomain, $bazDomain, $detachedWithRedirects, $fooDomain],
|
||||
$this->repo->findDomainsWithout(null),
|
||||
);
|
||||
self::assertEquals(
|
||||
[$barDomain, $bazDomain, $detachedWithRedirects],
|
||||
$this->repo->findDomainsWithout('foo.com'),
|
||||
);
|
||||
self::assertEquals(
|
||||
[$bazDomain, $detachedWithRedirects, $fooDomain],
|
||||
$this->repo->findDomainsWithout('bar.com'),
|
||||
);
|
||||
self::assertEquals(
|
||||
[$barDomain, $detachedWithRedirects, $fooDomain],
|
||||
$this->repo->findDomainsWithout('baz.com'),
|
||||
);
|
||||
self::assertEquals(
|
||||
[$barDomain, $bazDomain, $fooDomain],
|
||||
$this->repo->findDomainsWithout('detached-with-redirects.com'),
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
|
@ -59,18 +80,25 @@ class DomainRepositoryTest extends DatabaseTestCase
|
|||
$authorAndDomainApiKey = ApiKey::fromMeta(ApiKeyMeta::withRoles(RoleDefinition::forAuthoredShortUrls()));
|
||||
$this->getEntityManager()->persist($authorAndDomainApiKey);
|
||||
|
||||
$fooDomain = new Domain('foo.com');
|
||||
$fooDomain = Domain::withAuthority('foo.com');
|
||||
$this->getEntityManager()->persist($fooDomain);
|
||||
$this->getEntityManager()->persist($this->createShortUrl($fooDomain, $authorApiKey));
|
||||
|
||||
$barDomain = new Domain('bar.com');
|
||||
$barDomain = Domain::withAuthority('bar.com');
|
||||
$this->getEntityManager()->persist($barDomain);
|
||||
$this->getEntityManager()->persist($this->createShortUrl($barDomain, $authorAndDomainApiKey));
|
||||
|
||||
$bazDomain = new Domain('baz.com');
|
||||
$bazDomain = Domain::withAuthority('baz.com');
|
||||
$this->getEntityManager()->persist($bazDomain);
|
||||
$this->getEntityManager()->persist($this->createShortUrl($bazDomain, $authorApiKey));
|
||||
|
||||
// $detachedDomain = Domain::withAuthority('detached.com');
|
||||
// $this->getEntityManager()->persist($detachedDomain);
|
||||
//
|
||||
// $detachedWithRedirects = Domain::withAuthority('detached-with-redirects.com');
|
||||
// $detachedWithRedirects->configureNotFoundRedirects(new NotFoundRedirects('foo.com', 'bar.com'));
|
||||
// $this->getEntityManager()->persist($detachedWithRedirects);
|
||||
|
||||
$this->getEntityManager()->flush();
|
||||
|
||||
$authorAndDomainApiKey->registerRole(RoleDefinition::forDomain($fooDomain));
|
||||
|
@ -79,12 +107,21 @@ class DomainRepositoryTest extends DatabaseTestCase
|
|||
$this->getEntityManager()->persist($fooDomainApiKey);
|
||||
|
||||
$barDomainApiKey = ApiKey::fromMeta(ApiKeyMeta::withRoles(RoleDefinition::forDomain($barDomain)));
|
||||
$this->getEntityManager()->persist($fooDomainApiKey);
|
||||
$this->getEntityManager()->persist($barDomainApiKey);
|
||||
|
||||
// $detachedWithRedirectsApiKey = ApiKey::fromMeta(
|
||||
// ApiKeyMeta::withRoles(RoleDefinition::forDomain($detachedWithRedirects)),
|
||||
// );
|
||||
// $this->getEntityManager()->persist($detachedWithRedirectsApiKey);
|
||||
|
||||
$this->getEntityManager()->flush();
|
||||
|
||||
self::assertEquals([$fooDomain], $this->repo->findDomainsWithout(null, $fooDomainApiKey));
|
||||
self::assertEquals([$barDomain], $this->repo->findDomainsWithout(null, $barDomainApiKey));
|
||||
// self::assertEquals(
|
||||
// [$detachedWithRedirects],
|
||||
// $this->repo->findDomainsWithout(null, $detachedWithRedirectsApiKey),
|
||||
// );
|
||||
self::assertEquals([$bazDomain, $fooDomain], $this->repo->findDomainsWithout(null, $authorApiKey));
|
||||
self::assertEquals([], $this->repo->findDomainsWithout(null, $authorAndDomainApiKey));
|
||||
}
|
||||
|
|
|
@ -340,9 +340,9 @@ class ShortUrlRepositoryTest extends DatabaseTestCase
|
|||
{
|
||||
$start = Chronos::parse('2020-03-05 20:18:30');
|
||||
|
||||
$wrongDomain = new Domain('wrong.com');
|
||||
$wrongDomain = Domain::withAuthority('wrong.com');
|
||||
$this->getEntityManager()->persist($wrongDomain);
|
||||
$rightDomain = new Domain('right.com');
|
||||
$rightDomain = Domain::withAuthority('right.com');
|
||||
$this->getEntityManager()->persist($rightDomain);
|
||||
|
||||
$this->getEntityManager()->flush();
|
||||
|
|
|
@ -97,7 +97,7 @@ class TagRepositoryTest extends DatabaseTestCase
|
|||
/** @test */
|
||||
public function tagExistsReturnsExpectedResultBasedOnApiKey(): void
|
||||
{
|
||||
$domain = new Domain('foo.com');
|
||||
$domain = Domain::withAuthority('foo.com');
|
||||
$this->getEntityManager()->persist($domain);
|
||||
$this->getEntityManager()->flush();
|
||||
|
||||
|
|
|
@ -222,7 +222,7 @@ class VisitRepositoryTest extends DatabaseTestCase
|
|||
/** @test */
|
||||
public function countVisitsReturnsExpectedResultBasedOnApiKey(): void
|
||||
{
|
||||
$domain = new Domain('foo.com');
|
||||
$domain = Domain::withAuthority('foo.com');
|
||||
$this->getEntityManager()->persist($domain);
|
||||
|
||||
$this->getEntityManager()->flush();
|
||||
|
|
114
module/Core/test/Config/NotFoundRedirectResolverTest.php
Normal file
114
module/Core/test/Config/NotFoundRedirectResolverTest.php
Normal file
|
@ -0,0 +1,114 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioTest\Shlink\Core\Config;
|
||||
|
||||
use Laminas\Diactoros\Response;
|
||||
use Laminas\Diactoros\ServerRequestFactory;
|
||||
use Laminas\Diactoros\Uri;
|
||||
use Mezzio\Router\Route;
|
||||
use Mezzio\Router\RouteResult;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Prophecy\Argument;
|
||||
use Prophecy\PhpUnit\ProphecyTrait;
|
||||
use Prophecy\Prophecy\ObjectProphecy;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\MiddlewareInterface;
|
||||
use Shlinkio\Shlink\Core\Action\RedirectAction;
|
||||
use Shlinkio\Shlink\Core\Config\NotFoundRedirectConfigInterface;
|
||||
use Shlinkio\Shlink\Core\Config\NotFoundRedirectResolver;
|
||||
use Shlinkio\Shlink\Core\ErrorHandler\Model\NotFoundType;
|
||||
use Shlinkio\Shlink\Core\Options\NotFoundRedirectOptions;
|
||||
use Shlinkio\Shlink\Core\Util\RedirectResponseHelperInterface;
|
||||
|
||||
class NotFoundRedirectResolverTest extends TestCase
|
||||
{
|
||||
use ProphecyTrait;
|
||||
|
||||
private NotFoundRedirectResolver $resolver;
|
||||
private ObjectProphecy $helper;
|
||||
private NotFoundRedirectConfigInterface $config;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->helper = $this->prophesize(RedirectResponseHelperInterface::class);
|
||||
$this->resolver = new NotFoundRedirectResolver($this->helper->reveal());
|
||||
|
||||
$this->config = new NotFoundRedirectOptions([
|
||||
'invalidShortUrl' => 'invalidShortUrl',
|
||||
'regular404' => 'regular404',
|
||||
'baseUrl' => 'baseUrl',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideRedirects
|
||||
*/
|
||||
public function expectedRedirectionIsReturnedDependingOnTheCase(
|
||||
NotFoundType $notFoundType,
|
||||
string $expectedRedirectTo,
|
||||
): void {
|
||||
$expectedResp = new Response();
|
||||
$buildResp = $this->helper->buildRedirectResponse($expectedRedirectTo)->willReturn($expectedResp);
|
||||
|
||||
$resp = $this->resolver->resolveRedirectResponse($notFoundType, $this->config);
|
||||
|
||||
self::assertSame($expectedResp, $resp);
|
||||
$buildResp->shouldHaveBeenCalledOnce();
|
||||
}
|
||||
|
||||
public function provideRedirects(): iterable
|
||||
{
|
||||
yield 'base URL with trailing slash' => [
|
||||
$this->notFoundType(ServerRequestFactory::fromGlobals()->withUri(new Uri('/'))),
|
||||
'baseUrl',
|
||||
];
|
||||
yield 'base URL without trailing slash' => [
|
||||
$this->notFoundType(ServerRequestFactory::fromGlobals()->withUri(new Uri(''))),
|
||||
'baseUrl',
|
||||
];
|
||||
yield 'regular 404' => [
|
||||
$this->notFoundType(ServerRequestFactory::fromGlobals()->withUri(new Uri('/foo/bar'))),
|
||||
'regular404',
|
||||
];
|
||||
yield 'invalid short URL' => [
|
||||
$this->notFoundType($this->requestForRoute(RedirectAction::class)),
|
||||
'invalidShortUrl',
|
||||
];
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function noResponseIsReturnedIfNoConditionsMatch(): void
|
||||
{
|
||||
$notFoundType = $this->notFoundType($this->requestForRoute('foo'));
|
||||
|
||||
$result = $this->resolver->resolveRedirectResponse($notFoundType, $this->config);
|
||||
|
||||
self::assertNull($result);
|
||||
$this->helper->buildRedirectResponse(Argument::cetera())->shouldNotHaveBeenCalled();
|
||||
}
|
||||
|
||||
private function notFoundType(ServerRequestInterface $req): NotFoundType
|
||||
{
|
||||
return NotFoundType::fromRequest($req, '');
|
||||
}
|
||||
|
||||
private function requestForRoute(string $routeName): ServerRequestInterface
|
||||
{
|
||||
return ServerRequestFactory::fromGlobals()
|
||||
->withAttribute(
|
||||
RouteResult::class,
|
||||
RouteResult::fromRoute(
|
||||
new Route(
|
||||
'',
|
||||
$this->prophesize(MiddlewareInterface::class)->reveal(),
|
||||
['GET'],
|
||||
$routeName,
|
||||
),
|
||||
),
|
||||
)
|
||||
->withUri(new Uri('/abc123'));
|
||||
}
|
||||
}
|
|
@ -9,11 +9,13 @@ use PHPUnit\Framework\TestCase;
|
|||
use Prophecy\Argument;
|
||||
use Prophecy\PhpUnit\ProphecyTrait;
|
||||
use Prophecy\Prophecy\ObjectProphecy;
|
||||
use Shlinkio\Shlink\Core\Config\NotFoundRedirects;
|
||||
use Shlinkio\Shlink\Core\Domain\DomainService;
|
||||
use Shlinkio\Shlink\Core\Domain\Model\DomainItem;
|
||||
use Shlinkio\Shlink\Core\Domain\Repository\DomainRepositoryInterface;
|
||||
use Shlinkio\Shlink\Core\Entity\Domain;
|
||||
use Shlinkio\Shlink\Core\Exception\DomainNotFoundException;
|
||||
use Shlinkio\Shlink\Core\Options\NotFoundRedirectOptions;
|
||||
use Shlinkio\Shlink\Rest\ApiKey\Model\ApiKeyMeta;
|
||||
use Shlinkio\Shlink\Rest\ApiKey\Model\RoleDefinition;
|
||||
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
||||
|
@ -28,7 +30,7 @@ class DomainServiceTest extends TestCase
|
|||
public function setUp(): void
|
||||
{
|
||||
$this->em = $this->prophesize(EntityManagerInterface::class);
|
||||
$this->domainService = new DomainService($this->em->reveal(), 'default.com');
|
||||
$this->domainService = new DomainService($this->em->reveal(), 'default.com', new NotFoundRedirectOptions());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -50,45 +52,56 @@ class DomainServiceTest extends TestCase
|
|||
|
||||
public function provideExcludedDomains(): iterable
|
||||
{
|
||||
$default = new DomainItem('default.com', true);
|
||||
$default = DomainItem::forDefaultDomain('default.com', new NotFoundRedirectOptions());
|
||||
$adminApiKey = ApiKey::create();
|
||||
$domainSpecificApiKey = ApiKey::fromMeta(
|
||||
ApiKeyMeta::withRoles(RoleDefinition::forDomain((new Domain(''))->setId('123'))),
|
||||
ApiKeyMeta::withRoles(RoleDefinition::forDomain(Domain::withAuthority('')->setId('123'))),
|
||||
);
|
||||
|
||||
yield 'empty list without API key' => [[], [$default], null];
|
||||
yield 'one item without API key' => [
|
||||
[new Domain('bar.com')],
|
||||
[$default, new DomainItem('bar.com', false)],
|
||||
[Domain::withAuthority('bar.com')],
|
||||
[$default, DomainItem::forExistingDomain(Domain::withAuthority('bar.com'))],
|
||||
null,
|
||||
];
|
||||
yield 'multiple items without API key' => [
|
||||
[new Domain('foo.com'), new Domain('bar.com')],
|
||||
[$default, new DomainItem('foo.com', false), new DomainItem('bar.com', false)],
|
||||
[Domain::withAuthority('foo.com'), Domain::withAuthority('bar.com')],
|
||||
[
|
||||
$default,
|
||||
DomainItem::forExistingDomain(Domain::withAuthority('foo.com')),
|
||||
DomainItem::forExistingDomain(Domain::withAuthority('bar.com')),
|
||||
],
|
||||
null,
|
||||
];
|
||||
|
||||
yield 'empty list with admin API key' => [[], [$default], $adminApiKey];
|
||||
yield 'one item with admin API key' => [
|
||||
[new Domain('bar.com')],
|
||||
[$default, new DomainItem('bar.com', false)],
|
||||
[Domain::withAuthority('bar.com')],
|
||||
[$default, DomainItem::forExistingDomain(Domain::withAuthority('bar.com'))],
|
||||
$adminApiKey,
|
||||
];
|
||||
yield 'multiple items with admin API key' => [
|
||||
[new Domain('foo.com'), new Domain('bar.com')],
|
||||
[$default, new DomainItem('foo.com', false), new DomainItem('bar.com', false)],
|
||||
[Domain::withAuthority('foo.com'), Domain::withAuthority('bar.com')],
|
||||
[
|
||||
$default,
|
||||
DomainItem::forExistingDomain(Domain::withAuthority('foo.com')),
|
||||
DomainItem::forExistingDomain(Domain::withAuthority('bar.com')),
|
||||
],
|
||||
$adminApiKey,
|
||||
];
|
||||
|
||||
yield 'empty list with domain-specific API key' => [[], [], $domainSpecificApiKey];
|
||||
yield 'one item with domain-specific API key' => [
|
||||
[new Domain('bar.com')],
|
||||
[new DomainItem('bar.com', false)],
|
||||
[Domain::withAuthority('bar.com')],
|
||||
[DomainItem::forExistingDomain(Domain::withAuthority('bar.com'))],
|
||||
$domainSpecificApiKey,
|
||||
];
|
||||
yield 'multiple items with domain-specific API key' => [
|
||||
[new Domain('foo.com'), new Domain('bar.com')],
|
||||
[new DomainItem('foo.com', false), new DomainItem('bar.com', false)],
|
||||
[Domain::withAuthority('foo.com'), Domain::withAuthority('bar.com')],
|
||||
[
|
||||
DomainItem::forExistingDomain(Domain::withAuthority('foo.com')),
|
||||
DomainItem::forExistingDomain(Domain::withAuthority('bar.com')),
|
||||
],
|
||||
$domainSpecificApiKey,
|
||||
];
|
||||
}
|
||||
|
@ -107,7 +120,7 @@ class DomainServiceTest extends TestCase
|
|||
/** @test */
|
||||
public function getDomainReturnsEntityWhenFound(): void
|
||||
{
|
||||
$domain = new Domain('');
|
||||
$domain = Domain::withAuthority('');
|
||||
$find = $this->em->find(Domain::class, '123')->willReturn($domain);
|
||||
|
||||
$result = $this->domainService->getDomain('123');
|
||||
|
@ -139,9 +152,39 @@ class DomainServiceTest extends TestCase
|
|||
$flush->shouldHaveBeenCalledOnce();
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideFoundDomains
|
||||
*/
|
||||
public function configureNotFoundRedirectsConfiguresFetchedDomain(?Domain $foundDomain): void
|
||||
{
|
||||
$authority = 'example.com';
|
||||
$repo = $this->prophesize(DomainRepositoryInterface::class);
|
||||
$repo->findOneBy(['authority' => $authority])->willReturn($foundDomain);
|
||||
$getRepo = $this->em->getRepository(Domain::class)->willReturn($repo->reveal());
|
||||
$persist = $this->em->persist($foundDomain ?? Argument::type(Domain::class));
|
||||
$flush = $this->em->flush();
|
||||
|
||||
$result = $this->domainService->configureNotFoundRedirects($authority, new NotFoundRedirects(
|
||||
'foo.com',
|
||||
'bar.com',
|
||||
'baz.com',
|
||||
));
|
||||
|
||||
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());
|
||||
$getRepo->shouldHaveBeenCalledOnce();
|
||||
$persist->shouldHaveBeenCalledOnce();
|
||||
$flush->shouldHaveBeenCalledTimes(2);
|
||||
}
|
||||
|
||||
public function provideFoundDomains(): iterable
|
||||
{
|
||||
yield 'domain not found' => [null];
|
||||
yield 'domain found' => [new Domain('')];
|
||||
yield 'domain found' => [Domain::withAuthority('')];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,21 +6,18 @@ namespace ShlinkioTest\Shlink\Core\ErrorHandler;
|
|||
|
||||
use Laminas\Diactoros\Response;
|
||||
use Laminas\Diactoros\ServerRequestFactory;
|
||||
use Laminas\Diactoros\Uri;
|
||||
use Mezzio\Router\Route;
|
||||
use Mezzio\Router\RouteResult;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Prophecy\Argument;
|
||||
use Prophecy\PhpUnit\ProphecyTrait;
|
||||
use Prophecy\Prophecy\ObjectProphecy;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\MiddlewareInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use Shlinkio\Shlink\Core\Action\RedirectAction;
|
||||
use Shlinkio\Shlink\Core\Config\NotFoundRedirectResolverInterface;
|
||||
use Shlinkio\Shlink\Core\Domain\DomainServiceInterface;
|
||||
use Shlinkio\Shlink\Core\Entity\Domain;
|
||||
use Shlinkio\Shlink\Core\ErrorHandler\Model\NotFoundType;
|
||||
use Shlinkio\Shlink\Core\ErrorHandler\NotFoundRedirectHandler;
|
||||
use Shlinkio\Shlink\Core\Options\NotFoundRedirectOptions;
|
||||
use Shlinkio\Shlink\Core\Util\RedirectResponseHelperInterface;
|
||||
|
||||
class NotFoundRedirectHandlerTest extends TestCase
|
||||
{
|
||||
|
@ -28,93 +25,103 @@ class NotFoundRedirectHandlerTest extends TestCase
|
|||
|
||||
private NotFoundRedirectHandler $middleware;
|
||||
private NotFoundRedirectOptions $redirectOptions;
|
||||
private ObjectProphecy $helper;
|
||||
private ObjectProphecy $resolver;
|
||||
private ObjectProphecy $domainService;
|
||||
private ObjectProphecy $next;
|
||||
private ServerRequestInterface $req;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->redirectOptions = new NotFoundRedirectOptions();
|
||||
$this->helper = $this->prophesize(RedirectResponseHelperInterface::class);
|
||||
$this->middleware = new NotFoundRedirectHandler($this->redirectOptions, $this->helper->reveal());
|
||||
$this->resolver = $this->prophesize(NotFoundRedirectResolverInterface::class);
|
||||
$this->domainService = $this->prophesize(DomainServiceInterface::class);
|
||||
|
||||
$this->middleware = new NotFoundRedirectHandler(
|
||||
$this->redirectOptions,
|
||||
$this->resolver->reveal(),
|
||||
$this->domainService->reveal(),
|
||||
);
|
||||
|
||||
$this->next = $this->prophesize(RequestHandlerInterface::class);
|
||||
$this->req = ServerRequestFactory::fromGlobals()->withAttribute(
|
||||
NotFoundType::class,
|
||||
$this->prophesize(NotFoundType::class)->reveal(),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @dataProvider provideRedirects
|
||||
* @dataProvider provideNonRedirectScenarios
|
||||
*/
|
||||
public function expectedRedirectionIsReturnedDependingOnTheCase(
|
||||
ServerRequestInterface $request,
|
||||
string $expectedRedirectTo,
|
||||
): void {
|
||||
$this->redirectOptions->invalidShortUrl = 'invalidShortUrl';
|
||||
$this->redirectOptions->regular404 = 'regular404';
|
||||
$this->redirectOptions->baseUrl = 'baseUrl';
|
||||
|
||||
public function nextIsCalledWhenNoRedirectIsResolved(callable $setUp): void
|
||||
{
|
||||
$expectedResp = new Response();
|
||||
$buildResp = $this->helper->buildRedirectResponse($expectedRedirectTo)->willReturn($expectedResp);
|
||||
|
||||
$next = $this->prophesize(RequestHandlerInterface::class);
|
||||
$handle = $next->handle($request)->willReturn(new Response());
|
||||
$setUp($this->domainService, $this->resolver);
|
||||
$handle = $this->next->handle($this->req)->willReturn($expectedResp);
|
||||
|
||||
$resp = $this->middleware->process($request, $next->reveal());
|
||||
$result = $this->middleware->process($this->req, $this->next->reveal());
|
||||
|
||||
self::assertSame($expectedResp, $resp);
|
||||
$buildResp->shouldHaveBeenCalledOnce();
|
||||
$handle->shouldNotHaveBeenCalled();
|
||||
}
|
||||
|
||||
public function provideRedirects(): iterable
|
||||
{
|
||||
yield 'base URL with trailing slash' => [
|
||||
$this->withNotFoundType(ServerRequestFactory::fromGlobals()->withUri(new Uri('/'))),
|
||||
'baseUrl',
|
||||
];
|
||||
yield 'base URL without trailing slash' => [
|
||||
$this->withNotFoundType(ServerRequestFactory::fromGlobals()->withUri(new Uri(''))),
|
||||
'baseUrl',
|
||||
];
|
||||
yield 'regular 404' => [
|
||||
$this->withNotFoundType(ServerRequestFactory::fromGlobals()->withUri(new Uri('/foo/bar'))),
|
||||
'regular404',
|
||||
];
|
||||
yield 'invalid short URL' => [
|
||||
$this->withNotFoundType(ServerRequestFactory::fromGlobals()
|
||||
->withAttribute(
|
||||
RouteResult::class,
|
||||
RouteResult::fromRoute(
|
||||
new Route(
|
||||
'',
|
||||
$this->prophesize(MiddlewareInterface::class)->reveal(),
|
||||
['GET'],
|
||||
RedirectAction::class,
|
||||
),
|
||||
),
|
||||
)
|
||||
->withUri(new Uri('/abc123'))),
|
||||
'invalidShortUrl',
|
||||
];
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function nextMiddlewareIsInvokedWhenNotRedirectNeedsToOccur(): void
|
||||
{
|
||||
$req = $this->withNotFoundType(ServerRequestFactory::fromGlobals());
|
||||
$resp = new Response();
|
||||
|
||||
$buildResp = $this->helper->buildRedirectResponse(Argument::cetera());
|
||||
|
||||
$next = $this->prophesize(RequestHandlerInterface::class);
|
||||
$handle = $next->handle($req)->willReturn($resp);
|
||||
|
||||
$result = $this->middleware->process($req, $next->reveal());
|
||||
|
||||
self::assertSame($resp, $result);
|
||||
$buildResp->shouldNotHaveBeenCalled();
|
||||
self::assertSame($expectedResp, $result);
|
||||
$handle->shouldHaveBeenCalledOnce();
|
||||
}
|
||||
|
||||
private function withNotFoundType(ServerRequestInterface $req): ServerRequestInterface
|
||||
public function provideNonRedirectScenarios(): iterable
|
||||
{
|
||||
$type = NotFoundType::fromRequest($req, '');
|
||||
return $req->withAttribute(NotFoundType::class, $type);
|
||||
yield 'no domain' => [function (ObjectProphecy $domainService, ObjectProphecy $resolver): void {
|
||||
$domainService->findByAuthority(Argument::cetera())
|
||||
->willReturn(null)
|
||||
->shouldBeCalledOnce();
|
||||
$resolver->resolveRedirectResponse(Argument::cetera())
|
||||
->willReturn(null)
|
||||
->shouldBeCalledOnce();
|
||||
}];
|
||||
yield 'non-redirecting domain' => [function (ObjectProphecy $domainService, ObjectProphecy $resolver): void {
|
||||
$domainService->findByAuthority(Argument::cetera())
|
||||
->willReturn(Domain::withAuthority(''))
|
||||
->shouldBeCalledOnce();
|
||||
$resolver->resolveRedirectResponse(Argument::cetera())
|
||||
->willReturn(null)
|
||||
->shouldBeCalledTimes(2);
|
||||
}];
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function globalRedirectIsUsedIfDomainRedirectIsNotFound(): void
|
||||
{
|
||||
$expectedResp = new Response();
|
||||
|
||||
$findDomain = $this->domainService->findByAuthority(Argument::cetera())->willReturn(null);
|
||||
$resolveRedirect = $this->resolver->resolveRedirectResponse(
|
||||
Argument::type(NotFoundType::class),
|
||||
$this->redirectOptions,
|
||||
)->willReturn($expectedResp);
|
||||
|
||||
$result = $this->middleware->process($this->req, $this->next->reveal());
|
||||
|
||||
self::assertSame($expectedResp, $result);
|
||||
$findDomain->shouldHaveBeenCalledOnce();
|
||||
$resolveRedirect->shouldHaveBeenCalledOnce();
|
||||
$this->next->handle(Argument::cetera())->shouldNotHaveBeenCalled();
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function domainRedirectIsUsedIfFound(): void
|
||||
{
|
||||
$expectedResp = new Response();
|
||||
$domain = Domain::withAuthority('');
|
||||
|
||||
$findDomain = $this->domainService->findByAuthority(Argument::cetera())->willReturn($domain);
|
||||
$resolveRedirect = $this->resolver->resolveRedirectResponse(
|
||||
Argument::type(NotFoundType::class),
|
||||
$domain,
|
||||
)->willReturn($expectedResp);
|
||||
|
||||
$result = $this->middleware->process($this->req, $this->next->reveal());
|
||||
|
||||
self::assertSame($expectedResp, $result);
|
||||
$findDomain->shouldHaveBeenCalledOnce();
|
||||
$resolveRedirect->shouldHaveBeenCalledOnce();
|
||||
$this->next->handle(Argument::cetera())->shouldNotHaveBeenCalled();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -60,7 +60,7 @@ class ShortCodeHelperTest extends TestCase
|
|||
public function provideDomains(): iterable
|
||||
{
|
||||
yield 'no domain' => [null, null];
|
||||
yield 'domain' => [new Domain($authority = 'doma.in'), $authority];
|
||||
yield 'domain' => [Domain::withAuthority($authority = 'doma.in'), $authority];
|
||||
}
|
||||
|
||||
/** @test */
|
||||
|
|
|
@ -68,7 +68,7 @@ class PersistenceShortUrlRelationResolverTest extends TestCase
|
|||
$authority = 'doma.in';
|
||||
|
||||
yield 'not found domain' => [null, $authority];
|
||||
yield 'found domain' => [new Domain($authority), $authority];
|
||||
yield 'found domain' => [Domain::withAuthority($authority), $authority];
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -32,6 +32,10 @@ class ListDomainsTest extends ApiTestCase
|
|||
'domain' => 'doma.in',
|
||||
'isDefault' => true,
|
||||
],
|
||||
[
|
||||
'domain' => 'detached-with-redirects.com',
|
||||
'isDefault' => false,
|
||||
],
|
||||
[
|
||||
'domain' => 'example.com',
|
||||
'isDefault' => false,
|
||||
|
|
|
@ -6,17 +6,23 @@ namespace ShlinkioApiTest\Shlink\Rest\Fixtures;
|
|||
|
||||
use Doctrine\Common\DataFixtures\AbstractFixture;
|
||||
use Doctrine\Persistence\ObjectManager;
|
||||
use Shlinkio\Shlink\Core\Config\NotFoundRedirects;
|
||||
use Shlinkio\Shlink\Core\Entity\Domain;
|
||||
|
||||
class DomainFixture extends AbstractFixture
|
||||
{
|
||||
public function load(ObjectManager $manager): void
|
||||
{
|
||||
$domain = new Domain('example.com');
|
||||
$domain = Domain::withAuthority('example.com');
|
||||
$manager->persist($domain);
|
||||
$this->addReference('example_domain', $domain);
|
||||
|
||||
$manager->persist(new Domain('this_domain_is_detached.com'));
|
||||
$manager->persist(Domain::withAuthority('this_domain_is_detached.com'));
|
||||
|
||||
$detachedWithRedirects = Domain::withAuthority('detached-with-redirects.com');
|
||||
$detachedWithRedirects->configureNotFoundRedirects(new NotFoundRedirects('foo.com', 'bar.com'));
|
||||
$manager->persist($detachedWithRedirects);
|
||||
|
||||
$manager->flush();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,6 +11,8 @@ use Prophecy\PhpUnit\ProphecyTrait;
|
|||
use Prophecy\Prophecy\ObjectProphecy;
|
||||
use Shlinkio\Shlink\Core\Domain\DomainServiceInterface;
|
||||
use Shlinkio\Shlink\Core\Domain\Model\DomainItem;
|
||||
use Shlinkio\Shlink\Core\Entity\Domain;
|
||||
use Shlinkio\Shlink\Core\Options\NotFoundRedirectOptions;
|
||||
use Shlinkio\Shlink\Rest\Action\Domain\ListDomainsAction;
|
||||
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
||||
|
||||
|
@ -32,8 +34,8 @@ class ListDomainsActionTest extends TestCase
|
|||
{
|
||||
$apiKey = ApiKey::create();
|
||||
$domains = [
|
||||
new DomainItem('bar.com', true),
|
||||
new DomainItem('baz.com', false),
|
||||
DomainItem::forDefaultDomain('bar.com', new NotFoundRedirectOptions()),
|
||||
DomainItem::forExistingDomain(Domain::withAuthority('baz.com')),
|
||||
];
|
||||
$listDomains = $this->domainService->listDomains($apiKey)->willReturn($domains);
|
||||
|
||||
|
|
|
@ -82,19 +82,23 @@ class OverrideDomainMiddlewareTest extends TestCase
|
|||
|
||||
public function provideBodies(): iterable
|
||||
{
|
||||
yield 'no domain provided' => [new Domain('foo.com'), [], [ShortUrlInputFilter::DOMAIN => 'foo.com']];
|
||||
yield 'no domain provided' => [
|
||||
Domain::withAuthority('foo.com'),
|
||||
[],
|
||||
[ShortUrlInputFilter::DOMAIN => 'foo.com'],
|
||||
];
|
||||
yield 'other domain provided' => [
|
||||
new Domain('bar.com'),
|
||||
Domain::withAuthority('bar.com'),
|
||||
[ShortUrlInputFilter::DOMAIN => 'foo.com'],
|
||||
[ShortUrlInputFilter::DOMAIN => 'bar.com'],
|
||||
];
|
||||
yield 'same domain provided' => [
|
||||
new Domain('baz.com'),
|
||||
Domain::withAuthority('baz.com'),
|
||||
[ShortUrlInputFilter::DOMAIN => 'baz.com'],
|
||||
[ShortUrlInputFilter::DOMAIN => 'baz.com'],
|
||||
];
|
||||
yield 'more body params' => [
|
||||
new Domain('doma.in'),
|
||||
Domain::withAuthority('doma.in'),
|
||||
[ShortUrlInputFilter::DOMAIN => 'baz.com', 'something' => 'else', 'foo' => 123],
|
||||
[ShortUrlInputFilter::DOMAIN => 'doma.in', 'something' => 'else', 'foo' => 123],
|
||||
];
|
||||
|
@ -106,7 +110,7 @@ class OverrideDomainMiddlewareTest extends TestCase
|
|||
*/
|
||||
public function setsRequestAttributeWhenMethodIsNotPost(string $method): void
|
||||
{
|
||||
$domain = new Domain('something.com');
|
||||
$domain = Domain::withAuthority('something.com');
|
||||
$request = $this->requestWithApiKey()->withMethod($method);
|
||||
$hasRole = $this->apiKey->hasRole(Role::DOMAIN_SPECIFIC)->willReturn(true);
|
||||
$getRoleMeta = $this->apiKey->getRoleMeta(Role::DOMAIN_SPECIFIC)->willReturn(['domain_id' => '123']);
|
||||
|
|
|
@ -55,7 +55,7 @@ class ApiKeyServiceTest extends TestCase
|
|||
yield 'no expiration date or name' => [null, null, []];
|
||||
yield 'expiration date' => [Chronos::parse('2030-01-01'), null, []];
|
||||
yield 'roles' => [null, null, [
|
||||
RoleDefinition::forDomain((new Domain(''))->setId('123')),
|
||||
RoleDefinition::forDomain(Domain::withAuthority('')->setId('123')),
|
||||
RoleDefinition::forAuthoredShortUrls(),
|
||||
]];
|
||||
yield 'single name' => [null, 'Alice', []];
|
||||
|
|
Loading…
Add table
Reference in a new issue