]>
Commit | Line | Data |
---|---|---|
4f5b44bd NL |
1 | <?php |
2 | ||
3 | /* | |
4 | * This file is part of Twig. | |
5 | * | |
6 | * (c) Fabien Potencier | |
7 | * | |
8 | * For the full copyright and license information, please view the LICENSE | |
9 | * file that was distributed with this source code. | |
10 | */ | |
11 | ||
12 | class Twig_Tests_Node_SandboxedModuleTest extends Twig_Test_NodeTestCase | |
13 | { | |
14 | /** | |
15 | * @covers Twig_Node_SandboxedModule::__construct | |
16 | */ | |
17 | public function testConstructor() | |
18 | { | |
19 | $body = new Twig_Node_Text('foo', 1); | |
20 | $parent = new Twig_Node_Expression_Constant('layout.twig', 1); | |
21 | $blocks = new Twig_Node(); | |
22 | $macros = new Twig_Node(); | |
23 | $traits = new Twig_Node(); | |
24 | $filename = 'foo.twig'; | |
25 | $node = new Twig_Node_Module($body, $parent, $blocks, $macros, $traits, new Twig_Node(array()), $filename); | |
26 | $node = new Twig_Node_SandboxedModule($node, array('for'), array('upper'), array('cycle')); | |
27 | ||
28 | $this->assertEquals($body, $node->getNode('body')); | |
29 | $this->assertEquals($blocks, $node->getNode('blocks')); | |
30 | $this->assertEquals($macros, $node->getNode('macros')); | |
31 | $this->assertEquals($parent, $node->getNode('parent')); | |
32 | $this->assertEquals($filename, $node->getAttribute('filename')); | |
33 | } | |
34 | ||
35 | /** | |
36 | * @covers Twig_Node_SandboxedModule::compile | |
37 | * @covers Twig_Node_SandboxedModule::compileDisplayBody | |
38 | * @covers Twig_Node_SandboxedModule::compileDisplayFooter | |
39 | * @dataProvider getTests | |
40 | */ | |
41 | public function testCompile($node, $source, $environment = null) | |
42 | { | |
43 | parent::testCompile($node, $source, $environment); | |
44 | } | |
45 | ||
46 | public function getTests() | |
47 | { | |
48 | $twig = new Twig_Environment(new Twig_Loader_String()); | |
49 | ||
50 | $tests = array(); | |
51 | ||
52 | $body = new Twig_Node_Text('foo', 1); | |
53 | $extends = null; | |
54 | $blocks = new Twig_Node(); | |
55 | $macros = new Twig_Node(); | |
56 | $traits = new Twig_Node(); | |
57 | $filename = 'foo.twig'; | |
58 | ||
59 | $node = new Twig_Node_Module($body, $extends, $blocks, $macros, $traits, new Twig_Node(array()), $filename); | |
60 | $node = new Twig_Node_SandboxedModule($node, array('for'), array('upper'), array('cycle')); | |
61 | ||
62 | $tests[] = array($node, <<<EOF | |
63 | <?php | |
64 | ||
65 | /* foo.twig */ | |
66 | class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template | |
67 | { | |
68 | public function __construct(Twig_Environment \$env) | |
69 | { | |
70 | parent::__construct(\$env); | |
71 | ||
72 | \$this->parent = false; | |
73 | ||
74 | \$this->blocks = array( | |
75 | ); | |
76 | } | |
77 | ||
78 | protected function doDisplay(array \$context, array \$blocks = array()) | |
79 | { | |
80 | \$this->checkSecurity(); | |
81 | // line 1 | |
82 | echo "foo"; | |
83 | } | |
84 | ||
85 | protected function checkSecurity() | |
86 | { | |
87 | \$this->env->getExtension('sandbox')->checkSecurity( | |
88 | array('upper'), | |
89 | array('for'), | |
90 | array('cycle') | |
91 | ); | |
92 | } | |
93 | ||
94 | public function getTemplateName() | |
95 | { | |
96 | return "foo.twig"; | |
97 | } | |
98 | ||
99 | public function getDebugInfo() | |
100 | { | |
101 | return array ( 20 => 1,); | |
102 | } | |
103 | } | |
104 | EOF | |
105 | , $twig); | |
106 | ||
107 | $body = new Twig_Node(); | |
108 | $extends = new Twig_Node_Expression_Constant('layout.twig', 1); | |
109 | $blocks = new Twig_Node(); | |
110 | $macros = new Twig_Node(); | |
111 | $traits = new Twig_Node(); | |
112 | $filename = 'foo.twig'; | |
113 | ||
114 | $node = new Twig_Node_Module($body, $extends, $blocks, $macros, $traits, new Twig_Node(array()), $filename); | |
115 | $node = new Twig_Node_SandboxedModule($node, array('for'), array('upper'), array('cycle')); | |
116 | ||
117 | $tests[] = array($node, <<<EOF | |
118 | <?php | |
119 | ||
120 | /* foo.twig */ | |
121 | class __TwigTemplate_be925a7b06dda0dfdbd18a1509f7eb34 extends Twig_Template | |
122 | { | |
123 | public function __construct(Twig_Environment \$env) | |
124 | { | |
125 | parent::__construct(\$env); | |
126 | ||
127 | \$this->parent = \$this->env->loadTemplate("layout.twig"); | |
128 | ||
129 | \$this->blocks = array( | |
130 | ); | |
131 | } | |
132 | ||
133 | protected function doGetParent(array \$context) | |
134 | { | |
135 | return "layout.twig"; | |
136 | } | |
137 | ||
138 | protected function doDisplay(array \$context, array \$blocks = array()) | |
139 | { | |
140 | \$this->checkSecurity(); | |
141 | \$this->parent->display(\$context, array_merge(\$this->blocks, \$blocks)); | |
142 | } | |
143 | ||
144 | protected function checkSecurity() | |
145 | { | |
146 | \$this->env->getExtension('sandbox')->checkSecurity( | |
147 | array('upper'), | |
148 | array('for'), | |
149 | array('cycle') | |
150 | ); | |
151 | } | |
152 | ||
153 | public function getTemplateName() | |
154 | { | |
155 | return "foo.twig"; | |
156 | } | |
157 | ||
158 | public function isTraitable() | |
159 | { | |
160 | return false; | |
161 | } | |
162 | ||
163 | public function getDebugInfo() | |
164 | { | |
165 | return array (); | |
166 | } | |
167 | } | |
168 | EOF | |
169 | , $twig); | |
170 | ||
171 | return $tests; | |
172 | } | |
173 | } |