Skip to content

Commit 457325b

Browse files
committed
Civi::url() - Add E2E test-coverage
1 parent a6c51f6 commit 457325b

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

tests/phpunit/E2E/Core/PathUrlTest.php

+50
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,56 @@ public function testPaths_getVariable() {
6868
}
6969
}
7070

71+
/**
72+
* Get URLs through Civi::url().
73+
*/
74+
public function testUrl(): void {
75+
// Make some reqeusts for actual URLs
76+
$this->assertUrlContentRegex(';crm-section event_date_time-section;', \Civi::url('frontend://civicrm/event/info?id=1', 'a'));
77+
$this->assertUrlContentRegex(';MIT-LICENSE.txt;', \Civi::url('asset://[civicrm.packages]/jquery/plugins/jquery.timeentry.js'));
78+
$this->assertUrlContentRegex(';Please enter a valid email address;', \Civi::url('assetBuilder://crm-l10n.js?locale=en_US'));
79+
$this->assertUrlContentRegex(';.module..crmSearchAdmin;', \Civi::url('ext://org.civicrm.search_kit/ang/crmSearchAdmin.module.js'));
80+
$this->assertUrlContentRegex(';MIT-LICENSE.txt;', \Civi::url('[civicrm.packages]/jquery/plugins/jquery.timeentry.js'));
81+
82+
// Check for well-formedness of some URLs
83+
$urlPats = [];
84+
switch (CIVICRM_UF) {
85+
case 'Drupal':
86+
case 'Drupal8':
87+
case 'Backdrop':
88+
$urlPats[] = [';/civicrm/event/info\?reset=1&id=9;', \Civi::url('frontend://civicrm/event/info?reset=1')->addQuery('id=9')];
89+
$urlPats[] = [';/civicrm/admin\?reset=1;', \Civi::url('backend://civicrm/admin')->addQuery(['reset' => 1])];
90+
$this->assertNotEmpty($_SERVER['HTTP_HOST']);
91+
break;
92+
93+
case 'WordPress':
94+
$urlPats[] = [';civiwp=CiviCRM.*civicrm.*event.*info.*reset=1&id=9;', \Civi::url('frontend://civicrm/event/info?reset=1')->addQuery('id=9')];
95+
$urlPats[] = [';/wp-admin.*civicrm.*admin.*reset=1;', \Civi::url('backend://civicrm/admin?reset=1')];
96+
break;
97+
98+
case 'Joomla':
99+
$urlPats[] = [';/index.php\?.*task=civicrm/event/info&reset=1&id=9;', \Civi::url('frontend://civicrm/event/inof?reset=1')->addQuery('id=9')];
100+
$urlPats[] = [';/administrator/.*task=civicrm/admin/reset=1;', \Civi::url('backend://civicrm/admin')->addQuery('reset=1')];
101+
break;
102+
103+
default:
104+
$this->fail('Unrecognized UF: ' . CIVICRM_UF);
105+
}
106+
107+
$urlPats[] = [';^https?://.*civicrm;', \Civi::url('frontend://civicrm/event/info?reset=1', 'a')];
108+
$urlPats[] = [';^https://.*civicrm;', \Civi::url('frontend://civicrm/event/info?reset=1', 'as')];
109+
110+
// Some test-harnesses have HTTP_HOST. Some don't. It's pre-req for truly relative URLs.
111+
if (!empty($_SERVER['HTTP_HOST'])) {
112+
$urlPats[] = [';^/.*civicrm.*ajax.*api4.*Contact.*get;', \Civi::url('backend://civicrm/ajax/api4/Contact/get', 'r')];
113+
}
114+
115+
$this->assertNotEmpty($urlPats);
116+
foreach ($urlPats as $urlPat) {
117+
$this->assertRegExp($urlPat[0], $urlPat[1]);
118+
}
119+
}
120+
71121
/**
72122
* @param string $expectContentRegex
73123
* @param string $url

0 commit comments

Comments
 (0)