3 require_once 'CiviTest/CiviUnitTestCase.php';
5 class CRM_Core_RegionTest
extends CiviUnitTestCase
{
9 'description' => 'Smarty-region rendering test',
16 require_once 'CRM/Core/Smarty.php';
17 require_once 'CRM/Core/Region.php';
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( );
25 * When a {crmRegion} is blank and when there are no extra snippets, the
28 function testBlank() {
29 $smarty = CRM_Core_Smarty
::singleton();
30 $actual = $smarty->fetch('string:{crmRegion name=testBlank}{/crmRegion}');
32 $this->assertEquals($expected, $actual);
36 * When a {crmRegion} is not blank and when there are no extra snippets,
37 * the output is only determined by the {crmRegion} block.
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);
47 * Disable the normal content of a {crmRegion} and apply different content from a snippet
49 function testOverride() {
50 CRM_Core_Region
::instance('testOverride')->update('default', array(
53 CRM_Core_Region
::instance('testOverride')->add(array(
54 'markup' => 'override<br/>',
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);
64 * Test that each of the major content formats are correctly evaluated
66 function testAllTypes() {
67 CRM_Core_Region
::instance('testAllTypes')->add(array(
68 'markup' => 'some-markup<br/>',
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',
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/>')),
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/>";')
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;')
88 CRM_Core_Region
::instance('testAllTypes')->add(array(
89 'scriptUrl' => '/foo%20bar.js',
91 CRM_Core_Region
::instance('testAllTypes')->add(array(
92 'script' => 'alert("hi");',
94 CRM_Core_Region
::instance('testAllTypes')->add(array(
95 'jquery' => '$("div");',
97 CRM_Core_Region
::instance('testAllTypes')->add(array(
98 'styleUrl' => '/foo%20bar.css',
100 CRM_Core_Region
::instance('testAllTypes')->add(array(
101 'style' => 'body { background: black; }',
104 $smarty = CRM_Core_Smarty
::singleton();
105 $actual = $smarty->fetch('string:{crmRegion name=testAllTypes}default<br/>{/crmRegion}');
106 $expected = "callback-ref<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"
118 $this->assertEquals($expected, $actual);
122 * Test of nested arrangement in which one {crmRegion} directly includes another {crmRegion}
124 function testDirectNest() {
125 CRM_Core_Region
::instance('testDirectNestOuter')->add(array(
126 'template' => 'string:O={$snippet.weight} ',
129 CRM_Core_Region
::instance('testDirectNestOuter')->add(array(
130 'template' => 'string:O={$snippet.weight} ',
134 CRM_Core_Region
::instance('testDirectNestInner')->add(array(
135 'template' => 'string:I={$snippet.weight} ',
138 CRM_Core_Region
::instance('testDirectNestInner')->add(array(
139 'template' => 'string:I={$snippet.weight} ',
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);
150 * Test of nested arrangement in which one {crmRegion} is enhanced with a snippet which, in turn, includes another {crmRegion}
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}',
159 CRM_Core_Region
::instance('testIndirectNestInner')->add(array(
160 'template' => 'string: I={$snippet.region}',
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);
170 * Output from an inner-region should not be executed verbatim; this is obvious but good to verify
172 function testNoInjection() {
173 CRM_Core_Region
::instance('testNoInjectionOuter')->add(array(
174 'template' => 'string:{$snippet.scarystuff} ',
175 'scarystuff' => '{$is_outer_scary}',
177 CRM_Core_Region
::instance('testNoInjectionInner')->add(array(
178 'template' => 'string:{$snippet.scarystuff} ',
179 'scarystuff' => '{$is_inner_scary}',
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);
192 * Make sure that standard Smarty variables ($smarty->assign(...)) as well
193 * as the magical $snippet variable both evaluate correctly.
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/>',
202 CRM_Core_Region
::instance('testSmartyVars')->add(array(
203 'template' => 'string:var-style-{$snippet.extrainfo}<br/>',
204 'extrainfo' => 'two',
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);
212 function testWeight() {
213 CRM_Core_Region
::instance('testWeight')->add(array(
214 'markup' => 'prepend-5<br/>',
217 CRM_Core_Region
::instance('testWeight')->add(array(
218 'markup' => 'append+3<br/>',
221 CRM_Core_Region
::instance('testWeight')->add(array(
222 'markup' => 'prepend-3<br/>',
225 CRM_Core_Region
::instance('testWeight')->add(array(
226 'markup' => 'append+5<br/>',
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);