From: Tim Otten Date: Tue, 25 Jul 2023 03:34:22 +0000 (-0700) Subject: UrlTest - Add coverage for variable-substitution X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=4a9c147a5de4b9caf2765cbfa21ce2dcbccb8e41;p=civicrm-core.git UrlTest - Add coverage for variable-substitution --- diff --git a/Civi/Core/Url.php b/Civi/Core/Url.php index 69ab20db08..c521998975 100644 --- a/Civi/Core/Url.php +++ b/Civi/Core/Url.php @@ -426,7 +426,7 @@ final class Url implements \JsonSerializable { * @return $this */ public function addVars(array $vars): Url { - $this->vars = array_merge($this->vars ?: [], $vars); + $this->vars = $vars + ($this->vars ?: []); return $this; } diff --git a/tests/phpunit/Civi/Core/UrlTest.php b/tests/phpunit/Civi/Core/UrlTest.php index 2fdcc779d6..c12933c13a 100644 --- a/tests/phpunit/Civi/Core/UrlTest.php +++ b/tests/phpunit/Civi/Core/UrlTest.php @@ -127,4 +127,26 @@ class UrlTest extends \CiviUnitTestCase { } } + public function testVars(): void { + $vars = ['hi' => 'hello world?', 'contact' => 123]; + + $examples = []; + $examples[] = ['civicrm/admin/hello+world%3F', Civi::url('backend://civicrm/admin/[hi]?x=1')]; + $examples[] = ['msg=hello+world%3F&id=123', Civi::url('backend://civicrm/admin?msg=[hi]&id=[contact]')]; + $examples[] = ['a=123&b=456', Civi::url('backend://civicrm/admin?a=[1]&b=[2]')->addVars([1 => 123, 2 => 456])]; + $examples[] = ['#/page?msg=hello+world%3F', Civi::url('backend://civicrm/a/#/page?msg=[hi]')]; + $examples[] = ['a=hello+world%3F&b=Au+re%2Fvoir', Civi::url('frontend://civicrm/user?a=[hi]&b=[bye]')->addVars(['bye' => 'Au re/voir'])]; + $examples[] = ['some_xyz=123', Civi::url('//civicrm/foo?some_[key]=123')->addVars(['key' => 'xyz'])]; + + // Unrecognized []'s are preserved as literals, which allows interop with deep form fields + $examples[] = ['some[key]=123', Civi::url('//civicrm/foo?some[key]=123')]; + + foreach ($examples as $key => $example) { + /** @var \Civi\Core\Url $url */ + [$expected, $url] = $example; + $url->addVars($vars); + $this->assertStringContainsString($expected, (string) $url, sprintf("%s at %d should be have matching output", __FUNCTION__, $key)); + } + } + }