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. *