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