SandboxedModuleTest.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 doGetParent(array \$context)
  60. {
  61. return false;
  62. }
  63. protected function doDisplay(array \$context, array \$blocks = array())
  64. {
  65. \$this->checkSecurity();
  66. echo "foo";
  67. }
  68. protected function checkSecurity() {
  69. \$this->env->getExtension('sandbox')->checkSecurity(
  70. array('upper'),
  71. array('for'),
  72. array('cycle')
  73. );
  74. }
  75. public function getTemplateName()
  76. {
  77. return "foo.twig";
  78. }
  79. public function isTraitable()
  80. {
  81. return true;
  82. }
  83. }
  84. EOF
  85. , $twig);
  86. $body = new Twig_Node();
  87. $extends = new Twig_Node_Expression_Constant('layout.twig', 0);
  88. $blocks = new Twig_Node();
  89. $macros = new Twig_Node();
  90. $traits = new Twig_Node();
  91. $filename = 'foo.twig';
  92. $node = new Twig_Node_Module($body, $extends, $blocks, $macros, $traits, $filename);
  93. $node = new Twig_Node_SandboxedModule($node, array('for'), array('upper'), array('cycle'));
  94. $tests[] = array($node, <<<EOF
  95. <?php
  96. /* foo.twig */
  97. class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template
  98. {
  99. protected function doGetParent(array \$context)
  100. {
  101. return "layout.twig";
  102. }
  103. protected function doDisplay(array \$context, array \$blocks = array())
  104. {
  105. \$this->getParent(\$context)->display(\$context, array_merge(\$this->blocks, \$blocks));
  106. }
  107. protected function checkSecurity() {
  108. \$this->env->getExtension('sandbox')->checkSecurity(
  109. array('upper'),
  110. array('for'),
  111. array('cycle')
  112. );
  113. \$this->parent->checkSecurity();
  114. }
  115. public function getTemplateName()
  116. {
  117. return "foo.twig";
  118. }
  119. public function isTraitable()
  120. {
  121. return false;
  122. }
  123. }
  124. EOF
  125. , $twig);
  126. return $tests;
  127. }
  128. }