3 namespace Drupal\KernelTests\Core\File;
6 * Tests the file_scan_directory() function.
10 class ScanDirectoryTest extends FileTestBase {
17 public static $modules = ['file_test'];
24 protected function setUp() {
26 // Hardcode the location of the simpletest files as it is already known
27 // and shouldn't change, and we don't yet have a way to retrieve their
28 // location from drupal_get_filename() in a cached way.
29 // @todo Remove as part of https://www.drupal.org/node/2186491
30 $this->path = 'core/modules/simpletest/files';
34 * Check the format of the returned values.
36 public function testReturn() {
37 // Grab a listing of all the JavaScript files and check that they're
38 // passed to the callback.
39 $all_files = file_scan_directory($this->path, '/^javascript-/');
41 $this->assertEqual(2, count($all_files), 'Found two, expected javascript files.');
43 // Check the first file.
44 $file = reset($all_files);
45 $this->assertEqual(key($all_files), $file->uri, 'Correct array key was used for the first returned file.');
46 $this->assertEqual($file->uri, $this->path . '/javascript-1.txt', 'First file name was set correctly.');
47 $this->assertEqual($file->filename, 'javascript-1.txt', 'First basename was set correctly');
48 $this->assertEqual($file->name, 'javascript-1', 'First name was set correctly.');
50 // Check the second file.
51 $file = next($all_files);
52 $this->assertEqual(key($all_files), $file->uri, 'Correct array key was used for the second returned file.');
53 $this->assertEqual($file->uri, $this->path . '/javascript-2.script', 'Second file name was set correctly.');
54 $this->assertEqual($file->filename, 'javascript-2.script', 'Second basename was set correctly');
55 $this->assertEqual($file->name, 'javascript-2', 'Second name was set correctly.');
59 * Check that the callback function is called correctly.
61 public function testOptionCallback() {
63 // When nothing is matched nothing should be passed to the callback.
64 $all_files = file_scan_directory($this->path, '/^NONEXISTINGFILENAME/', ['callback' => 'file_test_file_scan_callback']);
65 $this->assertEqual(0, count($all_files), 'No files were found.');
66 $results = file_test_file_scan_callback();
67 file_test_file_scan_callback_reset();
68 $this->assertEqual(0, count($results), 'No files were passed to the callback.');
70 // Grab a listing of all the JavaScript files and check that they're
71 // passed to the callback.
72 $all_files = file_scan_directory($this->path, '/^javascript-/', ['callback' => 'file_test_file_scan_callback']);
73 $this->assertEqual(2, count($all_files), 'Found two, expected javascript files.');
74 $results = file_test_file_scan_callback();
75 file_test_file_scan_callback_reset();
76 $this->assertEqual(2, count($results), 'Files were passed to the callback.');
80 * Check that the no-mask parameter is honored.
82 public function testOptionNoMask() {
83 // Grab a listing of all the JavaScript files.
84 $all_files = file_scan_directory($this->path, '/^javascript-/');
85 $this->assertEqual(2, count($all_files), 'Found two, expected javascript files.');
87 // Now use the nomask parameter to filter out the .script file.
88 $filtered_files = file_scan_directory($this->path, '/^javascript-/', ['nomask' => '/.script$/']);
89 $this->assertEqual(1, count($filtered_files), 'Filtered correctly.');
93 * Check that key parameter sets the return value's key.
95 public function testOptionKey() {
96 // "filename", for the path starting with $dir.
97 $expected = [$this->path . '/javascript-1.txt', $this->path . '/javascript-2.script'];
98 $actual = array_keys(file_scan_directory($this->path, '/^javascript-/', ['key' => 'filepath']));
100 $this->assertEqual($expected, $actual, 'Returned the correct values for the filename key.');
102 // "basename", for the basename of the file.
103 $expected = ['javascript-1.txt', 'javascript-2.script'];
104 $actual = array_keys(file_scan_directory($this->path, '/^javascript-/', ['key' => 'filename']));
106 $this->assertEqual($expected, $actual, 'Returned the correct values for the basename key.');
108 // "name" for the name of the file without an extension.
109 $expected = ['javascript-1', 'javascript-2'];
110 $actual = array_keys(file_scan_directory($this->path, '/^javascript-/', ['key' => 'name']));
112 $this->assertEqual($expected, $actual, 'Returned the correct values for the name key.');
114 // Invalid option that should default back to "filename".
115 $expected = [$this->path . '/javascript-1.txt', $this->path . '/javascript-2.script'];
116 $actual = array_keys(file_scan_directory($this->path, '/^javascript-/', ['key' => 'INVALID']));
118 $this->assertEqual($expected, $actual, 'An invalid key defaulted back to the default.');
122 * Check that the recurse option descends into subdirectories.
124 public function testOptionRecurse() {
125 $files = file_scan_directory($this->path . '/..', '/^javascript-/', ['recurse' => FALSE]);
126 $this->assertTrue(empty($files), "Without recursion couldn't find javascript files.");
128 $files = file_scan_directory($this->path . '/..', '/^javascript-/', ['recurse' => TRUE]);
129 $this->assertEqual(2, count($files), 'With recursion we found the expected javascript files.');
134 * Check that the min_depth options lets us ignore files in the starting
137 public function testOptionMinDepth() {
138 $files = file_scan_directory($this->path, '/^javascript-/', ['min_depth' => 0]);
139 $this->assertEqual(2, count($files), 'No minimum-depth gets files in current directory.');
141 $files = file_scan_directory($this->path, '/^javascript-/', ['min_depth' => 1]);
142 $this->assertTrue(empty($files), 'Minimum-depth of 1 successfully excludes files from current directory.');
146 * Tests file_scan_directory() obeys 'file_scan_ignore_directories' setting.
148 public function testIgnoreDirectories() {
149 $files = file_scan_directory('core/modules/system/tests/fixtures/IgnoreDirectories', '/\.txt$/');
150 $this->assertCount(2, $files, '2 text files found when not ignoring directories.');
152 $this->setSetting('file_scan_ignore_directories', ['frontend_framework']);
153 $files = file_scan_directory('core/modules/system/tests/fixtures/IgnoreDirectories', '/\.txt$/');
154 $this->assertCount(1, $files, '1 text files found when ignoring directories called "frontend_framework".');
156 // Ensure that the directories in file_scan_ignore_directories are escaped
158 $this->setSetting('file_scan_ignore_directories', ['frontend.*']);
159 $files = file_scan_directory('core/modules/system/tests/fixtures/IgnoreDirectories', '/\.txt$/');
160 $this->assertCount(2, $files, '2 text files found when ignoring a directory that is not there.');
162 $files = file_scan_directory('core/modules/system/tests/fixtures/IgnoreDirectories', '/\.txt$/', ['nomask' => '/^something_thing_else$/']);
163 $this->assertCount(2, $files, '2 text files found when an "nomask" option is passed in.');