mirror of
https://github.com/shlinkio/shlink.git
synced 2024-11-24 18:00:17 -06:00
Ensured URL titles are trimmed to avoid error when persisted in database
This commit is contained in:
parent
30207ce0c2
commit
562763199a
@ -192,6 +192,12 @@
|
|||||||
},
|
},
|
||||||
"config": {
|
"config": {
|
||||||
"sort-packages": true,
|
"sort-packages": true,
|
||||||
"platform-check": false
|
"platform-check": false,
|
||||||
|
"allow-plugins": {
|
||||||
|
"composer/package-versions-deprecated": true,
|
||||||
|
"dealerdirect/phpcodesniffer-composer-installer": true,
|
||||||
|
"infection/extension-installer": true,
|
||||||
|
"veewee/composer-run-parallel": true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ namespace Shlinkio\Shlink\Core\Validation;
|
|||||||
|
|
||||||
use Cocur\Slugify\Slugify;
|
use Cocur\Slugify\Slugify;
|
||||||
use DateTime;
|
use DateTime;
|
||||||
|
use Laminas\Filter;
|
||||||
use Laminas\InputFilter\Input;
|
use Laminas\InputFilter\Input;
|
||||||
use Laminas\InputFilter\InputFilter;
|
use Laminas\InputFilter\InputFilter;
|
||||||
use Laminas\Validator;
|
use Laminas\Validator;
|
||||||
@ -13,6 +14,8 @@ use Shlinkio\Shlink\Common\Validation;
|
|||||||
use Shlinkio\Shlink\Core\Util\CocurSymfonySluggerBridge;
|
use Shlinkio\Shlink\Core\Util\CocurSymfonySluggerBridge;
|
||||||
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
||||||
|
|
||||||
|
use function substr;
|
||||||
|
|
||||||
use const Shlinkio\Shlink\CUSTOM_SLUGS_REGEXP;
|
use const Shlinkio\Shlink\CUSTOM_SLUGS_REGEXP;
|
||||||
use const Shlinkio\Shlink\MIN_SHORT_CODES_LENGTH;
|
use const Shlinkio\Shlink\MIN_SHORT_CODES_LENGTH;
|
||||||
|
|
||||||
@ -107,7 +110,11 @@ class ShortUrlInputFilter extends InputFilter
|
|||||||
|
|
||||||
$this->add($this->createTagsInput(self::TAGS, false));
|
$this->add($this->createTagsInput(self::TAGS, false));
|
||||||
|
|
||||||
$this->add($this->createInput(self::TITLE, false));
|
$title = $this->createInput(self::TITLE, false);
|
||||||
|
$title->getFilterChain()->attach(new Filter\Callback(
|
||||||
|
static fn (?string $value) => $value === null ? $value : substr($value, 0, 512),
|
||||||
|
));
|
||||||
|
$this->add($title);
|
||||||
|
|
||||||
$this->add($this->createBooleanInput(self::CRAWLABLE, false));
|
$this->add($this->createBooleanInput(self::CRAWLABLE, false));
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,10 @@ use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
|
|||||||
use Shlinkio\Shlink\Core\Validation\ShortUrlInputFilter;
|
use Shlinkio\Shlink\Core\Validation\ShortUrlInputFilter;
|
||||||
use stdClass;
|
use stdClass;
|
||||||
|
|
||||||
|
use function str_pad;
|
||||||
|
|
||||||
|
use const STR_PAD_BOTH;
|
||||||
|
|
||||||
class ShortUrlMetaTest extends TestCase
|
class ShortUrlMetaTest extends TestCase
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
@ -99,4 +103,28 @@ class ShortUrlMetaTest extends TestCase
|
|||||||
yield ['谷歌', '谷歌'];
|
yield ['谷歌', '谷歌'];
|
||||||
yield ['гугл', 'гугл'];
|
yield ['гугл', 'гугл'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @dataProvider provideTitles
|
||||||
|
*/
|
||||||
|
public function titleIsCroppedIfTooLong(?string $title, ?string $expectedTitle): void
|
||||||
|
{
|
||||||
|
$meta = ShortUrlMeta::fromRawData([
|
||||||
|
'title' => $title,
|
||||||
|
'longUrl' => '',
|
||||||
|
]);
|
||||||
|
|
||||||
|
self::assertEquals($expectedTitle, $meta->getTitle());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function provideTitles(): iterable
|
||||||
|
{
|
||||||
|
yield [null, null];
|
||||||
|
yield ['foo', 'foo'];
|
||||||
|
yield [str_pad('bar', 600, ' ', STR_PAD_BOTH), 'bar'];
|
||||||
|
yield [str_pad('', 512, 'a'), str_pad('', 512, 'a')];
|
||||||
|
yield [str_pad('', 600, 'b'), str_pad('', 512, 'b')];
|
||||||
|
yield [str_pad('', 800, 'b'), str_pad('', 512, 'b')];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user