Skip to content

Commit

Permalink
Update unit tests, update new function, add unit tests for old function
Browse files Browse the repository at this point in the history
  • Loading branch information
Fragonite committed Sep 4, 2023
1 parent d5a9772 commit 1b307e7
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 24 deletions.
38 changes: 21 additions & 17 deletions classes/script_metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '/';
Expand Down
4 changes: 3 additions & 1 deletion classes/web_processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
35 changes: 29 additions & 6 deletions tests/tool_excimer_script_metadata_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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));
}

/**
Expand All @@ -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&param2=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'],
];
}

Expand Down

0 comments on commit 1b307e7

Please sign in to comment.