3 require_once 'CiviTest/CiviUnitTestCase.php';
6 * Class CRM_Core_RegionTest
8 class CRM_Core_RegionTest
extends CiviUnitTestCase
{
9 public function setUp() {
11 require_once 'CRM/Core/Smarty.php';
12 require_once 'CRM/Core/Region.php';
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';
16 civicrm_smarty_register_string_resource();
20 * When a {crmRegion} is blank and when there are no extra snippets, the
23 public function testBlank() {
24 $smarty = CRM_Core_Smarty
::singleton();
25 $actual = $smarty->fetch('string:{crmRegion name=testBlank}{/crmRegion}');
27 $this->assertEquals($expected, $actual);
31 * When a {crmRegion} is not blank and when there are no extra snippets,
32 * the output is only determined by the {crmRegion} block.
34 public function testDefault() {
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);
42 * Disable the normal content of a {crmRegion} and apply different content from a snippet
44 public function testOverride() {
45 CRM_Core_Region
::instance('testOverride')->update('default', array(
48 CRM_Core_Region
::instance('testOverride')->add(array(
49 'markup' => 'override<br/>',
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);
59 * Test that each of the major content formats are correctly evaluated.
61 public function testAllTypes() {
62 CRM_Core_Region
::instance('testAllTypes')->add(array(
63 'markup' => 'some-markup<br/>',
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',
70 CRM_Core_Region
::instance('testAllTypes')->add(array(
71 // note: returns a value which gets appended to the region
72 'callback' => 'implode',
73 'arguments' => array('-', array('callback', 'with', 'specific', 'args<br/>')),
75 CRM_Core_Region
::instance('testAllTypes')->add(array(
76 // note: returns a value which gets appended to the region
77 'callback' => create_function('&$spec, &$html', 'return "callback-return<br/>";'),
79 CRM_Core_Region
::instance('testAllTypes')->add(array(
80 // note: returns void; directly modifies region's $html
81 'callback' => create_function('&$spec, &$html', '$html = "callback-ref<br/>" . $html;'),
83 CRM_Core_Region
::instance('testAllTypes')->add(array(
84 'scriptUrl' => '/foo%20bar.js',
86 CRM_Core_Region
::instance('testAllTypes')->add(array(
87 'script' => 'alert("hi");',
89 CRM_Core_Region
::instance('testAllTypes')->add(array(
90 'jquery' => '$("div");',
92 CRM_Core_Region
::instance('testAllTypes')->add(array(
93 'styleUrl' => '/foo%20bar.css',
95 CRM_Core_Region
::instance('testAllTypes')->add(array(
96 'style' => 'body { background: black; }',
99 $smarty = CRM_Core_Smarty
::singleton();
100 $actual = $smarty->fetch('string:{crmRegion name=testAllTypes}default<br/>{/crmRegion}');
101 $expected = "callback-ref<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"
109 . "<script type=\"text/javascript\">\nCRM.\$(function(\$) {\n\$(\"div\");\n});\n</script>\n"
110 . "<link href=\"/foo%20bar.css\" rel=\"stylesheet\" type=\"text/css\"/>\n"
111 . "<style type=\"text/css\">\nbody { background: black; }\n</style>\n";
112 $this->assertEquals($expected, $actual);
116 * Test of nested arrangement in which one {crmRegion} directly includes another {crmRegion}
118 public function testDirectNest() {
119 CRM_Core_Region
::instance('testDirectNestOuter')->add(array(
120 'template' => 'string:O={$snippet.weight} ',
123 CRM_Core_Region
::instance('testDirectNestOuter')->add(array(
124 'template' => 'string:O={$snippet.weight} ',
128 CRM_Core_Region
::instance('testDirectNestInner')->add(array(
129 'template' => 'string:I={$snippet.weight} ',
132 CRM_Core_Region
::instance('testDirectNestInner')->add(array(
133 'template' => 'string:I={$snippet.weight} ',
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);
144 * Test of nested arrangement in which one {crmRegion} is enhanced with a snippet which, in turn, includes another {crmRegion}
146 public function testIndirectNest() {
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}',
153 CRM_Core_Region
::instance('testIndirectNestInner')->add(array(
154 'template' => 'string: I={$snippet.region}',
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);
164 * Output from an inner-region should not be executed verbatim; this is obvious but good to verify
166 public function testNoInjection() {
167 CRM_Core_Region
::instance('testNoInjectionOuter')->add(array(
168 'template' => 'string:{$snippet.scarystuff} ',
169 'scarystuff' => '{$is_outer_scary}',
171 CRM_Core_Region
::instance('testNoInjectionInner')->add(array(
172 'template' => 'string:{$snippet.scarystuff} ',
173 'scarystuff' => '{$is_inner_scary}',
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);
186 * Make sure that standard Smarty variables ($smarty->assign(...)) as well
187 * as the magical $snippet variable both evaluate correctly.
189 public function testSmartyVars() {
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/>',
196 CRM_Core_Region
::instance('testSmartyVars')->add(array(
197 'template' => 'string:var-style-{$snippet.extrainfo}<br/>',
198 'extrainfo' => 'two',
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);
206 public function testWeight() {
207 CRM_Core_Region
::instance('testWeight')->add(array(
208 'markup' => 'prepend-5<br/>',
211 CRM_Core_Region
::instance('testWeight')->add(array(
212 'markup' => 'append+3<br/>',
215 CRM_Core_Region
::instance('testWeight')->add(array(
216 'markup' => 'prepend-3<br/>',
219 CRM_Core_Region
::instance('testWeight')->add(array(
220 'markup' => 'append+5<br/>',
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);