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