summaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
Diffstat (limited to 'modules')
-rw-r--r--modules/simpletest/tests/file.test137
1 files changed, 131 insertions, 6 deletions
diff --git a/modules/simpletest/tests/file.test b/modules/simpletest/tests/file.test
index c42d69e20..2935bfd18 100644
--- a/modules/simpletest/tests/file.test
+++ b/modules/simpletest/tests/file.test
@@ -15,6 +15,32 @@ function file_test_validator($file, $errors) {
}
/**
+ * Helper function for testing file_scan_directory().
+ *
+ * Each time the function is called the file is stored in a static variable.
+ * When the function is called with $reset parameter TRUE the cache is cleared
+ * and the results returned.
+ *
+ * @param $file
+ * File object
+ * @param $reset
+ * Boolean indicating that the stored files should be removed and returned.
+ * @return
+ * An array of all previous $file parameters since $reset was last called.
+ */
+function file_test_file_scan_callback($file, $reset = FALSE) {
+ static $files = array();
+
+ if ($reset) {
+ $ret = $files;
+ $files = array();
+ return $ret;
+ }
+
+ $files[] = $file;
+}
+
+/**
* Base class for file tests that adds some additional file specific
* assertions and helper functions.
*/
@@ -786,20 +812,119 @@ class FileScanDirectoryTest extends FileTestCase {
);
}
+ function setUp() {
+ parent::setUp();
+ $this->path = $this->originalFileDirectory . '/simpletest';
+ }
+
/**
- * Check that the no-mask parameter is honored.
+ * Check the format of the returned values.
*/
- function testNoMask() {
- $path = $this->originalFileDirectory . '/simpletest';
+ function testReturn() {
+ // Grab a listing of all the JavaSscript files and check that they're
+ // passed to the callback.
+ $all_files = file_scan_directory($this->path, '/^javascript-/');
+ ksort($all_files);
+ $this->assertEqual(2, count($all_files), t('Found two, expected javascript files.'));
+
+ // Check the first file.
+ $file = reset($all_files);
+ $this->assertEqual(key($all_files), $file->filename, t('Correct array key was used for the first returned file.'));
+ $this->assertEqual($file->filename, $this->path . '/javascript-1.txt', t('First file name was set correctly.'));
+ $this->assertEqual($file->basename, 'javascript-1.txt', t('First basename was set correctly'));
+ $this->assertEqual($file->name, 'javascript-1', t('First name was set correctly.'));
- // Grab a listing of all the JS files.
- $all_files = file_scan_directory($path, '/javascript*/');
+ // Check the second file.
+ $file = next($all_files);
+ $this->assertEqual(key($all_files), $file->filename, t('Correct array key was used for the second returned file.'));
+ $this->assertEqual($file->filename, $this->path . '/javascript-2.script', t('Second file name was set correctly.'));
+ $this->assertEqual($file->basename, 'javascript-2.script', t('Second basename was set correctly'));
+ $this->assertEqual($file->name, 'javascript-2', t('Second name was set correctly.'));
+ }
+
+ /**
+ * Check that the callback function is called correctly.
+ */
+ function testOptionCallback() {
+ // When nothing is matched nothing should be passed to the callback.
+ $all_files = file_scan_directory($this->path, '/^NONEXISTINGFILENAME/', array('callback' => 'file_test_file_scan_callback'));
+ $this->assertEqual(0, count($all_files), t('No files were found.'));
+ $results = file_test_file_scan_callback(NULL, TRUE);
+ $this->assertEqual(0, count($results), t('No files were passed to the callback.'));
+
+ // Grab a listing of all the JavaSscript files and check that they're
+ // 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), t('Found two, expected javascript files.'));
+ $results = file_test_file_scan_callback(NULL, TRUE);
+ $this->assertEqual(2, count($results), t('Files were passed to the callback.'));
+ }
+
+ /**
+ * Check that the no-mask parameter is honored.
+ */
+ function testOptionNoMask() {
+ // Grab a listing of all the JavaSscript files.
+ $all_files = file_scan_directory($this->path, '/^javascript-/');
$this->assertEqual(2, count($all_files), t('Found two, expected javascript files.'));
// Now use the nomast parameter to filter out the .script file.
- $filtered_files = file_scan_directory($path, '/javascript*/', '/.script$/');
+ $filtered_files = file_scan_directory($this->path, '/^javascript-/', array('nomask' => '/.script$/'));
$this->assertEqual(1, count($filtered_files), t('Filtered correctly.'));
}
+
+ /**
+ * Check that key parameter sets the return value's key.
+ */
+ function testOptionKey() {
+ // "filename", for the path starting with $dir.
+ $expected = array($this->path . '/javascript-1.txt', $this->path . '/javascript-2.script');
+ $actual = array_keys(file_scan_directory($this->path, '/^javascript-/', array('key' => 'filename')));
+ sort($actual);
+ $this->assertEqual($expected, $actual, t('Returned the correct values for the filename key.'));
+
+ // "basename", for the basename of the file.
+ $expected = array('javascript-1.txt', 'javascript-2.script');
+ $actual = array_keys(file_scan_directory($this->path, '/^javascript-/', array('key' => 'basename')));
+ sort($actual);
+ $this->assertEqual($expected, $actual, t('Returned the correct values for the basename key.'));
+
+ // "name" for the name of the file without an extension.
+ $expected = array('javascript-1', 'javascript-2');
+ $actual = array_keys(file_scan_directory($this->path, '/^javascript-/', array('key' => 'name')));
+ sort($actual);
+ $this->assertEqual($expected, $actual, t('Returned the correct values for the name key.'));
+
+ // Invalid option that should default back to "filename".
+ $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, t('An invalid key defaulted back to the default.'));
+ }
+
+ /**
+ * Check that the recurse option decends into subdirectories.
+ */
+ function testOptionRecurse() {
+ $files = file_scan_directory($this->originalFileDirectory, '/^javascript-/', array('recurse' => FALSE));
+ $this->assertTrue(empty($files), t("Without recursion couldn't find javascript files."));
+
+ $files = file_scan_directory($this->originalFileDirectory, '/^javascript-/', array('recurse' => TRUE));
+ $this->assertEqual(2, count($files), t('With recursion we found the expected javascript files.'));
+ }
+
+
+ /**
+ * Check that the min_depth options lets us ignore files in the starting
+ * directory.
+ */
+ function testOptionMinDepth() {
+ $files = file_scan_directory($this->path, '/^javascript-/', array('min_depth' => 0));
+ $this->assertEqual(2, count($files), t('No minimum-depth gets files in current directory.'));
+
+ $files = file_scan_directory($this->path, '/^javascript-/', array('min_depth' => 1));
+ $this->assertTrue(empty($files), t("Minimum-depth of 1 successfully excludes files from current directory."));
+ }
}