Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | ||
3 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
4 | ||
aba1cd8b EM |
5 | /** |
6 | * Class CRM_Core_RegionTest | |
7 | */ | |
6a488035 | 8 | class CRM_Core_RegionTest extends CiviUnitTestCase { |
aba1cd8b EM |
9 | /** |
10 | * @return array | |
11 | */ | |
6a488035 TO |
12 | function get_info() { |
13 | return array( | |
14 | 'name' => 'Region', | |
15 | 'description' => 'Smarty-region rendering test', | |
16 | 'group' => 'Core', | |
17 | ); | |
18 | } | |
19 | ||
20 | function setUp() { | |
21 | parent::setUp(); | |
22 | require_once 'CRM/Core/Smarty.php'; | |
23 | require_once 'CRM/Core/Region.php'; | |
24 | ||
25 | // Templates injected into regions should normally be file names, but for unit-testing it's handy to use "string:" notation | |
26 | require_once 'CRM/Core/Smarty/resources/String.php'; | |
27 | civicrm_smarty_register_string_resource( ); | |
28 | } | |
29 | ||
30 | /** | |
31 | * When a {crmRegion} is blank and when there are no extra snippets, the | |
32 | * output is blank. | |
33 | */ | |
34 | function testBlank() { | |
35 | $smarty = CRM_Core_Smarty::singleton(); | |
36 | $actual = $smarty->fetch('string:{crmRegion name=testBlank}{/crmRegion}'); | |
37 | $expected = ''; | |
38 | $this->assertEquals($expected, $actual); | |
39 | } | |
40 | ||
41 | /** | |
42 | * When a {crmRegion} is not blank and when there are no extra snippets, | |
43 | * the output is only determined by the {crmRegion} block. | |
44 | */ | |
45 | function testDefault() { | |
46 | $smarty = CRM_Core_Smarty::singleton(); | |
47 | $actual = $smarty->fetch('string:{crmRegion name=testDefault}default<br/>{/crmRegion}'); | |
48 | $expected = 'default<br/>'; | |
49 | $this->assertEquals($expected, $actual); | |
50 | } | |
51 | ||
52 | /** | |
53 | * Disable the normal content of a {crmRegion} and apply different content from a snippet | |
54 | */ | |
55 | function testOverride() { | |
56 | CRM_Core_Region::instance('testOverride')->update('default', array( | |
57 | 'disabled' => TRUE, | |
58 | )); | |
59 | CRM_Core_Region::instance('testOverride')->add(array( | |
60 | 'markup' => 'override<br/>', | |
61 | )); | |
62 | ||
63 | $smarty = CRM_Core_Smarty::singleton(); | |
64 | $actual = $smarty->fetch('string:{crmRegion name=testOverride}default<br/>{/crmRegion}'); | |
65 | $expected = 'override<br/>'; | |
66 | $this->assertEquals($expected, $actual); | |
67 | } | |
68 | ||
69 | /** | |
70 | * Test that each of the major content formats are correctly evaluated | |
71 | */ | |
72 | function testAllTypes() { | |
73 | CRM_Core_Region::instance('testAllTypes')->add(array( | |
74 | 'markup' => 'some-markup<br/>', | |
75 | )); | |
76 | CRM_Core_Region::instance('testAllTypes')->add(array( | |
77 | // note: 'template' would normally be a file name | |
78 | 'template' => 'string:smarty-is-{$snippet.extrainfo}<br/>', | |
79 | 'extrainfo' => 'dynamic', | |
80 | )); | |
81 | CRM_Core_Region::instance('testAllTypes')->add(array( | |
82 | // note: returns a value which gets appended to the region | |
83 | 'callback' => 'implode', | |
84 | 'arguments' => array('-', array('callback','with','specific','args<br/>')), | |
85 | )); | |
86 | CRM_Core_Region::instance('testAllTypes')->add(array( | |
87 | // note: returns a value which gets appended to the region | |
88 | 'callback' => create_function('&$spec, &$html', 'return "callback-return<br/>";') | |
89 | )); | |
90 | CRM_Core_Region::instance('testAllTypes')->add(array( | |
91 | // note: returns void; directly modifies region's $html | |
92 | 'callback' => create_function('&$spec, &$html', '$html = "callback-ref<br/>" . $html;') | |
93 | )); | |
94 | CRM_Core_Region::instance('testAllTypes')->add(array( | |
95 | 'scriptUrl' => '/foo%20bar.js', | |
96 | )); | |
97 | CRM_Core_Region::instance('testAllTypes')->add(array( | |
98 | 'script' => 'alert("hi");', | |
99 | )); | |
100 | CRM_Core_Region::instance('testAllTypes')->add(array( | |
101 | 'jquery' => '$("div");', | |
102 | )); | |
103 | CRM_Core_Region::instance('testAllTypes')->add(array( | |
104 | 'styleUrl' => '/foo%20bar.css', | |
105 | )); | |
106 | CRM_Core_Region::instance('testAllTypes')->add(array( | |
107 | 'style' => 'body { background: black; }', | |
108 | )); | |
109 | ||
110 | $smarty = CRM_Core_Smarty::singleton(); | |
111 | $actual = $smarty->fetch('string:{crmRegion name=testAllTypes}default<br/>{/crmRegion}'); | |
112 | $expected = "callback-ref<br/>" | |
113 | . "default<br/>" | |
114 | . "some-markup<br/>" | |
115 | . "smarty-is-dynamic<br/>" | |
116 | . "callback-with-specific-args<br/>" | |
117 | . "callback-return<br/>" | |
118 | . "<script type=\"text/javascript\" src=\"/foo%20bar.js\">\n</script>\n" | |
119 | . "<script type=\"text/javascript\">\nalert(\"hi\");\n</script>\n" | |
3cc60a06 | 120 | . "<script type=\"text/javascript\">\nCRM.\$(function(\$) {\n\$(\"div\");\n});\n</script>\n" |
6a488035 TO |
121 | . "<link href=\"/foo%20bar.css\" rel=\"stylesheet\" type=\"text/css\"/>\n" |
122 | . "<style type=\"text/css\">\nbody { background: black; }\n</style>\n" | |
123 | ; | |
124 | $this->assertEquals($expected, $actual); | |
125 | } | |
126 | ||
127 | /** | |
128 | * Test of nested arrangement in which one {crmRegion} directly includes another {crmRegion} | |
129 | */ | |
130 | function testDirectNest() { | |
131 | CRM_Core_Region::instance('testDirectNestOuter')->add(array( | |
132 | 'template' => 'string:O={$snippet.weight} ', | |
133 | 'weight' => -5, | |
134 | )); | |
135 | CRM_Core_Region::instance('testDirectNestOuter')->add(array( | |
136 | 'template' => 'string:O={$snippet.weight} ', | |
137 | 'weight' => 5, | |
138 | )); | |
139 | ||
140 | CRM_Core_Region::instance('testDirectNestInner')->add(array( | |
141 | 'template' => 'string:I={$snippet.weight} ', | |
142 | 'weight' => -5, | |
143 | )); | |
144 | CRM_Core_Region::instance('testDirectNestInner')->add(array( | |
145 | 'template' => 'string:I={$snippet.weight} ', | |
146 | 'weight' => 5, | |
147 | )); | |
148 | ||
149 | $smarty = CRM_Core_Smarty::singleton(); | |
150 | $actual = $smarty->fetch('string:{crmRegion name=testDirectNestOuter}left {crmRegion name=testDirectNestInner}middle {/crmRegion}right {/crmRegion}'); | |
151 | $expected = 'O=-5 left I=-5 middle I=5 right O=5 '; | |
152 | $this->assertEquals($expected, $actual); | |
153 | } | |
154 | ||
155 | /** | |
156 | * Test of nested arrangement in which one {crmRegion} is enhanced with a snippet which, in turn, includes another {crmRegion} | |
157 | */ | |
158 | function testIndirectNest() { | |
159 | CRM_Core_Region::instance('testIndirectNestOuter')->add(array( | |
160 | // Note: all three $snippet references are bound to the $snippet which caused this template to be included, | |
161 | // regardless of any nested {crmRegion}s | |
162 | 'template' => 'string: O={$snippet.region}{crmRegion name=testIndirectNestInner} O={$snippet.region}{/crmRegion} O={$snippet.region}', | |
163 | )); | |
164 | ||
165 | CRM_Core_Region::instance('testIndirectNestInner')->add(array( | |
166 | 'template' => 'string: I={$snippet.region}', | |
167 | )); | |
168 | ||
169 | $smarty = CRM_Core_Smarty::singleton(); | |
170 | $actual = $smarty->fetch('string:{crmRegion name=testIndirectNestOuter}default{/crmRegion}'); | |
171 | $expected = 'default O=testIndirectNestOuter O=testIndirectNestOuter I=testIndirectNestInner O=testIndirectNestOuter'; | |
172 | $this->assertEquals($expected, $actual); | |
173 | } | |
174 | ||
175 | /** | |
176 | * Output from an inner-region should not be executed verbatim; this is obvious but good to verify | |
177 | */ | |
178 | function testNoInjection() { | |
179 | CRM_Core_Region::instance('testNoInjectionOuter')->add(array( | |
180 | 'template' => 'string:{$snippet.scarystuff} ', | |
181 | 'scarystuff' => '{$is_outer_scary}', | |
182 | )); | |
183 | CRM_Core_Region::instance('testNoInjectionInner')->add(array( | |
184 | 'template' => 'string:{$snippet.scarystuff} ', | |
185 | 'scarystuff' => '{$is_inner_scary}', | |
186 | )); | |
187 | ||
188 | $smarty = CRM_Core_Smarty::singleton(); | |
189 | $smarty->assign('is_outer_scary', 'egad'); | |
190 | $smarty->assign('is_inner_scary', 'egad'); | |
191 | $smarty->assign('also_scary', 'egad'); | |
192 | $actual = $smarty->fetch('string:{crmRegion name=testNoInjectionOuter}left {crmRegion name=testNoInjectionInner}middle {literal}{$also_scary}{/literal} {/crmRegion}right {/crmRegion}'); | |
193 | $expected = 'left middle {$also_scary} {$is_inner_scary} right {$is_outer_scary} '; | |
194 | $this->assertEquals($expected, $actual); | |
195 | } | |
196 | ||
197 | /** | |
198 | * Make sure that standard Smarty variables ($smarty->assign(...)) as well | |
199 | * as the magical $snippet variable both evaluate correctly. | |
200 | */ | |
201 | function testSmartyVars() { | |
202 | $smarty = CRM_Core_Smarty::singleton(); | |
203 | $smarty->assign('extrainfo', 'one'); | |
204 | CRM_Core_Region::instance('testSmartyVars')->add(array( | |
205 | 'template' => 'string:var-style-{$extrainfo}<br/>', | |
206 | )); | |
207 | ||
208 | CRM_Core_Region::instance('testSmartyVars')->add(array( | |
209 | 'template' => 'string:var-style-{$snippet.extrainfo}<br/>', | |
210 | 'extrainfo' => 'two', | |
211 | )); | |
212 | ||
213 | $actual = $smarty->fetch('string:{crmRegion name=testSmartyVars}default<br/>{/crmRegion}'); | |
214 | $expected = 'default<br/>var-style-one<br/>var-style-two<br/>'; | |
215 | $this->assertEquals($expected, $actual); | |
216 | } | |
217 | ||
218 | function testWeight() { | |
219 | CRM_Core_Region::instance('testWeight')->add(array( | |
220 | 'markup' => 'prepend-5<br/>', | |
221 | 'weight' => -5, | |
222 | )); | |
223 | CRM_Core_Region::instance('testWeight')->add(array( | |
224 | 'markup' => 'append+3<br/>', | |
225 | 'weight' => 3, | |
226 | )); | |
227 | CRM_Core_Region::instance('testWeight')->add(array( | |
228 | 'markup' => 'prepend-3<br/>', | |
229 | 'weight' => -3, | |
230 | )); | |
231 | CRM_Core_Region::instance('testWeight')->add(array( | |
232 | 'markup' => 'append+5<br/>', | |
233 | 'weight' => 5, | |
234 | )); | |
235 | ||
236 | $smarty = CRM_Core_Smarty::singleton(); | |
237 | $actual = $smarty->fetch('string:{crmRegion name=testWeight}default<br/>{/crmRegion}'); | |
238 | $expected = 'prepend-5<br/>prepend-3<br/>default<br/>append+3<br/>append+5<br/>'; | |
239 | $this->assertEquals($expected, $actual); | |
240 | } | |
241 | ||
242 | } |