DirectoryTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace Elgg\Filesystem;
  3. use PHPUnit_Framework_TestCase as TestCase;
  4. abstract class DirectoryTest extends TestCase {
  5. /**
  6. * Returns an array of one-element arrays. Those elements should
  7. * be fresh (empty) directory instances that use the relevant implementation.
  8. *
  9. * @return array
  10. */
  11. abstract public function emptyDirectoryProvider();
  12. /**
  13. * @dataProvider emptyDirectoryProvider
  14. */
  15. public function testCanRecursivelyListAllFilesInTheDirectory(Directory $directory) {
  16. $directory->putContents('/foo/bar/bar.php', 'bar');
  17. $directory->putContents('/foo/baz/baz.php', 'baz');
  18. $directory->putContents('/foo/foo.php', 'foo');
  19. $directory->putContents('/qux.php', 'qux');
  20. $this->assertEquals(4, count($directory->getFiles()));
  21. }
  22. /**
  23. * @dataProvider emptyDirectoryProvider
  24. */
  25. public function testCanRecursivelyListAllFilesAtAGivenSubdir(Directory $directory) {
  26. $directory->putContents('/foo/bar/bar.php', 'bar');
  27. $directory->putContents('/foo/baz/baz.php', 'baz');
  28. $directory->putContents('/foo/foo.php', 'foo');
  29. $directory->putContents('/qux.php', 'qux');
  30. $this->assertEquals(3, count($directory->getFiles('/foo/')));
  31. $this->assertEquals(3, count($directory->getFiles('/foo')));
  32. $this->assertEquals(3, count($directory->getFiles('foo/')));
  33. $this->assertEquals(3, count($directory->getFiles('foo')));
  34. $this->assertEquals(1, count($directory->getFiles('/foo/bar/')));
  35. $this->assertEquals(1, count($directory->getFiles('/foo/bar')));
  36. $this->assertEquals(1, count($directory->getFiles('foo/bar/')));
  37. $this->assertEquals(1, count($directory->getFiles('foo/bar')));
  38. }
  39. /**
  40. * @dataProvider emptyDirectoryProvider
  41. */
  42. public function testChrootReturnsANewDirectoryThatOnlyHasAccessToTheGivenSubdir(Directory $directory) {
  43. $directory->putContents('/foo/bar/bar.php', 'bar');
  44. $directory->putContents('/foo/baz/baz.php', 'baz');
  45. $directory->putContents('/foo/foo.php', 'foo');
  46. $directory->putContents('/qux.php', 'qux');
  47. $this->assertEquals(3, count($directory->chroot('/foo/')->getFiles()));
  48. $this->assertEquals(3, count($directory->chroot('foo/')->getFiles()));
  49. $this->assertEquals(3, count($directory->chroot('/foo')->getFiles()));
  50. $this->assertEquals(3, count($directory->chroot('foo')->getFiles()));
  51. $this->assertEquals(1, count($directory->chroot('/foo/bar/')->getFiles()));
  52. $this->assertEquals(1, count($directory->chroot('foo/bar/')->getFiles()));
  53. $this->assertEquals(1, count($directory->chroot('/foo/bar')->getFiles()));
  54. $this->assertEquals(1, count($directory->chroot('foo/bar')->getFiles()));
  55. }
  56. /**
  57. * @dataProvider emptyDirectoryProvider
  58. */
  59. public function testCanGetAnyFileInThisDirectoryEvenIfTheFileDoesNotExistYet(Directory $directory) {
  60. $this->assertFalse($directory->getFile('foo.php')->exists());
  61. }
  62. /**
  63. * @dataProvider emptyDirectoryProvider
  64. */
  65. public function testCannotChrootOutsideItself(Directory $directory) {
  66. $directory->putContents('/foo/bar.php', 'bar');
  67. $directory->putContents('/baz.php', 'baz');
  68. $directory->chroot('foo')->chroot('..');
  69. // TODO: Expect to throw exception? Silently cap at existing root?
  70. $this->markTestIncomplete();
  71. }
  72. /**
  73. * @dataProvider emptyDirectoryProvider
  74. */
  75. public function testCannotGetFileOutsideItself(Directory $directory) {
  76. $directory->putContents('/foo/bar.php', 'bar');
  77. $directory->putContents('/baz.php', 'baz');
  78. // TODO: Throw exception?
  79. $file = $directory->chroot('foo')->getFile('../baz.php');
  80. $this->assertNotEquals('baz', $file->getContents());
  81. }
  82. }