5 * Class CRM_Utils_SystemTest
8 class CRM_Utils_SystemTest
extends CiviUnitTestCase
{
10 public function setUp() {
14 public function testUrlQueryString() {
15 $config = CRM_Core_Config
::singleton();
16 $this->assertTrue($config->userSystem
instanceof CRM_Utils_System_UnitTests
);
17 $expected = '/index.php?q=civicrm/foo/bar&foo=ab&bar=cd%26ef';
18 $actual = CRM_Utils_System
::url('civicrm/foo/bar', 'foo=ab&bar=cd%26ef', FALSE, NULL, FALSE);
19 $this->assertEquals($expected, $actual);
22 public function testUrlQueryArray() {
23 $config = CRM_Core_Config
::singleton();
24 $this->assertTrue($config->userSystem
instanceof CRM_Utils_System_UnitTests
);
25 $expected = '/index.php?q=civicrm/foo/bar&foo=ab&bar=cd%26ef';
26 $actual = CRM_Utils_System
::url('civicrm/foo/bar', array(
29 ), FALSE, NULL, FALSE);
30 $this->assertEquals($expected, $actual);
33 public function testEvalUrl() {
34 $this->assertEquals(FALSE, CRM_Utils_System
::evalUrl(FALSE));
35 $this->assertEquals('http://example.com/', CRM_Utils_System
::evalUrl('http://example.com/'));
36 $this->assertEquals('http://example.com/?cms=UnitTests', CRM_Utils_System
::evalUrl('http://example.com/?cms={uf}'));
40 * Test the redirect hook.
43 * @param array $parsedUrl
45 * @dataProvider getURLs
47 public function testRedirectHook($url, $parsedUrl) {
48 $this->hookClass
->setHook('civicrm_alterRedirect', array($this, 'hook_civicrm_alterRedirect'));
50 CRM_Utils_System
::redirect($url, [
51 'expected' => $parsedUrl,
55 catch (CRM_Core_Exception
$e) {
56 $this->assertEquals(ts('hook called'), $e->getMessage());
59 $this->fail('Exception should have been thrown if hook was called');
63 * Hook for alterRedirect.
65 * We do some checks here.
67 * @param \Psr\Http\Message\UriInterface $urlQuery
68 * @param array $context
70 * @throws \CRM_Core_Exception
72 public function hook_civicrm_alterRedirect($urlQuery, $context) {
73 $this->assertEquals(CRM_Utils_Array
::value('scheme', $context['expected']), $urlQuery->getScheme());
74 $this->assertEquals(CRM_Utils_Array
::value('host', $context['expected']), $urlQuery->getHost());
75 $this->assertEquals(CRM_Utils_Array
::value('query', $context['expected']), $urlQuery->getQuery());
76 $this->assertEquals($context['original'], CRM_Utils_Url
::unparseUrl($urlQuery));
78 throw new CRM_Core_Exception(ts('hook called'));
82 * Get urls for testing.
86 public function getURLs() {
89 'https://example.com?ab=cd',
92 'host' => 'example.com',
97 'http://myuser:mypass@foo.bar:123/whiz?a=b&c=d',
105 'query' => 'a=b&c=d',
111 'path' => '/foo/bar',