SandboxedModuleTest.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. require_once dirname(__FILE__).'/TestCase.php';
  11. class Twig_Tests_Node_SandboxedModuleTest extends Twig_Tests_Node_TestCase
  12. {
  13. /**
  14. * @covers Twig_Node_SandboxedModule::__construct
  15. */
  16. public function testConstructor()
  17. {
  18. $body = new Twig_Node_Text('foo', 0);
  19. $parent = new Twig_Node_Expression_Constant('layout.twig', 0);
  20. $blocks = new Twig_Node();
  21. $macros = new Twig_Node();
  22. $traits = new Twig_Node();
  23. $filename = 'foo.twig';
  24. $node = new Twig_Node_Module($body, $parent, $blocks, $macros, $traits, $filename);
  25. $node = new Twig_Node_SandboxedModule($node, array('for'), array('upper'), array('cycle'));
  26. $this->assertEquals($body, $node->getNode('body'));
  27. $this->assertEquals($blocks, $node->getNode('blocks'));
  28. $this->assertEquals($macros, $node->getNode('macros'));
  29. $this->assertEquals($parent, $node->getNode('parent'));
  30. $this->assertEquals($filename, $node->getAttribute('filename'));
  31. }
  32. /**
  33. * @covers Twig_Node_SandboxedModule::compile
  34. * @covers Twig_Node_SandboxedModule::compileDisplayBody
  35. * @covers Twig_Node_SandboxedModule::compileDisplayFooter
  36. * @dataProvider getTests
  37. */
  38. public function testCompile($node, $source, $environment = null)
  39. {
  40. parent::testCompile($node, $source, $environment);
  41. }
  42. public function getTests()
  43. {
  44. $twig = new Twig_Environment(new Twig_Loader_String());
  45. $tests = array();
  46. $body = new Twig_Node_Text('foo', 0);
  47. $extends = null;
  48. $blocks = new Twig_Node();
  49. $macros = new Twig_Node();
  50. $traits = new Twig_Node();
  51. $filename = 'foo.twig';
  52. $node = new Twig_Node_Module($body, $extends, $blocks, $macros, $traits, $filename);
  53. $node = new Twig_Node_SandboxedModule($node, array('for'), array('upper'), array('cycle'));
  54. $tests[] = array($node, <<<EOF
  55. <?php
  56. /* foo.twig */
  57. class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template
  58. {
  59. protected function doDisplay(array \$context, array \$blocks = array())
  60. {
  61. \$this->checkSecurity();
  62. echo "foo";
  63. }
  64. protected function checkSecurity() {
  65. \$this->env->getExtension('sandbox')->checkSecurity(
  66. array('upper'),
  67. array('for'),
  68. array('cycle')
  69. );
  70. }
  71. public function getTemplateName()
  72. {
  73. return "foo.twig";
  74. }
  75. }
  76. EOF
  77. , $twig);
  78. $body = new Twig_Node();
  79. $extends = new Twig_Node_Expression_Constant('layout.twig', 0);
  80. $blocks = new Twig_Node();
  81. $macros = new Twig_Node();
  82. $traits = new Twig_Node();
  83. $filename = 'foo.twig';
  84. $node = new Twig_Node_Module($body, $extends, $blocks, $macros, $traits, $filename);
  85. $node = new Twig_Node_SandboxedModule($node, array('for'), array('upper'), array('cycle'));
  86. $tests[] = array($node, <<<EOF
  87. <?php
  88. /* foo.twig */
  89. class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template
  90. {
  91. protected function doGetParent(array \$context)
  92. {
  93. return "layout.twig";
  94. }
  95. protected function doDisplay(array \$context, array \$blocks = array())
  96. {
  97. \$this->getParent(\$context)->display(\$context, array_merge(\$this->blocks, \$blocks));
  98. }
  99. protected function checkSecurity() {
  100. \$this->env->getExtension('sandbox')->checkSecurity(
  101. array('upper'),
  102. array('for'),
  103. array('cycle')
  104. );
  105. \$this->parent->checkSecurity();
  106. }
  107. public function getTemplateName()
  108. {
  109. return "foo.twig";
  110. }
  111. public function isTraitable()
  112. {
  113. return false;
  114. }
  115. }
  116. EOF
  117. , $twig);
  118. return $tests;
  119. }
  120. }