View source
<?php
namespace Drupal\system\Tests\File;
class ScanDirectoryTest extends FileTestBase {
public static $modules = array(
'file_test',
);
protected $path;
protected function setUp() {
parent::setUp();
$this->path = 'core/modules/simpletest/files';
}
function testReturn() {
$all_files = file_scan_directory($this->path, '/^javascript-/');
ksort($all_files);
$this
->assertEqual(2, count($all_files), 'Found two, expected javascript files.');
$file = reset($all_files);
$this
->assertEqual(key($all_files), $file->uri, 'Correct array key was used for the first returned file.');
$this
->assertEqual($file->uri, $this->path . '/javascript-1.txt', 'First file name was set correctly.');
$this
->assertEqual($file->filename, 'javascript-1.txt', 'First basename was set correctly');
$this
->assertEqual($file->name, 'javascript-1', 'First name was set correctly.');
$file = next($all_files);
$this
->assertEqual(key($all_files), $file->uri, 'Correct array key was used for the second returned file.');
$this
->assertEqual($file->uri, $this->path . '/javascript-2.script', 'Second file name was set correctly.');
$this
->assertEqual($file->filename, 'javascript-2.script', 'Second basename was set correctly');
$this
->assertEqual($file->name, 'javascript-2', 'Second name was set correctly.');
}
function testOptionCallback() {
$all_files = file_scan_directory($this->path, '/^NONEXISTINGFILENAME/', array(
'callback' => 'file_test_file_scan_callback',
));
$this
->assertEqual(0, count($all_files), 'No files were found.');
$results = file_test_file_scan_callback();
file_test_file_scan_callback_reset();
$this
->assertEqual(0, count($results), 'No files were passed to the callback.');
$all_files = file_scan_directory($this->path, '/^javascript-/', array(
'callback' => 'file_test_file_scan_callback',
));
$this
->assertEqual(2, count($all_files), 'Found two, expected javascript files.');
$results = file_test_file_scan_callback();
file_test_file_scan_callback_reset();
$this
->assertEqual(2, count($results), 'Files were passed to the callback.');
}
function testOptionNoMask() {
$all_files = file_scan_directory($this->path, '/^javascript-/');
$this
->assertEqual(2, count($all_files), 'Found two, expected javascript files.');
$filtered_files = file_scan_directory($this->path, '/^javascript-/', array(
'nomask' => '/.script$/',
));
$this
->assertEqual(1, count($filtered_files), 'Filtered correctly.');
}
function testOptionKey() {
$expected = array(
$this->path . '/javascript-1.txt',
$this->path . '/javascript-2.script',
);
$actual = array_keys(file_scan_directory($this->path, '/^javascript-/', array(
'key' => 'filepath',
)));
sort($actual);
$this
->assertEqual($expected, $actual, 'Returned the correct values for the filename key.');
$expected = array(
'javascript-1.txt',
'javascript-2.script',
);
$actual = array_keys(file_scan_directory($this->path, '/^javascript-/', array(
'key' => 'filename',
)));
sort($actual);
$this
->assertEqual($expected, $actual, 'Returned the correct values for the basename key.');
$expected = array(
'javascript-1',
'javascript-2',
);
$actual = array_keys(file_scan_directory($this->path, '/^javascript-/', array(
'key' => 'name',
)));
sort($actual);
$this
->assertEqual($expected, $actual, 'Returned the correct values for the name key.');
$expected = array(
$this->path . '/javascript-1.txt',
$this->path . '/javascript-2.script',
);
$actual = array_keys(file_scan_directory($this->path, '/^javascript-/', array(
'key' => 'INVALID',
)));
sort($actual);
$this
->assertEqual($expected, $actual, 'An invalid key defaulted back to the default.');
}
function testOptionRecurse() {
$files = file_scan_directory($this->path . '/..', '/^javascript-/', array(
'recurse' => FALSE,
));
$this
->assertTrue(empty($files), "Without recursion couldn't find javascript files.");
$files = file_scan_directory($this->path . '/..', '/^javascript-/', array(
'recurse' => TRUE,
));
$this
->assertEqual(2, count($files), 'With recursion we found the expected javascript files.');
}
function testOptionMinDepth() {
$files = file_scan_directory($this->path, '/^javascript-/', array(
'min_depth' => 0,
));
$this
->assertEqual(2, count($files), 'No minimum-depth gets files in current directory.');
$files = file_scan_directory($this->path, '/^javascript-/', array(
'min_depth' => 1,
));
$this
->assertTrue(empty($files), 'Minimum-depth of 1 successfully excludes files from current directory.');
}
}