Skip to content

Commit

Permalink
[#272] Add new logic
Browse files Browse the repository at this point in the history
[#272] Update unit tests for new logic

[#272] Remove old logic (solves CI errors)

[#272] Solve more CI errors
  • Loading branch information
Fragonite committed Sep 6, 2023
1 parent b38ec2b commit 38e8740
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 54 deletions.
43 changes: 30 additions & 13 deletions classes/script_metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,23 +205,40 @@ public static function get_redactable_param_names(): array {
}

/**
* Gets the name of the script.
*
* Gets the name of the script. Parameters share the names of globals as a hint to the caller.
* @param string|null $me
* @param string|null $script
* @return string the request path for this profile.
*/
public static function get_request(): string {
global $SCRIPT, $ME, $CFG;
public static function get_normalised_relative_script_path($me, $script): string {
if (isset($me)) {
$scriptpath = $me;
} else if (isset($script)) {
$scriptpath = $script;
} else {
return self::REQUEST_UNKNOWN;
}

if (!isset($ME)) {
// If set, it will trim off the leading '/' to normalise web & cli requests.
$request = isset($SCRIPT) ? ltrim($SCRIPT, '/') : self::REQUEST_UNKNOWN;
return $request;
// 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);

// Remove leading and trailing slashes.
$scriptpath = trim($scriptpath, '/');

if ($scriptpath === '') {
return '/';
}

$request = (new \moodle_url($ME))->out_omit_querystring();
$request = str_replace($CFG->wwwroot, '', $request);
$request = ltrim($request, '/');
return $request;
return $scriptpath;
}

/**
Expand Down Expand Up @@ -401,7 +418,7 @@ public static function get_timer_interval(): float {
public static function get_sampling_period(): float {
$period = get_config('tool_excimer', 'sample_ms') / 1000;
$insensiblerange = $period >= self::SAMPLING_PERIOD_MIN && $period <= self::SAMPLING_PERIOD_MAX;
if (! $insensiblerange) {
if (!$insensiblerange) {
set_config('sample_ms', self::SAMPLING_PERIOD_DEFAULT * 1000, 'tool_excimer');
$period = self::SAMPLING_PERIOD_DEFAULT;
}
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_request();
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
58 changes: 18 additions & 40 deletions tests/tool_excimer_script_metadata_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,28 +188,6 @@ public function sampling_limit_provider(): array {
];
}

/**
* Tests script_metadata::get_request().
* @dataProvider relative_script_path_provider
* @covers \tool_excimer\script_metadata::get_request
* @param string $path
* @param string $expected
*/
public function test_get_request(string $path, string $expected) {
script_metadata::init();

global $SCRIPT, $ME;
$globalscript = $SCRIPT ?? null;
$globalme = $ME ?? null;

$SCRIPT = $path;
$ME = $path;
$this->assertEquals($expected, script_metadata::get_request());

$SCRIPT = $globalscript;
$ME = $globalme;
}

/**
* Tests script_metadata::get_normalised_relative_script_path().
* @dataProvider relative_script_path_provider
Expand All @@ -229,28 +207,28 @@ public function test_get_normalised_relative_script_path(string $path, string $e
*/
public function relative_script_path_provider(): array {
return [
['', ''],
['/', ''],
['//', ''],
['///', ''],
['', '/'],
['/', '/'],
['//', '/'],
['///', '/'],
['home', 'home'],
['/home', 'home'],
['home/', 'home/'],
['/home/', 'home/'],
['home/', 'home'],
['/home/', 'home'],
['hello/world', 'hello/world'],
['/hello/world', 'hello/world'],
['/hello/world/', 'hello/world/'],
['/////hello////world///', 'hello////world///'],
['/////hello////world/index.php', 'hello////world/index.php'],
['/////hello////world///index.php', 'hello////world///index.php'],
['/////hello////world/index.php///', 'hello////world/index.php'],
['/index.php', 'index.php'],
['/my//index.php////', 'my//index.php'],
['/my//index.php////index.php', 'my//index.php'],
['/my//index.php////index.php/', 'my//index.php'],
['/my//index.php////index.php/hello/world?param=value&param2=value', 'my//index.php'],
['https://example.com//index.php', 'https://example.com//index.php'],
['https://example.com/index.php/', 'https://example.com/index.php'],
['/hello/world/', 'hello/world'],
['/////hello////world///', 'hello/world'],
['/////hello////world/index.php', 'hello/world'],
['/////hello////world///index.php', 'hello/world'],
['/////hello////world/index.php///', 'hello/world'],
['/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'],
];
}

Expand Down

0 comments on commit 38e8740

Please sign in to comment.