FileSystemTempDirectoryTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace Drupal\KernelTests\Core\File;
  3. use Drupal\Component\FileSystem\FileSystem as FileSystemComponent;
  4. use Drupal\Core\File\FileSystem;
  5. use Drupal\KernelTests\KernelTestBase;
  6. /**
  7. * Tests for getTempDirectory on FileSystem.
  8. *
  9. * @group File
  10. * @coversDefaultClass \Drupal\Core\File\FileSystem
  11. */
  12. class FileSystemTempDirectoryTest extends KernelTestBase {
  13. /**
  14. * Modules to enable.
  15. *
  16. * @var array
  17. */
  18. public static $modules = ['system'];
  19. /**
  20. * The file system under test.
  21. *
  22. * @var \Drupal\Core\File\FileSystemInterface
  23. */
  24. protected $fileSystem;
  25. /**
  26. * {@inheritdoc}
  27. */
  28. protected function setUp() {
  29. parent::setUp();
  30. $stream_wrapper_manager = $this->container->get('stream_wrapper_manager');
  31. $logger = $this->container->get('logger.channel.file');
  32. $settings = $this->container->get('settings');
  33. $this->fileSystem = new FileSystem($stream_wrapper_manager, $settings, $logger);
  34. }
  35. /**
  36. * Tests 'file_temp_path' setting.
  37. *
  38. * @covers ::getTempDirectory
  39. */
  40. public function testGetTempDirectorySettings() {
  41. $tempDir = '/var/tmp/' . $this->randomMachineName();
  42. $this->setSetting('file_temp_path', $tempDir);
  43. $this->assertEquals($tempDir, $this->fileSystem->getTempDirectory());
  44. }
  45. /**
  46. * Tests 'path.temporary' config deprecation.
  47. *
  48. * @group legacy
  49. * @covers ::getTempDirectory
  50. * @expectedDeprecation The 'system.file' config 'path.temporary' is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Set 'file_temp_path' in settings.php instead. See https://www.drupal.org/node/3039255
  51. */
  52. public function testGetTempDirectoryDeprecation() {
  53. $tempDir = '/var/tmp/' . $this->randomMachineName();
  54. $this->config('system.file')
  55. ->set('path.temporary', $tempDir)
  56. ->save(TRUE);
  57. $dir = $this->fileSystem->getTempDirectory();
  58. $this->assertEquals($tempDir, $dir);
  59. }
  60. /**
  61. * Tests os default fallback.
  62. *
  63. * @covers ::getTempDirectory
  64. */
  65. public function testGetTempDirectoryOsDefault() {
  66. $tempDir = FileSystemComponent::getOsTemporaryDirectory();
  67. $dir = $this->fileSystem->getTempDirectory();
  68. $this->assertEquals($tempDir, $dir);
  69. }
  70. }