This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/core/tests/UrlsTest.php

288 lines
9.1 KiB
PHP
Raw Normal View History

2021-12-14 18:32:47 +00:00
<?php
declare(strict_types=1);
2020-03-13 09:23:54 +00:00
namespace Shimmie2;
2020-03-13 09:23:54 +00:00
use PHPUnit\Framework\TestCase;
2024-02-09 12:43:53 +00:00
use PHPUnit\Framework\Attributes\Depends;
2020-03-13 09:23:54 +00:00
2020-02-01 22:26:08 +00:00
require_once "core/urls.php";
2020-03-13 09:23:54 +00:00
class UrlsTest extends TestCase
2020-02-01 22:26:08 +00:00
{
2024-02-09 12:43:53 +00:00
/**
* An integration test for
* - search_link()
* - make_link()
* - _get_query()
* - get_search_terms()
*/
#[Depends("test_search_link")]
public function test_get_search_terms_from_search_link(): void
{
/**
* @param array<string> $vars
* @return array<string>
*/
2024-02-09 14:54:37 +00:00
$gst = function (array $terms): array {
2024-02-09 16:36:57 +00:00
$pre = new PageRequestEvent("GET", _get_query(search_link($terms)), [], []);
2024-02-11 11:34:09 +00:00
$pre->page_matches("post/list/{search}/{page}");
return Tag::explode($pre->get_arg('search'));
2024-02-09 14:54:37 +00:00
};
2024-02-09 12:43:53 +00:00
global $config;
2024-08-31 16:05:18 +00:00
foreach ([true, false] as $nice_urls) {
2024-02-14 12:35:58 +00:00
$config->set_bool(SetupConfig::NICE_URLS, $nice_urls);
2024-02-09 12:43:53 +00:00
$this->assertEquals(
["bar", "foo"],
2024-02-09 14:54:37 +00:00
$gst(["foo", "bar"])
2024-02-09 12:43:53 +00:00
);
$this->assertEquals(
["AC/DC"],
2024-02-09 14:54:37 +00:00
$gst(["AC/DC"])
2024-02-09 12:43:53 +00:00
);
$this->assertEquals(
["cat*", "rating=?"],
2024-02-09 14:54:37 +00:00
$gst(["rating=?", "cat*"]),
2024-02-09 12:43:53 +00:00
);
}
}
#[Depends("test_get_base_href")]
public function test_make_link(): void
{
global $config;
2024-08-31 16:05:18 +00:00
foreach ([true, false] as $nice_urls) {
2024-02-14 12:35:58 +00:00
$config->set_bool(SetupConfig::NICE_URLS, $nice_urls);
2024-02-09 14:54:37 +00:00
2024-02-09 12:43:53 +00:00
// basic
$this->assertEquals(
$nice_urls ? "/test/foo" : "/test/index.php?q=foo",
make_link("foo")
);
2024-02-09 14:54:37 +00:00
2024-02-09 12:43:53 +00:00
// remove leading slash from path
$this->assertEquals(
$nice_urls ? "/test/foo" : "/test/index.php?q=foo",
make_link("/foo")
);
// query
$this->assertEquals(
$nice_urls ? "/test/foo?a=1&b=2" : "/test/index.php?q=foo&a=1&b=2",
make_link("foo", "a=1&b=2")
);
2024-02-09 14:54:37 +00:00
2024-02-09 12:43:53 +00:00
// hash
$this->assertEquals(
$nice_urls ? "/test/foo#cake" : "/test/index.php?q=foo#cake",
make_link("foo", null, "cake")
);
2024-02-09 14:54:37 +00:00
2024-02-09 12:43:53 +00:00
// query + hash
$this->assertEquals(
$nice_urls ? "/test/foo?a=1&b=2#cake" : "/test/index.php?q=foo&a=1&b=2#cake",
make_link("foo", "a=1&b=2", "cake")
2024-02-09 14:54:37 +00:00
);
2024-02-09 12:43:53 +00:00
}
}
#[Depends("test_make_link")]
2024-01-15 14:31:51 +00:00
public function test_search_link(): void
{
2024-02-09 12:43:53 +00:00
global $config;
2024-08-31 16:05:18 +00:00
foreach ([true, false] as $nice_urls) {
2024-02-14 12:35:58 +00:00
$config->set_bool(SetupConfig::NICE_URLS, $nice_urls);
2024-02-09 12:43:53 +00:00
$this->assertEquals(
$nice_urls ? "/test/post/list/bar%20foo/1" : "/test/index.php?q=post/list/bar%20foo/1",
search_link(["foo", "bar"])
);
$this->assertEquals(
$nice_urls ? "/test/post/list/AC%2FDC/1" : "/test/index.php?q=post/list/AC%2FDC/1",
search_link(["AC/DC"])
);
$this->assertEquals(
$nice_urls ? "/test/post/list/cat%2A%20rating%3D%3F/1" : "/test/index.php?q=post/list/cat%2A%20rating%3D%3F/1",
search_link(["rating=?", "cat*"])
);
}
}
#[Depends("test_get_base_href")]
public function test_get_query(): void
{
// just validating an assumption that this test relies upon
$this->assertEquals(get_base_href(), "/test");
$this->assertEquals(
2024-02-09 12:43:53 +00:00
"tasty/cake",
2024-02-09 14:54:37 +00:00
_get_query("/test/tasty/cake"),
2024-02-09 12:43:53 +00:00
'http://$SERVER/$INSTALL_DIR/$PATH should return $PATH'
);
$this->assertEquals(
2024-02-09 12:43:53 +00:00
"tasty/cake",
_get_query("/test/index.php?q=tasty/cake"),
'http://$SERVER/$INSTALL_DIR/index.php?q=$PATH should return $PATH'
);
// even when we are /test/... publicly, and generating /test/... URLs,
// we should still be able to handle URLs at the root because that's
// what apache sends us when it is reverse-proxying a subdirectory
$this->assertEquals(
"tasty/cake",
_get_query("/tasty/cake"),
'http://$SERVER/$INSTALL_DIR/$PATH should return $PATH'
);
$this->assertEquals(
"tasty/cake",
_get_query("/index.php?q=tasty/cake"),
'http://$SERVER/$INSTALL_DIR/index.php?q=$PATH should return $PATH'
);
2020-02-01 22:26:08 +00:00
$this->assertEquals(
2024-02-09 12:43:53 +00:00
"tasty/cake%20pie",
_get_query("/test/index.php?q=tasty/cake%20pie"),
'URL encoded paths should be left alone'
2020-02-01 22:26:08 +00:00
);
$this->assertEquals(
2024-02-09 12:43:53 +00:00
"tasty/cake%20pie",
_get_query("/test/tasty/cake%20pie"),
'URL encoded queries should be left alone'
2020-02-01 22:26:08 +00:00
);
2020-03-28 14:11:14 +00:00
$this->assertEquals(
2024-02-09 12:43:53 +00:00
"",
_get_query("/test/"),
'If just viewing install directory, should return /'
2020-03-28 14:11:14 +00:00
);
$this->assertEquals(
2024-02-09 12:43:53 +00:00
"",
_get_query("/test/index.php"),
'If just viewing index.php, should return /'
2020-03-28 14:11:14 +00:00
);
$this->assertEquals(
2024-02-09 12:43:53 +00:00
"post/list/tasty%2Fcake/1",
_get_query("/test/post/list/tasty%2Fcake/1"),
'URL encoded niceurls should be left alone, even encoded slashes'
2020-03-28 14:11:14 +00:00
);
2024-02-09 12:43:53 +00:00
$this->assertEquals(
"post/list/tasty%2Fcake/1",
_get_query("/test/index.php?q=post/list/tasty%2Fcake/1"),
'URL encoded uglyurls should be left alone, even encoded slashes'
);
}
public function test_is_https_enabled(): void
{
$this->assertFalse(is_https_enabled(), "HTTPS should be disabled by default");
$_SERVER['HTTPS'] = "on";
$this->assertTrue(is_https_enabled(), "HTTPS should be enabled when set to 'on'");
unset($_SERVER['HTTPS']);
}
public function test_get_base_href(): void
{
// PHP_SELF should point to "the currently executing script
// relative to the document root"
$this->assertEquals("", get_base_href(["PHP_SELF" => "/index.php"]));
$this->assertEquals("/mydir", get_base_href(["PHP_SELF" => "/mydir/index.php"]));
// SCRIPT_FILENAME should point to "the absolute pathname of
// the currently executing script" and DOCUMENT_ROOT should
// point to "the document root directory under which the
// current script is executing"
$this->assertEquals("", get_base_href([
"PHP_SELF" => "<invalid>",
"SCRIPT_FILENAME" => "/var/www/html/index.php",
"DOCUMENT_ROOT" => "/var/www/html",
]), "root directory");
$this->assertEquals("/mydir", get_base_href([
"PHP_SELF" => "<invalid>",
"SCRIPT_FILENAME" => "/var/www/html/mydir/index.php",
"DOCUMENT_ROOT" => "/var/www/html",
]), "subdirectory");
$this->assertEquals("", get_base_href([
"PHP_SELF" => "<invalid>",
"SCRIPT_FILENAME" => "/var/www/html/index.php",
"DOCUMENT_ROOT" => "/var/www/html/",
]), "trailing slash in DOCUMENT_ROOT root should be ignored");
$this->assertEquals("/mydir", get_base_href([
"PHP_SELF" => "<invalid>",
"SCRIPT_FILENAME" => "/var/www/html/mydir/index.php",
"DOCUMENT_ROOT" => "/var/www/html/",
]), "trailing slash in DOCUMENT_ROOT subdir should be ignored");
2020-02-01 22:26:08 +00:00
}
2024-02-09 12:43:53 +00:00
#[Depends("test_is_https_enabled")]
#[Depends("test_get_base_href")]
2024-01-15 14:31:51 +00:00
public function test_make_http(): void
2020-02-01 22:26:08 +00:00
{
$this->assertEquals(
2020-10-24 12:46:49 +00:00
"http://cli-command/test/foo",
2024-02-09 12:43:53 +00:00
make_http("foo"),
"relative to shimmie root"
2020-02-01 22:26:08 +00:00
);
$this->assertEquals(
2020-10-24 12:46:49 +00:00
"http://cli-command/foo",
2024-02-09 12:43:53 +00:00
make_http("/foo"),
"relative to web server"
2020-02-01 22:26:08 +00:00
);
$this->assertEquals(
2020-03-25 11:47:00 +00:00
"https://foo.com",
2024-02-09 12:43:53 +00:00
make_http("https://foo.com"),
"absolute URL should be left alone"
2020-02-01 22:26:08 +00:00
);
}
2020-03-27 23:35:07 +00:00
2024-01-15 14:31:51 +00:00
public function test_modify_url(): void
2020-03-27 23:35:07 +00:00
{
$this->assertEquals(
"/foo/bar?a=3&b=2",
2023-11-11 21:49:12 +00:00
modify_url("/foo/bar?a=1&b=2", ["a" => "3"])
2020-03-27 23:35:07 +00:00
);
$this->assertEquals(
"https://blah.com/foo/bar?b=2",
2023-11-11 21:49:12 +00:00
modify_url("https://blah.com/foo/bar?a=1&b=2", ["a" => null])
2020-03-27 23:35:07 +00:00
);
$this->assertEquals(
"/foo/bar",
2023-11-11 21:49:12 +00:00
modify_url("/foo/bar?a=1&b=2", ["a" => null, "b" => null])
2020-03-27 23:35:07 +00:00
);
}
2020-03-28 14:11:14 +00:00
2024-01-15 14:31:51 +00:00
public function test_referer_or(): void
2020-03-28 14:11:14 +00:00
{
unset($_SERVER['HTTP_REFERER']);
$this->assertEquals(
"foo",
referer_or("foo")
);
$_SERVER['HTTP_REFERER'] = "cake";
$this->assertEquals(
"cake",
referer_or("foo")
);
$_SERVER['HTTP_REFERER'] = "cake";
$this->assertEquals(
"foo",
referer_or("foo", ["cake"])
);
}
2024-02-09 14:54:37 +00:00
public function tearDown(): void
{
global $config;
2024-02-14 12:35:58 +00:00
$config->set_bool(SetupConfig::NICE_URLS, true);
2024-02-09 14:54:37 +00:00
parent::tearDown();
}
2020-02-01 22:26:08 +00:00
}