|
<?php |
|
|
|
/** |
|
* Short URLs : yourls_is_shorturl() extensive tests |
|
* |
|
* @group shorturls |
|
*/ |
|
|
|
class ShortURL_Is_Shorturl_Tests extends PHPUnit\Framework\TestCase |
|
{ |
|
/** |
|
* Notes for my future self |
|
* |
|
* The purpose of these tests is to make sure yourls_is_shorturl() works correctly and consistently : |
|
* http://sho.rt/ozh, |
|
* https://shor.rt/ozh, |
|
* http://www.sho.rt/ozh |
|
* https://www.sho.rt/ozh |
|
* should all be considered short URLs (since short URL 'ozh' was created upon install) |
|
* |
|
* First off, we're adding a filter on 'get_yourls_site' to modify YOURLS_SITE value to 'http://sho.rt'. Why? |
|
* Because we'll be adding or removing 'www.' from YOURLS_SITE and we need to make sure its independent from the |
|
* actual value used by a test machine (if YOURLS_SITE is 127.0.0.1, it makes no sense to prefix with 'www.') |
|
* |
|
* Then we're using 4 very similar tests instead of one test with a data provider. Why? |
|
* Because data provider are executed before both setUpBeforeClass and setUp methods, and we need to have the |
|
* filter activated in order to manipulate YOURLS_SITE |
|
* See https://phpunit.readthedocs.io/en/9.5/writing-tests-for-phpunit.html |
|
* |
|
*/ |
|
|
|
protected static $base, |
|
$site_no_www, |
|
$site_www; |
|
|
|
public static function setUpBeforeClass(): void |
|
{ |
|
yourls_add_filter('get_yourls_site', function () { |
|
return 'https://sho.rt'; |
|
}); |
|
|
|
// Now that the filter is set, we can modify YOURLS_SITE |
|
self::set_yourls_site(); |
|
|
|
// We know that 'ozh' is a valid short URL (created upon install) |
|
// We need refresh its info, otherwise it'll be existent but empty |
|
yourls_get_keyword_infos('ozh', false); |
|
} |
|
|
|
public static function set_yourls_site() { |
|
self::$base = yourls_get_domain(yourls_get_yourls_site() ); |
|
self::$site_no_www = str_replace('www.', '', self::$base); |
|
self::$site_www = 'www.' . self::$site_no_www; |
|
} |
|
|
|
public static function tearDownAfterClass(): void |
|
{ |
|
yourls_remove_all_filters('get_yourls_site'); |
|
} |
|
|
|
// yourls_is_shorturl (also see crud.php/test_is_shorturl) |
|
// Make variations with http/https/www/no-www with filtered YOURLS_SITE and test them. |
|
|
|
function test_is_shorturl_https_no_www() |
|
{ |
|
$shorturl = http_build_url(yourls_get_yourls_site(), ['scheme' => 'https', 'host' => self::$site_no_www]) . '/ozh'; |
|
$this->assertTrue(yourls_is_shorturl($shorturl)); |
|
} |
|
|
|
function test_is_shorturl_http_no_www() |
|
{ |
|
$shorturl = http_build_url(yourls_get_yourls_site(), ['scheme' => 'http', 'host' => self::$site_no_www]) . '/ozh'; |
|
$this->assertTrue(yourls_is_shorturl($shorturl)); |
|
} |
|
|
|
function test_is_shorturl_https_www() |
|
{ |
|
$shorturl = http_build_url(yourls_get_yourls_site(), ['scheme' => 'https', 'host' => self::$site_www]) . '/ozh'; |
|
$this->assertTrue(yourls_is_shorturl($shorturl)); |
|
} |
|
|
|
function test_is_shorturl_http_www() |
|
{ |
|
$shorturl = http_build_url(yourls_get_yourls_site(), ['scheme' => 'http', 'host' => self::$site_www]) . '/ozh'; |
|
$this->assertTrue(yourls_is_shorturl($shorturl)); |
|
} |
|
|
|
} |