4 * This file is part of Twig.
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 class Twig_Tests_Loader_FilesystemTest extends PHPUnit_Framework_TestCase
14 public function testGetSourceContext()
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()));
23 * @dataProvider getSecurityTests
25 public function testSecurity($template)
27 $loader = new Twig_Loader_Filesystem(array(dirname(__FILE__).'/../Fixtures'));
30 $loader->getCacheKey($template);
32 } catch (Twig_Error_Loader $e) {
33 $this->assertNotContains('Unable to find template', $e->getMessage());
37 public function getSecurityTests()
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'),
63 * @dataProvider getBasePaths
65 public function testPaths($basePath, $cacheKey, $rootPath)
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');
75 $this->assertEquals(array(
76 $basePath.'/normal_final',
78 $basePath.'/normal_bis',
79 $basePath.'/normal_ter',
80 ), $loader->getPaths());
81 $this->assertEquals(array(
82 $basePath.'/named_final',
83 $basePath.'/named/../named_quater',
85 $basePath.'/named_bis',
86 $basePath.'/named_ter',
87 ), $loader->getPaths('named'));
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());
96 public function getBasePaths()
100 dirname(__FILE__).'/Fixtures',
101 'test/Twig/Tests/Loader/Fixtures/named_quater/named_absolute.html',
105 dirname(__FILE__).'/Fixtures/../Fixtures',
106 'test/Twig/Tests/Loader/Fixtures/named_quater/named_absolute.html',
110 'test/Twig/Tests/Loader/Fixtures',
111 'test/Twig/Tests/Loader/Fixtures/named_quater/named_absolute.html',
116 'Fixtures/named_quater/named_absolute.html',
117 getcwd().'/test/Twig/Tests/Loader',
121 'Fixtures/named_quater/named_absolute.html',
122 getcwd().'/test/../test/Twig/Tests/Loader',
127 public function testEmptyConstructor()
129 $loader = new Twig_Loader_Filesystem();
130 $this->assertEquals(array(), $loader->getPaths());
133 public function testGetNamespaces()
135 $loader = new Twig_Loader_Filesystem(sys_get_temp_dir());
136 $this->assertEquals(array(Twig_Loader_Filesystem::MAIN_NAMESPACE), $loader->getNamespaces());
138 $loader->addPath(sys_get_temp_dir(), 'named');
139 $this->assertEquals(array(Twig_Loader_Filesystem::MAIN_NAMESPACE, 'named'), $loader->getNamespaces());
142 public function testFindTemplateExceptionNamespace()
144 $basePath = dirname(__FILE__).'/Fixtures';
146 $loader = new Twig_Loader_Filesystem(array($basePath.'/normal'));
147 $loader->addPath($basePath.'/named', 'named');
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());
157 public function testFindTemplateWithCache()
159 $basePath = dirname(__FILE__).'/Fixtures';
161 $loader = new Twig_Loader_Filesystem(array($basePath.'/normal'));
162 $loader->addPath($basePath.'/named', 'named');
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);
168 // get index.html from the main namespace
169 $this->assertEquals("path\n", $loader->getSourceContext('index.html')->getCode());
172 public function testLoadTemplateAndRenderBlockWithCache()
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');
179 $twig = new Twig_Environment($loader);
181 $template = $twig->loadTemplate('blocks.html.twig');
182 $this->assertSame('block from theme 1', $template->renderBlock('b1', array()));
184 $template = $twig->loadTemplate('blocks.html.twig');
185 $this->assertSame('block from theme 2', $template->renderBlock('b2', array()));
188 public function getArrayInheritanceTests()
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'),
199 * @dataProvider getArrayInheritanceTests
201 * @param $templateName string Template name with array inheritance
203 public function testArrayInheritance($templateName)
205 $loader = new Twig_Loader_Filesystem(array());
206 $loader->addPath(dirname(__FILE__).'/Fixtures/inheritance');
208 $twig = new Twig_Environment($loader);
210 $template = $twig->loadTemplate($templateName);
211 $this->assertSame('VALID Child', $template->renderBlock('body', array()));
217 public function testLoadTemplateFromPhar()
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());