You are here

public function DirectoryTest::testFileCheckLocalDirectoryHandling in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/File/DirectoryTest.php \Drupal\KernelTests\Core\File\DirectoryTest::testFileCheckLocalDirectoryHandling()
  2. 9 core/tests/Drupal/KernelTests/Core/File/DirectoryTest.php \Drupal\KernelTests\Core\File\DirectoryTest::testFileCheckLocalDirectoryHandling()

Tests local directory handling functions.

File

core/tests/Drupal/KernelTests/Core/File/DirectoryTest.php, line 22

Class

DirectoryTest
Tests operations dealing with directories.

Namespace

Drupal\KernelTests\Core\File

Code

public function testFileCheckLocalDirectoryHandling() {
  $site_path = $this->container
    ->getParameter('site.path');
  $directory = $site_path . '/files';

  // Check a new recursively created local directory for correct file system
  // permissions.
  $parent = $this
    ->randomMachineName();
  $child = $this
    ->randomMachineName();

  // Files directory already exists.
  $this
    ->assertDirectoryExists($directory);

  // Make files directory writable only.
  $old_mode = fileperms($directory);

  // Create the directories.
  $parent_path = $directory . DIRECTORY_SEPARATOR . $parent;
  $child_path = $parent_path . DIRECTORY_SEPARATOR . $child;

  /** @var \Drupal\Core\File\FileSystemInterface $file_system */
  $file_system = \Drupal::service('file_system');
  $this
    ->assertTrue($file_system
    ->mkdir($child_path, 0775, TRUE), 'No error reported when creating new local directories.');

  // Ensure new directories also exist.
  $this
    ->assertDirectoryExists($parent_path);
  $this
    ->assertDirectoryExists($child_path);

  // Check that new directory permissions were set properly.
  $this
    ->assertDirectoryPermissions($parent_path, 0775);
  $this
    ->assertDirectoryPermissions($child_path, 0775);

  // Check that existing directory permissions were not modified.
  $this
    ->assertDirectoryPermissions($directory, $old_mode);

  // Check creating a directory using an absolute path.
  $absolute_path = $file_system
    ->realpath($directory) . DIRECTORY_SEPARATOR . $this
    ->randomMachineName() . DIRECTORY_SEPARATOR . $this
    ->randomMachineName();
  $this
    ->assertTrue($file_system
    ->mkdir($absolute_path, 0775, TRUE), 'No error reported when creating new absolute directories.', 'File');
  $this
    ->assertDirectoryPermissions($absolute_path, 0775);
}