diff --git a/module/Core/test/RedirectRule/ShortUrlRedirectionResolverTest.php b/module/Core/test/RedirectRule/ShortUrlRedirectionResolverTest.php index 03a5ce6c..2a71dfe2 100644 --- a/module/Core/test/RedirectRule/ShortUrlRedirectionResolverTest.php +++ b/module/Core/test/RedirectRule/ShortUrlRedirectionResolverTest.php @@ -2,7 +2,9 @@ namespace RedirectRule; +use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\EntityManagerInterface; +use Doctrine\ORM\EntityRepository; use Laminas\Diactoros\ServerRequestFactory; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Test; @@ -10,6 +12,8 @@ use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Psr\Http\Message\ServerRequestInterface; use Shlinkio\Shlink\Core\Model\DeviceType; +use Shlinkio\Shlink\Core\RedirectRule\Entity\RedirectCondition; +use Shlinkio\Shlink\Core\RedirectRule\Entity\ShortUrlRedirectRule; use Shlinkio\Shlink\Core\RedirectRule\ShortUrlRedirectionResolver; use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl; use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlCreation; @@ -30,8 +34,11 @@ class ShortUrlRedirectionResolverTest extends TestCase } #[Test, DataProvider('provideData')] - public function resolveLongUrlReturnsExpectedValue(ServerRequestInterface $request, string $expectedUrl): void - { + public function resolveLongUrlReturnsExpectedValue( + ServerRequestInterface $request, + ?RedirectCondition $condition, + string $expectedUrl, + ): void { $shortUrl = ShortUrl::create(ShortUrlCreation::fromRawData([ 'longUrl' => 'https://example.com/foo/bar', 'deviceLongUrls' => [ @@ -40,6 +47,16 @@ class ShortUrlRedirectionResolverTest extends TestCase ], ])); + $repo = $this->createMock(EntityRepository::class); + $repo->expects($this->once())->method('findBy')->willReturn($condition !== null ? [ + new ShortUrlRedirectRule($shortUrl, 1, 'https://example.com/from-rule', new ArrayCollection([ + $condition, + ])), + ] : []); + $this->em->expects($this->once())->method('getRepository')->with(ShortUrlRedirectRule::class)->willReturn( + $repo, + ); + $result = $this->resolver->resolveLongUrl($shortUrl, $request); self::assertEquals($expectedUrl, $result); @@ -52,9 +69,27 @@ class ShortUrlRedirectionResolverTest extends TestCase $userAgent, ); - yield 'unknown user agent' => [$request('Unknown'), 'https://example.com/foo/bar']; - yield 'desktop user agent' => [$request(DESKTOP_USER_AGENT), 'https://example.com/foo/bar']; - yield 'android user agent' => [$request(ANDROID_USER_AGENT), 'https://example.com/android']; - yield 'ios user agent' => [$request(IOS_USER_AGENT), 'https://example.com/ios']; + yield 'unknown user agent' => [ + $request('Unknown'), // This user agent won't match any device + RedirectCondition::forLanguage('es-ES'), // This condition won't match + 'https://example.com/foo/bar', + ]; + yield 'desktop user agent' => [$request(DESKTOP_USER_AGENT), null, 'https://example.com/foo/bar']; + yield 'android user agent' => [ + $request(ANDROID_USER_AGENT), + RedirectCondition::forQueryParam('foo', 'bar'), // This condition won't match + 'https://example.com/android', + ]; + yield 'ios user agent' => [$request(IOS_USER_AGENT), null, 'https://example.com/ios']; + yield 'matching language' => [ + $request()->withHeader('Accept-Language', 'es-ES'), + RedirectCondition::forLanguage('es-ES'), + 'https://example.com/from-rule', + ]; + yield 'matching query params' => [ + $request()->withQueryParams(['foo' => 'bar']), + RedirectCondition::forQueryParam('foo', 'bar'), + 'https://example.com/from-rule', + ]; } }