Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.3 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
29 | ||
30 | /** | |
31 | * Tests for linking to resource files | |
32 | */ | |
33 | class CRM_Core_ResourcesTest extends CiviUnitTestCase { | |
34 | function get_info() { | |
35 | return array( | |
36 | 'name' => 'Resources', | |
37 | 'description' => 'Tests for linking to resource files', | |
38 | 'group' => 'Core', | |
39 | ); | |
40 | } | |
41 | ||
42 | /** | |
43 | * @var CRM_Core_Resources | |
44 | */ | |
45 | protected $res; | |
46 | ||
47 | /** | |
48 | * @var CRM_Extension_Mapper | |
49 | */ | |
50 | protected $mapper; | |
51 | ||
52 | function setUp() { | |
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'; | |
63 | civicrm_smarty_register_string_resource( ); | |
64 | } | |
65 | ||
66 | function testAddScriptFile() { | |
67 | $this->res | |
68 | ->addScriptFile('com.example.ext', 'foo%20bar.js', 0, 'testAddScriptFile') | |
69 | ->addScriptFile('com.example.ext', 'foo%20bar.js', 0, 'testAddScriptFile') // extra | |
70 | ->addScriptFile('civicrm', 'foo%20bar.js', 0, 'testAddScriptFile') | |
71 | ; | |
72 | ||
73 | $smarty = CRM_Core_Smarty::singleton(); | |
74 | $actual = $smarty->fetch('string:{crmRegion name=testAddScriptFile}{/crmRegion}'); | |
75 | $expected = "" // stable ordering: alphabetical by (snippet.weight,snippet.name) | |
76 | . "<script type=\"text/javascript\" src=\"http://core-app/foo%20bar.js?r=resTest\">\n</script>\n" | |
77 | . "<script type=\"text/javascript\" src=\"http://ext-dir/com.example.ext/foo%20bar.js?r=resTest\">\n</script>\n" | |
78 | ; | |
79 | $this->assertEquals($expected, $actual); | |
80 | } | |
81 | ||
82 | /** | |
83 | * When adding a script file, any ts() expressions should be translated and added to the 'strings' | |
84 | * | |
85 | * FIXME: This can't work because the tests run in English and CRM_Core_Resources optimizes | |
86 | * away the English data from $settings['strings'] | |
87 | function testAddScriptFile_strings() { | |
88 | file_put_contents($this->mapper->keyToBasePath('com.example.ext') . '/hello.js', 'alert(ts("Hello world"));'); | |
89 | $this->res->addScriptFile('com.example.ext', 'hello.js', 0, 'testAddScriptFile_strings'); | |
90 | $settings = $this->res->getSettings(); | |
91 | $expected = array('Hello world'); | |
92 | $this->assertEquals($expected, $settings['strings']); | |
93 | } | |
94 | */ | |
95 | ||
96 | function testAddScriptURL() { | |
97 | $this->res | |
98 | ->addScriptUrl('/whiz/foo%20bar.js', 0, 'testAddScriptURL') | |
99 | ->addScriptUrl('/whiz/foo%20bar.js', 0, 'testAddScriptURL') // extra | |
100 | ->addScriptUrl('/whizbang/foo%20bar.js', 0, 'testAddScriptURL') | |
101 | ; | |
102 | ||
103 | $smarty = CRM_Core_Smarty::singleton(); | |
104 | $actual = $smarty->fetch('string:{crmRegion name=testAddScriptURL}{/crmRegion}'); | |
105 | $expected = "" // stable ordering: alphabetical by (snippet.weight,snippet.name) | |
106 | . "<script type=\"text/javascript\" src=\"/whiz/foo%20bar.js\">\n</script>\n" | |
107 | . "<script type=\"text/javascript\" src=\"/whizbang/foo%20bar.js\">\n</script>\n" | |
108 | ; | |
109 | $this->assertEquals($expected, $actual); | |
110 | } | |
111 | ||
112 | function testAddScript() { | |
113 | $this->res | |
114 | ->addScript('alert("hi");', 0, 'testAddScript') | |
115 | ->addScript('alert("there");', 0, 'testAddScript') | |
116 | ; | |
117 | ||
118 | $smarty = CRM_Core_Smarty::singleton(); | |
119 | $actual = $smarty->fetch('string:{crmRegion name=testAddScript}{/crmRegion}'); | |
120 | $expected = "" | |
121 | . "<script type=\"text/javascript\">\nalert(\"hi\");\n</script>\n" | |
122 | . "<script type=\"text/javascript\">\nalert(\"there\");\n</script>\n" | |
123 | ; | |
124 | $this->assertEquals($expected, $actual); | |
125 | } | |
126 | ||
127 | function testAddSetting() { | |
128 | $this->res | |
129 | ->addSetting(array('fruit' => array('mine' => 'apple'))) | |
130 | ->addSetting(array('fruit' => array('yours' => 'orange'))) | |
131 | ; | |
132 | $this->assertTreeEquals( | |
133 | array('fruit' => array('yours' => 'orange', 'mine' => 'apple')), | |
134 | $this->res->getSettings() | |
135 | ); | |
136 | $actual = $this->res->renderSetting(); | |
137 | $expected = 'CRM = cj.extend(true, ' . json_encode(array('fruit' => array('yours' => 'orange', 'mine' => 'apple'))) . ', CRM);'; | |
138 | $this->assertEquals($expected, $actual); | |
139 | } | |
140 | ||
141 | function testAddSettingFactory() { | |
142 | $this->res->addSettingsFactory(function() { | |
143 | return array('fruit' => array('yours' => 'orange')); | |
144 | }); | |
145 | $this->res->addSettingsFactory(function() { | |
146 | return array('fruit' => array('mine' => 'apple')); | |
147 | }); | |
148 | ||
149 | $actual = $this->res->getSettings(); | |
150 | $expected = array('fruit' => array('yours' => 'orange', 'mine' => 'apple')); | |
151 | $this->assertTreeEquals($expected, $actual); | |
152 | } | |
153 | ||
154 | function testAddSettingAndSettingFactory() { | |
155 | $this->res->addSetting(array('fruit' => array('mine' => 'apple'))); | |
156 | ||
157 | $muckableValue = array('fruit' => array('yours' => 'orange', 'theirs' => 'apricot')); | |
158 | $this->res->addSettingsFactory(function() use (&$muckableValue) { | |
159 | return $muckableValue; | |
160 | }); | |
161 | $actual = $this->res->getSettings(); | |
162 | $expected = array('fruit' => array('mine' => 'apple', 'yours' => 'orange', 'theirs' => 'apricot')); | |
163 | $this->assertTreeEquals($expected, $actual); | |
164 | ||
165 | // note: the setting is not fixed based on what the factory returns when registered; it's based | |
166 | // on what the factory returns when getSettings is called | |
167 | $muckableValue = array('fruit' => array('yours' => 'banana')); | |
168 | $actual = $this->res->getSettings(); | |
169 | $expected = array('fruit' => array('mine' => 'apple', 'yours' => 'banana')); | |
170 | $this->assertTreeEquals($expected, $actual); | |
171 | } | |
172 | ||
173 | function testCrmJS() { | |
174 | $smarty = CRM_Core_Smarty::singleton(); | |
175 | ||
176 | $actual = $smarty->fetch('string:{crmScript ext=com.example.ext file=foo%20bar.js region=testCrmJS}'); | |
177 | $this->assertEquals('', $actual); | |
178 | ||
179 | $actual = $smarty->fetch('string:{crmScript url=/whiz/foo%20bar.js region=testCrmJS weight=1}'); | |
180 | $this->assertEquals('', $actual); | |
181 | ||
182 | $actual = $smarty->fetch('string:{crmRegion name=testCrmJS}{/crmRegion}'); | |
183 | $expected = "" // stable ordering: alphabetical by (snippet.weight,snippet.name) | |
184 | . "<script type=\"text/javascript\" src=\"http://ext-dir/com.example.ext/foo%20bar.js?r=resTest\">\n</script>\n" | |
185 | . "<script type=\"text/javascript\" src=\"/whiz/foo%20bar.js\">\n</script>\n" | |
186 | ; | |
187 | $this->assertEquals($expected, $actual); | |
188 | } | |
189 | ||
190 | function testAddStyleFile() { | |
191 | $this->res | |
192 | ->addStyleFile('com.example.ext', 'foo%20bar.css', 0, 'testAddStyleFile') | |
193 | ->addStyleFile('com.example.ext', 'foo%20bar.css', 0, 'testAddStyleFile') // extra | |
194 | ->addStyleFile('civicrm', 'foo%20bar.css', 0, 'testAddStyleFile') | |
195 | ; | |
196 | ||
197 | $smarty = CRM_Core_Smarty::singleton(); | |
198 | $actual = $smarty->fetch('string:{crmRegion name=testAddStyleFile}{/crmRegion}'); | |
199 | $expected = "" // stable ordering: alphabetical by (snippet.weight,snippet.name) | |
200 | . "<link href=\"http://core-app/foo%20bar.css?r=resTest\" rel=\"stylesheet\" type=\"text/css\"/>\n" | |
201 | . "<link href=\"http://ext-dir/com.example.ext/foo%20bar.css?r=resTest\" rel=\"stylesheet\" type=\"text/css\"/>\n" | |
202 | ; | |
203 | $this->assertEquals($expected, $actual); | |
204 | } | |
205 | ||
206 | function testAddStyleURL() { | |
207 | $this->res | |
208 | ->addStyleUrl('/whiz/foo%20bar.css', 0, 'testAddStyleURL') | |
209 | ->addStyleUrl('/whiz/foo%20bar.css', 0, 'testAddStyleURL') // extra | |
210 | ->addStyleUrl('/whizbang/foo%20bar.css', 0, 'testAddStyleURL') | |
211 | ; | |
212 | ||
213 | $smarty = CRM_Core_Smarty::singleton(); | |
214 | $actual = $smarty->fetch('string:{crmRegion name=testAddStyleURL}{/crmRegion}'); | |
215 | $expected = "" // stable ordering: alphabetical by (snippet.weight,snippet.name) | |
216 | . "<link href=\"/whiz/foo%20bar.css\" rel=\"stylesheet\" type=\"text/css\"/>\n" | |
217 | . "<link href=\"/whizbang/foo%20bar.css\" rel=\"stylesheet\" type=\"text/css\"/>\n" | |
218 | ; | |
219 | $this->assertEquals($expected, $actual); | |
220 | } | |
221 | ||
222 | function testAddStyle() { | |
223 | $this->res | |
224 | ->addStyle('body { background: black; }', 0, 'testAddStyle') | |
225 | ->addStyle('body { text-color: black; }', 0, 'testAddStyle') | |
226 | ; | |
227 | ||
228 | $smarty = CRM_Core_Smarty::singleton(); | |
229 | $actual = $smarty->fetch('string:{crmRegion name=testAddStyle}{/crmRegion}'); | |
230 | $expected = "" | |
231 | . "<style type=\"text/css\">\nbody { background: black; }\n</style>\n" | |
232 | . "<style type=\"text/css\">\nbody { text-color: black; }\n</style>\n" | |
233 | ; | |
234 | $this->assertEquals($expected, $actual); | |
235 | } | |
236 | ||
237 | function testCrmCSS() { | |
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" | |
249 | . "<link href=\"/whiz/foo%20bar.css\" rel=\"stylesheet\" type=\"text/css\"/>\n" | |
250 | ; | |
251 | $this->assertEquals($expected, $actual); | |
252 | } | |
253 | ||
254 | function testGetURL() { | |
255 | $this->assertEquals( | |
256 | 'http://core-app/dir/file%20name.txt', | |
257 | $this->res->getURL('civicrm', 'dir/file%20name.txt') | |
258 | ); | |
259 | $this->assertEquals( | |
260 | 'http://ext-dir/com.example.ext/dir/file%20name.txt', | |
261 | $this->res->getURL('com.example.ext', 'dir/file%20name.txt') | |
262 | ); | |
263 | $this->assertEquals( | |
264 | 'http://core-app/', | |
265 | $this->res->getURL('civicrm') | |
266 | ); | |
267 | $this->assertEquals( | |
268 | 'http://ext-dir/com.example.ext/', | |
269 | $this->res->getURL('com.example.ext') | |
270 | ); | |
271 | } | |
272 | ||
273 | function testCrmResURL() { | |
274 | $smarty = CRM_Core_Smarty::singleton(); | |
275 | ||
276 | $actual = $smarty->fetch('string:{crmResURL ext=com.example.ext file=foo%20bar.png}'); | |
277 | $this->assertEquals('http://ext-dir/com.example.ext/foo%20bar.png', $actual); | |
278 | ||
279 | $actual = $smarty->fetch('string:{crmResURL ext=com.example.ext file=foo%20bar.png addCacheCode=1}'); | |
280 | $this->assertEquals('http://ext-dir/com.example.ext/foo%20bar.png?r=resTest', $actual); | |
281 | ||
282 | $actual = $smarty->fetch('string:{crmResURL ext=com.example.ext}'); | |
283 | $this->assertEquals('http://ext-dir/com.example.ext/', $actual); | |
284 | } | |
285 | ||
286 | /** | |
287 | * @param CRM_Utils_Cache_Interface $cache | |
288 | * @param null $cacheKey | |
289 | * @param string $appendPathGarbage | |
290 | * @return array(string $basedir, CRM_Extension_Container_Interface, CRM_Extension_Mapper) | |
291 | */ | |
292 | function _createMapper(CRM_Utils_Cache_Interface $cache = NULL, $cacheKey = NULL) { | |
293 | $basedir = rtrim($this->createTempDir('ext-'), '/'); | |
294 | mkdir("$basedir/com.example.ext"); | |
295 | file_put_contents("$basedir/com.example.ext/info.xml", "<extension key='com.example.ext' type='report'><file>oddball</file></extension>"); | |
296 | // not needed for now // file_put_contents("$basedir/weird/bar/oddball.php", "<?php\n"); | |
297 | $c = new CRM_Extension_Container_Basic($basedir, 'http://ext-dir', $cache, $cacheKey); | |
298 | $mapper = new CRM_Extension_Mapper($c, NULL, NULL, '/pathto/civicrm', 'http://core-app'); | |
299 | return array($basedir, $c, $mapper); | |
300 | } | |
301 | ||
302 | } |