diff --git a/CHANGELOG.md b/CHANGELOG.md index f2491f84..4ede9184 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,29 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com), and this project adheres to [Semantic Versioning](https://semver.org). +## 2.2.1 - 2020-05-11 + +#### Added + +* *Nothing* + +#### Changed + +* *Nothing* + +#### Deprecated + +* *Nothing* + +#### Removed + +* *Nothing* + +#### Fixed + +* [#764](https://github.com/shlinkio/shlink/issues/764) Fixed error when trying to match an existing short URL which does not have `validSince` and/or `validUntil`, but you are providing either one of them for the new one. + + ## 2.2.0 - 2020-05-09 #### Added 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, + ]; + } }