Same filename and directory in other branches
- 8.9.x core/tests/Drupal/KernelTests/Core/File/DirectoryTest.php
- 9 core/tests/Drupal/KernelTests/Core/File/DirectoryTest.php
Namespace
Drupal\KernelTests\Core\File
File
core/tests/Drupal/KernelTests/Core/File/DirectoryTest.php
View source
<?php
declare (strict_types=1);
namespace Drupal\KernelTests\Core\File;
use Drupal\Component\FileSecurity\FileSecurity;
use Drupal\Component\FileSystem\FileSystem;
use Drupal\Core\Database\Database;
use Drupal\Core\File\Exception\FileException;
use Drupal\Core\File\FileExists;
use Drupal\Core\File\FileSystemInterface;
class DirectoryTest extends FileTestBase {
public function testFileCheckLocalDirectoryHandling() {
$site_path = $this->container
->getParameter('site.path');
$directory = $site_path . '/files';
$parent = $this
->randomMachineName();
$child = $this
->randomMachineName();
$this
->assertDirectoryExists($directory);
$old_mode = fileperms($directory);
$parent_path = $directory . DIRECTORY_SEPARATOR . $parent;
$child_path = $parent_path . DIRECTORY_SEPARATOR . $child;
$file_system = \Drupal::service('file_system');
$this
->assertTrue($file_system
->mkdir($child_path, 0775, TRUE), 'No error reported when creating new local directories.');
$this
->assertDirectoryExists($parent_path);
$this
->assertDirectoryExists($child_path);
$this
->assertDirectoryPermissions($parent_path, 0775);
$this
->assertDirectoryPermissions($child_path, 0775);
$this
->assertDirectoryPermissions($directory, $old_mode);
$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.');
$this
->assertDirectoryPermissions($absolute_path, 0775);
}
public function testFileCheckDirectoryHandling() {
$default_scheme = 'public';
$directory = $default_scheme . '://' . $this
->randomMachineName() . '/' . $this
->randomMachineName();
$this
->assertDirectoryDoesNotExist($directory);
$file_system = \Drupal::service('file_system');
$this
->assertFalse($file_system
->prepareDirectory($directory, 0), 'Error reported for non-existing directory.');
$this
->assertTrue($file_system
->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY), 'No error reported when creating a new directory.');
$this
->assertDirectoryExists($directory);
$file_system = \Drupal::service('file_system');
if (!str_starts_with(PHP_OS, 'WIN')) {
@$file_system
->chmod($directory, 0444);
$this
->assertFalse($file_system
->prepareDirectory($directory, 0), 'Error reported for a non-writable directory.');
$this
->setSetting('file_chmod_directory', 0777);
$this
->assertTrue($file_system
->prepareDirectory($directory, FileSystemInterface::MODIFY_PERMISSIONS), 'No error reported when making directory writable.');
}
$this
->assertDirectoryPermissions($directory, 0777, 'file_chmod_directory setting is respected.');
@$file_system
->unlink($default_scheme . '://.htaccess');
$this
->assertFileDoesNotExist($default_scheme . '://.htaccess');
$this->container
->get('file.htaccess_writer')
->ensure();
$this
->assertFileExists($default_scheme . '://.htaccess');
@$file_system
->unlink($default_scheme . '://.htaccess');
$this
->assertFileDoesNotExist($default_scheme . '://.htaccess');
system_cron();
$this
->assertFileExists($default_scheme . '://.htaccess');
$file = file_get_contents($default_scheme . '://.htaccess');
$this
->assertEquals(FileSecurity::htaccessLines(FALSE), $file, 'The .htaccess file contains the proper content.');
}
public function testFileCreateNewFilepath() {
$basename = 'xyz.txt';
$directory = 'core/misc';
$original = $directory . '/' . $basename;
$file_system = \Drupal::service('file_system');
$path = $file_system
->createFilename($basename, $directory);
$this
->assertEquals($original, $path, "New filepath {$path} equals {$original}.");
$basename = 'druplicon.png';
$original = $directory . '/' . $basename;
$expected = $directory . '/druplicon_0.png';
$path = $file_system
->createFilename($basename, $directory);
$this
->assertEquals($expected, $path, "Creating a new filepath from {$path} equals {$original} (expected {$expected}).");
}
public function testFileDestination() {
$destination = 'core/misc/xyz.txt';
$file_system = \Drupal::service('file_system');
$path = $file_system
->getDestinationFilename($destination, FileExists::Replace);
$this
->assertEquals($destination, $path, 'Non-existing filepath destination is correct with FileExists::Replace.');
$path = $file_system
->getDestinationFilename($destination, FileExists::Rename);
$this
->assertEquals($destination, $path, 'Non-existing filepath destination is correct with FileExists::Rename.');
$path = $file_system
->getDestinationFilename($destination, FileExists::Error);
$this
->assertEquals($destination, $path, 'Non-existing filepath destination is correct with FileExists::Error.');
$destination = 'core/misc/druplicon.png';
$path = $file_system
->getDestinationFilename($destination, FileExists::Replace);
$this
->assertEquals($destination, $path, 'Existing filepath destination remains the same with FileExists::Replace.');
$path = $file_system
->getDestinationFilename($destination, FileExists::Rename);
$this
->assertNotEquals($destination, $path, 'A new filepath destination is created when filepath destination already exists with FileExists::Rename.');
$path = $file_system
->getDestinationFilename($destination, FileExists::Error);
$this
->assertFalse($path, 'An error is returned when filepath destination already exists with FileExists::Error.');
$this
->expectException(FileException::class);
$this
->expectExceptionMessage("");
$file_system
->getDestinationFilename("", FileExists::Replace);
}
public function testFileDirectoryTemp() {
$tmp_directory = \Drupal::service('file_system')
->getTempDirectory();
$this
->assertNotEmpty($tmp_directory);
$this
->assertEquals($tmp_directory, FileSystem::getOsTemporaryDirectory());
}
public function testDirectoryCreation() {
$file_system = $this->container
->get('file_system');
$dir = $this->siteDirectory . '/files';
$this
->assertTrue($file_system
->mkdir($dir . '/foo/bar', 0775, TRUE));
$this
->assertTrue($file_system
->mkdir($dir . '/foo/baz/', 0775, TRUE));
}
public function testMultiplePrepareDirectory() {
if (!function_exists('pcntl_fork')) {
$this
->markTestSkipped('Requires the pcntl_fork() function');
}
$directories = [];
for ($i = 1; $i <= 10; $i++) {
$directories[] = 'public://a/b/c/d/e/f/g/h/' . $i;
}
$file_system = $this->container
->get('file_system');
$time_to_start = microtime(TRUE) + 0.1;
foreach ($directories as $directory) {
$pid = pcntl_fork();
if ($pid == -1) {
$this
->fail("Error forking");
}
elseif ($pid == 0) {
usleep((int) (($time_to_start - microtime(TRUE)) * 1000000));
$file_system
->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY);
exit;
}
}
while (pcntl_waitpid(0, $status) != -1) {
}
foreach ($directories as $directory) {
$this
->assertDirectoryExists($directory);
}
Database::removeConnection('default');
}
}
Classes