From 1b307e7f881280685d33ba2b831b6761751eba15 Mon Sep 17 00:00:00 2001 From: Alexander Van der Bellen Date: Mon, 4 Sep 2023 12:42:47 +0800 Subject: [PATCH] Update unit tests, update new function, add unit tests for old function --- classes/script_metadata.php | 38 ++++++++++++--------- classes/web_processor.php | 4 ++- tests/tool_excimer_script_metadata_test.php | 35 +++++++++++++++---- 3 files changed, 53 insertions(+), 24 deletions(-) diff --git a/classes/script_metadata.php b/classes/script_metadata.php index ddeb11b..eedcdc5 100644 --- a/classes/script_metadata.php +++ b/classes/script_metadata.php @@ -226,30 +226,34 @@ public static function get_request(): string { /** * Gets the name of the script. - * @param string $scriptpath + * Parameters are share the names of globals as a hint to the caller. + * @param string $ME + * @param string $SCRIPT * @return string the request path for this profile. */ - public static function get_normalised_relative_script_path(string $scriptpath = null): string { - global $SCRIPT; - - if (!isset($scriptpath)) { - - if (!isset($SCRIPT)) { - return self::REQUEST_UNKNOWN; - } - + public static function get_normalised_relative_script_path(?string $ME, ?string $SCRIPT): string { + if (isset($ME)) { + $scriptpath = $ME; + } else if (isset($SCRIPT)) { $scriptpath = $SCRIPT; } else { - // The $SCRIPT global has a leading slash, no consecutive slashes, and appends 'index.php' if there is a trailing slash. - // This else block normalises $scriptpath to match the format of $SCRIPT. - - $scriptpath = '/' . $scriptpath; - $scriptpath = preg_replace('/\/+/', '/', $scriptpath); - $scriptpath = preg_replace('/\/$/', '/index.php', $scriptpath, 1); + return self::REQUEST_UNKNOWN; } + // Remove consecutive slashes. + $scriptpath = preg_replace('/\/+/', '/', $scriptpath); + + // Strip pathinfo. + $scriptpath = preg_replace('/\.php.*$/', '.php', $scriptpath, 1); + + // Strip off the query string ($ME includes this). + $scriptpath = preg_replace('/\?.*$/', '', $scriptpath); + + // Remove 'index.php' from the end ($SCRIPT includes this). $scriptpath = preg_replace('/\/index\.php$/', '', $scriptpath, 1); - $scriptpath = ltrim($scriptpath, '/'); + + // Remove leading and trailing slashes. + $scriptpath = trim($scriptpath, '/'); if ($scriptpath === '') { return '/'; diff --git a/classes/web_processor.php b/classes/web_processor.php index e9055db..502dc9a 100644 --- a/classes/web_processor.php +++ b/classes/web_processor.php @@ -59,7 +59,9 @@ public function init(manager $manager) { // Record and set initial memory usage at this point. $memoryusage = memory_get_usage(); - $request = script_metadata::get_normalised_relative_script_path(); + global $ME, $SCRIPT; + + $request = script_metadata::get_normalised_relative_script_path($ME, $SCRIPT); $starttime = (int) $manager->get_starttime(); $this->sampleset = new sample_set($request, $starttime); diff --git a/tests/tool_excimer_script_metadata_test.php b/tests/tool_excimer_script_metadata_test.php index a90bd48..a2f628f 100644 --- a/tests/tool_excimer_script_metadata_test.php +++ b/tests/tool_excimer_script_metadata_test.php @@ -188,6 +188,27 @@ public function sampling_limit_provider(): array { ]; } + /** + * Tests script_metadata::get_request(). + * @dataProvider normalised_relative_script_path_provider + * @covers \tool_excimer\script_metadata::get_request + */ + public function test_get_request(string $pathinfo, string $expected) { + script_metadata::init(); + + global $SCRIPT, $ME; + $globalscript = $SCRIPT ?? null; + $globalme = $ME ?? null; + + $SCRIPT = $pathinfo; + $ME = $pathinfo; + $this->assertEquals($expected, script_metadata::get_request()); + + $SCRIPT = $globalscript; + $ME = $globalme; + } + + /** * Tests script_metadata::get_normalised_relative_script_path(). * @dataProvider normalised_relative_script_path_provider @@ -197,7 +218,8 @@ public function sampling_limit_provider(): array { */ public function test_get_normalised_relative_script_path(string $pathinfo, string $expected) { script_metadata::init(); - $this->assertEquals($expected, script_metadata::get_normalised_relative_script_path($pathinfo)); + // $this->assertEquals($expected, script_metadata::get_normalised_relative_script_path($pathinfo, null)); + $this->assertEquals($expected, script_metadata::get_normalised_relative_script_path(null, $pathinfo)); } /** @@ -220,13 +242,14 @@ public function normalised_relative_script_path_provider(): array { ['/////hello////world///', 'hello/world'], ['/////hello////world/index.php', 'hello/world'], ['/////hello////world///index.php', 'hello/world'], - ['/////hello////world/index.php///', 'hello/world/index.php'], + ['/////hello////world/index.php///', 'hello/world'], ['/index.php', '/'], - ['/my//index.php////', 'my/index.php'], - ['/my//index.php////index.php', 'my/index.php'], - ['/my//index.php////index.php/', 'my/index.php/index.php'], + ['/my//index.php////', 'my'], + ['/my//index.php////index.php', 'my'], + ['/my//index.php////index.php/', 'my'], + ['/my//index.php////index.php/hello/world?param=value¶m2=value', 'my'], ['https://example.com//index.php', 'https:/example.com'], - ['https://example.com/index.php/', 'https:/example.com/index.php'], + ['https://example.com/index.php/', 'https:/example.com'], ]; }