FilesystemTest.php 3.2KB

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