Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
2fe49090 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
8c9251b3 | 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 | 27 | |
6a488035 TO |
28 | /** |
29 | * Tests for linking to resource files | |
acb109b7 | 30 | * @group headless |
6a488035 TO |
31 | */ |
32 | class CRM_Core_ResourcesTest extends CiviUnitTestCase { | |
6a488035 TO |
33 | |
34 | /** | |
35 | * @var CRM_Core_Resources | |
36 | */ | |
37 | protected $res; | |
38 | ||
39 | /** | |
40 | * @var CRM_Extension_Mapper | |
41 | */ | |
42 | protected $mapper; | |
43 | ||
6f12c6eb | 44 | /** |
45 | * @var string for testing cache buster generation | |
46 | */ | |
47 | protected $cacheBusterString = 'xBkdk3'; | |
48 | ||
42a40a1c | 49 | protected $originalRequest; |
50 | protected $originalGet; | |
51 | ||
00be9182 | 52 | public function setUp() { |
6a488035 TO |
53 | parent::setUp(); |
54 | ||
55 | list ($this->basedir, $this->container, $this->mapper) = $this->_createMapper(); | |
56 | $cache = new CRM_Utils_Cache_Arraycache(array()); | |
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'; | |
481a74f4 | 63 | civicrm_smarty_register_string_resource(); |
42a40a1c | 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; | |
6a488035 TO |
75 | } |
76 | ||
00be9182 | 77 | public function testAddScriptFile() { |
6a488035 TO |
78 | $this->res |
79 | ->addScriptFile('com.example.ext', 'foo%20bar.js', 0, 'testAddScriptFile') | |
92915c55 | 80 | ->addScriptFile('com.example.ext', 'foo%20bar.js', 0, 'testAddScriptFile')// extra |
6c6e6187 | 81 | ->addScriptFile('civicrm', 'foo%20bar.js', 0, 'testAddScriptFile'); |
6a488035 TO |
82 | |
83 | $smarty = CRM_Core_Smarty::singleton(); | |
84 | $actual = $smarty->fetch('string:{crmRegion name=testAddScriptFile}{/crmRegion}'); | |
85 | $expected = "" // stable ordering: alphabetical by (snippet.weight,snippet.name) | |
86 | . "<script type=\"text/javascript\" src=\"http://core-app/foo%20bar.js?r=resTest\">\n</script>\n" | |
6c6e6187 | 87 | . "<script type=\"text/javascript\" src=\"http://ext-dir/com.example.ext/foo%20bar.js?r=resTest\">\n</script>\n"; |
6a488035 TO |
88 | $this->assertEquals($expected, $actual); |
89 | } | |
90 | ||
91 | /** | |
92 | * When adding a script file, any ts() expressions should be translated and added to the 'strings' | |
93 | * | |
94 | * FIXME: This can't work because the tests run in English and CRM_Core_Resources optimizes | |
95 | * away the English data from $settings['strings'] | |
92915c55 TO |
96 | * public function testAddScriptFile_strings() { |
97 | * file_put_contents($this->mapper->keyToBasePath('com.example.ext') . '/hello.js', 'alert(ts("Hello world"));'); | |
98 | * $this->res->addScriptFile('com.example.ext', 'hello.js', 0, 'testAddScriptFile_strings'); | |
99 | * $settings = $this->res->getSettings(); | |
100 | * $expected = array('Hello world'); | |
101 | * $this->assertEquals($expected, $settings['strings']); | |
102 | * } | |
6c6e6187 | 103 | */ |
6a488035 | 104 | |
a1a2a83d TO |
105 | /** |
106 | * Ensure that adding a script URL creates expected markup. | |
107 | */ | |
00be9182 | 108 | public function testAddScriptURL() { |
6a488035 TO |
109 | $this->res |
110 | ->addScriptUrl('/whiz/foo%20bar.js', 0, 'testAddScriptURL') | |
92915c55 | 111 | ->addScriptUrl('/whiz/foo%20bar.js', 0, 'testAddScriptURL')// extra |
6c6e6187 | 112 | ->addScriptUrl('/whizbang/foo%20bar.js', 0, 'testAddScriptURL'); |
6a488035 TO |
113 | |
114 | $smarty = CRM_Core_Smarty::singleton(); | |
115 | $actual = $smarty->fetch('string:{crmRegion name=testAddScriptURL}{/crmRegion}'); | |
116 | $expected = "" // stable ordering: alphabetical by (snippet.weight,snippet.name) | |
117 | . "<script type=\"text/javascript\" src=\"/whiz/foo%20bar.js\">\n</script>\n" | |
6c6e6187 | 118 | . "<script type=\"text/javascript\" src=\"/whizbang/foo%20bar.js\">\n</script>\n"; |
6a488035 TO |
119 | $this->assertEquals($expected, $actual); |
120 | } | |
121 | ||
00be9182 | 122 | public function testAddScript() { |
6a488035 TO |
123 | $this->res |
124 | ->addScript('alert("hi");', 0, 'testAddScript') | |
6c6e6187 | 125 | ->addScript('alert("there");', 0, 'testAddScript'); |
6a488035 TO |
126 | |
127 | $smarty = CRM_Core_Smarty::singleton(); | |
128 | $actual = $smarty->fetch('string:{crmRegion name=testAddScript}{/crmRegion}'); | |
129 | $expected = "" | |
130 | . "<script type=\"text/javascript\">\nalert(\"hi\");\n</script>\n" | |
6c6e6187 | 131 | . "<script type=\"text/javascript\">\nalert(\"there\");\n</script>\n"; |
6a488035 TO |
132 | $this->assertEquals($expected, $actual); |
133 | } | |
134 | ||
00be9182 | 135 | public function testAddVars() { |
73bcd446 CW |
136 | $this->res |
137 | ->addVars('food', array('fruit' => array('mine' => 'apple', 'ours' => 'banana'))) | |
6c6e6187 | 138 | ->addVars('food', array('fruit' => array('mine' => 'new apple', 'yours' => 'orange'))); |
73bcd446 | 139 | $this->assertTreeEquals( |
92915c55 TO |
140 | array( |
141 | 'vars' => array( | |
142 | 'food' => array( | |
143 | 'fruit' => array( | |
144 | 'yours' => 'orange', | |
145 | 'mine' => 'new apple', | |
af9b09df TO |
146 | 'ours' => 'banana', |
147 | ), | |
148 | ), | |
149 | ), | |
92915c55 | 150 | ), |
73bcd446 CW |
151 | $this->res->getSettings() |
152 | ); | |
153 | } | |
154 | ||
00be9182 | 155 | public function testAddSetting() { |
6a488035 TO |
156 | $this->res |
157 | ->addSetting(array('fruit' => array('mine' => 'apple'))) | |
6c6e6187 | 158 | ->addSetting(array('fruit' => array('yours' => 'orange'))); |
6a488035 TO |
159 | $this->assertTreeEquals( |
160 | array('fruit' => array('yours' => 'orange', 'mine' => 'apple')), | |
161 | $this->res->getSettings() | |
162 | ); | |
163 | $actual = $this->res->renderSetting(); | |
6521a306 CW |
164 | $expected = json_encode(array('fruit' => array('yours' => 'orange', 'mine' => 'apple'))); |
165 | $this->assertTrue(strpos($actual, $expected) !== FALSE); | |
6a488035 TO |
166 | } |
167 | ||
898951c6 TO |
168 | public function testAddSettingHook() { |
169 | $test = $this; | |
170 | Civi::dispatcher()->addListener('hook_civicrm_alterResourceSettings', function($event) use ($test) { | |
171 | $test->assertEquals('apple', $event->data['fruit']['mine']); | |
172 | $event->data['fruit']['mine'] = 'banana'; | |
173 | }); | |
174 | $this->res->addSetting(array('fruit' => array('mine' => 'apple'))); | |
175 | $settings = $this->res->getSettings(); | |
176 | $this->assertTreeEquals(array('fruit' => array('mine' => 'banana')), $settings); | |
177 | } | |
178 | ||
00be9182 | 179 | public function testAddSettingFactory() { |
92915c55 | 180 | $this->res->addSettingsFactory(function () { |
6a488035 TO |
181 | return array('fruit' => array('yours' => 'orange')); |
182 | }); | |
92915c55 | 183 | $this->res->addSettingsFactory(function () { |
6a488035 TO |
184 | return array('fruit' => array('mine' => 'apple')); |
185 | }); | |
186 | ||
187 | $actual = $this->res->getSettings(); | |
188 | $expected = array('fruit' => array('yours' => 'orange', 'mine' => 'apple')); | |
189 | $this->assertTreeEquals($expected, $actual); | |
190 | } | |
191 | ||
00be9182 | 192 | public function testAddSettingAndSettingFactory() { |
6a488035 TO |
193 | $this->res->addSetting(array('fruit' => array('mine' => 'apple'))); |
194 | ||
195 | $muckableValue = array('fruit' => array('yours' => 'orange', 'theirs' => 'apricot')); | |
92915c55 | 196 | $this->res->addSettingsFactory(function () use (&$muckableValue) { |
6a488035 TO |
197 | return $muckableValue; |
198 | }); | |
199 | $actual = $this->res->getSettings(); | |
200 | $expected = array('fruit' => array('mine' => 'apple', 'yours' => 'orange', 'theirs' => 'apricot')); | |
201 | $this->assertTreeEquals($expected, $actual); | |
202 | ||
203 | // note: the setting is not fixed based on what the factory returns when registered; it's based | |
204 | // on what the factory returns when getSettings is called | |
205 | $muckableValue = array('fruit' => array('yours' => 'banana')); | |
206 | $actual = $this->res->getSettings(); | |
207 | $expected = array('fruit' => array('mine' => 'apple', 'yours' => 'banana')); | |
208 | $this->assertTreeEquals($expected, $actual); | |
209 | } | |
210 | ||
00be9182 | 211 | public function testCrmJS() { |
6a488035 TO |
212 | $smarty = CRM_Core_Smarty::singleton(); |
213 | ||
214 | $actual = $smarty->fetch('string:{crmScript ext=com.example.ext file=foo%20bar.js region=testCrmJS}'); | |
215 | $this->assertEquals('', $actual); | |
216 | ||
217 | $actual = $smarty->fetch('string:{crmScript url=/whiz/foo%20bar.js region=testCrmJS weight=1}'); | |
218 | $this->assertEquals('', $actual); | |
219 | ||
220 | $actual = $smarty->fetch('string:{crmRegion name=testCrmJS}{/crmRegion}'); | |
221 | $expected = "" // stable ordering: alphabetical by (snippet.weight,snippet.name) | |
222 | . "<script type=\"text/javascript\" src=\"http://ext-dir/com.example.ext/foo%20bar.js?r=resTest\">\n</script>\n" | |
6c6e6187 | 223 | . "<script type=\"text/javascript\" src=\"/whiz/foo%20bar.js\">\n</script>\n"; |
6a488035 TO |
224 | $this->assertEquals($expected, $actual); |
225 | } | |
226 | ||
00be9182 | 227 | public function testAddStyleFile() { |
6a488035 TO |
228 | $this->res |
229 | ->addStyleFile('com.example.ext', 'foo%20bar.css', 0, 'testAddStyleFile') | |
92915c55 | 230 | ->addStyleFile('com.example.ext', 'foo%20bar.css', 0, 'testAddStyleFile')// extra |
6c6e6187 | 231 | ->addStyleFile('civicrm', 'foo%20bar.css', 0, 'testAddStyleFile'); |
6a488035 TO |
232 | |
233 | $smarty = CRM_Core_Smarty::singleton(); | |
234 | $actual = $smarty->fetch('string:{crmRegion name=testAddStyleFile}{/crmRegion}'); | |
235 | $expected = "" // stable ordering: alphabetical by (snippet.weight,snippet.name) | |
236 | . "<link href=\"http://core-app/foo%20bar.css?r=resTest\" rel=\"stylesheet\" type=\"text/css\"/>\n" | |
6c6e6187 | 237 | . "<link href=\"http://ext-dir/com.example.ext/foo%20bar.css?r=resTest\" rel=\"stylesheet\" type=\"text/css\"/>\n"; |
6a488035 TO |
238 | $this->assertEquals($expected, $actual); |
239 | } | |
240 | ||
00be9182 | 241 | public function testAddStyleURL() { |
6a488035 TO |
242 | $this->res |
243 | ->addStyleUrl('/whiz/foo%20bar.css', 0, 'testAddStyleURL') | |
92915c55 | 244 | ->addStyleUrl('/whiz/foo%20bar.css', 0, 'testAddStyleURL')// extra |
6c6e6187 | 245 | ->addStyleUrl('/whizbang/foo%20bar.css', 0, 'testAddStyleURL'); |
6a488035 TO |
246 | |
247 | $smarty = CRM_Core_Smarty::singleton(); | |
248 | $actual = $smarty->fetch('string:{crmRegion name=testAddStyleURL}{/crmRegion}'); | |
249 | $expected = "" // stable ordering: alphabetical by (snippet.weight,snippet.name) | |
250 | . "<link href=\"/whiz/foo%20bar.css\" rel=\"stylesheet\" type=\"text/css\"/>\n" | |
6c6e6187 | 251 | . "<link href=\"/whizbang/foo%20bar.css\" rel=\"stylesheet\" type=\"text/css\"/>\n"; |
6a488035 TO |
252 | $this->assertEquals($expected, $actual); |
253 | } | |
254 | ||
00be9182 | 255 | public function testAddStyle() { |
6a488035 TO |
256 | $this->res |
257 | ->addStyle('body { background: black; }', 0, 'testAddStyle') | |
6c6e6187 | 258 | ->addStyle('body { text-color: black; }', 0, 'testAddStyle'); |
6a488035 TO |
259 | |
260 | $smarty = CRM_Core_Smarty::singleton(); | |
261 | $actual = $smarty->fetch('string:{crmRegion name=testAddStyle}{/crmRegion}'); | |
262 | $expected = "" | |
263 | . "<style type=\"text/css\">\nbody { background: black; }\n</style>\n" | |
6c6e6187 | 264 | . "<style type=\"text/css\">\nbody { text-color: black; }\n</style>\n"; |
6a488035 TO |
265 | $this->assertEquals($expected, $actual); |
266 | } | |
267 | ||
00be9182 | 268 | public function testCrmCSS() { |
6a488035 TO |
269 | $smarty = CRM_Core_Smarty::singleton(); |
270 | ||
271 | $actual = $smarty->fetch('string:{crmStyle ext=com.example.ext file=foo%20bar.css region=testCrmCSS}'); | |
272 | $this->assertEquals('', $actual); | |
273 | ||
274 | $actual = $smarty->fetch('string:{crmStyle url=/whiz/foo%20bar.css region=testCrmCSS weight=1}'); | |
275 | $this->assertEquals('', $actual); | |
276 | ||
277 | $actual = $smarty->fetch('string:{crmRegion name=testCrmCSS}{/crmRegion}'); | |
278 | $expected = "" // stable ordering: alphabetical by (snippet.weight,snippet.name) | |
279 | . "<link href=\"http://ext-dir/com.example.ext/foo%20bar.css?r=resTest\" rel=\"stylesheet\" type=\"text/css\"/>\n" | |
6c6e6187 | 280 | . "<link href=\"/whiz/foo%20bar.css\" rel=\"stylesheet\" type=\"text/css\"/>\n"; |
6a488035 TO |
281 | $this->assertEquals($expected, $actual); |
282 | } | |
283 | ||
00be9182 | 284 | public function testGetURL() { |
6a488035 TO |
285 | $this->assertEquals( |
286 | 'http://core-app/dir/file%20name.txt', | |
287 | $this->res->getURL('civicrm', 'dir/file%20name.txt') | |
288 | ); | |
289 | $this->assertEquals( | |
290 | 'http://ext-dir/com.example.ext/dir/file%20name.txt', | |
291 | $this->res->getURL('com.example.ext', 'dir/file%20name.txt') | |
292 | ); | |
293 | $this->assertEquals( | |
294 | 'http://core-app/', | |
295 | $this->res->getURL('civicrm') | |
296 | ); | |
297 | $this->assertEquals( | |
298 | 'http://ext-dir/com.example.ext/', | |
299 | $this->res->getURL('com.example.ext') | |
300 | ); | |
301 | } | |
302 | ||
00be9182 | 303 | public function testCrmResURL() { |
6a488035 TO |
304 | $smarty = CRM_Core_Smarty::singleton(); |
305 | ||
306 | $actual = $smarty->fetch('string:{crmResURL ext=com.example.ext file=foo%20bar.png}'); | |
307 | $this->assertEquals('http://ext-dir/com.example.ext/foo%20bar.png', $actual); | |
308 | ||
309 | $actual = $smarty->fetch('string:{crmResURL ext=com.example.ext file=foo%20bar.png addCacheCode=1}'); | |
310 | $this->assertEquals('http://ext-dir/com.example.ext/foo%20bar.png?r=resTest', $actual); | |
311 | ||
312 | $actual = $smarty->fetch('string:{crmResURL ext=com.example.ext}'); | |
313 | $this->assertEquals('http://ext-dir/com.example.ext/', $actual); | |
314 | } | |
315 | ||
16cd1eca TO |
316 | public function testGlob() { |
317 | $this->assertEquals( | |
318 | array('info.xml'), | |
319 | $this->res->glob('com.example.ext', 'info.xml') | |
320 | ); | |
321 | $this->assertEquals( | |
322 | array('js/example.js'), | |
323 | $this->res->glob('com.example.ext', 'js/*.js') | |
324 | ); | |
325 | $this->assertEquals( | |
326 | array('js/example.js'), | |
327 | $this->res->glob('com.example.ext', array('js/*.js')) | |
328 | ); | |
329 | } | |
330 | ||
42a40a1c | 331 | /** |
332 | * @dataProvider ajaxModeData | |
333 | */ | |
334 | public function testIsAjaxMode($query, $result) { | |
335 | $_REQUEST = $_GET = $query; | |
336 | $this->assertEquals($result, CRM_Core_Resources::isAjaxMode()); | |
337 | } | |
338 | ||
339 | public function ajaxModeData() { | |
340 | return array( | |
341 | array(array('q' => 'civicrm/ajax/foo'), TRUE), | |
342 | array(array('q' => 'civicrm/test/page'), FALSE), | |
343 | array(array('q' => 'civicrm/test/page', 'snippet' => 'json'), TRUE), | |
344 | array(array('q' => 'civicrm/test/page', 'snippet' => 'foo'), FALSE), | |
345 | ); | |
346 | } | |
347 | ||
6a488035 TO |
348 | /** |
349 | * @param CRM_Utils_Cache_Interface $cache | |
72b3a70c | 350 | * @param string $cacheKey |
2a6da8d7 | 351 | * |
72b3a70c CW |
352 | * @return array |
353 | * [string $basedir, CRM_Extension_Container_Interface, CRM_Extension_Mapper] | |
6a488035 | 354 | */ |
00be9182 | 355 | public function _createMapper(CRM_Utils_Cache_Interface $cache = NULL, $cacheKey = NULL) { |
6a488035 TO |
356 | $basedir = rtrim($this->createTempDir('ext-'), '/'); |
357 | mkdir("$basedir/com.example.ext"); | |
16cd1eca | 358 | mkdir("$basedir/com.example.ext/js"); |
6a488035 | 359 | file_put_contents("$basedir/com.example.ext/info.xml", "<extension key='com.example.ext' type='report'><file>oddball</file></extension>"); |
16cd1eca | 360 | file_put_contents("$basedir/com.example.ext/js/example.js", "alert('Boo!');"); |
6a488035 TO |
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 array($basedir, $c, $mapper); | |
365 | } | |
366 | ||
6f12c6eb | 367 | /** |
368 | * @param string $url | |
369 | * @param string $expected | |
370 | * | |
371 | * @dataProvider urlForCacheCodeProvider | |
372 | */ | |
373 | public function testAddingCacheCode($url, $expected) { | |
374 | $resources = CRM_Core_Resources::singleton(); | |
595caa48 | 375 | $resources->setCacheCode($this->cacheBusterString); |
6f12c6eb | 376 | $this->assertEquals($expected, $resources->addCacheCode($url)); |
377 | } | |
378 | ||
379 | /** | |
380 | * @return array | |
381 | */ | |
382 | public function urlForCacheCodeProvider() { | |
383 | return array( | |
384 | array( | |
385 | 'http://www.civicrm.org', | |
386 | 'http://www.civicrm.org?r=' . $this->cacheBusterString, | |
387 | ), | |
388 | array( | |
389 | 'www.civicrm.org/custom.css?foo=bar', | |
390 | 'www.civicrm.org/custom.css?foo=bar&r=' . $this->cacheBusterString, | |
391 | ), | |
6f12c6eb | 392 | array( |
393 | 'civicrm.org/custom.css?car=blue&foo=bar', | |
394 | 'civicrm.org/custom.css?car=blue&foo=bar&r=' . $this->cacheBusterString, | |
395 | ), | |
396 | ); | |
397 | } | |
398 | ||
6a488035 | 399 | } |