Merge pull request #15339 from seamuslee001/hook_before_boot
[civicrm-core.git] / tests / phpunit / CRM / Utils / SystemTest.php
CommitLineData
6a488035
TO
1<?php
2
f9bdf062 3
aba1cd8b
EM
4/**
5 * Class CRM_Utils_SystemTest
acb109b7 6 * @group headless
aba1cd8b 7 */
6a488035
TO
8class CRM_Utils_SystemTest extends CiviUnitTestCase {
9
00be9182 10 public function setUp() {
6a488035
TO
11 parent::setUp();
12 }
13
00be9182 14 public function testUrlQueryString() {
6a488035
TO
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';
6c6e6187 18 $actual = CRM_Utils_System::url('civicrm/foo/bar', 'foo=ab&bar=cd%26ef', FALSE, NULL, FALSE);
6a488035
TO
19 $this->assertEquals($expected, $actual);
20 }
b6708aeb 21
00be9182 22 public function testUrlQueryArray() {
6a488035
TO
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';
9099cab3 26 $actual = CRM_Utils_System::url('civicrm/foo/bar', [
6a488035
TO
27 'foo' => 'ab',
28 'bar' => 'cd&ef',
9099cab3 29 ], FALSE, NULL, FALSE);
6a488035
TO
30 $this->assertEquals($expected, $actual);
31 }
efceedd4
TO
32
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}'));
37 }
96025800 38
f9bdf062 39 /**
40 * Test the redirect hook.
41 *
42 * @param string $url
43 * @param array $parsedUrl
44 *
45 * @dataProvider getURLs
46 */
47 public function testRedirectHook($url, $parsedUrl) {
9099cab3 48 $this->hookClass->setHook('civicrm_alterRedirect', [$this, 'hook_civicrm_alterRedirect']);
f9bdf062 49 try {
50 CRM_Utils_System::redirect($url, [
51 'expected' => $parsedUrl,
39b959db 52 'original' => $url,
f9bdf062 53 ]);
54 }
55 catch (CRM_Core_Exception $e) {
56 $this->assertEquals(ts('hook called'), $e->getMessage());
57 return;
58 }
59 $this->fail('Exception should have been thrown if hook was called');
60 }
61
62 /**
63 * Hook for alterRedirect.
64 *
65 * We do some checks here.
66 *
39b959db 67 * @param \Psr\Http\Message\UriInterface $urlQuery
f9bdf062 68 * @param array $context
69 *
70 * @throws \CRM_Core_Exception
71 */
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));
77
78 throw new CRM_Core_Exception(ts('hook called'));
79 }
80
81 /**
82 * Get urls for testing.
83 *
84 * @return array
85 */
86 public function getURLs() {
87 return [
39b9dbb3
TO
88 [
89 'https://example.com?ab=cd',
90 [
91 'scheme' => 'https',
92 'host' => 'example.com',
93 'query' => 'ab=cd',
94 ],
39b959db 95 ],
39b9dbb3
TO
96 [
97 'http://myuser:mypass@foo.bar:123/whiz?a=b&c=d',
98 [
99 'scheme' => 'http',
100 'host' => 'foo.bar',
101 'port' => 123,
102 'user' => 'myuser',
103 'pass' => 'mypass',
104 'path' => '/whiz',
105 'query' => 'a=b&c=d',
106 ],
39b959db 107 ],
39b9dbb3
TO
108 [
109 '/foo/bar',
110 [
111 'path' => '/foo/bar',
112 ],
39b959db 113 ],
f9bdf062 114 ];
115 }
116
a0b08c60
D
117 /**
118 * Demonstrate the, um, "flexibility" of isNull
119 */
120 public function testIsNull() {
121 $this->assertTrue(CRM_Utils_System::isNull(NULL));
122 $this->assertTrue(CRM_Utils_System::isNull(''));
123 $this->assertTrue(CRM_Utils_System::isNull('null'));
124 // Not sure how to test this one because phpunit itself throws an error.
125 // $this->assertTrue(CRM_Utils_System::isNull($someUnsetVariable));
126
127 // but...
128 $this->assertFalse(CRM_Utils_System::isNull('NULL'));
129 $this->assertFalse(CRM_Utils_System::isNull('Null'));
130
131 // probably ok?
132 $this->assertTrue(CRM_Utils_System::isNull([]));
133
134 // ok
135 $this->assertFalse(CRM_Utils_System::isNull(0));
136
137 // sure
138 $arr = [
139 1 => NULL,
140 ];
141 $this->assertTrue(CRM_Utils_System::isNull($arr[1]));
142 $this->assertTrue(CRM_Utils_System::isNull($arr));
143
144 // but then a little confusing
145 $arr = [
146 'IN' => NULL,
147 ];
148 $this->assertFalse(CRM_Utils_System::isNull($arr));
149
150 // now just guessing
151 $obj = new StdClass();
152 $this->assertFalse(CRM_Utils_System::isNull($obj));
153 $obj->anything = NULL;
154 $this->assertFalse(CRM_Utils_System::isNull($obj));
155
156 // this is ok
157 $arr = [
158 1 => [
159 'foo' => 'bar',
160 ],
161 2 => [
162 'a' => NULL,
163 ],
164 ];
165 $this->assertFalse(CRM_Utils_System::isNull($arr));
166
167 $arr = [
168 1 => $obj,
169 ];
170 $this->assertFalse(CRM_Utils_System::isNull($arr));
171
172 // sure
173 $arr = [
174 1 => NULL,
175 2 => '',
176 3 => 'null',
177 ];
178 $this->assertTrue(CRM_Utils_System::isNull($arr));
179 }
180
6a488035 181}