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