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