2018-01-07 13:07:12 -06:00
|
|
|
<?php
|
2019-10-05 10:26:10 -05:00
|
|
|
|
2018-01-07 13:07:12 -06:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioTest\Shlink\Core\Model;
|
|
|
|
|
2018-09-29 05:52:32 -05:00
|
|
|
use Cake\Chronos\Chronos;
|
2018-01-07 13:07:12 -06:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Shlinkio\Shlink\Core\Exception\ValidationException;
|
|
|
|
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
|
2021-01-31 00:44:46 -06:00
|
|
|
use Shlinkio\Shlink\Core\Validation\ShortUrlInputFilter;
|
2019-12-06 15:38:22 -06:00
|
|
|
use stdClass;
|
2018-01-07 13:07:12 -06:00
|
|
|
|
|
|
|
class ShortUrlMetaTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @param array $data
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideInvalidData
|
|
|
|
*/
|
2019-02-17 13:28:34 -06:00
|
|
|
public function exceptionIsThrownIfProvidedDataIsInvalid(array $data): void
|
2018-01-07 13:07:12 -06:00
|
|
|
{
|
|
|
|
$this->expectException(ValidationException::class);
|
2020-01-26 01:42:51 -06:00
|
|
|
ShortUrlMeta::fromRawData($data);
|
2018-01-07 13:07:12 -06:00
|
|
|
}
|
|
|
|
|
2019-02-17 13:28:34 -06:00
|
|
|
public function provideInvalidData(): iterable
|
2018-01-07 13:07:12 -06:00
|
|
|
{
|
2019-02-17 13:28:34 -06:00
|
|
|
yield [[
|
2021-01-31 00:44:46 -06:00
|
|
|
ShortUrlInputFilter::VALID_SINCE => '',
|
|
|
|
ShortUrlInputFilter::VALID_UNTIL => '',
|
|
|
|
ShortUrlInputFilter::CUSTOM_SLUG => 'foobar',
|
|
|
|
ShortUrlInputFilter::MAX_VISITS => 'invalid',
|
2019-02-17 13:28:34 -06:00
|
|
|
]];
|
|
|
|
yield [[
|
2021-01-31 00:44:46 -06:00
|
|
|
ShortUrlInputFilter::VALID_SINCE => '2017',
|
|
|
|
ShortUrlInputFilter::MAX_VISITS => 5,
|
2019-02-17 13:28:34 -06:00
|
|
|
]];
|
2019-12-06 15:38:22 -06:00
|
|
|
yield [[
|
2021-01-31 00:44:46 -06:00
|
|
|
ShortUrlInputFilter::VALID_SINCE => new stdClass(),
|
|
|
|
ShortUrlInputFilter::VALID_UNTIL => 'foo',
|
2019-12-06 15:38:22 -06:00
|
|
|
]];
|
|
|
|
yield [[
|
2021-01-31 00:44:46 -06:00
|
|
|
ShortUrlInputFilter::VALID_UNTIL => 500,
|
|
|
|
ShortUrlInputFilter::DOMAIN => 4,
|
2019-12-06 15:38:22 -06:00
|
|
|
]];
|
2020-02-18 11:57:24 -06:00
|
|
|
yield [[
|
2021-01-31 00:44:46 -06:00
|
|
|
ShortUrlInputFilter::SHORT_CODE_LENGTH => 3,
|
2020-02-18 11:57:24 -06:00
|
|
|
]];
|
2020-03-06 13:01:41 -06:00
|
|
|
yield [[
|
2021-01-31 00:44:46 -06:00
|
|
|
ShortUrlInputFilter::CUSTOM_SLUG => '/',
|
2020-03-06 13:01:41 -06:00
|
|
|
]];
|
|
|
|
yield [[
|
2021-01-31 00:44:46 -06:00
|
|
|
ShortUrlInputFilter::CUSTOM_SLUG => '',
|
2020-03-06 13:01:41 -06:00
|
|
|
]];
|
|
|
|
yield [[
|
2021-01-31 00:44:46 -06:00
|
|
|
ShortUrlInputFilter::CUSTOM_SLUG => ' ',
|
2020-03-06 13:01:41 -06:00
|
|
|
]];
|
2021-01-30 07:18:44 -06:00
|
|
|
yield [[
|
2021-01-31 00:44:46 -06:00
|
|
|
ShortUrlInputFilter::LONG_URL => [],
|
2021-01-30 07:18:44 -06:00
|
|
|
]];
|
2018-01-07 13:07:12 -06:00
|
|
|
}
|
|
|
|
|
2020-06-08 11:08:46 -05:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideCustomSlugs
|
|
|
|
*/
|
|
|
|
public function properlyCreatedInstanceReturnsValues(string $customSlug, string $expectedSlug): void
|
2018-01-07 13:07:12 -06:00
|
|
|
{
|
2021-01-30 07:18:44 -06:00
|
|
|
$meta = ShortUrlMeta::fromRawData([
|
|
|
|
'validSince' => Chronos::parse('2015-01-01')->toAtomString(),
|
|
|
|
'customSlug' => $customSlug,
|
|
|
|
'longUrl' => '',
|
|
|
|
]);
|
2018-01-07 13:07:12 -06:00
|
|
|
|
2020-10-03 17:35:14 -05:00
|
|
|
self::assertTrue($meta->hasValidSince());
|
|
|
|
self::assertEquals(Chronos::parse('2015-01-01'), $meta->getValidSince());
|
2018-01-07 13:07:12 -06:00
|
|
|
|
2020-10-03 17:35:14 -05:00
|
|
|
self::assertFalse($meta->hasValidUntil());
|
|
|
|
self::assertNull($meta->getValidUntil());
|
2018-01-07 13:07:12 -06:00
|
|
|
|
2020-10-03 17:35:14 -05:00
|
|
|
self::assertTrue($meta->hasCustomSlug());
|
|
|
|
self::assertEquals($expectedSlug, $meta->getCustomSlug());
|
2018-01-07 13:07:12 -06:00
|
|
|
|
2020-10-03 17:35:14 -05:00
|
|
|
self::assertFalse($meta->hasMaxVisits());
|
|
|
|
self::assertNull($meta->getMaxVisits());
|
2018-01-07 13:07:12 -06:00
|
|
|
}
|
2020-06-08 11:08:46 -05:00
|
|
|
|
|
|
|
public function provideCustomSlugs(): iterable
|
|
|
|
{
|
|
|
|
yield ['foobar', 'foobar'];
|
|
|
|
yield ['foo bar', 'foo-bar'];
|
|
|
|
yield ['wp-admin.php', 'wp-admin.php'];
|
|
|
|
yield ['UPPER_lower', 'UPPER_lower'];
|
|
|
|
yield ['more~url_special.chars', 'more~url_special.chars'];
|
2020-12-02 11:38:23 -06:00
|
|
|
yield ['äéñ', 'äen'];
|
|
|
|
yield ['구글', '구글'];
|
|
|
|
yield ['グーグル', 'グーグル'];
|
|
|
|
yield ['谷歌', '谷歌'];
|
|
|
|
yield ['гугл', 'гугл'];
|
2020-06-08 11:08:46 -05:00
|
|
|
}
|
2018-01-07 13:07:12 -06:00
|
|
|
}
|