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', [
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', [$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',
120 public function testExternUrl() {
121 $siteKey = mt_rand();
123 $restUrl = CRM_Utils_System
::externUrl('extern/rest', "entity=Contact&action=get&key=$siteKey&api_key=$apiKey");
124 $this->assertContains('extern/rest.php', $restUrl);
125 $this->assertContains('?', $restUrl);
126 $this->assertContains('entity=Contact', $restUrl);
127 $this->assertContains('action=get', $restUrl);
128 $this->assertContains("key=$siteKey", $restUrl);
129 $this->assertContains("api_key=$apiKey", $restUrl);
133 * Test the alterExternUrl hook.
135 * @param string $path
136 * @param array $expected
138 * @dataProvider getExternURLs
140 public function testAlterExternUrlHook($path, $expected) {
141 Civi
::service('dispatcher')->addListener('hook_civicrm_alterExternUrl', [$this, 'hook_civicrm_alterExternUrl']);
142 $externUrl = CRM_Utils_System
::externUrl($path, $expected['query']);
143 $this->assertContains('path/altered/by/hook', $externUrl, 'Hook failed to alter URL path');
144 $this->assertContains($expected['query'] . '&thisWas=alteredByHook', $externUrl, 'Hook failed to alter URL query');
148 * Hook for alterExternUrl.
150 * @param \Civi\Core\Event\GenericHookEvent $event
151 * @param string $hookName
153 public function hook_civicrm_alterExternUrl(\Civi\Core\Event\GenericHookEvent
$event, $hookName) {
154 $this->assertEquals('hook_civicrm_alterExternUrl', $hookName);
155 $this->assertTrue($event->hasField('url'));
156 $this->assertTrue($event->hasField('path'));
157 $this->assertTrue($event->hasField('query'));
158 $this->assertTrue($event->hasField('fragment'));
159 $this->assertTrue($event->hasField('absolute'));
160 $this->assertTrue($event->hasField('isSSL'));
161 $event->url
= $event->url
->withPath('path/altered/by/hook');
162 $event->url
= $event->url
->withQuery($event->query
. '&thisWas=alteredByHook');
166 * Get extern url params for testing.
170 public function getExternURLs() {
175 'path' => 'extern/url',
176 'query' => 'u=1&qid=1',
182 'path' => 'extern/open',
190 * Demonstrate the, um, "flexibility" of isNull
192 public function testIsNull() {
193 $this->assertTrue(CRM_Utils_System
::isNull(NULL));
194 $this->assertTrue(CRM_Utils_System
::isNull(''));
195 $this->assertTrue(CRM_Utils_System
::isNull('null'));
196 // Not sure how to test this one because phpunit itself throws an error.
197 // $this->assertTrue(CRM_Utils_System::isNull($someUnsetVariable));
200 $this->assertFalse(CRM_Utils_System
::isNull('NULL'));
201 $this->assertFalse(CRM_Utils_System
::isNull('Null'));
204 $this->assertTrue(CRM_Utils_System
::isNull([]));
207 $this->assertFalse(CRM_Utils_System
::isNull(0));
213 $this->assertTrue(CRM_Utils_System
::isNull($arr[1]));
214 $this->assertTrue(CRM_Utils_System
::isNull($arr));
216 // but then a little confusing
220 $this->assertFalse(CRM_Utils_System
::isNull($arr));
223 $obj = new StdClass();
224 $this->assertFalse(CRM_Utils_System
::isNull($obj));
225 $obj->anything
= NULL;
226 $this->assertFalse(CRM_Utils_System
::isNull($obj));
237 $this->assertFalse(CRM_Utils_System
::isNull($arr));
242 $this->assertFalse(CRM_Utils_System
::isNull($arr));
250 $this->assertTrue(CRM_Utils_System
::isNull($arr));