fix tests

This commit is contained in:
Shish 2024-02-09 14:54:37 +00:00
parent eebb4c53e6
commit 9ba9bf0fdf
4 changed files with 27 additions and 21 deletions

View file

@ -25,12 +25,11 @@ class UrlsTest extends TestCase
* @param array<string> $vars * @param array<string> $vars
* @return array<string> * @return array<string>
*/ */
function _gst(array $terms): array $gst = function (array $terms): array {
{
$pre = new PageRequestEvent("GET", _get_query(search_link($terms))); $pre = new PageRequestEvent("GET", _get_query(search_link($terms)));
$pre->page_matches("post/list"); $pre->page_matches("post/list");
return $pre->get_search_terms(); return $pre->get_search_terms();
} };
global $config; global $config;
foreach([true, false] as $nice_urls) { foreach([true, false] as $nice_urls) {
@ -38,15 +37,15 @@ class UrlsTest extends TestCase
$this->assertEquals( $this->assertEquals(
["bar", "foo"], ["bar", "foo"],
_gst(["foo", "bar"]) $gst(["foo", "bar"])
); );
$this->assertEquals( $this->assertEquals(
["AC/DC"], ["AC/DC"],
_gst(["AC/DC"]) $gst(["AC/DC"])
); );
$this->assertEquals( $this->assertEquals(
["cat*", "rating=?"], ["cat*", "rating=?"],
_gst(["rating=?", "cat*"]), $gst(["rating=?", "cat*"]),
); );
} }
} }
@ -57,13 +56,13 @@ class UrlsTest extends TestCase
global $config; global $config;
foreach([true, false] as $nice_urls) { foreach([true, false] as $nice_urls) {
$config->set_bool('nice_urls', $nice_urls); $config->set_bool('nice_urls', $nice_urls);
// basic // basic
$this->assertEquals( $this->assertEquals(
$nice_urls ? "/test/foo" : "/test/index.php?q=foo", $nice_urls ? "/test/foo" : "/test/index.php?q=foo",
make_link("foo") make_link("foo")
); );
// remove leading slash from path // remove leading slash from path
$this->assertEquals( $this->assertEquals(
$nice_urls ? "/test/foo" : "/test/index.php?q=foo", $nice_urls ? "/test/foo" : "/test/index.php?q=foo",
@ -75,18 +74,18 @@ class UrlsTest extends TestCase
$nice_urls ? "/test/foo?a=1&b=2" : "/test/index.php?q=foo&a=1&b=2", $nice_urls ? "/test/foo?a=1&b=2" : "/test/index.php?q=foo&a=1&b=2",
make_link("foo", "a=1&b=2") make_link("foo", "a=1&b=2")
); );
// hash // hash
$this->assertEquals( $this->assertEquals(
$nice_urls ? "/test/foo#cake" : "/test/index.php?q=foo#cake", $nice_urls ? "/test/foo#cake" : "/test/index.php?q=foo#cake",
make_link("foo", null, "cake") make_link("foo", null, "cake")
); );
// query + hash // query + hash
$this->assertEquals( $this->assertEquals(
$nice_urls ? "/test/foo?a=1&b=2#cake" : "/test/index.php?q=foo&a=1&b=2#cake", $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") make_link("foo", "a=1&b=2", "cake")
); );
} }
} }
@ -120,7 +119,7 @@ class UrlsTest extends TestCase
$this->assertEquals( $this->assertEquals(
"tasty/cake", "tasty/cake",
_get_query("/test/tasty/cake"), _get_query("/test/tasty/cake"),
'http://$SERVER/$INSTALL_DIR/$PATH should return $PATH' 'http://$SERVER/$INSTALL_DIR/$PATH should return $PATH'
); );
$this->assertEquals( $this->assertEquals(
@ -264,4 +263,11 @@ class UrlsTest extends TestCase
referer_or("foo", ["cake"]) referer_or("foo", ["cake"])
); );
} }
public function tearDown(): void
{
global $config;
$config->set_bool('nice_urls', true);
parent::tearDown();
}
} }

View file

@ -69,9 +69,9 @@ function make_link(?string $page = null, ?string $query = null, ?string $fragmen
/** /**
* Figure out the current page from a link that make_link() generated * Figure out the current page from a link that make_link() generated
* *
* SHIT: notes for the future, because the web stack is a pile of hacks * SHIT: notes for the future, because the web stack is a pile of hacks
* *
* - According to some specs, "/" is for URL dividers with heiracial * - According to some specs, "/" is for URL dividers with heiracial
* significance and %2F is for slashes that are just slashes. This * significance and %2F is for slashes that are just slashes. This
* is what shimmie currently does - eg if you search for "AC/DC", * is what shimmie currently does - eg if you search for "AC/DC",
@ -83,7 +83,7 @@ function make_link(?string $page = null, ?string $query = null, ?string $fragmen
* can parse it for ourselves * can parse it for ourselves
* - <input type="hidden" name="q" value="post/list"> generates * - <input type="hidden" name="q" value="post/list"> generates
* q=post%2Flist * q=post%2Flist
* *
* This function should always return strings with no leading slashes * This function should always return strings with no leading slashes
*/ */
function _get_query(?string $uri = null): string function _get_query(?string $uri = null): string
@ -123,10 +123,10 @@ function _get_query(?string $uri = null): string
* function should return /gallery * function should return /gallery
* *
* PHP really, really sucks. * PHP really, really sucks.
* *
* This function should always return strings with no trailing * This function should always return strings with no trailing
* slashes, so that it can be used like `get_base_href() . "/data/asset.abc"` * slashes, so that it can be used like `get_base_href() . "/data/asset.abc"`
* *
* @param array<string, string>|null $server_settings * @param array<string, string>|null $server_settings
*/ */
function get_base_href(?array $server_settings = null): string function get_base_href(?array $server_settings = null): string

View file

@ -10,7 +10,7 @@ class DownloadTest extends ShimmiePHPUnitTestCase
{ {
global $page; global $page;
$image_id = $this->post_image("tests/pbx_screenshot.jpg", "pbx computer screenshot"); $image_id = $this->post_image("tests/pbx_screenshot.jpg", "pbx computer screenshot");
$this->get_page("/image/$image_id"); $this->get_page("image/$image_id");
$this->assertEquals(PageMode::FILE, $page->mode); $this->assertEquals(PageMode::FILE, $page->mode);
} }
} }

View file

@ -19,7 +19,7 @@ class LinkScanTest extends ShimmiePHPUnitTestCase
But there is no http://example.com/post/view/65432 But there is no http://example.com/post/view/65432
"; ";
$page = $this->post_page("post/list", ["search" => $text]); $page = $this->get_page("post/list", ["search" => $text]);
$this->assertEquals(PageMode::REDIRECT, $page->mode); $this->assertEquals(PageMode::REDIRECT, $page->mode);
$this->assertEquals("/test/post/list/id%3D{$image_id_1}%2C{$image_id_2}/1", $page->redirect); $this->assertEquals("/test/post/list/id%3D{$image_id_1}%2C{$image_id_2}/1", $page->redirect);
@ -35,7 +35,7 @@ class LinkScanTest extends ShimmiePHPUnitTestCase
Look at http://example.com/_images/feb01bab5698a11dd87416724c7a89e3/foobar.jpg Look at http://example.com/_images/feb01bab5698a11dd87416724c7a89e3/foobar.jpg
there is an image or search for e106ea2983e1b77f11e00c0c54e53805 but one that there is an image or search for e106ea2983e1b77f11e00c0c54e53805 but one that
doesn't exist is e106ea2983e1b77f11e00c0c54e50000 o.o"; doesn't exist is e106ea2983e1b77f11e00c0c54e50000 o.o";
$page = $this->post_page("post/list", ["search" => $text]); $page = $this->get_page("post/list", ["search" => $text]);
$this->assertEquals(PageMode::REDIRECT, $page->mode); $this->assertEquals(PageMode::REDIRECT, $page->mode);
$this->assertEquals("/test/post/list/id%3D{$image_id_1}%2C{$image_id_2}/1", $page->redirect); $this->assertEquals("/test/post/list/id%3D{$image_id_1}%2C{$image_id_2}/1", $page->redirect);
@ -48,7 +48,7 @@ class LinkScanTest extends ShimmiePHPUnitTestCase
$this->post_image("tests/favicon.png", "TeStCase"); $this->post_image("tests/favicon.png", "TeStCase");
$text = "Look at feb01bab5698a11dd87416724c7a89e3/foobar.jpg"; $text = "Look at feb01bab5698a11dd87416724c7a89e3/foobar.jpg";
$page = $this->post_page("post/list", ["search" => $text]); $page = $this->get_page("post/list", ["search" => $text]);
$this->assertEquals(PageMode::REDIRECT, $page->mode); $this->assertEquals(PageMode::REDIRECT, $page->mode);
$this->assertEquals("/test/post/list/at%20feb01bab5698a11dd87416724c7a89e3%2Ffoobar.jpg%20Look/1", $page->redirect); $this->assertEquals("/test/post/list/at%20feb01bab5698a11dd87416724c7a89e3%2Ffoobar.jpg%20Look/1", $page->redirect);