Version 1
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / File / ScanDirectoryTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\File;
4
5 /**
6  * Tests the file_scan_directory() function.
7  *
8  * @group File
9  */
10 class ScanDirectoryTest extends FileTestBase {
11
12   /**
13    * Modules to enable.
14    *
15    * @var array
16    */
17   public static $modules = ['file_test'];
18
19   /**
20    * @var string
21    */
22   protected $path;
23
24   protected function setUp() {
25     parent::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';
31   }
32
33   /**
34    * Check the format of the returned values.
35    */
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-/');
40     ksort($all_files);
41     $this->assertEqual(2, count($all_files), 'Found two, expected javascript files.');
42
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.');
49
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.');
56   }
57
58   /**
59    * Check that the callback function is called correctly.
60    */
61   public function testOptionCallback() {
62
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.');
69
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.');
77   }
78
79   /**
80    * Check that the no-mask parameter is honored.
81    */
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.');
86
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.');
90   }
91
92   /**
93    * Check that key parameter sets the return value's key.
94    */
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']));
99     sort($actual);
100     $this->assertEqual($expected, $actual, 'Returned the correct values for the filename key.');
101
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']));
105     sort($actual);
106     $this->assertEqual($expected, $actual, 'Returned the correct values for the basename key.');
107
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']));
111     sort($actual);
112     $this->assertEqual($expected, $actual, 'Returned the correct values for the name key.');
113
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']));
117     sort($actual);
118     $this->assertEqual($expected, $actual, 'An invalid key defaulted back to the default.');
119   }
120
121   /**
122    * Check that the recurse option descends into subdirectories.
123    */
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.");
127
128     $files = file_scan_directory($this->path . '/..', '/^javascript-/', ['recurse' => TRUE]);
129     $this->assertEqual(2, count($files), 'With recursion we found the expected javascript files.');
130   }
131
132
133   /**
134    * Check that the min_depth options lets us ignore files in the starting
135    * directory.
136    */
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.');
140
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.');
143   }
144
145   /**
146    * Tests file_scan_directory() obeys 'file_scan_ignore_directories' setting.
147    */
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.');
151
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".');
155
156     // Ensure that the directories in file_scan_ignore_directories are escaped
157     // using preg_quote.
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.');
161
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.');
164   }
165
166 }