MustacheInjectionTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. require_once '../Mustache.php';
  3. /**
  4. * @group mustache_injection
  5. */
  6. class MustacheInjectionSectionTest extends PHPUnit_Framework_TestCase {
  7. // interpolation
  8. public function testInterpolationInjection() {
  9. $data = array(
  10. 'a' => '{{ b }}',
  11. 'b' => 'FAIL'
  12. );
  13. $template = '{{ a }}';
  14. $output = '{{ b }}';
  15. $m = new Mustache();
  16. $this->assertEquals($output, $m->render($template, $data));
  17. }
  18. public function testUnescapedInterpolationInjection() {
  19. $data = array(
  20. 'a' => '{{ b }}',
  21. 'b' => 'FAIL'
  22. );
  23. $template = '{{{ a }}}';
  24. $output = '{{ b }}';
  25. $m = new Mustache();
  26. $this->assertEquals($output, $m->render($template, $data));
  27. }
  28. // sections
  29. public function testSectionInjection() {
  30. $data = array(
  31. 'a' => true,
  32. 'b' => '{{ c }}',
  33. 'c' => 'FAIL'
  34. );
  35. $template = '{{# a }}{{ b }}{{/ a }}';
  36. $output = '{{ c }}';
  37. $m = new Mustache();
  38. $this->assertEquals($output, $m->render($template, $data));
  39. }
  40. public function testUnescapedSectionInjection() {
  41. $data = array(
  42. 'a' => true,
  43. 'b' => '{{ c }}',
  44. 'c' => 'FAIL'
  45. );
  46. $template = '{{# a }}{{{ b }}}{{/ a }}';
  47. $output = '{{ c }}';
  48. $m = new Mustache();
  49. $this->assertEquals($output, $m->render($template, $data));
  50. }
  51. // partials
  52. public function testPartialInjection() {
  53. $data = array(
  54. 'a' => '{{ b }}',
  55. 'b' => 'FAIL'
  56. );
  57. $template = '{{> partial }}';
  58. $partials = array(
  59. 'partial' => '{{ a }}',
  60. );
  61. $output = '{{ b }}';
  62. $m = new Mustache();
  63. $this->assertEquals($output, $m->render($template, $data, $partials));
  64. }
  65. public function testPartialUnescapedInjection() {
  66. $data = array(
  67. 'a' => '{{ b }}',
  68. 'b' => 'FAIL'
  69. );
  70. $template = '{{> partial }}';
  71. $partials = array(
  72. 'partial' => '{{{ a }}}',
  73. );
  74. $output = '{{ b }}';
  75. $m = new Mustache();
  76. $this->assertEquals($output, $m->render($template, $data, $partials));
  77. }
  78. // lambdas
  79. public function testLambdaInterpolationInjection() {
  80. $data = array(
  81. 'a' => array($this, 'interpolationLambda'),
  82. 'b' => '{{ c }}',
  83. 'c' => 'FAIL'
  84. );
  85. $template = '{{ a }}';
  86. $output = '{{ c }}';
  87. $m = new Mustache();
  88. $this->assertEquals($output, $m->render($template, $data));
  89. }
  90. public function interpolationLambda() {
  91. return '{{ b }}';
  92. }
  93. public function testLambdaSectionInjection() {
  94. $data = array(
  95. 'a' => array($this, 'sectionLambda'),
  96. 'b' => '{{ c }}',
  97. 'c' => 'FAIL'
  98. );
  99. $template = '{{# a }}b{{/ a }}';
  100. $output = '{{ c }}';
  101. $m = new Mustache();
  102. $this->assertEquals($output, $m->render($template, $data));
  103. }
  104. public function sectionLambda($content) {
  105. return '{{ ' . $content . ' }}';
  106. }
  107. }