View source
<?php
namespace Drupal\KernelTests\Core\File;
class ScanDirectoryTest extends FileTestBase {
protected static $modules = [
'file_test',
];
protected $path;
protected $fileSystem;
protected function setUp() : void {
parent::setUp();
$this->path = 'core/tests/fixtures/files';
$this->fileSystem = $this->container
->get('file_system');
}
public function testReturn() {
$all_files = $this->fileSystem
->scanDirectory($this->path, '/^javascript-/');
ksort($all_files);
$this
->assertCount(2, $all_files, 'Found two, expected javascript files.');
$file = reset($all_files);
$this
->assertEquals(key($all_files), $file->uri, 'Correct array key was used for the first returned file.');
$this
->assertEquals($this->path . '/javascript-1.txt', $file->uri, 'First file name was set correctly.');
$this
->assertEquals('javascript-1.txt', $file->filename, 'First basename was set correctly');
$this
->assertEquals('javascript-1', $file->name, 'First name was set correctly.');
$file = next($all_files);
$this
->assertEquals(key($all_files), $file->uri, 'Correct array key was used for the second returned file.');
$this
->assertEquals($this->path . '/javascript-2.script', $file->uri, 'Second file name was set correctly.');
$this
->assertEquals('javascript-2.script', $file->filename, 'Second basename was set correctly');
$this
->assertEquals('javascript-2', $file->name, 'Second name was set correctly.');
}
public function testOptionCallback() {
$all_files = $this->fileSystem
->scanDirectory($this->path, '/^NONEXISTINGFILENAME/', [
'callback' => 'file_test_file_scan_callback',
]);
$this
->assertCount(0, $all_files, 'No files were found.');
$results = file_test_file_scan_callback();
file_test_file_scan_callback_reset();
$this
->assertCount(0, $results, 'No files were passed to the callback.');
$all_files = $this->fileSystem
->scanDirectory($this->path, '/^javascript-/', [
'callback' => 'file_test_file_scan_callback',
]);
$this
->assertCount(2, $all_files, 'Found two, expected javascript files.');
$results = file_test_file_scan_callback();
file_test_file_scan_callback_reset();
$this
->assertCount(2, $results, 'Files were passed to the callback.');
}
public function testOptionNoMask() {
$all_files = $this->fileSystem
->scanDirectory($this->path, '/^javascript-/');
$this
->assertCount(2, $all_files, 'Found two, expected javascript files.');
$filtered_files = $this->fileSystem
->scanDirectory($this->path, '/^javascript-/', [
'nomask' => '/.script$/',
]);
$this
->assertCount(1, $filtered_files, 'Filtered correctly.');
}
public function testOptionKey() {
$expected = [
$this->path . '/javascript-1.txt',
$this->path . '/javascript-2.script',
];
$actual = array_keys($this->fileSystem
->scanDirectory($this->path, '/^javascript-/', [
'key' => 'filepath',
]));
sort($actual);
$this
->assertEquals($expected, $actual, 'Returned the correct values for the filename key.');
$expected = [
'javascript-1.txt',
'javascript-2.script',
];
$actual = array_keys($this->fileSystem
->scanDirectory($this->path, '/^javascript-/', [
'key' => 'filename',
]));
sort($actual);
$this
->assertEquals($expected, $actual, 'Returned the correct values for the basename key.');
$expected = [
'javascript-1',
'javascript-2',
];
$actual = array_keys($this->fileSystem
->scanDirectory($this->path, '/^javascript-/', [
'key' => 'name',
]));
sort($actual);
$this
->assertEquals($expected, $actual, 'Returned the correct values for the name key.');
$expected = [
$this->path . '/javascript-1.txt',
$this->path . '/javascript-2.script',
];
$actual = array_keys($this->fileSystem
->scanDirectory($this->path, '/^javascript-/', [
'key' => 'INVALID',
]));
sort($actual);
$this
->assertEquals($expected, $actual, 'An invalid key defaulted back to the default.');
}
public function testOptionRecurse() {
$files = $this->fileSystem
->scanDirectory($this->path . '/..', '/^javascript-/', [
'recurse' => FALSE,
]);
$this
->assertTrue(empty($files), "Without recursion couldn't find javascript files.");
$files = $this->fileSystem
->scanDirectory($this->path . '/..', '/^javascript-/', [
'recurse' => TRUE,
]);
$this
->assertCount(2, $files, 'With recursion we found the expected javascript files.');
}
public function testOptionMinDepth() {
$files = $this->fileSystem
->scanDirectory($this->path, '/^javascript-/', [
'min_depth' => 0,
]);
$this
->assertCount(2, $files, 'No minimum-depth gets files in current directory.');
$files = $this->fileSystem
->scanDirectory($this->path, '/^javascript-/', [
'min_depth' => 1,
]);
$this
->assertTrue(empty($files), 'Minimum-depth of 1 successfully excludes files from current directory.');
}
public function testIgnoreDirectories() {
$files = $this->fileSystem
->scanDirectory('core/modules/system/tests/fixtures/IgnoreDirectories', '/\\.txt$/');
$this
->assertCount(2, $files, '2 text files found when not ignoring directories.');
$this
->setSetting('file_scan_ignore_directories', [
'frontend_framework',
]);
$files = $this->fileSystem
->scanDirectory('core/modules/system/tests/fixtures/IgnoreDirectories', '/\\.txt$/');
$this
->assertCount(1, $files, '1 text files found when ignoring directories called "frontend_framework".');
$this
->setSetting('file_scan_ignore_directories', [
'frontend.*',
]);
$files = $this->fileSystem
->scanDirectory('core/modules/system/tests/fixtures/IgnoreDirectories', '/\\.txt$/');
$this
->assertCount(2, $files, '2 text files found when ignoring a directory that is not there.');
$files = $this->fileSystem
->scanDirectory('core/modules/system/tests/fixtures/IgnoreDirectories', '/\\.txt$/', [
'nomask' => '/^something_thing_else$/',
]);
$this
->assertCount(2, $files, '2 text files found when an "nomask" option is passed in.');
}
}