3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * Tests for linking to resource files
32 class CRM_Core_ResourcesTest
extends CiviUnitTestCase
{
35 * @var CRM_Core_Resources
40 * @var CRM_Extension_Mapper
45 * @var string for testing cache buster generation
47 protected $cacheBusterString = 'xBkdk3';
49 protected $originalRequest;
50 protected $originalGet;
52 public function setUp() {
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
);
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();
65 $this->originalRequest
= $_REQUEST;
66 $this->originalGet
= $_GET;
70 * Restore globals so this test doesn't interfere with others.
72 public function tearDown() {
73 $_REQUEST = $this->originalRequest
;
74 $_GET = $this->originalGet
;
77 public function testAddScriptFile() {
79 ->addScriptFile('com.example.ext', 'foo%20bar.js', 0, 'testAddScriptFile')
81 ->addScriptFile('com.example.ext', 'foo%20bar.js', 0, 'testAddScriptFile')
82 ->addScriptFile('civicrm', 'foo%20bar.js', 0, 'testAddScriptFile');
84 $smarty = CRM_Core_Smarty
::singleton();
85 $actual = $smarty->fetch('string:{crmRegion name=testAddScriptFile}{/crmRegion}');
86 // stable ordering: alphabetical by (snippet.weight,snippet.name)
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);
94 * When adding a script file, any ts() expressions should be translated and added to the 'strings'
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']);
108 * Ensure that adding a script URL creates expected markup.
110 public function testAddScriptURL() {
112 ->addScriptUrl('/whiz/foo%20bar.js', 0, 'testAddScriptURL')
114 ->addScriptUrl('/whiz/foo%20bar.js', 0, 'testAddScriptURL')
115 ->addScriptUrl('/whizbang/foo%20bar.js', 0, 'testAddScriptURL');
117 $smarty = CRM_Core_Smarty
::singleton();
118 $actual = $smarty->fetch('string:{crmRegion name=testAddScriptURL}{/crmRegion}');
119 // stable ordering: alphabetical by (snippet.weight,snippet.name)
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);
126 public function testAddScript() {
128 ->addScript('alert("hi");', 0, 'testAddScript')
129 ->addScript('alert("there");', 0, 'testAddScript');
131 $smarty = CRM_Core_Smarty
::singleton();
132 $actual = $smarty->fetch('string:{crmRegion name=testAddScript}{/crmRegion}');
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);
139 public function testAddVars() {
141 ->addVars('food', ['fruit' => ['mine' => 'apple', 'ours' => 'banana']])
142 ->addVars('food', ['fruit' => ['mine' => 'new apple', 'yours' => 'orange']]);
143 $this->assertTreeEquals(
149 'mine' => 'new apple',
155 $this->res
->getSettings()
159 public function testAddSetting() {
161 ->addSetting(['fruit' => ['mine' => 'apple']])
162 ->addSetting(['fruit' => ['yours' => 'orange']]);
163 $this->assertTreeEquals(
164 ['fruit' => ['yours' => 'orange', 'mine' => 'apple']],
165 $this->res
->getSettings()
167 $actual = $this->res
->renderSetting();
168 $expected = json_encode(['fruit' => ['yours' => 'orange', 'mine' => 'apple']]);
169 $this->assertTrue(strpos($actual, $expected) !== FALSE);
172 public function testAddSettingHook() {
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';
178 $this->res
->addSetting(['fruit' => ['mine' => 'apple']]);
179 $settings = $this->res
->getSettings();
180 $this->assertTreeEquals(['fruit' => ['mine' => 'banana']], $settings);
183 public function testAddSettingFactory() {
184 $this->res
->addSettingsFactory(function () {
185 return ['fruit' => ['yours' => 'orange']];
187 $this->res
->addSettingsFactory(function () {
188 return ['fruit' => ['mine' => 'apple']];
191 $actual = $this->res
->getSettings();
192 $expected = ['fruit' => ['yours' => 'orange', 'mine' => 'apple']];
193 $this->assertTreeEquals($expected, $actual);
196 public function testAddSettingAndSettingFactory() {
197 $this->res
->addSetting(['fruit' => ['mine' => 'apple']]);
199 $muckableValue = ['fruit' => ['yours' => 'orange', 'theirs' => 'apricot']];
200 $this->res
->addSettingsFactory(function () use (&$muckableValue) {
201 return $muckableValue;
203 $actual = $this->res
->getSettings();
204 $expected = ['fruit' => ['mine' => 'apple', 'yours' => 'orange', 'theirs' => 'apricot']];
205 $this->assertTreeEquals($expected, $actual);
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);
215 public function testCrmJS() {
216 $smarty = CRM_Core_Smarty
::singleton();
218 $actual = $smarty->fetch('string:{crmScript ext=com.example.ext file=foo%20bar.js region=testCrmJS}');
219 $this->assertEquals('', $actual);
221 $actual = $smarty->fetch('string:{crmScript url=/whiz/foo%20bar.js region=testCrmJS weight=1}');
222 $this->assertEquals('', $actual);
224 $actual = $smarty->fetch('string:{crmRegion name=testCrmJS}{/crmRegion}');
225 // stable ordering: alphabetical by (snippet.weight,snippet.name)
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);
232 public function testAddStyleFile() {
234 ->addStyleFile('com.example.ext', 'foo%20bar.css', 0, 'testAddStyleFile')
236 ->addStyleFile('com.example.ext', 'foo%20bar.css', 0, 'testAddStyleFile')
237 ->addStyleFile('civicrm', 'foo%20bar.css', 0, 'testAddStyleFile');
239 $smarty = CRM_Core_Smarty
::singleton();
240 $actual = $smarty->fetch('string:{crmRegion name=testAddStyleFile}{/crmRegion}');
241 // stable ordering: alphabetical by (snippet.weight,snippet.name)
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);
248 public function testAddStyleURL() {
250 ->addStyleUrl('/whiz/foo%20bar.css', 0, 'testAddStyleURL')
252 ->addStyleUrl('/whiz/foo%20bar.css', 0, 'testAddStyleURL')
253 ->addStyleUrl('/whizbang/foo%20bar.css', 0, 'testAddStyleURL');
255 $smarty = CRM_Core_Smarty
::singleton();
256 $actual = $smarty->fetch('string:{crmRegion name=testAddStyleURL}{/crmRegion}');
257 // stable ordering: alphabetical by (snippet.weight,snippet.name)
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);
264 public function testAddStyle() {
266 ->addStyle('body { background: black; }', 0, 'testAddStyle')
267 ->addStyle('body { text-color: black; }', 0, 'testAddStyle');
269 $smarty = CRM_Core_Smarty
::singleton();
270 $actual = $smarty->fetch('string:{crmRegion name=testAddStyle}{/crmRegion}');
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);
277 public function testCrmCSS() {
278 $smarty = CRM_Core_Smarty
::singleton();
280 $actual = $smarty->fetch('string:{crmStyle ext=com.example.ext file=foo%20bar.css region=testCrmCSS}');
281 $this->assertEquals('', $actual);
283 $actual = $smarty->fetch('string:{crmStyle url=/whiz/foo%20bar.css region=testCrmCSS weight=1}');
284 $this->assertEquals('', $actual);
286 $actual = $smarty->fetch('string:{crmRegion name=testCrmCSS}{/crmRegion}');
287 // stable ordering: alphabetical by (snippet.weight,snippet.name)
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);
294 public function testGetURL() {
296 'http://core-app/dir/file%20name.txt',
297 $this->res
->getURL('civicrm', 'dir/file%20name.txt')
300 'http://ext-dir/com.example.ext/dir/file%20name.txt',
301 $this->res
->getURL('com.example.ext', 'dir/file%20name.txt')
305 $this->res
->getURL('civicrm')
308 'http://ext-dir/com.example.ext/',
309 $this->res
->getURL('com.example.ext')
313 public function testCrmResURL() {
314 $smarty = CRM_Core_Smarty
::singleton();
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);
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);
322 $actual = $smarty->fetch('string:{crmResURL ext=com.example.ext}');
323 $this->assertEquals('http://ext-dir/com.example.ext/', $actual);
326 public function testGlob() {
329 $this->res
->glob('com.example.ext', 'info.xml')
333 $this->res
->glob('com.example.ext', 'js/*.js')
337 $this->res
->glob('com.example.ext', ['js/*.js'])
342 * @dataProvider ajaxModeData
344 public function testIsAjaxMode($query, $result) {
345 $_REQUEST = $_GET = $query;
346 $this->assertEquals($result, CRM_Core_Resources
::isAjaxMode());
349 public function ajaxModeData() {
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],
361 * @param CRM_Utils_Cache_Interface $cache
362 * @param string $cacheKey
365 * [string $basedir, CRM_Extension_Container_Interface, CRM_Extension_Mapper]
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];
381 * @param string $expected
383 * @dataProvider urlForCacheCodeProvider
385 public function testAddingCacheCode($url, $expected) {
386 $resources = CRM_Core_Resources
::singleton();
387 $resources->setCacheCode($this->cacheBusterString
);
388 $this->assertEquals($expected, $resources->addCacheCode($url));
394 public function urlForCacheCodeProvider() {
397 'http://www.civicrm.org',
398 'http://www.civicrm.org?r=' . $this->cacheBusterString
,
401 'www.civicrm.org/custom.css?foo=bar',
402 'www.civicrm.org/custom.css?foo=bar&r=' . $this->cacheBusterString
,
405 'civicrm.org/custom.css?car=blue&foo=bar',
406 'civicrm.org/custom.css?car=blue&foo=bar&r=' . $this->cacheBusterString
,
414 public function urlsToCheckIfFullyFormed() {
416 ['civicrm/test/page', FALSE],
419 ['/civicrm/test/page', TRUE],
420 ['http://test.com/civicrm/test/page', TRUE],
421 ['https://test.com/civicrm/test/page', TRUE],
427 * @param string $expected
429 * @dataProvider urlsToCheckIfFullyFormed
431 public function testIsFullyFormedUrl($url, $expected) {
432 $this->assertEquals($expected, CRM_Core_Resources
::isFullyFormedUrl($url));
436 * Test for hook_civicrm_entityRefFilters().
439 public function testEntityRefFiltersHook() {
440 CRM_Utils_Hook_UnitTests
::singleton()->setHook('civicrm_entityRefFilters', [$this, 'entityRefFilters']);
441 $data = CRM_Core_Resources
::getEntityRefMetadata();
442 $this->assertEquals(count($data['links']['Contact']), 4);
443 $this->assertEquals(!empty($data['links']['Contact']['new_staff']), TRUE);
447 * @param array $filters
448 * @param array $links
450 public function entityRefFilters(&$filters, &$links) {
451 $links['Contact']['new_staff'] = [
452 'label' => ts('New Staff'),
453 'url' => '/civicrm/profile/create&reset=1&context=dialog&gid=5',
454 'type' => 'Individual',