CRM-17646 - Consolodate all custom field option lookups
[civicrm-core.git] / tests / phpunit / CRM / Core / ResourcesTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
81621fee 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
TO
27
28require_once 'CiviTest/CiviUnitTestCase.php';
29
30/**
31 * Tests for linking to resource files
32 */
33class CRM_Core_ResourcesTest extends CiviUnitTestCase {
6a488035
TO
34
35 /**
36 * @var CRM_Core_Resources
37 */
38 protected $res;
39
40 /**
41 * @var CRM_Extension_Mapper
42 */
43 protected $mapper;
44
00be9182 45 public function setUp() {
6a488035
TO
46 parent::setUp();
47
48 list ($this->basedir, $this->container, $this->mapper) = $this->_createMapper();
49 $cache = new CRM_Utils_Cache_Arraycache(array());
50 $this->res = new CRM_Core_Resources($this->mapper, $cache, NULL);
51 $this->res->setCacheCode('resTest');
52 CRM_Core_Resources::singleton($this->res);
53
54 // Templates injected into regions should normally be file names, but for unit-testing it's handy to use "string:" notation
55 require_once 'CRM/Core/Smarty/resources/String.php';
481a74f4 56 civicrm_smarty_register_string_resource();
6a488035
TO
57 }
58
00be9182 59 public function testAddScriptFile() {
6a488035
TO
60 $this->res
61 ->addScriptFile('com.example.ext', 'foo%20bar.js', 0, 'testAddScriptFile')
92915c55 62 ->addScriptFile('com.example.ext', 'foo%20bar.js', 0, 'testAddScriptFile')// extra
6c6e6187 63 ->addScriptFile('civicrm', 'foo%20bar.js', 0, 'testAddScriptFile');
6a488035
TO
64
65 $smarty = CRM_Core_Smarty::singleton();
66 $actual = $smarty->fetch('string:{crmRegion name=testAddScriptFile}{/crmRegion}');
67 $expected = "" // stable ordering: alphabetical by (snippet.weight,snippet.name)
68 . "<script type=\"text/javascript\" src=\"http://core-app/foo%20bar.js?r=resTest\">\n</script>\n"
6c6e6187 69 . "<script type=\"text/javascript\" src=\"http://ext-dir/com.example.ext/foo%20bar.js?r=resTest\">\n</script>\n";
6a488035
TO
70 $this->assertEquals($expected, $actual);
71 }
72
73 /**
74 * When adding a script file, any ts() expressions should be translated and added to the 'strings'
75 *
76 * FIXME: This can't work because the tests run in English and CRM_Core_Resources optimizes
77 * away the English data from $settings['strings']
92915c55
TO
78 * public function testAddScriptFile_strings() {
79 * file_put_contents($this->mapper->keyToBasePath('com.example.ext') . '/hello.js', 'alert(ts("Hello world"));');
80 * $this->res->addScriptFile('com.example.ext', 'hello.js', 0, 'testAddScriptFile_strings');
81 * $settings = $this->res->getSettings();
82 * $expected = array('Hello world');
83 * $this->assertEquals($expected, $settings['strings']);
84 * }
6c6e6187 85 */
6a488035 86
a1a2a83d
TO
87 /**
88 * Ensure that adding a script URL creates expected markup.
89 */
00be9182 90 public function testAddScriptURL() {
6a488035
TO
91 $this->res
92 ->addScriptUrl('/whiz/foo%20bar.js', 0, 'testAddScriptURL')
92915c55 93 ->addScriptUrl('/whiz/foo%20bar.js', 0, 'testAddScriptURL')// extra
6c6e6187 94 ->addScriptUrl('/whizbang/foo%20bar.js', 0, 'testAddScriptURL');
6a488035
TO
95
96 $smarty = CRM_Core_Smarty::singleton();
97 $actual = $smarty->fetch('string:{crmRegion name=testAddScriptURL}{/crmRegion}');
98 $expected = "" // stable ordering: alphabetical by (snippet.weight,snippet.name)
99 . "<script type=\"text/javascript\" src=\"/whiz/foo%20bar.js\">\n</script>\n"
6c6e6187 100 . "<script type=\"text/javascript\" src=\"/whizbang/foo%20bar.js\">\n</script>\n";
6a488035
TO
101 $this->assertEquals($expected, $actual);
102 }
103
00be9182 104 public function testAddScript() {
6a488035
TO
105 $this->res
106 ->addScript('alert("hi");', 0, 'testAddScript')
6c6e6187 107 ->addScript('alert("there");', 0, 'testAddScript');
6a488035
TO
108
109 $smarty = CRM_Core_Smarty::singleton();
110 $actual = $smarty->fetch('string:{crmRegion name=testAddScript}{/crmRegion}');
111 $expected = ""
112 . "<script type=\"text/javascript\">\nalert(\"hi\");\n</script>\n"
6c6e6187 113 . "<script type=\"text/javascript\">\nalert(\"there\");\n</script>\n";
6a488035
TO
114 $this->assertEquals($expected, $actual);
115 }
116
00be9182 117 public function testAddVars() {
73bcd446
CW
118 $this->res
119 ->addVars('food', array('fruit' => array('mine' => 'apple', 'ours' => 'banana')))
6c6e6187 120 ->addVars('food', array('fruit' => array('mine' => 'new apple', 'yours' => 'orange')));
73bcd446 121 $this->assertTreeEquals(
92915c55
TO
122 array(
123 'vars' => array(
124 'food' => array(
125 'fruit' => array(
126 'yours' => 'orange',
127 'mine' => 'new apple',
af9b09df
TO
128 'ours' => 'banana',
129 ),
130 ),
131 ),
92915c55 132 ),
73bcd446
CW
133 $this->res->getSettings()
134 );
135 }
136
00be9182 137 public function testAddSetting() {
6a488035
TO
138 $this->res
139 ->addSetting(array('fruit' => array('mine' => 'apple')))
6c6e6187 140 ->addSetting(array('fruit' => array('yours' => 'orange')));
6a488035
TO
141 $this->assertTreeEquals(
142 array('fruit' => array('yours' => 'orange', 'mine' => 'apple')),
143 $this->res->getSettings()
144 );
145 $actual = $this->res->renderSetting();
6521a306
CW
146 $expected = json_encode(array('fruit' => array('yours' => 'orange', 'mine' => 'apple')));
147 $this->assertTrue(strpos($actual, $expected) !== FALSE);
6a488035
TO
148 }
149
00be9182 150 public function testAddSettingFactory() {
92915c55 151 $this->res->addSettingsFactory(function () {
6a488035
TO
152 return array('fruit' => array('yours' => 'orange'));
153 });
92915c55 154 $this->res->addSettingsFactory(function () {
6a488035
TO
155 return array('fruit' => array('mine' => 'apple'));
156 });
157
158 $actual = $this->res->getSettings();
159 $expected = array('fruit' => array('yours' => 'orange', 'mine' => 'apple'));
160 $this->assertTreeEquals($expected, $actual);
161 }
162
00be9182 163 public function testAddSettingAndSettingFactory() {
6a488035
TO
164 $this->res->addSetting(array('fruit' => array('mine' => 'apple')));
165
166 $muckableValue = array('fruit' => array('yours' => 'orange', 'theirs' => 'apricot'));
92915c55 167 $this->res->addSettingsFactory(function () use (&$muckableValue) {
6a488035
TO
168 return $muckableValue;
169 });
170 $actual = $this->res->getSettings();
171 $expected = array('fruit' => array('mine' => 'apple', 'yours' => 'orange', 'theirs' => 'apricot'));
172 $this->assertTreeEquals($expected, $actual);
173
174 // note: the setting is not fixed based on what the factory returns when registered; it's based
175 // on what the factory returns when getSettings is called
176 $muckableValue = array('fruit' => array('yours' => 'banana'));
177 $actual = $this->res->getSettings();
178 $expected = array('fruit' => array('mine' => 'apple', 'yours' => 'banana'));
179 $this->assertTreeEquals($expected, $actual);
180 }
181
00be9182 182 public function testCrmJS() {
6a488035
TO
183 $smarty = CRM_Core_Smarty::singleton();
184
185 $actual = $smarty->fetch('string:{crmScript ext=com.example.ext file=foo%20bar.js region=testCrmJS}');
186 $this->assertEquals('', $actual);
187
188 $actual = $smarty->fetch('string:{crmScript url=/whiz/foo%20bar.js region=testCrmJS weight=1}');
189 $this->assertEquals('', $actual);
190
191 $actual = $smarty->fetch('string:{crmRegion name=testCrmJS}{/crmRegion}');
192 $expected = "" // stable ordering: alphabetical by (snippet.weight,snippet.name)
193 . "<script type=\"text/javascript\" src=\"http://ext-dir/com.example.ext/foo%20bar.js?r=resTest\">\n</script>\n"
6c6e6187 194 . "<script type=\"text/javascript\" src=\"/whiz/foo%20bar.js\">\n</script>\n";
6a488035
TO
195 $this->assertEquals($expected, $actual);
196 }
197
00be9182 198 public function testAddStyleFile() {
6a488035
TO
199 $this->res
200 ->addStyleFile('com.example.ext', 'foo%20bar.css', 0, 'testAddStyleFile')
92915c55 201 ->addStyleFile('com.example.ext', 'foo%20bar.css', 0, 'testAddStyleFile')// extra
6c6e6187 202 ->addStyleFile('civicrm', 'foo%20bar.css', 0, 'testAddStyleFile');
6a488035
TO
203
204 $smarty = CRM_Core_Smarty::singleton();
205 $actual = $smarty->fetch('string:{crmRegion name=testAddStyleFile}{/crmRegion}');
206 $expected = "" // stable ordering: alphabetical by (snippet.weight,snippet.name)
207 . "<link href=\"http://core-app/foo%20bar.css?r=resTest\" rel=\"stylesheet\" type=\"text/css\"/>\n"
6c6e6187 208 . "<link href=\"http://ext-dir/com.example.ext/foo%20bar.css?r=resTest\" rel=\"stylesheet\" type=\"text/css\"/>\n";
6a488035
TO
209 $this->assertEquals($expected, $actual);
210 }
211
00be9182 212 public function testAddStyleURL() {
6a488035
TO
213 $this->res
214 ->addStyleUrl('/whiz/foo%20bar.css', 0, 'testAddStyleURL')
92915c55 215 ->addStyleUrl('/whiz/foo%20bar.css', 0, 'testAddStyleURL')// extra
6c6e6187 216 ->addStyleUrl('/whizbang/foo%20bar.css', 0, 'testAddStyleURL');
6a488035
TO
217
218 $smarty = CRM_Core_Smarty::singleton();
219 $actual = $smarty->fetch('string:{crmRegion name=testAddStyleURL}{/crmRegion}');
220 $expected = "" // stable ordering: alphabetical by (snippet.weight,snippet.name)
221 . "<link href=\"/whiz/foo%20bar.css\" rel=\"stylesheet\" type=\"text/css\"/>\n"
6c6e6187 222 . "<link href=\"/whizbang/foo%20bar.css\" rel=\"stylesheet\" type=\"text/css\"/>\n";
6a488035
TO
223 $this->assertEquals($expected, $actual);
224 }
225
00be9182 226 public function testAddStyle() {
6a488035
TO
227 $this->res
228 ->addStyle('body { background: black; }', 0, 'testAddStyle')
6c6e6187 229 ->addStyle('body { text-color: black; }', 0, 'testAddStyle');
6a488035
TO
230
231 $smarty = CRM_Core_Smarty::singleton();
232 $actual = $smarty->fetch('string:{crmRegion name=testAddStyle}{/crmRegion}');
233 $expected = ""
234 . "<style type=\"text/css\">\nbody { background: black; }\n</style>\n"
6c6e6187 235 . "<style type=\"text/css\">\nbody { text-color: black; }\n</style>\n";
6a488035
TO
236 $this->assertEquals($expected, $actual);
237 }
238
00be9182 239 public function testCrmCSS() {
6a488035
TO
240 $smarty = CRM_Core_Smarty::singleton();
241
242 $actual = $smarty->fetch('string:{crmStyle ext=com.example.ext file=foo%20bar.css region=testCrmCSS}');
243 $this->assertEquals('', $actual);
244
245 $actual = $smarty->fetch('string:{crmStyle url=/whiz/foo%20bar.css region=testCrmCSS weight=1}');
246 $this->assertEquals('', $actual);
247
248 $actual = $smarty->fetch('string:{crmRegion name=testCrmCSS}{/crmRegion}');
249 $expected = "" // stable ordering: alphabetical by (snippet.weight,snippet.name)
250 . "<link href=\"http://ext-dir/com.example.ext/foo%20bar.css?r=resTest\" rel=\"stylesheet\" type=\"text/css\"/>\n"
6c6e6187 251 . "<link href=\"/whiz/foo%20bar.css\" rel=\"stylesheet\" type=\"text/css\"/>\n";
6a488035
TO
252 $this->assertEquals($expected, $actual);
253 }
254
00be9182 255 public function testGetURL() {
6a488035
TO
256 $this->assertEquals(
257 'http://core-app/dir/file%20name.txt',
258 $this->res->getURL('civicrm', 'dir/file%20name.txt')
259 );
260 $this->assertEquals(
261 'http://ext-dir/com.example.ext/dir/file%20name.txt',
262 $this->res->getURL('com.example.ext', 'dir/file%20name.txt')
263 );
264 $this->assertEquals(
265 'http://core-app/',
266 $this->res->getURL('civicrm')
267 );
268 $this->assertEquals(
269 'http://ext-dir/com.example.ext/',
270 $this->res->getURL('com.example.ext')
271 );
272 }
273
00be9182 274 public function testCrmResURL() {
6a488035
TO
275 $smarty = CRM_Core_Smarty::singleton();
276
277 $actual = $smarty->fetch('string:{crmResURL ext=com.example.ext file=foo%20bar.png}');
278 $this->assertEquals('http://ext-dir/com.example.ext/foo%20bar.png', $actual);
279
280 $actual = $smarty->fetch('string:{crmResURL ext=com.example.ext file=foo%20bar.png addCacheCode=1}');
281 $this->assertEquals('http://ext-dir/com.example.ext/foo%20bar.png?r=resTest', $actual);
282
283 $actual = $smarty->fetch('string:{crmResURL ext=com.example.ext}');
284 $this->assertEquals('http://ext-dir/com.example.ext/', $actual);
285 }
286
16cd1eca
TO
287 public function testGlob() {
288 $this->assertEquals(
289 array('info.xml'),
290 $this->res->glob('com.example.ext', 'info.xml')
291 );
292 $this->assertEquals(
293 array('js/example.js'),
294 $this->res->glob('com.example.ext', 'js/*.js')
295 );
296 $this->assertEquals(
297 array('js/example.js'),
298 $this->res->glob('com.example.ext', array('js/*.js'))
299 );
300 }
301
6a488035
TO
302 /**
303 * @param CRM_Utils_Cache_Interface $cache
72b3a70c 304 * @param string $cacheKey
2a6da8d7 305 *
72b3a70c
CW
306 * @return array
307 * [string $basedir, CRM_Extension_Container_Interface, CRM_Extension_Mapper]
6a488035 308 */
00be9182 309 public function _createMapper(CRM_Utils_Cache_Interface $cache = NULL, $cacheKey = NULL) {
6a488035
TO
310 $basedir = rtrim($this->createTempDir('ext-'), '/');
311 mkdir("$basedir/com.example.ext");
16cd1eca 312 mkdir("$basedir/com.example.ext/js");
6a488035 313 file_put_contents("$basedir/com.example.ext/info.xml", "<extension key='com.example.ext' type='report'><file>oddball</file></extension>");
16cd1eca 314 file_put_contents("$basedir/com.example.ext/js/example.js", "alert('Boo!');");
6a488035
TO
315 // not needed for now // file_put_contents("$basedir/weird/bar/oddball.php", "<?php\n");
316 $c = new CRM_Extension_Container_Basic($basedir, 'http://ext-dir', $cache, $cacheKey);
317 $mapper = new CRM_Extension_Mapper($c, NULL, NULL, '/pathto/civicrm', 'http://core-app');
318 return array($basedir, $c, $mapper);
319 }
320
321}