]>
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_Loader_FilesystemTest extends PHPUnit_Framework_TestCase | |
13 | { | |
14 | /** | |
15 | * @dataProvider getSecurityTests | |
16 | */ | |
17 | public function testSecurity($template) | |
18 | { | |
19 | $loader = new Twig_Loader_Filesystem(array(dirname(__FILE__).'/../Fixtures')); | |
20 | ||
21 | try { | |
22 | $loader->getCacheKey($template); | |
23 | $this->fail(); | |
24 | } catch (Twig_Error_Loader $e) { | |
25 | $this->assertNotContains('Unable to find template', $e->getMessage()); | |
26 | } | |
27 | } | |
28 | ||
29 | public function getSecurityTests() | |
30 | { | |
31 | return array( | |
32 | array("AutoloaderTest\0.php"), | |
33 | array('..\\AutoloaderTest.php'), | |
34 | array('..\\\\\\AutoloaderTest.php'), | |
35 | array('../AutoloaderTest.php'), | |
36 | array('..////AutoloaderTest.php'), | |
37 | array('./../AutoloaderTest.php'), | |
38 | array('.\\..\\AutoloaderTest.php'), | |
39 | array('././././././../AutoloaderTest.php'), | |
40 | array('.\\./.\\./.\\./../AutoloaderTest.php'), | |
41 | array('foo/../../AutoloaderTest.php'), | |
42 | array('foo\\..\\..\\AutoloaderTest.php'), | |
43 | array('foo/../bar/../../AutoloaderTest.php'), | |
44 | array('foo/bar/../../../AutoloaderTest.php'), | |
45 | array('filters/../../AutoloaderTest.php'), | |
46 | array('filters//..//..//AutoloaderTest.php'), | |
47 | array('filters\\..\\..\\AutoloaderTest.php'), | |
48 | array('filters\\\\..\\\\..\\\\AutoloaderTest.php'), | |
49 | array('filters\\//../\\/\\..\\AutoloaderTest.php'), | |
50 | array('/../AutoloaderTest.php'), | |
51 | ); | |
52 | } | |
53 | ||
54 | public function testPaths() | |
55 | { | |
56 | $basePath = dirname(__FILE__).'/Fixtures'; | |
57 | ||
58 | $loader = new Twig_Loader_Filesystem(array($basePath.'/normal', $basePath.'/normal_bis')); | |
59 | $loader->setPaths(array($basePath.'/named', $basePath.'/named_bis'), 'named'); | |
60 | $loader->addPath($basePath.'/named_ter', 'named'); | |
61 | $loader->addPath($basePath.'/normal_ter'); | |
62 | $loader->prependPath($basePath.'/normal_final'); | |
63 | $loader->prependPath($basePath.'/named_final', 'named'); | |
64 | ||
65 | $this->assertEquals(array( | |
66 | $basePath.'/normal_final', | |
67 | $basePath.'/normal', | |
68 | $basePath.'/normal_bis', | |
69 | $basePath.'/normal_ter', | |
70 | ), $loader->getPaths()); | |
71 | $this->assertEquals(array( | |
72 | $basePath.'/named_final', | |
73 | $basePath.'/named', | |
74 | $basePath.'/named_bis', | |
75 | $basePath.'/named_ter', | |
76 | ), $loader->getPaths('named')); | |
77 | ||
78 | $this->assertEquals("path (final)\n", $loader->getSource('index.html')); | |
79 | $this->assertEquals("path (final)\n", $loader->getSource('@__main__/index.html')); | |
80 | $this->assertEquals("named path (final)\n", $loader->getSource('@named/index.html')); | |
81 | } | |
82 | ||
83 | public function testEmptyConstructor() | |
84 | { | |
85 | $loader = new Twig_Loader_Filesystem(); | |
86 | $this->assertEquals(array(), $loader->getPaths()); | |
87 | } | |
88 | ||
89 | public function testGetNamespaces() | |
90 | { | |
91 | $loader = new Twig_Loader_Filesystem(sys_get_temp_dir()); | |
92 | $this->assertEquals(array(Twig_Loader_Filesystem::MAIN_NAMESPACE), $loader->getNamespaces()); | |
93 | ||
94 | $loader->addPath(sys_get_temp_dir(), 'named'); | |
95 | $this->assertEquals(array(Twig_Loader_Filesystem::MAIN_NAMESPACE, 'named'), $loader->getNamespaces()); | |
96 | } | |
97 | } |