dedupe BASE_URL / BASE_HREF

This commit is contained in:
Shish 2020-02-01 22:26:08 +00:00
parent e9ab6aa802
commit ce8da04d3a
5 changed files with 50 additions and 13 deletions

View file

@ -31,6 +31,6 @@ _d("WH_SPLITS", 1); // int how many levels of subfolders to put in
_d("VERSION", '2.8-dev'); // string shimmie version
_d("TIMEZONE", null); // string timezone
_d("EXTRA_EXTS", ""); // string optional extra extensions
_d("BASE_URL", null); // string force a specific base URL (default is auto-detect)
_d("BASE_HREF", null); // string force a specific base URL (default is auto-detect)
_d("TRACE_FILE", null); // string file to log performance data into
_d("TRACE_THRESHOLD", 0.0); // float log pages which take more time than this many seconds

39
core/tests/urls.test.php Normal file
View file

@ -0,0 +1,39 @@
<?php declare(strict_types=1);
require_once "core/urls.php";
class UrlsTest extends \PHPUnit\Framework\TestCase
{
public function test_make_link()
{
$this->assertEquals(
"/test/foo",
make_link("foo")
);
$this->assertEquals(
"/test/foo",
make_link("/foo")
);
}
public function test_make_http()
{
// relative to shimmie install
$this->assertEquals(
"http://<cli command>/test/foo",
make_http("foo")
);
// relative to web server
$this->assertEquals(
"http://<cli command>/foo",
make_http("/foo")
);
// absolute
$this->assertEquals(
"http://foo.com",
make_http("http://foo.com")
);
}
}

View file

@ -34,12 +34,11 @@ function make_link(?string $page=null, ?string $query=null): string
$page = $config->get_string(SetupConfig::MAIN_PAGE);
}
if (!is_null(BASE_URL)) {
$base = BASE_URL;
} elseif (NICE_URLS || $config->get_bool('nice_urls', false)) {
$base = str_replace('/'.basename($_SERVER["SCRIPT_FILENAME"]), "", $_SERVER["PHP_SELF"]);
$install_dir = get_base_href();
if (NICE_URLS || $config->get_bool('nice_urls', false)) {
$base = $install_dir;
} else {
$base = "./".basename($_SERVER["SCRIPT_FILENAME"])."?q=";
$base = "$install_dir/index.php?q=";
}
if (is_null($query)) {

View file

@ -30,21 +30,21 @@ class ViewImageTest extends ShimmiePHPUnitTestCase
$page = $this->get_page("post/next/$image_id_1");
$this->assertEquals(404, $page->code);
$page = $this->get_page("post/prev/$image_id_1");
$this->assertEquals("/post/view/$image_id_2", $page->redirect);
$this->assertEquals("/test/post/view/$image_id_2", $page->redirect);
// When searching, we skip the middle
$page = $this->get_page("post/prev/$image_id_1?search=test");
$this->assertEquals("/post/view/$image_id_2", $page->redirect);
$this->assertEquals("/test/post/view/$image_id_2", $page->redirect);
// Middle image: has next and prev
$page = $this->get_page("post/next/$image_id_2");
$this->assertEquals("/post/view/$image_id_1", $page->redirect);
$this->assertEquals("/test/post/view/$image_id_1", $page->redirect);
$page = $this->get_page("post/prev/$image_id_2");
$this->assertEquals("/post/view/$image_id_3", $page->redirect);
$this->assertEquals("/test/post/view/$image_id_3", $page->redirect);
// Last image has next, no prev
$page = $this->get_page("post/next/$image_id_3");
$this->assertEquals("/post/view/$image_id_2", $page->redirect);
$this->assertEquals("/test/post/view/$image_id_2", $page->redirect);
$page = $this->get_page("post/prev/$image_id_3");
$this->assertEquals(404, $page->code);
}

View file

@ -13,9 +13,8 @@ define("SPEED_HAX", false);
define("NICE_URLS", true);
define("WH_SPLITS", 1);
define("VERSION", 'unit-tests');
define("BASE_URL", "/");
define("TRACE_FILE", null);
define("TRACE_THRESHOLD", 0.0);
define("TIMEZONE", 'UTC');
define("BASE_HREF", "/");
define("BASE_HREF", "/test");
define("CLI_LOG_LEVEL", 50);