c8cbaf7184c8fdac2c5c565978c0c1da4b5d538a
[yaffs-website] / vendor / twig / twig / test / Twig / Tests / Loader / FilesystemTest.php
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_Loader_FilesystemTest extends PHPUnit_Framework_TestCase
13 {
14     public function testGetSourceContext()
15     {
16         $path = dirname(__FILE__).'/../Fixtures';
17         $loader = new Twig_Loader_Filesystem(array($path));
18         $this->assertEquals('errors/index.html', $loader->getSourceContext('errors/index.html')->getName());
19         $this->assertEquals(realpath($path.'/errors/index.html'), realpath($loader->getSourceContext('errors/index.html')->getPath()));
20     }
21
22     /**
23      * @dataProvider getSecurityTests
24      */
25     public function testSecurity($template)
26     {
27         $loader = new Twig_Loader_Filesystem(array(dirname(__FILE__).'/../Fixtures'));
28
29         try {
30             $loader->getCacheKey($template);
31             $this->fail();
32         } catch (Twig_Error_Loader $e) {
33             $this->assertNotContains('Unable to find template', $e->getMessage());
34         }
35     }
36
37     public function getSecurityTests()
38     {
39         return array(
40             array("AutoloaderTest\0.php"),
41             array('..\\AutoloaderTest.php'),
42             array('..\\\\\\AutoloaderTest.php'),
43             array('../AutoloaderTest.php'),
44             array('..////AutoloaderTest.php'),
45             array('./../AutoloaderTest.php'),
46             array('.\\..\\AutoloaderTest.php'),
47             array('././././././../AutoloaderTest.php'),
48             array('.\\./.\\./.\\./../AutoloaderTest.php'),
49             array('foo/../../AutoloaderTest.php'),
50             array('foo\\..\\..\\AutoloaderTest.php'),
51             array('foo/../bar/../../AutoloaderTest.php'),
52             array('foo/bar/../../../AutoloaderTest.php'),
53             array('filters/../../AutoloaderTest.php'),
54             array('filters//..//..//AutoloaderTest.php'),
55             array('filters\\..\\..\\AutoloaderTest.php'),
56             array('filters\\\\..\\\\..\\\\AutoloaderTest.php'),
57             array('filters\\//../\\/\\..\\AutoloaderTest.php'),
58             array('/../AutoloaderTest.php'),
59         );
60     }
61
62     /**
63      * @dataProvider getBasePaths
64      */
65     public function testPaths($basePath, $cacheKey, $rootPath)
66     {
67         $loader = new Twig_Loader_Filesystem(array($basePath.'/normal', $basePath.'/normal_bis'), $rootPath);
68         $loader->setPaths(array($basePath.'/named', $basePath.'/named_bis'), 'named');
69         $loader->addPath($basePath.'/named_ter', 'named');
70         $loader->addPath($basePath.'/normal_ter');
71         $loader->prependPath($basePath.'/normal_final');
72         $loader->prependPath($basePath.'/named/../named_quater', 'named');
73         $loader->prependPath($basePath.'/named_final', 'named');
74
75         $this->assertEquals(array(
76             $basePath.'/normal_final',
77             $basePath.'/normal',
78             $basePath.'/normal_bis',
79             $basePath.'/normal_ter',
80         ), $loader->getPaths());
81         $this->assertEquals(array(
82             $basePath.'/named_final',
83             $basePath.'/named/../named_quater',
84             $basePath.'/named',
85             $basePath.'/named_bis',
86             $basePath.'/named_ter',
87         ), $loader->getPaths('named'));
88
89         // do not use realpath here as it would make the test unuseful
90         $this->assertEquals($cacheKey, str_replace('\\', '/', $loader->getCacheKey('@named/named_absolute.html')));
91         $this->assertEquals("path (final)\n", $loader->getSourceContext('index.html')->getCode());
92         $this->assertEquals("path (final)\n", $loader->getSourceContext('@__main__/index.html')->getCode());
93         $this->assertEquals("named path (final)\n", $loader->getSourceContext('@named/index.html')->getCode());
94     }
95
96     public function getBasePaths()
97     {
98         return array(
99             array(
100                 dirname(__FILE__).'/Fixtures',
101                 'test/Twig/Tests/Loader/Fixtures/named_quater/named_absolute.html',
102                 null,
103             ),
104             array(
105                 dirname(__FILE__).'/Fixtures/../Fixtures',
106                 'test/Twig/Tests/Loader/Fixtures/named_quater/named_absolute.html',
107                 null,
108             ),
109             array(
110                 'test/Twig/Tests/Loader/Fixtures',
111                 'test/Twig/Tests/Loader/Fixtures/named_quater/named_absolute.html',
112                 getcwd(),
113             ),
114             array(
115                 'Fixtures',
116                 'Fixtures/named_quater/named_absolute.html',
117                 getcwd().'/test/Twig/Tests/Loader',
118             ),
119             array(
120                 'Fixtures',
121                 'Fixtures/named_quater/named_absolute.html',
122                 getcwd().'/test/../test/Twig/Tests/Loader',
123             ),
124         );
125     }
126
127     public function testEmptyConstructor()
128     {
129         $loader = new Twig_Loader_Filesystem();
130         $this->assertEquals(array(), $loader->getPaths());
131     }
132
133     public function testGetNamespaces()
134     {
135         $loader = new Twig_Loader_Filesystem(sys_get_temp_dir());
136         $this->assertEquals(array(Twig_Loader_Filesystem::MAIN_NAMESPACE), $loader->getNamespaces());
137
138         $loader->addPath(sys_get_temp_dir(), 'named');
139         $this->assertEquals(array(Twig_Loader_Filesystem::MAIN_NAMESPACE, 'named'), $loader->getNamespaces());
140     }
141
142     public function testFindTemplateExceptionNamespace()
143     {
144         $basePath = dirname(__FILE__).'/Fixtures';
145
146         $loader = new Twig_Loader_Filesystem(array($basePath.'/normal'));
147         $loader->addPath($basePath.'/named', 'named');
148
149         try {
150             $loader->getSourceContext('@named/nowhere.html');
151         } catch (Exception $e) {
152             $this->assertInstanceof('Twig_Error_Loader', $e);
153             $this->assertContains('Unable to find template "@named/nowhere.html"', $e->getMessage());
154         }
155     }
156
157     public function testFindTemplateWithCache()
158     {
159         $basePath = dirname(__FILE__).'/Fixtures';
160
161         $loader = new Twig_Loader_Filesystem(array($basePath.'/normal'));
162         $loader->addPath($basePath.'/named', 'named');
163
164         // prime the cache for index.html in the named namespace
165         $namedSource = $loader->getSourceContext('@named/index.html')->getCode();
166         $this->assertEquals("named path\n", $namedSource);
167
168         // get index.html from the main namespace
169         $this->assertEquals("path\n", $loader->getSourceContext('index.html')->getCode());
170     }
171
172     public function testLoadTemplateAndRenderBlockWithCache()
173     {
174         $loader = new Twig_Loader_Filesystem(array());
175         $loader->addPath(dirname(__FILE__).'/Fixtures/themes/theme2');
176         $loader->addPath(dirname(__FILE__).'/Fixtures/themes/theme1');
177         $loader->addPath(dirname(__FILE__).'/Fixtures/themes/theme1', 'default_theme');
178
179         $twig = new Twig_Environment($loader);
180
181         $template = $twig->loadTemplate('blocks.html.twig');
182         $this->assertSame('block from theme 1', $template->renderBlock('b1', array()));
183
184         $template = $twig->loadTemplate('blocks.html.twig');
185         $this->assertSame('block from theme 2', $template->renderBlock('b2', array()));
186     }
187
188     public function getArrayInheritanceTests()
189     {
190         return array(
191             'valid array inheritance' => array('array_inheritance_valid_parent.html.twig'),
192             'array inheritance with null first template' => array('array_inheritance_null_parent.html.twig'),
193             'array inheritance with empty first template' => array('array_inheritance_empty_parent.html.twig'),
194             'array inheritance with non-existent first template' => array('array_inheritance_nonexistent_parent.html.twig'),
195         );
196     }
197
198     /**
199      * @dataProvider getArrayInheritanceTests
200      *
201      * @param $templateName string Template name with array inheritance
202      */
203     public function testArrayInheritance($templateName)
204     {
205         $loader = new Twig_Loader_Filesystem(array());
206         $loader->addPath(dirname(__FILE__).'/Fixtures/inheritance');
207
208         $twig = new Twig_Environment($loader);
209
210         $template = $twig->loadTemplate($templateName);
211         $this->assertSame('VALID Child', $template->renderBlock('body', array()));
212     }
213
214     /**
215      * @requires PHP 5.3
216      */
217     public function testLoadTemplateFromPhar()
218     {
219         $loader = new Twig_Loader_Filesystem(array());
220         // phar-sample.phar was created with the following script:
221         // $f = new Phar('phar-test.phar');
222         // $f->addFromString('hello.twig', 'hello from phar');
223         $loader->addPath('phar://'.dirname(__FILE__).'/Fixtures/phar/phar-sample.phar');
224         $this->assertSame('hello from phar', $loader->getSourceContext('hello.twig')->getCode());
225     }
226 }