3 require_once 'CiviTest/CiviUnitTestCase.php';
6 * Class CRM_Core_RegionTest
8 class CRM_Core_RegionTest
extends CiviUnitTestCase
{
15 'description' => 'Smarty-region rendering test',
22 require_once 'CRM/Core/Smarty.php';
23 require_once 'CRM/Core/Region.php';
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( );
31 * When a {crmRegion} is blank and when there are no extra snippets, the
34 function testBlank() {
35 $smarty = CRM_Core_Smarty
::singleton();
36 $actual = $smarty->fetch('string:{crmRegion name=testBlank}{/crmRegion}');
38 $this->assertEquals($expected, $actual);
42 * When a {crmRegion} is not blank and when there are no extra snippets,
43 * the output is only determined by the {crmRegion} block.
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);
53 * Disable the normal content of a {crmRegion} and apply different content from a snippet
55 function testOverride() {
56 CRM_Core_Region
::instance('testOverride')->update('default', array(
59 CRM_Core_Region
::instance('testOverride')->add(array(
60 'markup' => 'override<br/>',
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);
70 * Test that each of the major content formats are correctly evaluated
72 function testAllTypes() {
73 CRM_Core_Region
::instance('testAllTypes')->add(array(
74 'markup' => 'some-markup<br/>',
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',
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/>')),
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/>";')
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;')
94 CRM_Core_Region
::instance('testAllTypes')->add(array(
95 'scriptUrl' => '/foo%20bar.js',
97 CRM_Core_Region
::instance('testAllTypes')->add(array(
98 'script' => 'alert("hi");',
100 CRM_Core_Region
::instance('testAllTypes')->add(array(
101 'jquery' => '$("div");',
103 CRM_Core_Region
::instance('testAllTypes')->add(array(
104 'styleUrl' => '/foo%20bar.css',
106 CRM_Core_Region
::instance('testAllTypes')->add(array(
107 'style' => 'body { background: black; }',
110 $smarty = CRM_Core_Smarty
::singleton();
111 $actual = $smarty->fetch('string:{crmRegion name=testAllTypes}default<br/>{/crmRegion}');
112 $expected = "callback-ref<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"
120 . "<script type=\"text/javascript\">\nCRM.\$(function(\$) {\n\$(\"div\");\n});\n</script>\n"
121 . "<link href=\"/foo%20bar.css\" rel=\"stylesheet\" type=\"text/css\"/>\n"
122 . "<style type=\"text/css\">\nbody { background: black; }\n</style>\n"
124 $this->assertEquals($expected, $actual);
128 * Test of nested arrangement in which one {crmRegion} directly includes another {crmRegion}
130 function testDirectNest() {
131 CRM_Core_Region
::instance('testDirectNestOuter')->add(array(
132 'template' => 'string:O={$snippet.weight} ',
135 CRM_Core_Region
::instance('testDirectNestOuter')->add(array(
136 'template' => 'string:O={$snippet.weight} ',
140 CRM_Core_Region
::instance('testDirectNestInner')->add(array(
141 'template' => 'string:I={$snippet.weight} ',
144 CRM_Core_Region
::instance('testDirectNestInner')->add(array(
145 'template' => 'string:I={$snippet.weight} ',
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);
156 * Test of nested arrangement in which one {crmRegion} is enhanced with a snippet which, in turn, includes another {crmRegion}
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}',
165 CRM_Core_Region
::instance('testIndirectNestInner')->add(array(
166 'template' => 'string: I={$snippet.region}',
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);
176 * Output from an inner-region should not be executed verbatim; this is obvious but good to verify
178 function testNoInjection() {
179 CRM_Core_Region
::instance('testNoInjectionOuter')->add(array(
180 'template' => 'string:{$snippet.scarystuff} ',
181 'scarystuff' => '{$is_outer_scary}',
183 CRM_Core_Region
::instance('testNoInjectionInner')->add(array(
184 'template' => 'string:{$snippet.scarystuff} ',
185 'scarystuff' => '{$is_inner_scary}',
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);
198 * Make sure that standard Smarty variables ($smarty->assign(...)) as well
199 * as the magical $snippet variable both evaluate correctly.
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/>',
208 CRM_Core_Region
::instance('testSmartyVars')->add(array(
209 'template' => 'string:var-style-{$snippet.extrainfo}<br/>',
210 'extrainfo' => 'two',
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);
218 function testWeight() {
219 CRM_Core_Region
::instance('testWeight')->add(array(
220 'markup' => 'prepend-5<br/>',
223 CRM_Core_Region
::instance('testWeight')->add(array(
224 'markup' => 'append+3<br/>',
227 CRM_Core_Region
::instance('testWeight')->add(array(
228 'markup' => 'prepend-3<br/>',
231 CRM_Core_Region
::instance('testWeight')->add(array(
232 'markup' => 'append+5<br/>',
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);