Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
aba1cd8b EM |
2 | |
3 | /** | |
4 | * Class CRM_Utils_StringTest | |
acb109b7 | 5 | * @group headless |
aba1cd8b | 6 | */ |
6a488035 | 7 | class CRM_Utils_StringTest extends CiviUnitTestCase { |
6a488035 | 8 | |
00be9182 | 9 | public function setUp() { |
6a488035 TO |
10 | parent::setUp(); |
11 | } | |
12 | ||
00be9182 | 13 | public function testStripPathChars() { |
6a488035 TO |
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 | ||
6a488035 TO |
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 | ||
00be9182 | 30 | public function testExtractName() { |
6a488035 TO |
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']); | |
e65f9c8f E |
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)); | |
6a488035 TO |
76 | } |
77 | } | |
78 | ||
00be9182 | 79 | public function testEllipsify() { |
6a488035 TO |
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)); | |
7a0ea0f3 | 88 | } |
69a8e7e6 | 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; | |
06abe035 | 92 | $this->assertEquals(TRUE, mb_check_encoding(CRM_Utils_String::ellipsify($input, $maxLen), 'UTF-8')); |
6a488035 TO |
93 | } |
94 | ||
00be9182 | 95 | public function testRandom() { |
6a488035 TO |
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 | } | |
fc7a0aee | 106 | |
4cbe18b8 EM |
107 | /** |
108 | * @return array | |
109 | */ | |
fc7a0aee TO |
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 | |
1e1fdcf6 EM |
121 | * @param $input |
122 | * @param $defaultPrefix | |
123 | * @param $expected | |
fc7a0aee TO |
124 | */ |
125 | public function testParsePrefix($input, $defaultPrefix, $expected) { | |
126 | $actual = CRM_Utils_String::parsePrefix(':', $input, $defaultPrefix); | |
127 | $this->assertEquals($expected, $actual); | |
128 | } | |
a5b8726f | 129 | |
7fe37828 EM |
130 | /** |
131 | * @return array | |
132 | */ | |
00be9182 | 133 | public function booleanDataProvider() { |
a5b8726f TO |
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 | |
5a4f6742 CW |
160 | * @param bool $expected |
161 | * * @dataProvider booleanDataProvider | |
a5b8726f | 162 | */ |
00be9182 | 163 | public function testStrToBool($input, $expected) { |
a5b8726f TO |
164 | $actual = CRM_Utils_String::strtobool($input); |
165 | $this->assertTrue($expected === $actual); | |
166 | } | |
167 | ||
83d511e6 TO |
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 | ||
6c094ca6 SM |
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() { | |
6c094ca6 | 253 | $config = CRM_Core_Config::singleton(); |
e0d4afb7 SM |
254 | $urlParts = parse_url($config->userFrameworkBaseURL); |
255 | $localDomain = $urlParts['host']; | |
6c094ca6 SM |
256 | $externalDomain = 'example.org'; |
257 | ||
258 | // Ensure that $externalDomain really is different from $localDomain | |
259 | if ($externalDomain == $localDomain) { | |
260 | $externalDomain = 'example.net'; | |
261 | } | |
262 | ||
263 | return array( | |
bb80d2f9 | 264 | 'prototypical example' => array( |
6c094ca6 SM |
265 | "https://$localDomain/sites/default/files/coffee-mug.jpg", |
266 | FALSE, | |
267 | '/sites/default/files/coffee-mug.jpg', | |
268 | ), | |
bb80d2f9 | 269 | 'external domain with https' => array( |
6c094ca6 SM |
270 | "https://$externalDomain/sites/default/files/coffee-mug.jpg", |
271 | FALSE, | |
272 | "https://$externalDomain/sites/default/files/coffee-mug.jpg", | |
273 | ), | |
bb80d2f9 | 274 | 'external domain with http forced to https' => array( |
6c094ca6 SM |
275 | "http://$externalDomain/sites/default/files/coffee-mug.jpg", |
276 | TRUE, | |
277 | "https://$externalDomain/sites/default/files/coffee-mug.jpg", | |
278 | ), | |
bb80d2f9 | 279 | 'external domain with http not forced' => array( |
6c094ca6 SM |
280 | "http://$externalDomain/sites/default/files/coffee-mug.jpg", |
281 | FALSE, | |
282 | "http://$externalDomain/sites/default/files/coffee-mug.jpg", | |
283 | ), | |
bb80d2f9 | 284 | 'local URL' => array( |
6c094ca6 SM |
285 | "/sites/default/files/coffee-mug.jpg", |
286 | FALSE, | |
287 | "/sites/default/files/coffee-mug.jpg", | |
288 | ), | |
bb80d2f9 | 289 | 'local URL without a forward slash' => array( |
6c094ca6 SM |
290 | "sites/default/files/coffee-mug.jpg", |
291 | FALSE, | |
292 | "/sites/default/files/coffee-mug.jpg", | |
293 | ), | |
bb80d2f9 | 294 | 'empty input' => array( |
6c094ca6 SM |
295 | '', |
296 | FALSE, | |
297 | '', | |
298 | ), | |
299 | ); | |
300 | } | |
301 | ||
302 | /** | |
303 | * @param string $url | |
304 | * @param array $expected | |
305 | * | |
306 | * @dataProvider parseURLProvider | |
307 | */ | |
308 | public function testSimpleParseUrl($url, $expected) { | |
309 | $this->assertEquals( | |
310 | $expected, | |
311 | CRM_Utils_String::simpleParseUrl($url) | |
312 | ); | |
313 | } | |
314 | ||
315 | /** | |
316 | * Used for testSimpleParseUrl above | |
317 | * | |
318 | * @return array | |
319 | */ | |
320 | public function parseURLProvider() { | |
321 | return array( | |
bb80d2f9 | 322 | "prototypical example" => array( |
6c094ca6 SM |
323 | "https://example.com:8000/foo/bar/?id=1#fragment", |
324 | array( | |
325 | 'host+port' => "example.com:8000", | |
326 | 'path+query' => "/foo/bar/?id=1", | |
327 | ), | |
328 | ), | |
bb80d2f9 | 329 | "empty" => array( |
6c094ca6 SM |
330 | "", |
331 | array( | |
332 | 'host+port' => "", | |
333 | 'path+query' => "", | |
334 | ), | |
335 | ), | |
bb80d2f9 | 336 | "path only" => array( |
6c094ca6 SM |
337 | "/foo/bar/image.png", |
338 | array( | |
339 | 'host+port' => "", | |
340 | 'path+query' => "/foo/bar/image.png", | |
341 | ), | |
342 | ), | |
343 | ); | |
344 | } | |
345 | ||
6a488035 | 346 | } |