Merge pull request #13867 from jitendrapurohit/paypalpro
[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 $cases = array(); // array(0 => $input, 1 => $expectedOutput)
135 $cases[] = array(TRUE, TRUE);
136 $cases[] = array(FALSE, FALSE);
137 $cases[] = array(1, TRUE);
138 $cases[] = array(0, FALSE);
139 $cases[] = array('1', TRUE);
140 $cases[] = array('0', FALSE);
141 $cases[] = array(TRUE, TRUE);
142 $cases[] = array(FALSE, FALSE);
143 $cases[] = array('Y', TRUE);
144 $cases[] = array('N', FALSE);
145 $cases[] = array('y', TRUE);
146 $cases[] = array('n', FALSE);
147 $cases[] = array('Yes', TRUE);
148 $cases[] = array('No', FALSE);
149 $cases[] = array('True', TRUE);
150 $cases[] = array('False', FALSE);
151 $cases[] = array('yEs', TRUE);
152 $cases[] = array('nO', FALSE);
153 $cases[] = array('tRuE', TRUE);
154 $cases[] = array('FaLsE', FALSE);
155 return $cases;
156 }
157
158 /**
159 * @param $input
160 * @param bool $expected
161 * * @dataProvider booleanDataProvider
162 */
163 public function testStrToBool($input, $expected) {
164 $actual = CRM_Utils_String::strtobool($input);
165 $this->assertTrue($expected === $actual);
166 }
167
168 public function startEndCases() {
169 $cases = array();
170 $cases[] = array('startsWith', 'foo', '', TRUE);
171 $cases[] = array('startsWith', 'foo', 'f', TRUE);
172 $cases[] = array('startsWith', 'foo', 'fo', TRUE);
173 $cases[] = array('startsWith', 'foo', 'foo', TRUE);
174 $cases[] = array('startsWith', 'foo', 'fooo', FALSE);
175 $cases[] = array('startsWith', 'foo', 'o', FALSE);
176 $cases[] = array('endsWith', 'foo', 'f', FALSE);
177 $cases[] = array('endsWith', 'foo', '', TRUE);
178 $cases[] = array('endsWith', 'foo', 'o', TRUE);
179 $cases[] = array('endsWith', 'foo', 'oo', TRUE);
180 $cases[] = array('endsWith', 'foo', 'foo', TRUE);
181 $cases[] = array('endsWith', 'foo', 'fooo', FALSE);
182 $cases[] = array('endsWith', 'foo*', '*', TRUE);
183 return $cases;
184 }
185
186 /**
187 * @param string $func
188 * One of: 'startsWith' or 'endsWith'.
189 * @param $string
190 * @param $fragment
191 * @param $expectedResult
192 * @dataProvider startEndCases
193 */
194 public function testStartEndWith($func, $string, $fragment, $expectedResult) {
195 $actualResult = \CRM_Utils_String::$func($string, $fragment);
196 $this->assertEquals($expectedResult, $actualResult, "Checking $func($string,$fragment)");
197 }
198
199 public function wildcardCases() {
200 $cases = array();
201 $cases[] = array('*', array('foo.bar.1', 'foo.bar.2', 'foo.whiz', 'bang.bang'));
202 $cases[] = array('foo.*', array('foo.bar.1', 'foo.bar.2', 'foo.whiz'));
203 $cases[] = array('foo.bar.*', array('foo.bar.1', 'foo.bar.2'));
204 $cases[] = array(array('foo.bar.*', 'foo.bar.2'), array('foo.bar.1', 'foo.bar.2'));
205 $cases[] = array(array('foo.bar.2', 'foo.w*'), array('foo.bar.2', 'foo.whiz'));
206 return $cases;
207 }
208
209 /**
210 * @param $patterns
211 * @param $expectedResults
212 * @dataProvider wildcardCases
213 */
214 public function testFilterByWildCards($patterns, $expectedResults) {
215 $data = array('foo.bar.1', 'foo.bar.2', 'foo.whiz', 'bang.bang');
216
217 $actualResults = CRM_Utils_String::filterByWildcards($patterns, $data);
218 $this->assertEquals($expectedResults, $actualResults);
219
220 $patterns = (array) $patterns;
221 $patterns[] = 'noise';
222
223 $actualResults = CRM_Utils_String::filterByWildcards($patterns, $data, FALSE);
224 $this->assertEquals($expectedResults, $actualResults);
225
226 $actualResults = CRM_Utils_String::filterByWildcards($patterns, $data, TRUE);
227 $this->assertEquals(array_merge($expectedResults, array('noise')), $actualResults);
228 }
229
230 /**
231 * CRM-20821
232 * CRM-14283
233 *
234 * @param string $imageURL
235 * @param book $forceHttps
236 * @param string $expected
237 *
238 * @dataProvider simplifyURLProvider
239 */
240 public function testSimplifyURL($imageURL, $forceHttps, $expected) {
241 $this->assertEquals(
242 $expected,
243 CRM_Utils_String::simplifyURL($imageURL, $forceHttps)
244 );
245 }
246
247 /**
248 * Used for testNormalizeImageURL above
249 *
250 * @return array
251 */
252 public function simplifyURLProvider() {
253 $config = CRM_Core_Config::singleton();
254 $urlParts = CRM_Utils_String::simpleParseUrl($config->userFrameworkBaseURL);
255 $localDomain = $urlParts['host+port'];
256 if (empty($localDomain)) {
257 throw new \Exception("Failed to determine local base URL");
258 }
259 $externalDomain = 'example.org';
260
261 // Ensure that $externalDomain really is different from $localDomain
262 if ($externalDomain == $localDomain) {
263 $externalDomain = 'example.net';
264 }
265
266 return array(
267 'prototypical example' => array(
268 "https://$localDomain/sites/default/files/coffee-mug.jpg",
269 FALSE,
270 '/sites/default/files/coffee-mug.jpg',
271 ),
272 'external domain with https' => array(
273 "https://$externalDomain/sites/default/files/coffee-mug.jpg",
274 FALSE,
275 "https://$externalDomain/sites/default/files/coffee-mug.jpg",
276 ),
277 'external domain with http forced to https' => array(
278 "http://$externalDomain/sites/default/files/coffee-mug.jpg",
279 TRUE,
280 "https://$externalDomain/sites/default/files/coffee-mug.jpg",
281 ),
282 'external domain with http not forced' => array(
283 "http://$externalDomain/sites/default/files/coffee-mug.jpg",
284 FALSE,
285 "http://$externalDomain/sites/default/files/coffee-mug.jpg",
286 ),
287 'local URL' => array(
288 "/sites/default/files/coffee-mug.jpg",
289 FALSE,
290 "/sites/default/files/coffee-mug.jpg",
291 ),
292 'local URL without a forward slash' => array(
293 "sites/default/files/coffee-mug.jpg",
294 FALSE,
295 "/sites/default/files/coffee-mug.jpg",
296 ),
297 'empty input' => array(
298 '',
299 FALSE,
300 '',
301 ),
302 );
303 }
304
305 /**
306 * @param string $url
307 * @param array $expected
308 *
309 * @dataProvider parseURLProvider
310 */
311 public function testSimpleParseUrl($url, $expected) {
312 $this->assertEquals(
313 $expected,
314 CRM_Utils_String::simpleParseUrl($url)
315 );
316 }
317
318 /**
319 * Used for testSimpleParseUrl above
320 *
321 * @return array
322 */
323 public function parseURLProvider() {
324 return array(
325 "prototypical example" => array(
326 "https://example.com:8000/foo/bar/?id=1#fragment",
327 array(
328 'host+port' => "example.com:8000",
329 'path+query' => "/foo/bar/?id=1",
330 ),
331 ),
332 "default port example" => array(
333 "https://example.com/foo/bar/?id=1#fragment",
334 array(
335 'host+port' => "example.com",
336 'path+query' => "/foo/bar/?id=1",
337 ),
338 ),
339 "empty" => array(
340 "",
341 array(
342 'host+port' => "",
343 'path+query' => "",
344 ),
345 ),
346 "path only" => array(
347 "/foo/bar/image.png",
348 array(
349 'host+port' => "",
350 'path+query' => "/foo/bar/image.png",
351 ),
352 ),
353 );
354 }
355
356 public function purifyHTMLProvider() {
357 $tests = [];
358 $tests[] = ['<span onmouseover=alert(0)>HOVER</span>', '<span>HOVER</span>'];
359 $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>'];
360 return $tests;
361 }
362
363 /**
364 * Test ouput of purifyHTML
365 * @param string $testString
366 * @param string $expectedString
367 * @dataProvider purifyHTMLProvider
368 */
369 public function testPurifyHTML($testString, $expectedString) {
370 $this->assertEquals($expectedString, CRM_Utils_String::purifyHTML($testString));
371 }
372
373 }