function DirectoryTest::testFileCheckDirectoryHandling
Same name in other branches
- 9 core/tests/Drupal/KernelTests/Core/File/DirectoryTest.php \Drupal\KernelTests\Core\File\DirectoryTest::testFileCheckDirectoryHandling()
- 8.9.x core/tests/Drupal/KernelTests/Core/File/DirectoryTest.php \Drupal\KernelTests\Core\File\DirectoryTest::testFileCheckDirectoryHandling()
- 10 core/tests/Drupal/KernelTests/Core/File/DirectoryTest.php \Drupal\KernelTests\Core\File\DirectoryTest::testFileCheckDirectoryHandling()
Tests directory handling functions.
File
-
core/
tests/ Drupal/ KernelTests/ Core/ File/ DirectoryTest.php, line 65
Class
- DirectoryTest
- Tests operations dealing with directories.
Namespace
Drupal\KernelTests\Core\FileCode
public function testFileCheckDirectoryHandling() : void {
// A directory to operate on.
$default_scheme = 'public';
$directory = $default_scheme . '://' . $this->randomMachineName() . '/' . $this->randomMachineName();
$this->assertDirectoryDoesNotExist($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.');
// Make a directory.
$this->assertTrue($file_system->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY), 'No error reported when creating a new directory.');
// Make sure directory actually exists.
$this->assertDirectoryExists($directory);
$file_system = \Drupal::service('file_system');
if (!str_starts_with(PHP_OS, '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.');
// 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.');
}
// 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->assertFileDoesNotExist($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->assertFileDoesNotExist($default_scheme . '://.htaccess');
system_cron();
$this->assertFileExists($default_scheme . '://.htaccess');
// Verify contents of .htaccess file.
$file = file_get_contents($default_scheme . '://.htaccess');
$this->assertEquals(FileSecurity::htaccessLines(FALSE), $file, 'The .htaccess file contains the proper content.');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.