You are here

public function DirectoryTest::testFileCheckDirectoryHandling 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::testFileCheckDirectoryHandling()

Test directory handling functions.

File

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

Class

DirectoryTest
Tests operations dealing with directories.

Namespace

Drupal\KernelTests\Core\File

Code

public function testFileCheckDirectoryHandling() {

  // A directory to operate on.
  $default_scheme = 'public';
  $directory = $default_scheme . '://' . $this
    ->randomMachineName() . '/' . $this
    ->randomMachineName();
  $this
    ->assertDirectoryNotExists($directory);

  // Non-existent directory.

  /** @var \Drupal\Core\File\FileSystemInterface $file_system */
  $file_system = \Drupal::service('file_system');
  $this
    ->assertFalse($file_system
    ->prepareDirectory($directory, 0), 'Error reported for non-existing directory.', 'File');

  // Make a directory.
  $this
    ->assertTrue($file_system
    ->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY), 'No error reported when creating a new directory.', 'File');

  // Make sure directory actually exists.
  $this
    ->assertDirectoryExists($directory);
  $file_system = \Drupal::service('file_system');
  if (substr(PHP_OS, 0, 3) != 'WIN') {

    // PHP on Windows doesn't support any kind of useful read-only mode for
    // directories. When executing a chmod() on a directory, PHP only sets the
    // read-only flag, which doesn't prevent files to actually be written
    // in the directory on any recent version of Windows.
    // Make directory read only.
    @$file_system
      ->chmod($directory, 0444);
    $this
      ->assertFalse($file_system
      ->prepareDirectory($directory, 0), 'Error reported for a non-writable directory.', 'File');

    // Test directory permission modification.
    $this
      ->setSetting('file_chmod_directory', 0777);
    $this
      ->assertTrue($file_system
      ->prepareDirectory($directory, FileSystemInterface::MODIFY_PERMISSIONS), 'No error reported when making directory writable.', 'File');
  }

  // Test that the directory has the correct permissions.
  $this
    ->assertDirectoryPermissions($directory, 0777, 'file_chmod_directory setting is respected.');

  // Remove .htaccess file to then test that it gets re-created.
  @$file_system
    ->unlink($default_scheme . '://.htaccess');
  $this
    ->assertFileNotExists($default_scheme . '://.htaccess');
  $this->container
    ->get('file.htaccess_writer')
    ->ensure();
  $this
    ->assertFileExists($default_scheme . '://.htaccess');

  // Remove .htaccess file again to test that it is re-created by a cron run.
  @$file_system
    ->unlink($default_scheme . '://.htaccess');
  $this
    ->assertFileNotExists($default_scheme . '://.htaccess');
  system_cron();
  $this
    ->assertFileExists($default_scheme . '://.htaccess');

  // Verify contents of .htaccess file.
  $file = file_get_contents($default_scheme . '://.htaccess');
  $this
    ->assertEqual($file, FileSecurity::htaccessLines(FALSE), 'The .htaccess file contains the proper content.', 'File');
}