Security update to Drupal 8.4.6
[yaffs-website] / vendor / twig / twig / test / Twig / Tests / IntegrationTest.php
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_SimpleFilter('§', array($this, '§Filter')),
140             new Twig_SimpleFilter('escape_and_nl2br', array($this, 'escape_and_nl2br'), array('needs_environment' => true, 'is_safe' => array('html'))),
141             new Twig_SimpleFilter('nl2br', array($this, 'nl2br'), array('pre_escape' => 'html', 'is_safe' => array('html'))),
142             new Twig_SimpleFilter('escape_something', array($this, 'escape_something'), array('is_safe' => array('something'))),
143             new Twig_SimpleFilter('preserves_safety', array($this, 'preserves_safety'), array('preserves_safety' => array('html'))),
144             new Twig_SimpleFilter('static_call_string', 'TwigTestExtension::staticCall'),
145             new Twig_SimpleFilter('static_call_array', array('TwigTestExtension', 'staticCall')),
146             new Twig_SimpleFilter('magic_call', array($this, 'magicCall')),
147             new Twig_SimpleFilter('magic_call_string', 'TwigTestExtension::magicStaticCall'),
148             new Twig_SimpleFilter('magic_call_array', array('TwigTestExtension', 'magicStaticCall')),
149             new Twig_SimpleFilter('*_path', array($this, 'dynamic_path')),
150             new Twig_SimpleFilter('*_foo_*_bar', array($this, 'dynamic_foo')),
151         );
152     }
153
154     public function getFunctions()
155     {
156         return array(
157             new Twig_SimpleFunction('§', array($this, '§Function')),
158             new Twig_SimpleFunction('safe_br', array($this, 'br'), array('is_safe' => array('html'))),
159             new Twig_SimpleFunction('unsafe_br', array($this, 'br')),
160             new Twig_SimpleFunction('static_call_string', 'TwigTestExtension::staticCall'),
161             new Twig_SimpleFunction('static_call_array', array('TwigTestExtension', 'staticCall')),
162             new Twig_SimpleFunction('*_path', array($this, 'dynamic_path')),
163             new Twig_SimpleFunction('*_foo_*_bar', array($this, 'dynamic_foo')),
164         );
165     }
166
167     public function getTests()
168     {
169         return array(
170             new Twig_SimpleTest('multi word', array($this, 'is_multi_word')),
171         );
172     }
173
174     public function §Filter($value)
175     {
176         return "§{$value}§";
177     }
178
179     public function §Function($value)
180     {
181         return "§{$value}§";
182     }
183
184     /**
185      * nl2br which also escapes, for testing escaper filters.
186      */
187     public function escape_and_nl2br($env, $value, $sep = '<br />')
188     {
189         return $this->nl2br(twig_escape_filter($env, $value, 'html'), $sep);
190     }
191
192     /**
193      * nl2br only, for testing filters with pre_escape.
194      */
195     public function nl2br($value, $sep = '<br />')
196     {
197         // not secure if $value contains html tags (not only entities)
198         // don't use
199         return str_replace("\n", "$sep\n", $value);
200     }
201
202     public function dynamic_path($element, $item)
203     {
204         return $element.'/'.$item;
205     }
206
207     public function dynamic_foo($foo, $bar, $item)
208     {
209         return $foo.'/'.$bar.'/'.$item;
210     }
211
212     public function escape_something($value)
213     {
214         return strtoupper($value);
215     }
216
217     public function preserves_safety($value)
218     {
219         return strtoupper($value);
220     }
221
222     public static function staticCall($value)
223     {
224         return "*$value*";
225     }
226
227     public function br()
228     {
229         return '<br />';
230     }
231
232     public function is_multi_word($value)
233     {
234         return false !== strpos($value, ' ');
235     }
236
237     public function __call($method, $arguments)
238     {
239         if ('magicCall' !== $method) {
240             throw new BadMethodCallException('Unexpected call to __call');
241         }
242
243         return 'magic_'.$arguments[0];
244     }
245
246     public static function __callStatic($method, $arguments)
247     {
248         if ('magicStaticCall' !== $method) {
249             throw new BadMethodCallException('Unexpected call to __callStatic');
250         }
251
252         return 'static_magic_'.$arguments[0];
253     }
254 }
255
256 /**
257  * This class is used in tests for the "length" filter and "empty" test. It asserts that __call is not
258  * used to convert such objects to strings.
259  */
260 class MagicCallStub
261 {
262     public function __call($name, $args)
263     {
264         throw new Exception('__call shall not be called');
265     }
266 }
267
268 class ToStringStub
269 {
270     /**
271      * @var string
272      */
273     private $string;
274
275     public function __construct($string)
276     {
277         $this->string = $string;
278     }
279
280     public function __toString()
281     {
282         return $this->string;
283     }
284 }
285
286 /**
287  * This class is used in tests for the length filter and empty test to show
288  * that when \Countable is implemented, it is preferred over the __toString()
289  * method.
290  */
291 class CountableStub implements \Countable
292 {
293     private $count;
294
295     public function __construct($count)
296     {
297         $this->count = $count;
298     }
299
300     public function count()
301     {
302         return $this->count;
303     }
304
305     public function __toString()
306     {
307         throw new Exception('__toString shall not be called on \Countables');
308     }
309 }
310
311 /**
312  * This class is used in tests for the length filter.
313  */
314 class IteratorAggregateStub implements \IteratorAggregate
315 {
316     private $data;
317
318     public function __construct(array $data)
319     {
320         $this->data = $data;
321     }
322
323     public function getIterator()
324     {
325         return new ArrayIterator($this->data);
326     }
327 }