mirror of
https://github.com/shlinkio/shlink.git
synced 2026-08-19 01:04:43 -05:00
Ensured dates parsing does not mask actual validation errors
This commit is contained in:
@@ -4,7 +4,6 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Shlinkio\Shlink\CLI\Command\ShortUrl;
|
namespace Shlinkio\Shlink\CLI\Command\ShortUrl;
|
||||||
|
|
||||||
use Cake\Chronos\Chronos;
|
|
||||||
use Shlinkio\Shlink\CLI\Util\ExitCodes;
|
use Shlinkio\Shlink\CLI\Util\ExitCodes;
|
||||||
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
|
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
|
||||||
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
|
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
|
||||||
@@ -127,8 +126,8 @@ class GenerateShortUrlCommand extends Command
|
|||||||
new Uri($longUrl),
|
new Uri($longUrl),
|
||||||
$tags,
|
$tags,
|
||||||
ShortUrlMeta::createFromParams(
|
ShortUrlMeta::createFromParams(
|
||||||
$this->getOptionalDate($input, 'validSince'),
|
$input->getOption('validSince'),
|
||||||
$this->getOptionalDate($input, 'validUntil'),
|
$input->getOption('validUntil'),
|
||||||
$customSlug,
|
$customSlug,
|
||||||
$maxVisits !== null ? (int) $maxVisits : null,
|
$maxVisits !== null ? (int) $maxVisits : null,
|
||||||
$input->getOption('findIfExists'),
|
$input->getOption('findIfExists'),
|
||||||
@@ -151,10 +150,4 @@ class GenerateShortUrlCommand extends Command
|
|||||||
return ExitCodes::EXIT_FAILURE;
|
return ExitCodes::EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getOptionalDate(InputInterface $input, string $fieldName): ?Chronos
|
|
||||||
{
|
|
||||||
$since = $input->getOption($fieldName);
|
|
||||||
return $since !== null ? Chronos::parse($since) : null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
|||||||
namespace Shlinkio\Shlink\Core\Model;
|
namespace Shlinkio\Shlink\Core\Model;
|
||||||
|
|
||||||
use Cake\Chronos\Chronos;
|
use Cake\Chronos\Chronos;
|
||||||
|
use DateTimeInterface;
|
||||||
use Shlinkio\Shlink\Core\Exception\ValidationException;
|
use Shlinkio\Shlink\Core\Exception\ValidationException;
|
||||||
use Shlinkio\Shlink\Core\Validation\ShortUrlMetaInputFilter;
|
use Shlinkio\Shlink\Core\Validation\ShortUrlMetaInputFilter;
|
||||||
|
|
||||||
@@ -96,7 +97,7 @@ final class ShortUrlMeta
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string|Chronos|null $date
|
* @param string|DateTimeInterface|Chronos|null $date
|
||||||
*/
|
*/
|
||||||
private function parseDateField($date): ?Chronos
|
private function parseDateField($date): ?Chronos
|
||||||
{
|
{
|
||||||
@@ -104,6 +105,10 @@ final class ShortUrlMeta
|
|||||||
return $date;
|
return $date;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($date instanceof DateTimeInterface) {
|
||||||
|
return Chronos::instance($date);
|
||||||
|
}
|
||||||
|
|
||||||
return Chronos::parse($date);
|
return Chronos::parse($date);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ use PHPUnit\Framework\TestCase;
|
|||||||
use Shlinkio\Shlink\Core\Exception\ValidationException;
|
use Shlinkio\Shlink\Core\Exception\ValidationException;
|
||||||
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
|
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
|
||||||
use Shlinkio\Shlink\Core\Validation\ShortUrlMetaInputFilter;
|
use Shlinkio\Shlink\Core\Validation\ShortUrlMetaInputFilter;
|
||||||
|
use stdClass;
|
||||||
|
|
||||||
class ShortUrlMetaTest extends TestCase
|
class ShortUrlMetaTest extends TestCase
|
||||||
{
|
{
|
||||||
@@ -35,6 +36,17 @@ class ShortUrlMetaTest extends TestCase
|
|||||||
ShortUrlMetaInputFilter::VALID_SINCE => '2017',
|
ShortUrlMetaInputFilter::VALID_SINCE => '2017',
|
||||||
ShortUrlMetaInputFilter::MAX_VISITS => 5,
|
ShortUrlMetaInputFilter::MAX_VISITS => 5,
|
||||||
]];
|
]];
|
||||||
|
yield [[
|
||||||
|
ShortUrlMetaInputFilter::VALID_SINCE => new stdClass(),
|
||||||
|
ShortUrlMetaInputFilter::VALID_UNTIL => 'foo',
|
||||||
|
]];
|
||||||
|
yield [[
|
||||||
|
ShortUrlMetaInputFilter::VALID_UNTIL => 500,
|
||||||
|
]];
|
||||||
|
yield [[
|
||||||
|
ShortUrlMetaInputFilter::MAX_VISITS => new stdClass(),
|
||||||
|
ShortUrlMetaInputFilter::DOMAIN => 4,
|
||||||
|
]];
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Shlinkio\Shlink\Rest\Action\ShortUrl;
|
namespace Shlinkio\Shlink\Rest\Action\ShortUrl;
|
||||||
|
|
||||||
use Cake\Chronos\Chronos;
|
|
||||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||||
use Shlinkio\Shlink\Core\Exception\InvalidArgumentException;
|
use Shlinkio\Shlink\Core\Exception\InvalidArgumentException;
|
||||||
use Shlinkio\Shlink\Core\Exception\ValidationException;
|
use Shlinkio\Shlink\Core\Exception\ValidationException;
|
||||||
@@ -32,8 +31,8 @@ class CreateShortUrlAction extends AbstractCreateShortUrlAction
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
$meta = ShortUrlMeta::createFromParams(
|
$meta = ShortUrlMeta::createFromParams(
|
||||||
$this->getOptionalDate($postData, 'validSince'),
|
$postData['validSince'] ?? null,
|
||||||
$this->getOptionalDate($postData, 'validUntil'),
|
$postData['validUntil'] ?? null,
|
||||||
$postData['customSlug'] ?? null,
|
$postData['customSlug'] ?? null,
|
||||||
$postData['maxVisits'] ?? null,
|
$postData['maxVisits'] ?? null,
|
||||||
$postData['findIfExists'] ?? null,
|
$postData['findIfExists'] ?? null,
|
||||||
@@ -45,9 +44,4 @@ class CreateShortUrlAction extends AbstractCreateShortUrlAction
|
|||||||
throw new InvalidArgumentException('Provided meta data is not valid', -1, $e);
|
throw new InvalidArgumentException('Provided meta data is not valid', -1, $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getOptionalDate(array $postData, string $fieldName): ?Chronos
|
|
||||||
{
|
|
||||||
return isset($postData[$fieldName]) ? Chronos::parse($postData[$fieldName]) : null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user