3 use Psr\Http\Message\UriInterface
;
6 * Class CRM_Utils_SystemTest
9 class CRM_Utils_SystemTest
extends CiviUnitTestCase
{
11 public function setUp() {
15 public function testUrlQueryString() {
16 $config = CRM_Core_Config
::singleton();
17 $this->assertTrue($config->userSystem
instanceof CRM_Utils_System_UnitTests
);
18 $expected = '/index.php?q=civicrm/foo/bar&foo=ab&bar=cd%26ef';
19 $actual = CRM_Utils_System
::url('civicrm/foo/bar', 'foo=ab&bar=cd%26ef', FALSE, NULL, FALSE);
20 $this->assertEquals($expected, $actual);
23 public function testUrlQueryArray() {
24 $config = CRM_Core_Config
::singleton();
25 $this->assertTrue($config->userSystem
instanceof CRM_Utils_System_UnitTests
);
26 $expected = '/index.php?q=civicrm/foo/bar&foo=ab&bar=cd%26ef';
27 $actual = CRM_Utils_System
::url('civicrm/foo/bar', array(
30 ), FALSE, NULL, FALSE);
31 $this->assertEquals($expected, $actual);
34 public function testEvalUrl() {
35 $this->assertEquals(FALSE, CRM_Utils_System
::evalUrl(FALSE));
36 $this->assertEquals('http://example.com/', CRM_Utils_System
::evalUrl('http://example.com/'));
37 $this->assertEquals('http://example.com/?cms=UnitTests', CRM_Utils_System
::evalUrl('http://example.com/?cms={uf}'));
41 * Test the redirect hook.
44 * @param array $parsedUrl
46 * @dataProvider getURLs
48 public function testRedirectHook($url, $parsedUrl) {
49 $this->hookClass
->setHook('civicrm_alterRedirect', array($this, 'hook_civicrm_alterRedirect'));
51 CRM_Utils_System
::redirect($url, [
52 'expected' => $parsedUrl,
56 catch (CRM_Core_Exception
$e) {
57 $this->assertEquals(ts('hook called'), $e->getMessage());
60 $this->fail('Exception should have been thrown if hook was called');
64 * Hook for alterRedirect.
66 * We do some checks here.
68 * @param UriInterface $urlQuery
69 * @param array $context
71 * @throws \CRM_Core_Exception
73 public function hook_civicrm_alterRedirect($urlQuery, $context) {
74 $this->assertEquals(CRM_Utils_Array
::value('scheme', $context['expected']), $urlQuery->getScheme());
75 $this->assertEquals(CRM_Utils_Array
::value('host', $context['expected']), $urlQuery->getHost());
76 $this->assertEquals(CRM_Utils_Array
::value('query', $context['expected']), $urlQuery->getQuery());
77 $this->assertEquals($context['original'], CRM_Utils_Url
::unparseUrl($urlQuery));
79 throw new CRM_Core_Exception(ts('hook called'));
83 * Get urls for testing.
87 public function getURLs() {
89 ['https://example.com?ab=cd', [
91 'host' => 'example.com',
94 ['http://myuser:mypass@foo.bar:123/whiz?a=b&c=d', [
101 'query' => 'a=b&c=d',