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