From f1b5d55d1eac468636022170590d3ac07c5c53c7 Mon Sep 17 00:00:00 2001 From: ndm2 Date: Fri, 26 Aug 2016 03:08:35 +0200 Subject: [PATCH] Make `Folder::inPath()` even more strict. Passing an empty path never worked properly, it was triggering a warning, didn't worked on Windows, and the behavior that the current top level directory would be assumed for empty paths wasn't documented. Similar is true for relative paths. While they did match at one point, this was incorrect behavior, and matching actual path fragments seems out of scope for this method. This change makes the `$path` argument required, and throws an exception in case it is not an absolute path. --- src/Filesystem/Folder.php | 11 +++++---- tests/TestCase/Filesystem/FolderTest.php | 31 ++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/Filesystem/Folder.php b/src/Filesystem/Folder.php index 1c5c94a9aa6..4bf6146d208 100644 --- a/src/Filesystem/Folder.php +++ b/src/Filesystem/Folder.php @@ -16,6 +16,7 @@ use DirectoryIterator; use Exception; +use InvalidArgumentException; use RecursiveDirectoryIterator; use RecursiveIteratorIterator; @@ -418,15 +419,15 @@ public function inCakePath($path = '') /** * Returns true if the Folder is in the given path. * - * @param string $path The absolute path to check that the current `pwd()` resides within. If omitted - * (or empty), the current top level directory is assumed. + * @param string $path The absolute path to check that the current `pwd()` resides within. * @param bool $reverse Reverse the search, check if the given `$path` resides within the current `pwd()`. * @return bool + * @throws \InvalidArgumentException When the given `$path` argument is not an absolute path. */ - public function inPath($path = '', $reverse = false) + public function inPath($path, $reverse = false) { - if (empty($path)) { - $path = realpath(DS); + if (!Folder::isAbsolute($path)) { + throw new InvalidArgumentException('The $path argument is expected to be an absolute path.'); } $dir = Folder::slashTerm($path); diff --git a/tests/TestCase/Filesystem/FolderTest.php b/tests/TestCase/Filesystem/FolderTest.php index cc5d1c51324..2f6f7d4bb5f 100644 --- a/tests/TestCase/Filesystem/FolderTest.php +++ b/tests/TestCase/Filesystem/FolderTest.php @@ -102,8 +102,9 @@ public function testInPath() $result = $Base->pwd(); $this->assertEquals($basePath, $result); + // is "/" in "/tests/test_app/" - $result = $Base->inPath('', true); + $result = $Base->inPath(realpath(DS), true); $this->assertFalse($result, true); // is "/tests/test_app/" in "/tests/test_app/" @@ -128,7 +129,7 @@ public function testInPath() // is "/tests/test_app/" in "/" - $result = $Base->inPath(); + $result = $Base->inPath(realpath(DS)); $this->assertTrue($result); // is "/tests/test_app/" in "/tests/test_app/" @@ -154,6 +155,32 @@ public function testInPath() $this->assertFalse($result); } + /** + * Data provider for the testInPathInvalidPathArgument test + * + * @return array + */ + public function inPathInvalidPathArgumentDataProvider() + { + return [ + [''], + ['relative/path/'], + ['unknown://stream-wrapper'] + ]; + } + + /** + * @dataProvider inPathInvalidPathArgumentDataProvider + * @param string $path + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage The $path argument is expected to be an absolute path. + */ + public function testInPathInvalidPathArgument($path) + { + $Folder = new Folder(); + $Folder->inPath($path); + } + /** * test creation of single and multiple paths. *