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