SandboxedModuleTest.php 4.5KB

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