You are here

public function DirectoryTest::testFileCheckLocalDirectoryHandling in Drupal 8

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

Test local directory handling functions.

File

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

Class

DirectoryTest
Tests operations dealing with directories.

Namespace

Drupal\KernelTests\Core\File

Code

public function testFileCheckLocalDirectoryHandling() {
  $site_path = $this->container
    ->get('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), t('No error reported when creating new local directories.'), 'File');

  // 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);
}