Merge pull request #13986 from seamuslee001/coder_upgrade_uf_tag_sms
[civicrm-core.git] / tests / phpunit / CRM / Utils / StringTest.php
1 <?php
2
3 /**
4 * Class CRM_Utils_StringTest
5 * @group headless
6 */
7 class CRM_Utils_StringTest extends CiviUnitTestCase {
8
9 public function setUp() {
10 parent::setUp();
11 }
12
13 public function testStripPathChars() {
14 $testSet = array(
15 '' => '',
16 NULL => NULL,
17 'civicrm' => 'civicrm',
18 'civicrm/dashboard' => 'civicrm/dashboard',
19 'civicrm/contribute/transact' => 'civicrm/contribute/transact',
20 'civicrm/<hack>attempt</hack>' => 'civicrm/_hack_attempt_/hack_',
21 'civicrm dashboard & force = 1,;' => 'civicrm_dashboard___force___1__',
22 );
23
24 foreach ($testSet as $in => $expected) {
25 $out = CRM_Utils_String::stripPathChars($in);
26 $this->assertEquals($out, $expected, "Output does not match");
27 }
28 }
29
30 public function testExtractName() {
31 $cases = array(
32 array(
33 'full_name' => 'Alan',
34 'first_name' => 'Alan',
35 ),
36 array(
37 'full_name' => 'Alan Arkin',
38 'first_name' => 'Alan',
39 'last_name' => 'Arkin',
40 ),
41 array(
42 'full_name' => '"Alan Arkin"',
43 'first_name' => 'Alan',
44 'last_name' => 'Arkin',
45 ),
46 array(
47 'full_name' => 'Alan A Arkin',
48 'first_name' => 'Alan',
49 'middle_name' => 'A',
50 'last_name' => 'Arkin',
51 ),
52 array(
53 'full_name' => 'Adams, Amy',
54 'first_name' => 'Amy',
55 'last_name' => 'Adams',
56 ),
57 array(
58 'full_name' => 'Adams, Amy A',
59 'first_name' => 'Amy',
60 'middle_name' => 'A',
61 'last_name' => 'Adams',
62 ),
63 array(
64 'full_name' => '"Adams, Amy A"',
65 'first_name' => 'Amy',
66 'middle_name' => 'A',
67 'last_name' => 'Adams',
68 ),
69 );
70 foreach ($cases as $case) {
71 $actual = array();
72 CRM_Utils_String::extractName($case['full_name'], $actual);
73 $this->assertEquals($actual['first_name'], $case['first_name']);
74 $this->assertEquals(CRM_Utils_Array::value('last_name', $actual), CRM_Utils_Array::value('last_name', $case));
75 $this->assertEquals(CRM_Utils_Array::value('middle_name', $actual), CRM_Utils_Array::value('middle_name', $case));
76 }
77 }
78
79 public function testEllipsify() {
80 $maxLen = 5;
81 $cases = array(
82 '1' => '1',
83 '12345' => '12345',
84 '123456' => '12...',
85 );
86 foreach ($cases as $input => $expected) {
87 $this->assertEquals($expected, CRM_Utils_String::ellipsify($input, $maxLen));
88 }
89 // test utf-8 string, CRM-18997
90 $input = 'Registro de eventos on-line: Taller: "Onboarding - Cómo integrar exitosamente a los nuevos talentos dentro de su organización - Formación práctica."';
91 $maxLen = 128;
92 $this->assertEquals(TRUE, mb_check_encoding(CRM_Utils_String::ellipsify($input, $maxLen), 'UTF-8'));
93 }
94
95 public function testRandom() {
96 for ($i = 0; $i < 4; $i++) {
97 $actual = CRM_Utils_String::createRandom(4, 'abc');
98 $this->assertEquals(4, strlen($actual));
99 $this->assertRegExp('/^[abc]+$/', $actual);
100
101 $actual = CRM_Utils_String::createRandom(6, '12345678');
102 $this->assertEquals(6, strlen($actual));
103 $this->assertRegExp('/^[12345678]+$/', $actual);
104 }
105 }
106
107 /**
108 * @return array
109 */
110 public function parsePrefixData() {
111 $cases = array();
112 $cases[] = array('administer CiviCRM', NULL, array(NULL, 'administer CiviCRM'));
113 $cases[] = array('administer CiviCRM', 'com_civicrm', array('com_civicrm', 'administer CiviCRM'));
114 $cases[] = array('Drupal:access user profiles', NULL, array('Drupal', 'access user profiles'));
115 $cases[] = array('Joomla:component:perm', NULL, array('Joomla', 'component:perm'));
116 return $cases;
117 }
118
119 /**
120 * @dataProvider parsePrefixData
121 * @param $input
122 * @param $defaultPrefix
123 * @param $expected
124 */
125 public function testParsePrefix($input, $defaultPrefix, $expected) {
126 $actual = CRM_Utils_String::parsePrefix(':', $input, $defaultPrefix);
127 $this->assertEquals($expected, $actual);
128 }
129
130 /**
131 * @return array
132 */
133 public function booleanDataProvider() {
134 // array(0 => $input, 1 => $expectedOutput)
135 $cases = array();
136 $cases[] = array(TRUE, TRUE);
137 $cases[] = array(FALSE, FALSE);
138 $cases[] = array(1, TRUE);
139 $cases[] = array(0, FALSE);
140 $cases[] = array('1', TRUE);
141 $cases[] = array('0', FALSE);
142 $cases[] = array(TRUE, TRUE);
143 $cases[] = array(FALSE, FALSE);
144 $cases[] = array('Y', TRUE);
145 $cases[] = array('N', FALSE);
146 $cases[] = array('y', TRUE);
147 $cases[] = array('n', FALSE);
148 $cases[] = array('Yes', TRUE);
149 $cases[] = array('No', FALSE);
150 $cases[] = array('True', TRUE);
151 $cases[] = array('False', FALSE);
152 $cases[] = array('yEs', TRUE);
153 $cases[] = array('nO', FALSE);
154 $cases[] = array('tRuE', TRUE);
155 $cases[] = array('FaLsE', FALSE);
156 return $cases;
157 }
158
159 /**
160 * @param $input
161 * @param bool $expected
162 * * @dataProvider booleanDataProvider
163 */
164 public function testStrToBool($input, $expected) {
165 $actual = CRM_Utils_String::strtobool($input);
166 $this->assertTrue($expected === $actual);
167 }
168
169 public function startEndCases() {
170 $cases = array();
171 $cases[] = array('startsWith', 'foo', '', TRUE);
172 $cases[] = array('startsWith', 'foo', 'f', TRUE);
173 $cases[] = array('startsWith', 'foo', 'fo', TRUE);
174 $cases[] = array('startsWith', 'foo', 'foo', TRUE);
175 $cases[] = array('startsWith', 'foo', 'fooo', FALSE);
176 $cases[] = array('startsWith', 'foo', 'o', FALSE);
177 $cases[] = array('endsWith', 'foo', 'f', FALSE);
178 $cases[] = array('endsWith', 'foo', '', TRUE);
179 $cases[] = array('endsWith', 'foo', 'o', TRUE);
180 $cases[] = array('endsWith', 'foo', 'oo', TRUE);
181 $cases[] = array('endsWith', 'foo', 'foo', TRUE);
182 $cases[] = array('endsWith', 'foo', 'fooo', FALSE);
183 $cases[] = array('endsWith', 'foo*', '*', TRUE);
184 return $cases;
185 }
186
187 /**
188 * @param string $func
189 * One of: 'startsWith' or 'endsWith'.
190 * @param $string
191 * @param $fragment
192 * @param $expectedResult
193 * @dataProvider startEndCases
194 */
195 public function testStartEndWith($func, $string, $fragment, $expectedResult) {
196 $actualResult = \CRM_Utils_String::$func($string, $fragment);
197 $this->assertEquals($expectedResult, $actualResult, "Checking $func($string,$fragment)");
198 }
199
200 public function wildcardCases() {
201 $cases = array();
202 $cases[] = array('*', array('foo.bar.1', 'foo.bar.2', 'foo.whiz', 'bang.bang'));
203 $cases[] = array('foo.*', array('foo.bar.1', 'foo.bar.2', 'foo.whiz'));
204 $cases[] = array('foo.bar.*', array('foo.bar.1', 'foo.bar.2'));
205 $cases[] = array(array('foo.bar.*', 'foo.bar.2'), array('foo.bar.1', 'foo.bar.2'));
206 $cases[] = array(array('foo.bar.2', 'foo.w*'), array('foo.bar.2', 'foo.whiz'));
207 return $cases;
208 }
209
210 /**
211 * @param $patterns
212 * @param $expectedResults
213 * @dataProvider wildcardCases
214 */
215 public function testFilterByWildCards($patterns, $expectedResults) {
216 $data = array('foo.bar.1', 'foo.bar.2', 'foo.whiz', 'bang.bang');
217
218 $actualResults = CRM_Utils_String::filterByWildcards($patterns, $data);
219 $this->assertEquals($expectedResults, $actualResults);
220
221 $patterns = (array) $patterns;
222 $patterns[] = 'noise';
223
224 $actualResults = CRM_Utils_String::filterByWildcards($patterns, $data, FALSE);
225 $this->assertEquals($expectedResults, $actualResults);
226
227 $actualResults = CRM_Utils_String::filterByWildcards($patterns, $data, TRUE);
228 $this->assertEquals(array_merge($expectedResults, array('noise')), $actualResults);
229 }
230
231 /**
232 * CRM-20821
233 * CRM-14283
234 *
235 * @param string $imageURL
236 * @param book $forceHttps
237 * @param string $expected
238 *
239 * @dataProvider simplifyURLProvider
240 */
241 public function testSimplifyURL($imageURL, $forceHttps, $expected) {
242 $this->assertEquals(
243 $expected,
244 CRM_Utils_String::simplifyURL($imageURL, $forceHttps)
245 );
246 }
247
248 /**
249 * Used for testNormalizeImageURL above
250 *
251 * @return array
252 */
253 public function simplifyURLProvider() {
254 $config = CRM_Core_Config::singleton();
255 $urlParts = CRM_Utils_String::simpleParseUrl($config->userFrameworkBaseURL);
256 $localDomain = $urlParts['host+port'];
257 if (empty($localDomain)) {
258 throw new \Exception("Failed to determine local base URL");
259 }
260 $externalDomain = 'example.org';
261
262 // Ensure that $externalDomain really is different from $localDomain
263 if ($externalDomain == $localDomain) {
264 $externalDomain = 'example.net';
265 }
266
267 return array(
268 'prototypical example' => array(
269 "https://$localDomain/sites/default/files/coffee-mug.jpg",
270 FALSE,
271 '/sites/default/files/coffee-mug.jpg',
272 ),
273 'external domain with https' => array(
274 "https://$externalDomain/sites/default/files/coffee-mug.jpg",
275 FALSE,
276 "https://$externalDomain/sites/default/files/coffee-mug.jpg",
277 ),
278 'external domain with http forced to https' => array(
279 "http://$externalDomain/sites/default/files/coffee-mug.jpg",
280 TRUE,
281 "https://$externalDomain/sites/default/files/coffee-mug.jpg",
282 ),
283 'external domain with http not forced' => array(
284 "http://$externalDomain/sites/default/files/coffee-mug.jpg",
285 FALSE,
286 "http://$externalDomain/sites/default/files/coffee-mug.jpg",
287 ),
288 'local URL' => array(
289 "/sites/default/files/coffee-mug.jpg",
290 FALSE,
291 "/sites/default/files/coffee-mug.jpg",
292 ),
293 'local URL without a forward slash' => array(
294 "sites/default/files/coffee-mug.jpg",
295 FALSE,
296 "/sites/default/files/coffee-mug.jpg",
297 ),
298 'empty input' => array(
299 '',
300 FALSE,
301 '',
302 ),
303 );
304 }
305
306 /**
307 * @param string $url
308 * @param array $expected
309 *
310 * @dataProvider parseURLProvider
311 */
312 public function testSimpleParseUrl($url, $expected) {
313 $this->assertEquals(
314 $expected,
315 CRM_Utils_String::simpleParseUrl($url)
316 );
317 }
318
319 /**
320 * Used for testSimpleParseUrl above
321 *
322 * @return array
323 */
324 public function parseURLProvider() {
325 return array(
326 "prototypical example" => array(
327 "https://example.com:8000/foo/bar/?id=1#fragment",
328 array(
329 'host+port' => "example.com:8000",
330 'path+query' => "/foo/bar/?id=1",
331 ),
332 ),
333 "default port example" => array(
334 "https://example.com/foo/bar/?id=1#fragment",
335 array(
336 'host+port' => "example.com",
337 'path+query' => "/foo/bar/?id=1",
338 ),
339 ),
340 "empty" => array(
341 "",
342 array(
343 'host+port' => "",
344 'path+query' => "",
345 ),
346 ),
347 "path only" => array(
348 "/foo/bar/image.png",
349 array(
350 'host+port' => "",
351 'path+query' => "/foo/bar/image.png",
352 ),
353 ),
354 );
355 }
356
357 public function purifyHTMLProvider() {
358 $tests = [];
359 $tests[] = ['<span onmouseover=alert(0)>HOVER</span>', '<span>HOVER</span>'];
360 $tests[] = ['<a href="https://civicrm.org" target="_blank" class="button-purple">hello</a>', '<a href="https://civicrm.org" target="_blank" class="button-purple" rel="noreferrer noopener">hello</a>'];
361 return $tests;
362 }
363
364 /**
365 * Test ouput of purifyHTML
366 * @param string $testString
367 * @param string $expectedString
368 * @dataProvider purifyHTMLProvider
369 */
370 public function testPurifyHTML($testString, $expectedString) {
371 $this->assertEquals($expectedString, CRM_Utils_String::purifyHTML($testString));
372 }
373
374 }