3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
13 * Tests for linking to resource files
16 class CRM_Core_ResourcesTest
extends CiviUnitTestCase
{
19 * @var CRM_Core_Resources
24 * @var CRM_Extension_Mapper
30 * For testing cache buster generation
32 protected $cacheBusterString = 'xBkdk3';
34 protected $originalRequest;
35 protected $originalGet;
37 public function setUp() {
40 list ($this->basedir
, $this->container
, $this->mapper
) = $this->_createMapper();
41 $cache = new CRM_Utils_Cache_Arraycache([]);
42 $this->res
= new CRM_Core_Resources($this->mapper
, $cache, NULL);
43 $this->res
->setCacheCode('resTest');
44 CRM_Core_Resources
::singleton($this->res
);
46 // Templates injected into regions should normally be file names, but for unit-testing it's handy to use "string:" notation
47 require_once 'CRM/Core/Smarty/resources/String.php';
48 civicrm_smarty_register_string_resource();
50 $this->originalRequest
= $_REQUEST;
51 $this->originalGet
= $_GET;
55 * Restore globals so this test doesn't interfere with others.
57 public function tearDown() {
58 $_REQUEST = $this->originalRequest
;
59 $_GET = $this->originalGet
;
62 public function testAddScriptFile() {
64 ->addScriptFile('com.example.ext', 'foo%20bar.js', 0, 'testAddScriptFile')
66 ->addScriptFile('com.example.ext', 'foo%20bar.js', 0, 'testAddScriptFile')
67 ->addScriptFile('civicrm', 'foo%20bar.js', 0, 'testAddScriptFile');
69 $smarty = CRM_Core_Smarty
::singleton();
70 $actual = $smarty->fetch('string:{crmRegion name=testAddScriptFile}{/crmRegion}');
71 // stable ordering: alphabetical by (snippet.weight,snippet.name)
73 . "<script type=\"text/javascript\" src=\"http://core-app/foo%20bar.js?r=resTest\">\n</script>\n"
74 . "<script type=\"text/javascript\" src=\"http://ext-dir/com.example.ext/foo%20bar.js?r=resTest\">\n</script>\n";
75 $this->assertEquals($expected, $actual);
79 * When adding a script file, any ts() expressions should be translated and added to the 'strings'
81 * FIXME: This can't work because the tests run in English and CRM_Core_Resources optimizes
82 * away the English data from $settings['strings']
83 * public function testAddScriptFile_strings() {
84 * file_put_contents($this->mapper->keyToBasePath('com.example.ext') . '/hello.js', 'alert(ts("Hello world"));');
85 * $this->res->addScriptFile('com.example.ext', 'hello.js', 0, 'testAddScriptFile_strings');
86 * $settings = $this->res->getSettings();
87 * $expected = array('Hello world');
88 * $this->assertEquals($expected, $settings['strings']);
93 * Ensure that adding a script URL creates expected markup.
95 public function testAddScriptURL() {
97 ->addScriptUrl('/whiz/foo%20bar.js', 0, 'testAddScriptURL')
99 ->addScriptUrl('/whiz/foo%20bar.js', 0, 'testAddScriptURL')
100 ->addScriptUrl('/whizbang/foo%20bar.js', 0, 'testAddScriptURL');
102 $smarty = CRM_Core_Smarty
::singleton();
103 $actual = $smarty->fetch('string:{crmRegion name=testAddScriptURL}{/crmRegion}');
104 // stable ordering: alphabetical by (snippet.weight,snippet.name)
106 . "<script type=\"text/javascript\" src=\"/whiz/foo%20bar.js\">\n</script>\n"
107 . "<script type=\"text/javascript\" src=\"/whizbang/foo%20bar.js\">\n</script>\n";
108 $this->assertEquals($expected, $actual);
111 public function testAddScript() {
113 ->addScript('alert("hi");', 0, 'testAddScript')
114 ->addScript('alert("there");', 0, 'testAddScript');
116 $smarty = CRM_Core_Smarty
::singleton();
117 $actual = $smarty->fetch('string:{crmRegion name=testAddScript}{/crmRegion}');
119 . "<script type=\"text/javascript\">\nalert(\"hi\");\n</script>\n"
120 . "<script type=\"text/javascript\">\nalert(\"there\");\n</script>\n";
121 $this->assertEquals($expected, $actual);
124 public function testAddVars() {
126 ->addVars('food', ['fruit' => ['mine' => 'apple', 'ours' => 'banana']])
127 ->addVars('food', ['fruit' => ['mine' => 'new apple', 'yours' => 'orange']]);
128 $this->assertTreeEquals(
134 'mine' => 'new apple',
140 $this->res
->getSettings()
144 public function testAddSetting() {
146 ->addSetting(['fruit' => ['mine' => 'apple']])
147 ->addSetting(['fruit' => ['yours' => 'orange']]);
148 $this->assertTreeEquals(
149 ['fruit' => ['yours' => 'orange', 'mine' => 'apple']],
150 $this->res
->getSettings()
152 $actual = $this->res
->renderSetting();
153 $expected = json_encode(['fruit' => ['yours' => 'orange', 'mine' => 'apple']]);
154 $this->assertTrue(strpos($actual, $expected) !== FALSE);
157 public function testAddSettingHook() {
159 Civi
::dispatcher()->addListener('hook_civicrm_alterResourceSettings', function($event) use ($test) {
160 $test->assertEquals('apple', $event->data
['fruit']['mine']);
161 $event->data
['fruit']['mine'] = 'banana';
163 $this->res
->addSetting(['fruit' => ['mine' => 'apple']]);
164 $settings = $this->res
->getSettings();
165 $this->assertTreeEquals(['fruit' => ['mine' => 'banana']], $settings);
168 public function testAddSettingFactory() {
169 $this->res
->addSettingsFactory(function () {
170 return ['fruit' => ['yours' => 'orange']];
172 $this->res
->addSettingsFactory(function () {
173 return ['fruit' => ['mine' => 'apple']];
176 $actual = $this->res
->getSettings();
177 $expected = ['fruit' => ['yours' => 'orange', 'mine' => 'apple']];
178 $this->assertTreeEquals($expected, $actual);
181 public function testAddSettingAndSettingFactory() {
182 $this->res
->addSetting(['fruit' => ['mine' => 'apple']]);
184 $muckableValue = ['fruit' => ['yours' => 'orange', 'theirs' => 'apricot']];
185 $this->res
->addSettingsFactory(function () use (&$muckableValue) {
186 return $muckableValue;
188 $actual = $this->res
->getSettings();
189 $expected = ['fruit' => ['mine' => 'apple', 'yours' => 'orange', 'theirs' => 'apricot']];
190 $this->assertTreeEquals($expected, $actual);
192 // note: the setting is not fixed based on what the factory returns when registered; it's based
193 // on what the factory returns when getSettings is called
194 $muckableValue = ['fruit' => ['yours' => 'banana']];
195 $actual = $this->res
->getSettings();
196 $expected = ['fruit' => ['mine' => 'apple', 'yours' => 'banana']];
197 $this->assertTreeEquals($expected, $actual);
200 public function testCrmJS() {
201 $smarty = CRM_Core_Smarty
::singleton();
203 $actual = $smarty->fetch('string:{crmScript ext=com.example.ext file=foo%20bar.js region=testCrmJS}');
204 $this->assertEquals('', $actual);
206 $actual = $smarty->fetch('string:{crmScript url=/whiz/foo%20bar.js region=testCrmJS weight=1}');
207 $this->assertEquals('', $actual);
209 $actual = $smarty->fetch('string:{crmRegion name=testCrmJS}{/crmRegion}');
210 // stable ordering: alphabetical by (snippet.weight,snippet.name)
212 . "<script type=\"text/javascript\" src=\"http://ext-dir/com.example.ext/foo%20bar.js?r=resTest\">\n</script>\n"
213 . "<script type=\"text/javascript\" src=\"/whiz/foo%20bar.js\">\n</script>\n";
214 $this->assertEquals($expected, $actual);
217 public function testAddStyleFile() {
219 ->addStyleFile('com.example.ext', 'foo%20bar.css', 0, 'testAddStyleFile')
221 ->addStyleFile('com.example.ext', 'foo%20bar.css', 0, 'testAddStyleFile')
222 ->addStyleFile('civicrm', 'foo%20bar.css', 0, 'testAddStyleFile');
224 $smarty = CRM_Core_Smarty
::singleton();
225 $actual = $smarty->fetch('string:{crmRegion name=testAddStyleFile}{/crmRegion}');
226 // stable ordering: alphabetical by (snippet.weight,snippet.name)
228 . "<link href=\"http://core-app/foo%20bar.css?r=resTest\" rel=\"stylesheet\" type=\"text/css\"/>\n"
229 . "<link href=\"http://ext-dir/com.example.ext/foo%20bar.css?r=resTest\" rel=\"stylesheet\" type=\"text/css\"/>\n";
230 $this->assertEquals($expected, $actual);
233 public function testAddStyleURL() {
235 ->addStyleUrl('/whiz/foo%20bar.css', 0, 'testAddStyleURL')
237 ->addStyleUrl('/whiz/foo%20bar.css', 0, 'testAddStyleURL')
238 ->addStyleUrl('/whizbang/foo%20bar.css', 0, 'testAddStyleURL');
240 $smarty = CRM_Core_Smarty
::singleton();
241 $actual = $smarty->fetch('string:{crmRegion name=testAddStyleURL}{/crmRegion}');
242 // stable ordering: alphabetical by (snippet.weight,snippet.name)
244 . "<link href=\"/whiz/foo%20bar.css\" rel=\"stylesheet\" type=\"text/css\"/>\n"
245 . "<link href=\"/whizbang/foo%20bar.css\" rel=\"stylesheet\" type=\"text/css\"/>\n";
246 $this->assertEquals($expected, $actual);
249 public function testAddStyle() {
251 ->addStyle('body { background: black; }', 0, 'testAddStyle')
252 ->addStyle('body { text-color: black; }', 0, 'testAddStyle');
254 $smarty = CRM_Core_Smarty
::singleton();
255 $actual = $smarty->fetch('string:{crmRegion name=testAddStyle}{/crmRegion}');
257 . "<style type=\"text/css\">\nbody { background: black; }\n</style>\n"
258 . "<style type=\"text/css\">\nbody { text-color: black; }\n</style>\n";
259 $this->assertEquals($expected, $actual);
262 public function testCrmCSS() {
263 $smarty = CRM_Core_Smarty
::singleton();
265 $actual = $smarty->fetch('string:{crmStyle ext=com.example.ext file=foo%20bar.css region=testCrmCSS}');
266 $this->assertEquals('', $actual);
268 $actual = $smarty->fetch('string:{crmStyle url=/whiz/foo%20bar.css region=testCrmCSS weight=1}');
269 $this->assertEquals('', $actual);
271 $actual = $smarty->fetch('string:{crmRegion name=testCrmCSS}{/crmRegion}');
272 // stable ordering: alphabetical by (snippet.weight,snippet.name)
274 . "<link href=\"http://ext-dir/com.example.ext/foo%20bar.css?r=resTest\" rel=\"stylesheet\" type=\"text/css\"/>\n"
275 . "<link href=\"/whiz/foo%20bar.css\" rel=\"stylesheet\" type=\"text/css\"/>\n";
276 $this->assertEquals($expected, $actual);
279 public function testGetURL() {
281 'http://core-app/dir/file%20name.txt',
282 $this->res
->getURL('civicrm', 'dir/file%20name.txt')
285 'http://ext-dir/com.example.ext/dir/file%20name.txt',
286 $this->res
->getURL('com.example.ext', 'dir/file%20name.txt')
290 $this->res
->getURL('civicrm')
293 'http://ext-dir/com.example.ext/',
294 $this->res
->getURL('com.example.ext')
298 public function testCrmResURL() {
299 $smarty = CRM_Core_Smarty
::singleton();
301 $actual = $smarty->fetch('string:{crmResURL ext=com.example.ext file=foo%20bar.png}');
302 $this->assertEquals('http://ext-dir/com.example.ext/foo%20bar.png', $actual);
304 $actual = $smarty->fetch('string:{crmResURL ext=com.example.ext file=foo%20bar.png addCacheCode=1}');
305 $this->assertEquals('http://ext-dir/com.example.ext/foo%20bar.png?r=resTest', $actual);
307 $actual = $smarty->fetch('string:{crmResURL ext=com.example.ext}');
308 $this->assertEquals('http://ext-dir/com.example.ext/', $actual);
310 $actual = $smarty->fetch('string:{crmResURL expr="[civicrm.root]/foo"}');
311 $this->assertEquals(Civi
::paths()->getUrl('[civicrm.root]/foo'), $actual);
314 public function testGlob() {
317 $this->res
->glob('com.example.ext', 'info.xml')
321 $this->res
->glob('com.example.ext', 'js/*.js')
325 $this->res
->glob('com.example.ext', ['js/*.js'])
330 * @dataProvider ajaxModeData
332 public function testIsAjaxMode($query, $result) {
333 $_REQUEST = $_GET = $query;
334 $this->assertEquals($result, CRM_Core_Resources
::isAjaxMode());
337 public function ajaxModeData() {
339 [['q' => 'civicrm/ajax/foo'], TRUE],
340 [['q' => 'civicrm/angularprofiles/template'], TRUE],
341 [['q' => 'civicrm/asset/builder'], TRUE],
342 [['q' => 'civicrm/test/page'], FALSE],
343 [['q' => 'civicrm/test/page', 'snippet' => 'json'], TRUE],
344 [['q' => 'civicrm/test/page', 'snippet' => 'foo'], FALSE],
349 * @param CRM_Utils_Cache_Interface $cache
350 * @param string $cacheKey
353 * [string $basedir, CRM_Extension_Container_Interface, CRM_Extension_Mapper]
355 public function _createMapper(CRM_Utils_Cache_Interface
$cache = NULL, $cacheKey = NULL) {
356 $basedir = rtrim($this->createTempDir('ext-'), '/');
357 mkdir("$basedir/com.example.ext");
358 mkdir("$basedir/com.example.ext/js");
359 file_put_contents("$basedir/com.example.ext/info.xml", "<extension key='com.example.ext' type='report'><file>oddball</file></extension>");
360 file_put_contents("$basedir/com.example.ext/js/example.js", "alert('Boo!');");
361 // not needed for now // file_put_contents("$basedir/weird/bar/oddball.php", "<?php\n");
362 $c = new CRM_Extension_Container_Basic($basedir, 'http://ext-dir', $cache, $cacheKey);
363 $mapper = new CRM_Extension_Mapper($c, NULL, NULL, '/pathto/civicrm', 'http://core-app');
364 return [$basedir, $c, $mapper];
369 * @param string $expected
371 * @dataProvider urlForCacheCodeProvider
373 public function testAddingCacheCode($url, $expected) {
374 $resources = CRM_Core_Resources
::singleton();
375 $resources->setCacheCode($this->cacheBusterString
);
376 $this->assertEquals($expected, $resources->addCacheCode($url));
382 public function urlForCacheCodeProvider() {
385 'http://www.civicrm.org',
386 'http://www.civicrm.org?r=' . $this->cacheBusterString
,
389 'www.civicrm.org/custom.css?foo=bar',
390 'www.civicrm.org/custom.css?foo=bar&r=' . $this->cacheBusterString
,
393 'civicrm.org/custom.css?car=blue&foo=bar',
394 'civicrm.org/custom.css?car=blue&foo=bar&r=' . $this->cacheBusterString
,
402 public function urlsToCheckIfFullyFormed() {
404 ['civicrm/test/page', FALSE],
407 ['/civicrm/test/page', TRUE],
408 ['http://test.com/civicrm/test/page', TRUE],
409 ['https://test.com/civicrm/test/page', TRUE],
415 * @param string $expected
417 * @dataProvider urlsToCheckIfFullyFormed
419 public function testIsFullyFormedUrl($url, $expected) {
420 $this->assertEquals($expected, CRM_Core_Resources
::isFullyFormedUrl($url));
424 * Test for hook_civicrm_entityRefFilters().
427 public function testEntityRefFiltersHook() {
428 CRM_Utils_Hook_UnitTests
::singleton()->setHook('civicrm_entityRefFilters', [$this, 'entityRefFilters']);
429 $data = CRM_Core_Resources
::getEntityRefMetadata();
430 $this->assertEquals(count($data['links']['Contact']), 4);
431 $this->assertEquals(!empty($data['links']['Contact']['new_staff']), TRUE);
435 * @param array $filters
436 * @param array $links
438 public function entityRefFilters(&$filters, &$links) {
439 $links['Contact']['new_staff'] = [
440 'label' => ts('New Staff'),
441 'url' => '/civicrm/profile/create&reset=1&context=dialog&gid=5',
442 'type' => 'Individual',