From ce8da04d3a4be6ad65b17d1bea23601d64598718 Mon Sep 17 00:00:00 2001 From: Shish Date: Sat, 1 Feb 2020 22:26:08 +0000 Subject: [PATCH] dedupe BASE_URL / BASE_HREF --- core/sys_config.php | 2 +- core/tests/urls.test.php | 39 +++++++++++++++++++++++++++++++++++++++ core/urls.php | 9 ++++----- ext/view/test.php | 10 +++++----- tests/defines.php | 3 +-- 5 files changed, 50 insertions(+), 13 deletions(-) create mode 100644 core/tests/urls.test.php diff --git a/core/sys_config.php b/core/sys_config.php index 1c24ef4d..ac5b93ff 100644 --- a/core/sys_config.php +++ b/core/sys_config.php @@ -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 diff --git a/core/tests/urls.test.php b/core/tests/urls.test.php new file mode 100644 index 00000000..6dff4d82 --- /dev/null +++ b/core/tests/urls.test.php @@ -0,0 +1,39 @@ +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:///test/foo", + make_http("foo") + ); + + // relative to web server + $this->assertEquals( + "http:///foo", + make_http("/foo") + ); + + // absolute + $this->assertEquals( + "http://foo.com", + make_http("http://foo.com") + ); + } +} diff --git a/core/urls.php b/core/urls.php index 99cab424..c9ff9d87 100644 --- a/core/urls.php +++ b/core/urls.php @@ -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)) { diff --git a/ext/view/test.php b/ext/view/test.php index e15f13e3..96a9f80b 100644 --- a/ext/view/test.php +++ b/ext/view/test.php @@ -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); } diff --git a/tests/defines.php b/tests/defines.php index c92bea74..51a9ccc7 100644 --- a/tests/defines.php +++ b/tests/defines.php @@ -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);