4 * This file is part of Twig.
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 // This function is defined to check that escaping strategies
13 // like html works even if a function with the same name is defined.
19 class Twig_Tests_IntegrationTest extends Twig_Test_IntegrationTestCase
21 public function getExtensions()
23 $policy = new Twig_Sandbox_SecurityPolicy(array(), array(), array(), array(), array());
26 new Twig_Extension_Debug(),
27 new Twig_Extension_Sandbox($policy, false),
28 new Twig_Extension_StringLoader(),
29 new TwigTestExtension(),
33 public function getFixturesDir()
35 return dirname(__FILE__).'/Fixtures/';
39 function test_foo($value = 'foo')
44 class TwigTestFoo implements Iterator
46 const BAR_NAME = 'bar';
49 public $array = array(1, 2);
51 public function bar($param1 = null, $param2 = null)
53 return 'bar'.($param1 ? '_'.$param1 : '').($param2 ? '-'.$param2 : '');
56 public function getFoo()
61 public function getSelf()
81 public function strToLower($value)
83 return strtolower($value);
86 public function rewind()
91 public function current()
93 return $this->array[$this->position];
101 public function next()
106 public function valid()
108 return isset($this->array[$this->position]);
112 class TwigTestTokenParser_§ extends Twig_TokenParser
114 public function parse(Twig_Token $token)
116 $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
118 return new Twig_Node_Print(new Twig_Node_Expression_Constant('§', -1), -1);
121 public function getTag()
127 class TwigTestExtension extends Twig_Extension
129 public function getTokenParsers()
132 new TwigTestTokenParser_§(),
136 public function getFilters()
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')),
154 public function getFunctions()
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')),
167 public function getTests()
170 new Twig_SimpleTest('multi word', array($this, 'is_multi_word')),
174 public function §Filter($value)
179 public function §Function($value)
185 * nl2br which also escapes, for testing escaper filters.
187 public function escape_and_nl2br($env, $value, $sep = '<br />')
189 return $this->nl2br(twig_escape_filter($env, $value, 'html'), $sep);
193 * nl2br only, for testing filters with pre_escape.
195 public function nl2br($value, $sep = '<br />')
197 // not secure if $value contains html tags (not only entities)
199 return str_replace("\n", "$sep\n", $value);
202 public function dynamic_path($element, $item)
204 return $element.'/'.$item;
207 public function dynamic_foo($foo, $bar, $item)
209 return $foo.'/'.$bar.'/'.$item;
212 public function escape_something($value)
214 return strtoupper($value);
217 public function preserves_safety($value)
219 return strtoupper($value);
222 public static function staticCall($value)
232 public function is_multi_word($value)
234 return false !== strpos($value, ' ');
237 public function __call($method, $arguments)
239 if ('magicCall' !== $method) {
240 throw new BadMethodCallException('Unexpected call to __call');
243 return 'magic_'.$arguments[0];
246 public static function __callStatic($method, $arguments)
248 if ('magicStaticCall' !== $method) {
249 throw new BadMethodCallException('Unexpected call to __callStatic');
252 return 'static_magic_'.$arguments[0];
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.
262 public function __call($name, $args)
264 throw new Exception('__call shall not be called');
275 public function __construct($string)
277 $this->string = $string;
280 public function __toString()
282 return $this->string;
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()
291 class CountableStub implements \Countable
295 public function __construct($count)
297 $this->count = $count;
300 public function count()
305 public function __toString()
307 throw new Exception('__toString shall not be called on \Countables');
312 * This class is used in tests for the length filter.
314 class IteratorAggregateStub implements \IteratorAggregate
318 public function __construct(array $data)
323 public function getIterator()
325 return new ArrayIterator($this->data);