IntegrationTest.php 4.9KB

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