SandboxedModuleTest.php 4.5KB

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