TemporaryFileTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace Maatwebsite\Excel\Tests;
  3. use Maatwebsite\Excel\Files\TemporaryFileFactory;
  4. use Maatwebsite\Excel\Tests\Helpers\FileHelper;
  5. class TemporaryFileTest extends TestCase
  6. {
  7. private $defaultDirectoryPermissions;
  8. private $defaultFilePermissions;
  9. /**
  10. * Setup the test environment.
  11. */
  12. protected function setUp(): void
  13. {
  14. parent::setUp();
  15. $path = FileHelper::absolutePath('rights-test-permissions', 'local');
  16. mkdir($path);
  17. $this->defaultDirectoryPermissions = substr(sprintf('%o', fileperms($path)), -4);
  18. $filePath = $path . DIRECTORY_SEPARATOR . 'file-permissions';
  19. touch($filePath);
  20. $this->defaultFilePermissions = substr(sprintf('%o', fileperms($filePath)), -4);
  21. @unlink($filePath);
  22. @rmdir($path);
  23. }
  24. /**
  25. * @test
  26. */
  27. public function can_use_default_rights()
  28. {
  29. $path = FileHelper::absolutePath('rights-test', 'local');
  30. FileHelper::recursiveDelete($path);
  31. config()->set('excel.temporary_files.local_path', $path);
  32. $temporaryFileFactory = app(TemporaryFileFactory::class);
  33. $temporaryFile = $temporaryFileFactory->makeLocal(null, 'txt');
  34. $temporaryFile->put('data-set');
  35. $this->assertFileExists($temporaryFile->getLocalPath());
  36. $this->assertEquals($this->defaultDirectoryPermissions, substr(sprintf('%o', fileperms(dirname($temporaryFile->getLocalPath()))), -4));
  37. $this->assertEquals($this->defaultFilePermissions, substr(sprintf('%o', fileperms($temporaryFile->getLocalPath())), -4));
  38. }
  39. /**
  40. * @test
  41. */
  42. public function can_use_dir_rights()
  43. {
  44. $path = FileHelper::absolutePath('rights-test', 'local');
  45. FileHelper::recursiveDelete($path);
  46. config()->set('excel.temporary_files.local_path', $path);
  47. config()->set('excel.temporary_files.local_permissions.dir', 0700);
  48. $temporaryFileFactory = app(TemporaryFileFactory::class);
  49. $temporaryFile = $temporaryFileFactory->makeLocal(null, 'txt');
  50. $temporaryFile->put('data-set');
  51. $this->assertFileExists($temporaryFile->getLocalPath());
  52. $this->assertEquals('0700', substr(sprintf('%o', fileperms(dirname($temporaryFile->getLocalPath()))), -4));
  53. $this->assertEquals($this->defaultFilePermissions, substr(sprintf('%o', fileperms($temporaryFile->getLocalPath())), -4));
  54. }
  55. /**
  56. * @test
  57. */
  58. public function can_use_file_rights()
  59. {
  60. $path = FileHelper::absolutePath('rights-test', 'local');
  61. FileHelper::recursiveDelete($path);
  62. config()->set('excel.temporary_files.local_path', $path);
  63. config()->set('excel.temporary_files.local_permissions.file', 0600);
  64. $temporaryFileFactory = app(TemporaryFileFactory::class);
  65. $temporaryFile = $temporaryFileFactory->makeLocal(null, 'txt');
  66. $temporaryFile->put('data-set');
  67. $this->assertFileExists($temporaryFile->getLocalPath());
  68. $this->assertEquals($this->defaultDirectoryPermissions, substr(sprintf('%o', fileperms(dirname($temporaryFile->getLocalPath()))), -4));
  69. $this->assertEquals('0600', substr(sprintf('%o', fileperms($temporaryFile->getLocalPath())), -4));
  70. }
  71. }