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
29 * @var string for testing cache buster generation
31 protected $cacheBusterString = 'xBkdk3';
33 protected $originalRequest;
34 protected $originalGet;
36 public function setUp() {
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
);
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();
49 $this->originalRequest
= $_REQUEST;
50 $this->originalGet
= $_GET;
54 * Restore globals so this test doesn't interfere with others.
56 public function tearDown() {
57 $_REQUEST = $this->originalRequest
;
58 $_GET = $this->originalGet
;
61 public function testAddScriptFile() {
63 ->addScriptFile('com.example.ext', 'foo%20bar.js', 0, 'testAddScriptFile')
65 ->addScriptFile('com.example.ext', 'foo%20bar.js', 0, 'testAddScriptFile')
66 ->addScriptFile('civicrm', 'foo%20bar.js', 0, 'testAddScriptFile');
68 $smarty = CRM_Core_Smarty
::singleton();
69 $actual = $smarty->fetch('string:{crmRegion name=testAddScriptFile}{/crmRegion}');
70 // stable ordering: alphabetical by (snippet.weight,snippet.name)
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);
78 * When adding a script file, any ts() expressions should be translated and added to the 'strings'
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']);
92 * Ensure that adding a script URL creates expected markup.
94 public function testAddScriptURL() {
96 ->addScriptUrl('/whiz/foo%20bar.js', 0, 'testAddScriptURL')
98 ->addScriptUrl('/whiz/foo%20bar.js', 0, 'testAddScriptURL')
99 ->addScriptUrl('/whizbang/foo%20bar.js', 0, 'testAddScriptURL');
101 $smarty = CRM_Core_Smarty
::singleton();
102 $actual = $smarty->fetch('string:{crmRegion name=testAddScriptURL}{/crmRegion}');
103 // stable ordering: alphabetical by (snippet.weight,snippet.name)
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);
110 public function testAddScript() {
112 ->addScript('alert("hi");', 0, 'testAddScript')
113 ->addScript('alert("there");', 0, 'testAddScript');
115 $smarty = CRM_Core_Smarty
::singleton();
116 $actual = $smarty->fetch('string:{crmRegion name=testAddScript}{/crmRegion}');
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);
123 public function testAddVars() {
125 ->addVars('food', ['fruit' => ['mine' => 'apple', 'ours' => 'banana']])
126 ->addVars('food', ['fruit' => ['mine' => 'new apple', 'yours' => 'orange']]);
127 $this->assertTreeEquals(
133 'mine' => 'new apple',
139 $this->res
->getSettings()
143 public function testAddSetting() {
145 ->addSetting(['fruit' => ['mine' => 'apple']])
146 ->addSetting(['fruit' => ['yours' => 'orange']]);
147 $this->assertTreeEquals(
148 ['fruit' => ['yours' => 'orange', 'mine' => 'apple']],
149 $this->res
->getSettings()
151 $actual = $this->res
->renderSetting();
152 $expected = json_encode(['fruit' => ['yours' => 'orange', 'mine' => 'apple']]);
153 $this->assertTrue(strpos($actual, $expected) !== FALSE);
156 public function testAddSettingHook() {
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';
162 $this->res
->addSetting(['fruit' => ['mine' => 'apple']]);
163 $settings = $this->res
->getSettings();
164 $this->assertTreeEquals(['fruit' => ['mine' => 'banana']], $settings);
167 public function testAddSettingFactory() {
168 $this->res
->addSettingsFactory(function () {
169 return ['fruit' => ['yours' => 'orange']];
171 $this->res
->addSettingsFactory(function () {
172 return ['fruit' => ['mine' => 'apple']];
175 $actual = $this->res
->getSettings();
176 $expected = ['fruit' => ['yours' => 'orange', 'mine' => 'apple']];
177 $this->assertTreeEquals($expected, $actual);
180 public function testAddSettingAndSettingFactory() {
181 $this->res
->addSetting(['fruit' => ['mine' => 'apple']]);
183 $muckableValue = ['fruit' => ['yours' => 'orange', 'theirs' => 'apricot']];
184 $this->res
->addSettingsFactory(function () use (&$muckableValue) {
185 return $muckableValue;
187 $actual = $this->res
->getSettings();
188 $expected = ['fruit' => ['mine' => 'apple', 'yours' => 'orange', 'theirs' => 'apricot']];
189 $this->assertTreeEquals($expected, $actual);
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);
199 public function testCrmJS() {
200 $smarty = CRM_Core_Smarty
::singleton();
202 $actual = $smarty->fetch('string:{crmScript ext=com.example.ext file=foo%20bar.js region=testCrmJS}');
203 $this->assertEquals('', $actual);
205 $actual = $smarty->fetch('string:{crmScript url=/whiz/foo%20bar.js region=testCrmJS weight=1}');
206 $this->assertEquals('', $actual);
208 $actual = $smarty->fetch('string:{crmRegion name=testCrmJS}{/crmRegion}');
209 // stable ordering: alphabetical by (snippet.weight,snippet.name)
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);
216 public function testAddStyleFile() {
218 ->addStyleFile('com.example.ext', 'foo%20bar.css', 0, 'testAddStyleFile')
220 ->addStyleFile('com.example.ext', 'foo%20bar.css', 0, 'testAddStyleFile')
221 ->addStyleFile('civicrm', 'foo%20bar.css', 0, 'testAddStyleFile');
223 $smarty = CRM_Core_Smarty
::singleton();
224 $actual = $smarty->fetch('string:{crmRegion name=testAddStyleFile}{/crmRegion}');
225 // stable ordering: alphabetical by (snippet.weight,snippet.name)
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);
232 public function testAddStyleURL() {
234 ->addStyleUrl('/whiz/foo%20bar.css', 0, 'testAddStyleURL')
236 ->addStyleUrl('/whiz/foo%20bar.css', 0, 'testAddStyleURL')
237 ->addStyleUrl('/whizbang/foo%20bar.css', 0, 'testAddStyleURL');
239 $smarty = CRM_Core_Smarty
::singleton();
240 $actual = $smarty->fetch('string:{crmRegion name=testAddStyleURL}{/crmRegion}');
241 // stable ordering: alphabetical by (snippet.weight,snippet.name)
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);
248 public function testAddStyle() {
250 ->addStyle('body { background: black; }', 0, 'testAddStyle')
251 ->addStyle('body { text-color: black; }', 0, 'testAddStyle');
253 $smarty = CRM_Core_Smarty
::singleton();
254 $actual = $smarty->fetch('string:{crmRegion name=testAddStyle}{/crmRegion}');
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);
261 public function testCrmCSS() {
262 $smarty = CRM_Core_Smarty
::singleton();
264 $actual = $smarty->fetch('string:{crmStyle ext=com.example.ext file=foo%20bar.css region=testCrmCSS}');
265 $this->assertEquals('', $actual);
267 $actual = $smarty->fetch('string:{crmStyle url=/whiz/foo%20bar.css region=testCrmCSS weight=1}');
268 $this->assertEquals('', $actual);
270 $actual = $smarty->fetch('string:{crmRegion name=testCrmCSS}{/crmRegion}');
271 // stable ordering: alphabetical by (snippet.weight,snippet.name)
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);
278 public function testGetURL() {
280 'http://core-app/dir/file%20name.txt',
281 $this->res
->getURL('civicrm', 'dir/file%20name.txt')
284 'http://ext-dir/com.example.ext/dir/file%20name.txt',
285 $this->res
->getURL('com.example.ext', 'dir/file%20name.txt')
289 $this->res
->getURL('civicrm')
292 'http://ext-dir/com.example.ext/',
293 $this->res
->getURL('com.example.ext')
297 public function testCrmResURL() {
298 $smarty = CRM_Core_Smarty
::singleton();
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);
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);
306 $actual = $smarty->fetch('string:{crmResURL ext=com.example.ext}');
307 $this->assertEquals('http://ext-dir/com.example.ext/', $actual);
310 public function testGlob() {
313 $this->res
->glob('com.example.ext', 'info.xml')
317 $this->res
->glob('com.example.ext', 'js/*.js')
321 $this->res
->glob('com.example.ext', ['js/*.js'])
326 * @dataProvider ajaxModeData
328 public function testIsAjaxMode($query, $result) {
329 $_REQUEST = $_GET = $query;
330 $this->assertEquals($result, CRM_Core_Resources
::isAjaxMode());
333 public function ajaxModeData() {
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],
345 * @param CRM_Utils_Cache_Interface $cache
346 * @param string $cacheKey
349 * [string $basedir, CRM_Extension_Container_Interface, CRM_Extension_Mapper]
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];
365 * @param string $expected
367 * @dataProvider urlForCacheCodeProvider
369 public function testAddingCacheCode($url, $expected) {
370 $resources = CRM_Core_Resources
::singleton();
371 $resources->setCacheCode($this->cacheBusterString
);
372 $this->assertEquals($expected, $resources->addCacheCode($url));
378 public function urlForCacheCodeProvider() {
381 'http://www.civicrm.org',
382 'http://www.civicrm.org?r=' . $this->cacheBusterString
,
385 'www.civicrm.org/custom.css?foo=bar',
386 'www.civicrm.org/custom.css?foo=bar&r=' . $this->cacheBusterString
,
389 'civicrm.org/custom.css?car=blue&foo=bar',
390 'civicrm.org/custom.css?car=blue&foo=bar&r=' . $this->cacheBusterString
,
398 public function urlsToCheckIfFullyFormed() {
400 ['civicrm/test/page', FALSE],
403 ['/civicrm/test/page', TRUE],
404 ['http://test.com/civicrm/test/page', TRUE],
405 ['https://test.com/civicrm/test/page', TRUE],
411 * @param string $expected
413 * @dataProvider urlsToCheckIfFullyFormed
415 public function testIsFullyFormedUrl($url, $expected) {
416 $this->assertEquals($expected, CRM_Core_Resources
::isFullyFormedUrl($url));
420 * Test for hook_civicrm_entityRefFilters().
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);
431 * @param array $filters
432 * @param array $links
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',