Browse Source

Made default value and behavior of param `$exceptions` for Folder::tree() identical to same param in Folder::read()

ADmad 14 years ago
parent
commit
bcab3d0cb9

+ 46 - 5
lib/Cake/Test/Case/Utility/FolderTest.php

@@ -364,6 +364,41 @@ class FolderTest extends CakeTestCase {
 	}
 
 /**
+ * testFolderReadWithHiddenFiles method
+ *
+ * @return void
+ */
+	public function testFolderReadWithHiddenFiles() {
+		$this->skipIf(!is_writeable(TMP), 'Cant test Folder::read with hidden files unless the tmp folder is writable.');
+
+		$Folder = new Folder(TMP . 'folder_tree_hidden', true, 0777);
+		mkdir($Folder->path . DS . '.svn');
+		mkdir($Folder->path . DS . 'some_folder');
+		touch($Folder->path . DS . 'not_hidden.txt');
+		touch($Folder->path . DS . '.hidden.txt');
+
+		$expected = array(
+			array('some_folder'),
+			array('not_hidden.txt'),
+		);
+		$result = $Folder->read(true, true);
+		$this->assertEquals($expected, $result);
+
+		$expected = array(
+			array(
+				'.svn',
+				'some_folder'
+			),
+			array(
+				'.hidden.txt',
+				'not_hidden.txt'
+			),
+		);
+		$result = $Folder->read(true);
+		$this->assertEquals($expected, $result);
+	}
+
+/**
  * testFolderTree method
  *
  * @return void
@@ -417,41 +452,47 @@ class FolderTest extends CakeTestCase {
  * @return void
  */
 	public function testFolderTreeWithHiddenFiles() {
-		$this->skipIf(!is_writeable(TMP), 'Cant test Folder::tree with hidden files unless the tmp folder is writable.');
+		$this->skipIf(!is_writeable(TMP), 'Can\'t test Folder::tree with hidden files unless the tmp folder is writable.');
 
 		$Folder = new Folder(TMP . 'folder_tree_hidden', true, 0777);
 		mkdir($Folder->path . DS . '.svn', 0777, true);
 		touch($Folder->path . DS . '.svn' . DS . 'InHiddenFolder.php');
+		mkdir($Folder->path . DS . '.svn' . DS . 'inhiddenfolder');
+		touch($Folder->path . DS . '.svn' . DS . 'inhiddenfolder' . DS . 'NestedInHiddenFolder.php');
 		touch($Folder->path . DS . 'not_hidden.txt');
 		touch($Folder->path . DS . '.hidden.txt');
+		mkdir($Folder->path . DS . 'visible_folder' . DS . '.git', 0777, true);
 
 		$expected = array(
 			array(
 				$Folder->path,
+				$Folder->path . DS . 'visible_folder',
 			),
 			array(
 				$Folder->path . DS . 'not_hidden.txt',
 			),
 		);
 
-		$result = $Folder->tree(null, false);
-		sort($result[1]);
-		sort($expected[1]);
+		$result = $Folder->tree(null, true);
 		$this->assertEquals($expected, $result);
 
 		$expected = array(
 			array(
 				$Folder->path,
+				$Folder->path . DS . 'visible_folder',
+				$Folder->path . DS . 'visible_folder' . DS . '.git',
 				$Folder->path . DS . '.svn',
+				$Folder->path . DS . '.svn' . DS . 'inhiddenfolder',
 			),
 			array(
 				$Folder->path . DS . 'not_hidden.txt',
 				$Folder->path . DS . '.hidden.txt',
+				$Folder->path . DS . '.svn' . DS . 'inhiddenfolder' . DS . 'NestedInHiddenFolder.php',
 				$Folder->path . DS . '.svn' . DS . 'InHiddenFolder.php',
 			),
 		);
 
-		$result = $Folder->tree(null, true);
+		$result = $Folder->tree(null, false);
 		sort($result[1]);
 		sort($expected[1]);
 		$this->assertEquals($expected, $result);

+ 1 - 4
lib/Cake/TestSuite/CakeTestSuite.php

@@ -49,12 +49,9 @@ class CakeTestSuite extends PHPUnit_Framework_TestSuite {
  */
 	public function addTestDirectoryRecursive($directory = '.') {
 		$Folder = new Folder($directory);
-		$files = $Folder->tree(null, false, 'files');
+		$files = $Folder->tree(null, true, 'files');
 
 		foreach ($files as $file) {
-			if (strpos($file, DS . '.') !== false) {
-				continue;
-			}
 			$this->addTestFile($file);
 		}
 	}

+ 11 - 7
lib/Cake/Utility/Folder.php

@@ -398,22 +398,19 @@ class Folder {
  * Returns an array of nested directories and files in each directory
  *
  * @param string $path the directory path to build the tree from
- * @param mixed $exceptions Array of files to exclude, false to exclude dot files.
- * @param string $type either file or dir. null returns both files and directories
+ * @param mixed $exceptions Either an array of files/folder to exclude
+ *   or boolean true to not grab dot files/folders
+ * @param string $type either 'file' or 'dir'. null returns both files and directories
  * @return mixed array of nested directories and files in each directory
  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::tree
  */
-	public function tree($path = null, $exceptions = true, $type = null) {
+	public function tree($path = null, $exceptions = false, $type = null) {
 		if ($path == null) {
 			$path = $this->path;
 		}
 		$files = array();
 		$directories = array($path);
-		$skipHidden = false;
 
-		if ($exceptions === false) {
-			$skipHidden = true;
-		}
 		if (is_array($exceptions)) {
 			$exceptions = array_flip($exceptions);
 			if (isset($exceptions['.'])) {
@@ -421,6 +418,13 @@ class Folder {
 				unset($exceptions['.']);
 			}
 		}
+		$skipHidden = false;
+		if ($exceptions === true) {
+			$skipHidden = true;
+		} elseif (isset($exceptions['.'])) {
+			$skipHidden = true;
+			unset($exceptions['.']);
+		}
 
 		try {
 			$directory = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::KEY_AS_PATHNAME | RecursiveDirectoryIterator::CURRENT_AS_SELF | RecursiveDirectoryIterator::SKIP_DOTS);