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 { |
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() { |
6a488035 | 45 | CRM_Core_Region::instance('testOverride')->update('default', array( |
92915c55 | 46 | 'disabled' => TRUE, |
6a488035 TO |
47 | )); |
48 | CRM_Core_Region::instance('testOverride')->add(array( | |
49 | 'markup' => 'override<br/>', | |
50 | )); | |
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() { |
6a488035 TO |
62 | CRM_Core_Region::instance('testAllTypes')->add(array( |
63 | 'markup' => 'some-markup<br/>', | |
64 | )); | |
65 | CRM_Core_Region::instance('testAllTypes')->add(array( | |
66 | // note: 'template' would normally be a file name | |
67 | 'template' => 'string:smarty-is-{$snippet.extrainfo}<br/>', | |
68 | 'extrainfo' => 'dynamic', | |
69 | )); | |
70 | CRM_Core_Region::instance('testAllTypes')->add(array( | |
71 | // note: returns a value which gets appended to the region | |
72 | 'callback' => 'implode', | |
6c6e6187 | 73 | 'arguments' => array('-', array('callback', 'with', 'specific', 'args<br/>')), |
6a488035 TO |
74 | )); |
75 | CRM_Core_Region::instance('testAllTypes')->add(array( | |
76 | // note: returns a value which gets appended to the region | |
21dfd5f5 | 77 | 'callback' => create_function('&$spec, &$html', 'return "callback-return<br/>";'), |
6a488035 TO |
78 | )); |
79 | CRM_Core_Region::instance('testAllTypes')->add(array( | |
80 | // note: returns void; directly modifies region's $html | |
21dfd5f5 | 81 | 'callback' => create_function('&$spec, &$html', '$html = "callback-ref<br/>" . $html;'), |
6a488035 TO |
82 | )); |
83 | CRM_Core_Region::instance('testAllTypes')->add(array( | |
84 | 'scriptUrl' => '/foo%20bar.js', | |
85 | )); | |
86 | CRM_Core_Region::instance('testAllTypes')->add(array( | |
87 | 'script' => 'alert("hi");', | |
88 | )); | |
89 | CRM_Core_Region::instance('testAllTypes')->add(array( | |
90 | 'jquery' => '$("div");', | |
91 | )); | |
92 | CRM_Core_Region::instance('testAllTypes')->add(array( | |
93 | 'styleUrl' => '/foo%20bar.css', | |
94 | )); | |
95 | CRM_Core_Region::instance('testAllTypes')->add(array( | |
96 | 'style' => 'body { background: black; }', | |
97 | )); | |
98 | ||
99 | $smarty = CRM_Core_Smarty::singleton(); | |
100 | $actual = $smarty->fetch('string:{crmRegion name=testAllTypes}default<br/>{/crmRegion}'); | |
101 | $expected = "callback-ref<br/>" | |
102 | . "default<br/>" | |
103 | . "some-markup<br/>" | |
104 | . "smarty-is-dynamic<br/>" | |
105 | . "callback-with-specific-args<br/>" | |
106 | . "callback-return<br/>" | |
107 | . "<script type=\"text/javascript\" src=\"/foo%20bar.js\">\n</script>\n" | |
108 | . "<script type=\"text/javascript\">\nalert(\"hi\");\n</script>\n" | |
3cc60a06 | 109 | . "<script type=\"text/javascript\">\nCRM.\$(function(\$) {\n\$(\"div\");\n});\n</script>\n" |
6a488035 | 110 | . "<link href=\"/foo%20bar.css\" rel=\"stylesheet\" type=\"text/css\"/>\n" |
6c6e6187 | 111 | . "<style type=\"text/css\">\nbody { background: black; }\n</style>\n"; |
6a488035 TO |
112 | $this->assertEquals($expected, $actual); |
113 | } | |
114 | ||
115 | /** | |
116 | * Test of nested arrangement in which one {crmRegion} directly includes another {crmRegion} | |
117 | */ | |
00be9182 | 118 | public function testDirectNest() { |
6a488035 TO |
119 | CRM_Core_Region::instance('testDirectNestOuter')->add(array( |
120 | 'template' => 'string:O={$snippet.weight} ', | |
121 | 'weight' => -5, | |
122 | )); | |
123 | CRM_Core_Region::instance('testDirectNestOuter')->add(array( | |
124 | 'template' => 'string:O={$snippet.weight} ', | |
125 | 'weight' => 5, | |
126 | )); | |
127 | ||
128 | CRM_Core_Region::instance('testDirectNestInner')->add(array( | |
129 | 'template' => 'string:I={$snippet.weight} ', | |
130 | 'weight' => -5, | |
131 | )); | |
132 | CRM_Core_Region::instance('testDirectNestInner')->add(array( | |
133 | 'template' => 'string:I={$snippet.weight} ', | |
134 | 'weight' => 5, | |
135 | )); | |
136 | ||
137 | $smarty = CRM_Core_Smarty::singleton(); | |
138 | $actual = $smarty->fetch('string:{crmRegion name=testDirectNestOuter}left {crmRegion name=testDirectNestInner}middle {/crmRegion}right {/crmRegion}'); | |
139 | $expected = 'O=-5 left I=-5 middle I=5 right O=5 '; | |
140 | $this->assertEquals($expected, $actual); | |
141 | } | |
142 | ||
143 | /** | |
144 | * Test of nested arrangement in which one {crmRegion} is enhanced with a snippet which, in turn, includes another {crmRegion} | |
145 | */ | |
00be9182 | 146 | public function testIndirectNest() { |
6a488035 TO |
147 | CRM_Core_Region::instance('testIndirectNestOuter')->add(array( |
148 | // Note: all three $snippet references are bound to the $snippet which caused this template to be included, | |
149 | // regardless of any nested {crmRegion}s | |
150 | 'template' => 'string: O={$snippet.region}{crmRegion name=testIndirectNestInner} O={$snippet.region}{/crmRegion} O={$snippet.region}', | |
151 | )); | |
152 | ||
153 | CRM_Core_Region::instance('testIndirectNestInner')->add(array( | |
154 | 'template' => 'string: I={$snippet.region}', | |
155 | )); | |
156 | ||
157 | $smarty = CRM_Core_Smarty::singleton(); | |
158 | $actual = $smarty->fetch('string:{crmRegion name=testIndirectNestOuter}default{/crmRegion}'); | |
159 | $expected = 'default O=testIndirectNestOuter O=testIndirectNestOuter I=testIndirectNestInner O=testIndirectNestOuter'; | |
160 | $this->assertEquals($expected, $actual); | |
161 | } | |
162 | ||
163 | /** | |
164 | * Output from an inner-region should not be executed verbatim; this is obvious but good to verify | |
165 | */ | |
00be9182 | 166 | public function testNoInjection() { |
6a488035 TO |
167 | CRM_Core_Region::instance('testNoInjectionOuter')->add(array( |
168 | 'template' => 'string:{$snippet.scarystuff} ', | |
169 | 'scarystuff' => '{$is_outer_scary}', | |
170 | )); | |
171 | CRM_Core_Region::instance('testNoInjectionInner')->add(array( | |
172 | 'template' => 'string:{$snippet.scarystuff} ', | |
173 | 'scarystuff' => '{$is_inner_scary}', | |
174 | )); | |
175 | ||
176 | $smarty = CRM_Core_Smarty::singleton(); | |
177 | $smarty->assign('is_outer_scary', 'egad'); | |
178 | $smarty->assign('is_inner_scary', 'egad'); | |
179 | $smarty->assign('also_scary', 'egad'); | |
180 | $actual = $smarty->fetch('string:{crmRegion name=testNoInjectionOuter}left {crmRegion name=testNoInjectionInner}middle {literal}{$also_scary}{/literal} {/crmRegion}right {/crmRegion}'); | |
181 | $expected = 'left middle {$also_scary} {$is_inner_scary} right {$is_outer_scary} '; | |
182 | $this->assertEquals($expected, $actual); | |
183 | } | |
184 | ||
185 | /** | |
186 | * Make sure that standard Smarty variables ($smarty->assign(...)) as well | |
187 | * as the magical $snippet variable both evaluate correctly. | |
188 | */ | |
00be9182 | 189 | public function testSmartyVars() { |
6a488035 TO |
190 | $smarty = CRM_Core_Smarty::singleton(); |
191 | $smarty->assign('extrainfo', 'one'); | |
192 | CRM_Core_Region::instance('testSmartyVars')->add(array( | |
193 | 'template' => 'string:var-style-{$extrainfo}<br/>', | |
194 | )); | |
195 | ||
196 | CRM_Core_Region::instance('testSmartyVars')->add(array( | |
197 | 'template' => 'string:var-style-{$snippet.extrainfo}<br/>', | |
198 | 'extrainfo' => 'two', | |
199 | )); | |
200 | ||
201 | $actual = $smarty->fetch('string:{crmRegion name=testSmartyVars}default<br/>{/crmRegion}'); | |
202 | $expected = 'default<br/>var-style-one<br/>var-style-two<br/>'; | |
203 | $this->assertEquals($expected, $actual); | |
204 | } | |
205 | ||
00be9182 | 206 | public function testWeight() { |
6a488035 TO |
207 | CRM_Core_Region::instance('testWeight')->add(array( |
208 | 'markup' => 'prepend-5<br/>', | |
209 | 'weight' => -5, | |
210 | )); | |
211 | CRM_Core_Region::instance('testWeight')->add(array( | |
212 | 'markup' => 'append+3<br/>', | |
213 | 'weight' => 3, | |
214 | )); | |
215 | CRM_Core_Region::instance('testWeight')->add(array( | |
216 | 'markup' => 'prepend-3<br/>', | |
217 | 'weight' => -3, | |
218 | )); | |
219 | CRM_Core_Region::instance('testWeight')->add(array( | |
220 | 'markup' => 'append+5<br/>', | |
221 | 'weight' => 5, | |
222 | )); | |
223 | ||
224 | $smarty = CRM_Core_Smarty::singleton(); | |
225 | $actual = $smarty->fetch('string:{crmRegion name=testWeight}default<br/>{/crmRegion}'); | |
226 | $expected = 'prepend-5<br/>prepend-3<br/>default<br/>append+3<br/>append+5<br/>'; | |
227 | $this->assertEquals($expected, $actual); | |
228 | } | |
229 | ||
230 | } |