From 1fa989652471f905835c4063505a246cfec12b7e Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Mon, 11 May 2020 13:12:55 +0200 Subject: [PATCH] Fixed error when trying to match creteria on a Short URL with dates --- module/Core/src/Entity/ShortUrl.php | 4 +-- module/Core/test/Entity/ShortUrlTest.php | 35 ++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/module/Core/src/Entity/ShortUrl.php b/module/Core/src/Entity/ShortUrl.php index 5453d791..0f2811fb 100644 --- a/module/Core/src/Entity/ShortUrl.php +++ b/module/Core/src/Entity/ShortUrl.php @@ -204,10 +204,10 @@ class ShortUrl extends AbstractEntity if ($meta->hasDomain() && $meta->getDomain() !== $this->resolveDomain()) { return false; } - if ($meta->hasValidSince() && ! $meta->getValidSince()->eq($this->validSince)) { + if ($meta->hasValidSince() && ($this->validSince === null || ! $meta->getValidSince()->eq($this->validSince))) { return false; } - if ($meta->hasValidUntil() && ! $meta->getValidUntil()->eq($this->validUntil)) { + if ($meta->hasValidUntil() && ($this->validUntil === null || ! $meta->getValidUntil()->eq($this->validUntil))) { return false; } diff --git a/module/Core/test/Entity/ShortUrlTest.php b/module/Core/test/Entity/ShortUrlTest.php index e410dedb..07308580 100644 --- a/module/Core/test/Entity/ShortUrlTest.php +++ b/module/Core/test/Entity/ShortUrlTest.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace ShlinkioTest\Shlink\Core\Entity; +use Cake\Chronos\Chronos; use PHPUnit\Framework\TestCase; use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\Exception\ShortCodeCannotBeRegeneratedException; @@ -74,4 +75,38 @@ class ShortUrlTest extends TestCase yield [null, DEFAULT_SHORT_CODES_LENGTH]; yield from map(range(4, 10), fn (int $value) => [$value, $value]); } + + /** + * @test + * @dataProvider provideCriteriaToMatch + */ + public function criteriaIsMatchedWhenDatesMatch(ShortUrl $shortUrl, ShortUrlMeta $meta, bool $expected): void + { + $this->assertEquals($expected, $shortUrl->matchesCriteria($meta, [])); + } + + public function provideCriteriaToMatch(): iterable + { + $start = Chronos::parse('2020-03-05 20:18:30'); + $end = Chronos::parse('2021-03-05 20:18:30'); + + yield [new ShortUrl('foo'), ShortUrlMeta::fromRawData(['validSince' => $start]), false]; + yield [new ShortUrl('foo'), ShortUrlMeta::fromRawData(['validUntil' => $end]), false]; + yield [new ShortUrl('foo'), ShortUrlMeta::fromRawData(['validSince' => $start, 'validUntil' => $end]), false]; + yield [ + new ShortUrl('foo', ShortUrlMeta::fromRawData(['validSince' => $start])), + ShortUrlMeta::fromRawData(['validSince' => $start]), + true, + ]; + yield [ + new ShortUrl('foo', ShortUrlMeta::fromRawData(['validUntil' => $end])), + ShortUrlMeta::fromRawData(['validUntil' => $end]), + true, + ]; + yield [ + new ShortUrl('foo', ShortUrlMeta::fromRawData(['validUntil' => $end, 'validSince' => $start])), + ShortUrlMeta::fromRawData(['validUntil' => $end, 'validSince' => $start]), + true, + ]; + } }