refactor drop_query_params() to deal with array params and add test

This commit is contained in:
Mario Vavti
2026-03-16 10:32:59 +01:00
parent 955ee217e3
commit 51ac502d97
3 changed files with 59 additions and 20 deletions

View File

@@ -150,28 +150,29 @@ function clean_query_string($s = '') {
*/
function drop_query_params($s, $p) {
$unescaped = unescape_tags($s);
$parsed = parse_url($unescaped);
$s = unescape_tags($s);
$parsed = parse_url($s);
$query = '';
$query_args = null;
if(isset($parsed['query'])) {
parse_str($parsed['query'], $query_args);
if (empty($parsed['query'])) {
// No query parameters were found, return the original string
return $s;
}
if(is_array($query_args)) {
foreach($query_args as $k => $v) {
if(in_array($k, $p))
continue;
$query .= (($query) ? '&' : '') . urlencode($k) . '=' . urlencode($v);
$query_args = [];
parse_str($parsed['query'], $query_args);
foreach($query_args as $k => $v) {
if (in_array($k, $p)) {
unset($query_args[$k]);
}
}
unset($parsed['query']);
if($query) {
$query = http_build_query($query_args, '', '&');
if ($query) {
$parsed['query'] = $query;
}

View File

@@ -0,0 +1,38 @@
<?php
/*
* SPDX-FileCopyrightText: 2026 The Hubzilla Community
* SPDX-FileContributor: Harald Eilertsen <haraldei@anduin.net>
*
* SPDX-License-Identifier: MIT
*/
namespace Zotlabs\Tests\Unit;
use PHPUnit\Framework\Attributes\Before;
use Zotlabs\Lib\Libzot;
class ZidTest extends UnitTestCase {
/**
* Test the drop_query_params function.
*
* @dataProvider drop_query_params_provider
*/
public function test_drop_query_params(string $url, array $drop_params, string $expected) : void {
$this->assertEquals(escape_tags($expected), drop_query_params($url, $drop_params));
}
public static function drop_query_params_provider() : array {
return [
// Query params with array
['https://www.example.net/en/pro/detail/some-detail?tx_news_pi1%5Bday%5D=15&tx_news_pi1%5Bmonth%5D=3&tx_news_pi1%5Byear%5D=2026&cHash=85200a0007de8fecd4cd55199146e19c', ['zid', 'f'], 'https://www.example.net/en/pro/detail/some-detail?tx_news_pi1%5Bday%5D=15&tx_news_pi1%5Bmonth%5D=3&tx_news_pi1%5Byear%5D=2026&cHash=85200a0007de8fecd4cd55199146e19c'],
// Query params with zid
['https://www.example.net/channel/test?zid=test@example.net', ['zid', 'f'], 'https://www.example.net/channel/test'],
// Query params with zid and empty f
['https://www.example.net/channel/test?f=&zid=test@example.net', ['zid', 'f'], 'https://www.example.net/channel/test'],
// Query params with zid and empty f encoded
['https://www.example.net/channel/test?f=&amp;zid=test@example.net', ['zid', 'f'], 'https://www.example.net/channel/test'],
];
}
}

View File

@@ -1,9 +1,9 @@
<?php return array(
'root' => array(
'name' => 'zotlabs/hubzilla',
'pretty_version' => 'dev-master',
'version' => 'dev-master',
'reference' => '2fb816139a2dbd42a9a0e29804e363d8636ec22f',
'pretty_version' => 'dev-11.2RC',
'version' => 'dev-11.2RC',
'reference' => '955ee217e30e12dec86bdcd936ce10abc3ed366a',
'type' => 'application',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
@@ -542,9 +542,9 @@
'dev_requirement' => false,
),
'zotlabs/hubzilla' => array(
'pretty_version' => 'dev-master',
'version' => 'dev-master',
'reference' => '2fb816139a2dbd42a9a0e29804e363d8636ec22f',
'pretty_version' => 'dev-11.2RC',
'version' => 'dev-11.2RC',
'reference' => '955ee217e30e12dec86bdcd936ce10abc3ed366a',
'type' => 'application',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),