Merge pull request #15881 from civicrm/5.20
[civicrm-core.git] / tests / phpunit / CRM / Core / ResourcesTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Tests for linking to resource files
14 * @group headless
15 */
16 class CRM_Core_ResourcesTest extends CiviUnitTestCase {
17
18 /**
19 * @var CRM_Core_Resources
20 */
21 protected $res;
22
23 /**
24 * @var CRM_Extension_Mapper
25 */
26 protected $mapper;
27
28 /**
29 * @var string for testing cache buster generation
30 */
31 protected $cacheBusterString = 'xBkdk3';
32
33 protected $originalRequest;
34 protected $originalGet;
35
36 public function setUp() {
37 parent::setUp();
38
39 list ($this->basedir, $this->container, $this->mapper) = $this->_createMapper();
40 $cache = new CRM_Utils_Cache_Arraycache([]);
41 $this->res = new CRM_Core_Resources($this->mapper, $cache, NULL);
42 $this->res->setCacheCode('resTest');
43 CRM_Core_Resources::singleton($this->res);
44
45 // Templates injected into regions should normally be file names, but for unit-testing it's handy to use "string:" notation
46 require_once 'CRM/Core/Smarty/resources/String.php';
47 civicrm_smarty_register_string_resource();
48
49 $this->originalRequest = $_REQUEST;
50 $this->originalGet = $_GET;
51 }
52
53 /**
54 * Restore globals so this test doesn't interfere with others.
55 */
56 public function tearDown() {
57 $_REQUEST = $this->originalRequest;
58 $_GET = $this->originalGet;
59 }
60
61 public function testAddScriptFile() {
62 $this->res
63 ->addScriptFile('com.example.ext', 'foo%20bar.js', 0, 'testAddScriptFile')
64 // extra
65 ->addScriptFile('com.example.ext', 'foo%20bar.js', 0, 'testAddScriptFile')
66 ->addScriptFile('civicrm', 'foo%20bar.js', 0, 'testAddScriptFile');
67
68 $smarty = CRM_Core_Smarty::singleton();
69 $actual = $smarty->fetch('string:{crmRegion name=testAddScriptFile}{/crmRegion}');
70 // stable ordering: alphabetical by (snippet.weight,snippet.name)
71 $expected = ""
72 . "<script type=\"text/javascript\" src=\"http://core-app/foo%20bar.js?r=resTest\">\n</script>\n"
73 . "<script type=\"text/javascript\" src=\"http://ext-dir/com.example.ext/foo%20bar.js?r=resTest\">\n</script>\n";
74 $this->assertEquals($expected, $actual);
75 }
76
77 /**
78 * When adding a script file, any ts() expressions should be translated and added to the 'strings'
79 *
80 * FIXME: This can't work because the tests run in English and CRM_Core_Resources optimizes
81 * away the English data from $settings['strings']
82 * public function testAddScriptFile_strings() {
83 * file_put_contents($this->mapper->keyToBasePath('com.example.ext') . '/hello.js', 'alert(ts("Hello world"));');
84 * $this->res->addScriptFile('com.example.ext', 'hello.js', 0, 'testAddScriptFile_strings');
85 * $settings = $this->res->getSettings();
86 * $expected = array('Hello world');
87 * $this->assertEquals($expected, $settings['strings']);
88 * }
89 */
90
91 /**
92 * Ensure that adding a script URL creates expected markup.
93 */
94 public function testAddScriptURL() {
95 $this->res
96 ->addScriptUrl('/whiz/foo%20bar.js', 0, 'testAddScriptURL')
97 // extra
98 ->addScriptUrl('/whiz/foo%20bar.js', 0, 'testAddScriptURL')
99 ->addScriptUrl('/whizbang/foo%20bar.js', 0, 'testAddScriptURL');
100
101 $smarty = CRM_Core_Smarty::singleton();
102 $actual = $smarty->fetch('string:{crmRegion name=testAddScriptURL}{/crmRegion}');
103 // stable ordering: alphabetical by (snippet.weight,snippet.name)
104 $expected = ""
105 . "<script type=\"text/javascript\" src=\"/whiz/foo%20bar.js\">\n</script>\n"
106 . "<script type=\"text/javascript\" src=\"/whizbang/foo%20bar.js\">\n</script>\n";
107 $this->assertEquals($expected, $actual);
108 }
109
110 public function testAddScript() {
111 $this->res
112 ->addScript('alert("hi");', 0, 'testAddScript')
113 ->addScript('alert("there");', 0, 'testAddScript');
114
115 $smarty = CRM_Core_Smarty::singleton();
116 $actual = $smarty->fetch('string:{crmRegion name=testAddScript}{/crmRegion}');
117 $expected = ""
118 . "<script type=\"text/javascript\">\nalert(\"hi\");\n</script>\n"
119 . "<script type=\"text/javascript\">\nalert(\"there\");\n</script>\n";
120 $this->assertEquals($expected, $actual);
121 }
122
123 public function testAddVars() {
124 $this->res
125 ->addVars('food', ['fruit' => ['mine' => 'apple', 'ours' => 'banana']])
126 ->addVars('food', ['fruit' => ['mine' => 'new apple', 'yours' => 'orange']]);
127 $this->assertTreeEquals(
128 [
129 'vars' => [
130 'food' => [
131 'fruit' => [
132 'yours' => 'orange',
133 'mine' => 'new apple',
134 'ours' => 'banana',
135 ],
136 ],
137 ],
138 ],
139 $this->res->getSettings()
140 );
141 }
142
143 public function testAddSetting() {
144 $this->res
145 ->addSetting(['fruit' => ['mine' => 'apple']])
146 ->addSetting(['fruit' => ['yours' => 'orange']]);
147 $this->assertTreeEquals(
148 ['fruit' => ['yours' => 'orange', 'mine' => 'apple']],
149 $this->res->getSettings()
150 );
151 $actual = $this->res->renderSetting();
152 $expected = json_encode(['fruit' => ['yours' => 'orange', 'mine' => 'apple']]);
153 $this->assertTrue(strpos($actual, $expected) !== FALSE);
154 }
155
156 public function testAddSettingHook() {
157 $test = $this;
158 Civi::dispatcher()->addListener('hook_civicrm_alterResourceSettings', function($event) use ($test) {
159 $test->assertEquals('apple', $event->data['fruit']['mine']);
160 $event->data['fruit']['mine'] = 'banana';
161 });
162 $this->res->addSetting(['fruit' => ['mine' => 'apple']]);
163 $settings = $this->res->getSettings();
164 $this->assertTreeEquals(['fruit' => ['mine' => 'banana']], $settings);
165 }
166
167 public function testAddSettingFactory() {
168 $this->res->addSettingsFactory(function () {
169 return ['fruit' => ['yours' => 'orange']];
170 });
171 $this->res->addSettingsFactory(function () {
172 return ['fruit' => ['mine' => 'apple']];
173 });
174
175 $actual = $this->res->getSettings();
176 $expected = ['fruit' => ['yours' => 'orange', 'mine' => 'apple']];
177 $this->assertTreeEquals($expected, $actual);
178 }
179
180 public function testAddSettingAndSettingFactory() {
181 $this->res->addSetting(['fruit' => ['mine' => 'apple']]);
182
183 $muckableValue = ['fruit' => ['yours' => 'orange', 'theirs' => 'apricot']];
184 $this->res->addSettingsFactory(function () use (&$muckableValue) {
185 return $muckableValue;
186 });
187 $actual = $this->res->getSettings();
188 $expected = ['fruit' => ['mine' => 'apple', 'yours' => 'orange', 'theirs' => 'apricot']];
189 $this->assertTreeEquals($expected, $actual);
190
191 // note: the setting is not fixed based on what the factory returns when registered; it's based
192 // on what the factory returns when getSettings is called
193 $muckableValue = ['fruit' => ['yours' => 'banana']];
194 $actual = $this->res->getSettings();
195 $expected = ['fruit' => ['mine' => 'apple', 'yours' => 'banana']];
196 $this->assertTreeEquals($expected, $actual);
197 }
198
199 public function testCrmJS() {
200 $smarty = CRM_Core_Smarty::singleton();
201
202 $actual = $smarty->fetch('string:{crmScript ext=com.example.ext file=foo%20bar.js region=testCrmJS}');
203 $this->assertEquals('', $actual);
204
205 $actual = $smarty->fetch('string:{crmScript url=/whiz/foo%20bar.js region=testCrmJS weight=1}');
206 $this->assertEquals('', $actual);
207
208 $actual = $smarty->fetch('string:{crmRegion name=testCrmJS}{/crmRegion}');
209 // stable ordering: alphabetical by (snippet.weight,snippet.name)
210 $expected = ""
211 . "<script type=\"text/javascript\" src=\"http://ext-dir/com.example.ext/foo%20bar.js?r=resTest\">\n</script>\n"
212 . "<script type=\"text/javascript\" src=\"/whiz/foo%20bar.js\">\n</script>\n";
213 $this->assertEquals($expected, $actual);
214 }
215
216 public function testAddStyleFile() {
217 $this->res
218 ->addStyleFile('com.example.ext', 'foo%20bar.css', 0, 'testAddStyleFile')
219 // extra
220 ->addStyleFile('com.example.ext', 'foo%20bar.css', 0, 'testAddStyleFile')
221 ->addStyleFile('civicrm', 'foo%20bar.css', 0, 'testAddStyleFile');
222
223 $smarty = CRM_Core_Smarty::singleton();
224 $actual = $smarty->fetch('string:{crmRegion name=testAddStyleFile}{/crmRegion}');
225 // stable ordering: alphabetical by (snippet.weight,snippet.name)
226 $expected = ""
227 . "<link href=\"http://core-app/foo%20bar.css?r=resTest\" rel=\"stylesheet\" type=\"text/css\"/>\n"
228 . "<link href=\"http://ext-dir/com.example.ext/foo%20bar.css?r=resTest\" rel=\"stylesheet\" type=\"text/css\"/>\n";
229 $this->assertEquals($expected, $actual);
230 }
231
232 public function testAddStyleURL() {
233 $this->res
234 ->addStyleUrl('/whiz/foo%20bar.css', 0, 'testAddStyleURL')
235 // extra
236 ->addStyleUrl('/whiz/foo%20bar.css', 0, 'testAddStyleURL')
237 ->addStyleUrl('/whizbang/foo%20bar.css', 0, 'testAddStyleURL');
238
239 $smarty = CRM_Core_Smarty::singleton();
240 $actual = $smarty->fetch('string:{crmRegion name=testAddStyleURL}{/crmRegion}');
241 // stable ordering: alphabetical by (snippet.weight,snippet.name)
242 $expected = ""
243 . "<link href=\"/whiz/foo%20bar.css\" rel=\"stylesheet\" type=\"text/css\"/>\n"
244 . "<link href=\"/whizbang/foo%20bar.css\" rel=\"stylesheet\" type=\"text/css\"/>\n";
245 $this->assertEquals($expected, $actual);
246 }
247
248 public function testAddStyle() {
249 $this->res
250 ->addStyle('body { background: black; }', 0, 'testAddStyle')
251 ->addStyle('body { text-color: black; }', 0, 'testAddStyle');
252
253 $smarty = CRM_Core_Smarty::singleton();
254 $actual = $smarty->fetch('string:{crmRegion name=testAddStyle}{/crmRegion}');
255 $expected = ""
256 . "<style type=\"text/css\">\nbody { background: black; }\n</style>\n"
257 . "<style type=\"text/css\">\nbody { text-color: black; }\n</style>\n";
258 $this->assertEquals($expected, $actual);
259 }
260
261 public function testCrmCSS() {
262 $smarty = CRM_Core_Smarty::singleton();
263
264 $actual = $smarty->fetch('string:{crmStyle ext=com.example.ext file=foo%20bar.css region=testCrmCSS}');
265 $this->assertEquals('', $actual);
266
267 $actual = $smarty->fetch('string:{crmStyle url=/whiz/foo%20bar.css region=testCrmCSS weight=1}');
268 $this->assertEquals('', $actual);
269
270 $actual = $smarty->fetch('string:{crmRegion name=testCrmCSS}{/crmRegion}');
271 // stable ordering: alphabetical by (snippet.weight,snippet.name)
272 $expected = ""
273 . "<link href=\"http://ext-dir/com.example.ext/foo%20bar.css?r=resTest\" rel=\"stylesheet\" type=\"text/css\"/>\n"
274 . "<link href=\"/whiz/foo%20bar.css\" rel=\"stylesheet\" type=\"text/css\"/>\n";
275 $this->assertEquals($expected, $actual);
276 }
277
278 public function testGetURL() {
279 $this->assertEquals(
280 'http://core-app/dir/file%20name.txt',
281 $this->res->getURL('civicrm', 'dir/file%20name.txt')
282 );
283 $this->assertEquals(
284 'http://ext-dir/com.example.ext/dir/file%20name.txt',
285 $this->res->getURL('com.example.ext', 'dir/file%20name.txt')
286 );
287 $this->assertEquals(
288 'http://core-app/',
289 $this->res->getURL('civicrm')
290 );
291 $this->assertEquals(
292 'http://ext-dir/com.example.ext/',
293 $this->res->getURL('com.example.ext')
294 );
295 }
296
297 public function testCrmResURL() {
298 $smarty = CRM_Core_Smarty::singleton();
299
300 $actual = $smarty->fetch('string:{crmResURL ext=com.example.ext file=foo%20bar.png}');
301 $this->assertEquals('http://ext-dir/com.example.ext/foo%20bar.png', $actual);
302
303 $actual = $smarty->fetch('string:{crmResURL ext=com.example.ext file=foo%20bar.png addCacheCode=1}');
304 $this->assertEquals('http://ext-dir/com.example.ext/foo%20bar.png?r=resTest', $actual);
305
306 $actual = $smarty->fetch('string:{crmResURL ext=com.example.ext}');
307 $this->assertEquals('http://ext-dir/com.example.ext/', $actual);
308 }
309
310 public function testGlob() {
311 $this->assertEquals(
312 ['info.xml'],
313 $this->res->glob('com.example.ext', 'info.xml')
314 );
315 $this->assertEquals(
316 ['js/example.js'],
317 $this->res->glob('com.example.ext', 'js/*.js')
318 );
319 $this->assertEquals(
320 ['js/example.js'],
321 $this->res->glob('com.example.ext', ['js/*.js'])
322 );
323 }
324
325 /**
326 * @dataProvider ajaxModeData
327 */
328 public function testIsAjaxMode($query, $result) {
329 $_REQUEST = $_GET = $query;
330 $this->assertEquals($result, CRM_Core_Resources::isAjaxMode());
331 }
332
333 public function ajaxModeData() {
334 return [
335 [['q' => 'civicrm/ajax/foo'], TRUE],
336 [['q' => 'civicrm/angularprofiles/template'], TRUE],
337 [['q' => 'civicrm/asset/builder'], TRUE],
338 [['q' => 'civicrm/test/page'], FALSE],
339 [['q' => 'civicrm/test/page', 'snippet' => 'json'], TRUE],
340 [['q' => 'civicrm/test/page', 'snippet' => 'foo'], FALSE],
341 ];
342 }
343
344 /**
345 * @param CRM_Utils_Cache_Interface $cache
346 * @param string $cacheKey
347 *
348 * @return array
349 * [string $basedir, CRM_Extension_Container_Interface, CRM_Extension_Mapper]
350 */
351 public function _createMapper(CRM_Utils_Cache_Interface $cache = NULL, $cacheKey = NULL) {
352 $basedir = rtrim($this->createTempDir('ext-'), '/');
353 mkdir("$basedir/com.example.ext");
354 mkdir("$basedir/com.example.ext/js");
355 file_put_contents("$basedir/com.example.ext/info.xml", "<extension key='com.example.ext' type='report'><file>oddball</file></extension>");
356 file_put_contents("$basedir/com.example.ext/js/example.js", "alert('Boo!');");
357 // not needed for now // file_put_contents("$basedir/weird/bar/oddball.php", "<?php\n");
358 $c = new CRM_Extension_Container_Basic($basedir, 'http://ext-dir', $cache, $cacheKey);
359 $mapper = new CRM_Extension_Mapper($c, NULL, NULL, '/pathto/civicrm', 'http://core-app');
360 return [$basedir, $c, $mapper];
361 }
362
363 /**
364 * @param string $url
365 * @param string $expected
366 *
367 * @dataProvider urlForCacheCodeProvider
368 */
369 public function testAddingCacheCode($url, $expected) {
370 $resources = CRM_Core_Resources::singleton();
371 $resources->setCacheCode($this->cacheBusterString);
372 $this->assertEquals($expected, $resources->addCacheCode($url));
373 }
374
375 /**
376 * @return array
377 */
378 public function urlForCacheCodeProvider() {
379 return [
380 [
381 'http://www.civicrm.org',
382 'http://www.civicrm.org?r=' . $this->cacheBusterString,
383 ],
384 [
385 'www.civicrm.org/custom.css?foo=bar',
386 'www.civicrm.org/custom.css?foo=bar&r=' . $this->cacheBusterString,
387 ],
388 [
389 'civicrm.org/custom.css?car=blue&foo=bar',
390 'civicrm.org/custom.css?car=blue&foo=bar&r=' . $this->cacheBusterString,
391 ],
392 ];
393 }
394
395 /**
396 * return array
397 */
398 public function urlsToCheckIfFullyFormed() {
399 return [
400 ['civicrm/test/page', FALSE],
401 ['#', FALSE],
402 ['', FALSE],
403 ['/civicrm/test/page', TRUE],
404 ['http://test.com/civicrm/test/page', TRUE],
405 ['https://test.com/civicrm/test/page', TRUE],
406 ];
407 }
408
409 /**
410 * @param string $url
411 * @param string $expected
412 *
413 * @dataProvider urlsToCheckIfFullyFormed
414 */
415 public function testIsFullyFormedUrl($url, $expected) {
416 $this->assertEquals($expected, CRM_Core_Resources::isFullyFormedUrl($url));
417 }
418
419 /**
420 * Test for hook_civicrm_entityRefFilters().
421 *
422 */
423 public function testEntityRefFiltersHook() {
424 CRM_Utils_Hook_UnitTests::singleton()->setHook('civicrm_entityRefFilters', [$this, 'entityRefFilters']);
425 $data = CRM_Core_Resources::getEntityRefMetadata();
426 $this->assertEquals(count($data['links']['Contact']), 4);
427 $this->assertEquals(!empty($data['links']['Contact']['new_staff']), TRUE);
428 }
429
430 /**
431 * @param array $filters
432 * @param array $links
433 */
434 public function entityRefFilters(&$filters, &$links) {
435 $links['Contact']['new_staff'] = [
436 'label' => ts('New Staff'),
437 'url' => '/civicrm/profile/create&reset=1&context=dialog&gid=5',
438 'type' => 'Individual',
439 'icon' => 'fa-user',
440 ];
441 }
442
443 }