]>
Commit | Line | Data |
---|---|---|
4f5b44bd NL |
1 | <?php |
2 | ||
3 | /* | |
4 | * This file is part of Twig. | |
5 | * | |
6 | * (c) Fabien Potencier | |
7 | * | |
8 | * For the full copyright and license information, please view the LICENSE | |
9 | * file that was distributed with this source code. | |
10 | */ | |
11 | ||
12 | // This function is defined to check that escaping strategies | |
13 | // like html works even if a function with the same name is defined. | |
14 | function html() | |
15 | { | |
16 | return 'foo'; | |
17 | } | |
18 | ||
19 | class Twig_Tests_IntegrationTest extends Twig_Test_IntegrationTestCase | |
20 | { | |
21 | public function getExtensions() | |
22 | { | |
23 | $policy = new Twig_Sandbox_SecurityPolicy(array(), array(), array(), array(), array()); | |
24 | ||
25 | return array( | |
26 | new Twig_Extension_Debug(), | |
27 | new Twig_Extension_Sandbox($policy, false), | |
28 | new Twig_Extension_StringLoader(), | |
29 | new TwigTestExtension(), | |
30 | ); | |
31 | } | |
32 | ||
33 | public function getFixturesDir() | |
34 | { | |
35 | return dirname(__FILE__).'/Fixtures/'; | |
36 | } | |
37 | } | |
38 | ||
39 | function test_foo($value = 'foo') | |
40 | { | |
41 | return $value; | |
42 | } | |
43 | ||
44 | class TwigTestFoo implements Iterator | |
45 | { | |
46 | const BAR_NAME = 'bar'; | |
47 | ||
48 | public $position = 0; | |
49 | public $array = array(1, 2); | |
50 | ||
51 | public function bar($param1 = null, $param2 = null) | |
52 | { | |
53 | return 'bar'.($param1 ? '_'.$param1 : '').($param2 ? '-'.$param2 : ''); | |
54 | } | |
55 | ||
56 | public function getFoo() | |
57 | { | |
58 | return 'foo'; | |
59 | } | |
60 | ||
61 | public function getSelf() | |
62 | { | |
63 | return $this; | |
64 | } | |
65 | ||
66 | public function is() | |
67 | { | |
68 | return 'is'; | |
69 | } | |
70 | ||
71 | public function in() | |
72 | { | |
73 | return 'in'; | |
74 | } | |
75 | ||
76 | public function not() | |
77 | { | |
78 | return 'not'; | |
79 | } | |
80 | ||
81 | public function strToLower($value) | |
82 | { | |
83 | return strtolower($value); | |
84 | } | |
85 | ||
86 | public function rewind() | |
87 | { | |
88 | $this->position = 0; | |
89 | } | |
90 | ||
91 | public function current() | |
92 | { | |
93 | return $this->array[$this->position]; | |
94 | } | |
95 | ||
96 | public function key() | |
97 | { | |
98 | return 'a'; | |
99 | } | |
100 | ||
101 | public function next() | |
102 | { | |
103 | ++$this->position; | |
104 | } | |
105 | ||
106 | public function valid() | |
107 | { | |
108 | return isset($this->array[$this->position]); | |
109 | } | |
110 | } | |
111 | ||
112 | class TwigTestTokenParser_§ extends Twig_TokenParser | |
113 | { | |
114 | public function parse(Twig_Token $token) | |
115 | { | |
116 | $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE); | |
117 | ||
118 | return new Twig_Node_Print(new Twig_Node_Expression_Constant('§', -1), -1); | |
119 | } | |
120 | ||
121 | public function getTag() | |
122 | { | |
123 | return '§'; | |
124 | } | |
125 | } | |
126 | ||
127 | class TwigTestExtension extends Twig_Extension | |
128 | { | |
129 | public function getTokenParsers() | |
130 | { | |
131 | return array( | |
132 | new TwigTestTokenParser_§(), | |
133 | ); | |
134 | } | |
135 | ||
136 | public function getFilters() | |
137 | { | |
138 | return array( | |
139 | '§' => new Twig_Filter_Method($this, '§Filter'), | |
140 | 'escape_and_nl2br' => new Twig_Filter_Method($this, 'escape_and_nl2br', array('needs_environment' => true, 'is_safe' => array('html'))), | |
141 | 'nl2br' => new Twig_Filter_Method($this, 'nl2br', array('pre_escape' => 'html', 'is_safe' => array('html'))), | |
142 | 'escape_something' => new Twig_Filter_Method($this, 'escape_something', array('is_safe' => array('something'))), | |
143 | 'preserves_safety' => new Twig_Filter_Method($this, 'preserves_safety', array('preserves_safety' => array('html'))), | |
144 | '*_path' => new Twig_Filter_Method($this, 'dynamic_path'), | |
145 | '*_foo_*_bar' => new Twig_Filter_Method($this, 'dynamic_foo'), | |
146 | ); | |
147 | } | |
148 | ||
149 | public function getFunctions() | |
150 | { | |
151 | return array( | |
152 | '§' => new Twig_Function_Method($this, '§Function'), | |
153 | 'safe_br' => new Twig_Function_Method($this, 'br', array('is_safe' => array('html'))), | |
154 | 'unsafe_br' => new Twig_Function_Method($this, 'br'), | |
155 | '*_path' => new Twig_Function_Method($this, 'dynamic_path'), | |
156 | '*_foo_*_bar' => new Twig_Function_Method($this, 'dynamic_foo'), | |
157 | ); | |
158 | } | |
159 | ||
160 | public function §Filter($value) | |
161 | { | |
162 | return "§{$value}§"; | |
163 | } | |
164 | ||
165 | public function §Function($value) | |
166 | { | |
167 | return "§{$value}§"; | |
168 | } | |
169 | ||
170 | /** | |
171 | * nl2br which also escapes, for testing escaper filters | |
172 | */ | |
173 | public function escape_and_nl2br($env, $value, $sep = '<br />') | |
174 | { | |
175 | return $this->nl2br(twig_escape_filter($env, $value, 'html'), $sep); | |
176 | } | |
177 | ||
178 | /** | |
179 | * nl2br only, for testing filters with pre_escape | |
180 | */ | |
181 | public function nl2br($value, $sep = '<br />') | |
182 | { | |
183 | // not secure if $value contains html tags (not only entities) | |
184 | // don't use | |
185 | return str_replace("\n", "$sep\n", $value); | |
186 | } | |
187 | ||
188 | public function dynamic_path($element, $item) | |
189 | { | |
190 | return $element.'/'.$item; | |
191 | } | |
192 | ||
193 | public function dynamic_foo($foo, $bar, $item) | |
194 | { | |
195 | return $foo.'/'.$bar.'/'.$item; | |
196 | } | |
197 | ||
198 | public function escape_something($value) | |
199 | { | |
200 | return strtoupper($value); | |
201 | } | |
202 | ||
203 | public function preserves_safety($value) | |
204 | { | |
205 | return strtoupper($value); | |
206 | } | |
207 | ||
208 | public function br() | |
209 | { | |
210 | return '<br />'; | |
211 | } | |
212 | ||
213 | public function getName() | |
214 | { | |
215 | return 'integration_test'; | |
216 | } | |
217 | } |