diff options
-rw-r--r-- | apps/files_trashbin/tests/ExpirationTest.php | 54 | ||||
-rw-r--r-- | apps/files_versions/tests/ExpirationTest.php | 53 | ||||
-rw-r--r-- | lib/private/AppFramework/Utility/TimeFactory.php | 11 | ||||
-rw-r--r-- | lib/private/Files/Filesystem.php | 41 | ||||
-rw-r--r-- | lib/public/AppFramework/Utility/ITimeFactory.php | 10 | ||||
-rw-r--r-- | tests/lib/Files/FilesystemTest.php | 129 |
6 files changed, 127 insertions, 171 deletions
diff --git a/apps/files_trashbin/tests/ExpirationTest.php b/apps/files_trashbin/tests/ExpirationTest.php index 396fbdfb887..7bfbea19256 100644 --- a/apps/files_trashbin/tests/ExpirationTest.php +++ b/apps/files_trashbin/tests/ExpirationTest.php @@ -23,8 +23,9 @@ */ use OCA\Files_Trashbin\Expiration; -use \OCA\Files_Trashbin\Tests; +use OCP\AppFramework\Utility\ITimeFactory; use OCP\IConfig; +use PHPUnit\Framework\MockObject\MockObject; class ExpirationTest extends \Test\TestCase { const SECONDS_PER_DAY = 86400; //60*60*24 @@ -182,58 +183,27 @@ class ExpirationTest extends \Test\TestCase { } /** - * * @param int $time - * @return \OCP\AppFramework\Utility\ITimeFactory + * @return ITimeFactory|MockObject */ private function getMockedTimeFactory($time){ - $mockedTimeFactory = $this->getMockBuilder('\OCP\AppFramework\Utility\ITimeFactory') - ->disableOriginalConstructor() - ->setMethods(['getTime']) - ->getMock() - ; - $mockedTimeFactory->expects($this->any())->method('getTime')->will( - $this->returnValue($time) - ); + $mockedTimeFactory = $this->createMock(ITimeFactory::class); + $mockedTimeFactory->expects($this->any()) + ->method('getTime') + ->willReturn($time); return $mockedTimeFactory; } /** - * * @param string $returnValue - * @return IConfig + * @return IConfig|MockObject */ private function getMockedConfig($returnValue){ - $mockedConfig = $this->getMockBuilder(IConfig::class) - ->disableOriginalConstructor() - ->setMethods( - [ - 'setSystemValues', - 'setSystemValue', - 'getSystemValue', - 'getFilteredSystemValue', - 'deleteSystemValue', - 'getAppKeys', - 'setAppValue', - 'getAppValue', - 'deleteAppValue', - 'deleteAppValues', - 'setUserValue', - 'getUserValue', - 'getUserValueForUsers', - 'getUserKeys', - 'deleteUserValue', - 'deleteAllUserValues', - 'deleteAppFromAllUsers', - 'getUsersForUserValue' - ] - ) - ->getMock() - ; - $mockedConfig->expects($this->any())->method('getSystemValue')->will( - $this->returnValue($returnValue) - ); + $mockedConfig = $this->createMock(IConfig::class); + $mockedConfig->expects($this->any()) + ->method('getSystemValue') + ->willReturn($returnValue); return $mockedConfig; } diff --git a/apps/files_versions/tests/ExpirationTest.php b/apps/files_versions/tests/ExpirationTest.php index 8cd288d75cc..a7c34b7ac70 100644 --- a/apps/files_versions/tests/ExpirationTest.php +++ b/apps/files_versions/tests/ExpirationTest.php @@ -25,7 +25,9 @@ namespace OCA\Files_Versions\Tests; use \OCA\Files_Versions\Expiration; +use OCP\AppFramework\Utility\ITimeFactory; use OCP\IConfig; +use PHPUnit\Framework\MockObject\MockObject; class ExpirationTest extends \Test\TestCase { const SECONDS_PER_DAY = 86400; //60*60*24 @@ -151,58 +153,27 @@ class ExpirationTest extends \Test\TestCase { } /** - * * @param int $time - * @return \OCP\AppFramework\Utility\ITimeFactory + * @return ITimeFactory|MockObject */ private function getMockedTimeFactory($time){ - $mockedTimeFactory = $this->getMockBuilder('\OCP\AppFramework\Utility\ITimeFactory') - ->disableOriginalConstructor() - ->setMethods(['getTime']) - ->getMock() - ; - $mockedTimeFactory->expects($this->any())->method('getTime')->will( - $this->returnValue($time) - ); + $mockedTimeFactory = $this->createMock(ITimeFactory::class); + $mockedTimeFactory->expects($this->any()) + ->method('getTime') + ->willReturn($time); return $mockedTimeFactory; } /** - * * @param string $returnValue - * @return \OCP\IConfig + * @return IConfig|MockObject */ private function getMockedConfig($returnValue){ - $mockedConfig = $this->getMockBuilder(IConfig::class) - ->disableOriginalConstructor() - ->setMethods( - [ - 'setSystemValues', - 'setSystemValue', - 'getSystemValue', - 'getFilteredSystemValue', - 'deleteSystemValue', - 'getAppKeys', - 'setAppValue', - 'getAppValue', - 'deleteAppValue', - 'deleteAppValues', - 'setUserValue', - 'getUserValue', - 'getUserValueForUsers', - 'getUserKeys', - 'deleteUserValue', - 'deleteAllUserValues', - 'deleteAppFromAllUsers', - 'getUsersForUserValue' - ] - ) - ->getMock() - ; - $mockedConfig->expects($this->any())->method('getSystemValue')->will( - $this->returnValue($returnValue) - ); + $mockedConfig = $this->createMock(IConfig::class); + $mockedConfig->expects($this->any()) + ->method('getSystemValue') + ->willReturn($returnValue); return $mockedConfig; } diff --git a/lib/private/AppFramework/Utility/TimeFactory.php b/lib/private/AppFramework/Utility/TimeFactory.php index fc3bf72609f..4526f9b1abb 100644 --- a/lib/private/AppFramework/Utility/TimeFactory.php +++ b/lib/private/AppFramework/Utility/TimeFactory.php @@ -37,9 +37,18 @@ class TimeFactory implements ITimeFactory { /** * @return int the result of a call to time() */ - public function getTime() : int { + public function getTime(): int { return time(); } + /** + * @param string $time + * @param \DateTimeZone $timezone + * @return \DateTime + * @since 15.0.0 + */ + public function getDateTime(string $time = 'now', \DateTimeZone $timezone = null): \DateTime { + return new \DateTime($time, $timezone); + } } diff --git a/lib/private/Files/Filesystem.php b/lib/private/Files/Filesystem.php index 12e59e5ba2d..ba9c85deeee 100644 --- a/lib/private/Files/Filesystem.php +++ b/lib/private/Files/Filesystem.php @@ -812,7 +812,7 @@ class Filesystem { return self::$normalizedPathCache[$cacheKey]; } - if ($path == '') { + if ($path === '') { return '/'; } @@ -821,38 +821,29 @@ class Filesystem { $path = \OC_Util::normalizeUnicode($path); } - //no windows style slashes - $path = str_replace('\\', '/', $path); + //add leading slash, if it is already there we strip it anyway + $path = '/' . $path; - //add leading slash - if ($path[0] !== '/') { - $path = '/' . $path; - } + $patterns = [ + '/\\\\/s', // no windows style slashes + '/\/\.(\/\.)?\//s', // remove '/./' + '/\/{2,}/s', // remove squence of slashes + '/\/\.$/s', // remove trailing /. + ]; - // remove '/./' - // ugly, but str_replace() can't replace them all in one go - // as the replacement itself is part of the search string - // which will only be found during the next iteration - while (strpos($path, '/./') !== false) { - $path = str_replace('/./', '/', $path); - } - // remove sequences of slashes - $path = preg_replace('#/{2,}#', '/', $path); + do { + $count = 0; + $path = preg_replace($patterns, '/', $path, -1, $count); + } while ($count > 0); //remove trailing slash - if ($stripTrailingSlash and strlen($path) > 1) { + if ($stripTrailingSlash && strlen($path) > 1) { $path = rtrim($path, '/'); } - // remove trailing '/.' - if (substr($path, -2) == '/.') { - $path = substr($path, 0, -2); - } - - $normalizedPath = $path; - self::$normalizedPathCache[$cacheKey] = $normalizedPath; + self::$normalizedPathCache[$cacheKey] = $path; - return $normalizedPath; + return $path; } /** diff --git a/lib/public/AppFramework/Utility/ITimeFactory.php b/lib/public/AppFramework/Utility/ITimeFactory.php index 7951d6ad8e6..0367f037a6e 100644 --- a/lib/public/AppFramework/Utility/ITimeFactory.php +++ b/lib/public/AppFramework/Utility/ITimeFactory.php @@ -35,6 +35,14 @@ interface ITimeFactory { * @return int the result of a call to time() * @since 8.0.0 */ - public function getTime() : int; + public function getTime(): int; + + /** + * @param string $time + * @param \DateTimeZone $timezone + * @return \DateTime + * @since 15.0.0 + */ + public function getDateTime(string $time = 'now', \DateTimeZone $timezone = null): \DateTime; } diff --git a/tests/lib/Files/FilesystemTest.php b/tests/lib/Files/FilesystemTest.php index a98af220ba1..6fc6fbe9d7c 100644 --- a/tests/lib/Files/FilesystemTest.php +++ b/tests/lib/Files/FilesystemTest.php @@ -115,73 +115,80 @@ class FilesystemTest extends \Test\TestCase { } public function normalizePathData() { - return array( - array('/', ''), - array('/', '/'), - array('/', '//'), - array('/', '/', false), - array('/', '//', false), - - array('/path', '/path/'), - array('/path/', '/path/', false), - array('/path', 'path'), - - array('/foo/bar', '/foo//bar/'), - array('/foo/bar/', '/foo//bar/', false), - array('/foo/bar', '/foo////bar'), - array('/foo/bar', '/foo/////bar'), - array('/foo/bar', '/foo/bar/.'), - array('/foo/bar', '/foo/bar/./'), - array('/foo/bar/', '/foo/bar/./', false), - array('/foo/bar', '/foo/bar/./.'), - array('/foo/bar', '/foo/bar/././'), - array('/foo/bar/', '/foo/bar/././', false), - array('/foo/bar', '/foo/./bar/'), - array('/foo/bar/', '/foo/./bar/', false), - array('/foo/.bar', '/foo/.bar/'), - array('/foo/.bar/', '/foo/.bar/', false), - array('/foo/.bar/tee', '/foo/.bar/tee'), + return [ + ['/', ''], + ['/', '/'], + ['/', '//'], + ['/', '/', false], + ['/', '//', false], + + ['/path', '/path/'], + ['/path/', '/path/', false], + ['/path', 'path'], + + ['/foo/bar', '/foo//bar/'], + ['/foo/bar/', '/foo//bar/', false], + ['/foo/bar', '/foo////bar'], + ['/foo/bar', '/foo/////bar'], + ['/foo/bar', '/foo/bar/.'], + ['/foo/bar', '/foo/bar/./'], + ['/foo/bar/', '/foo/bar/./', false], + ['/foo/bar', '/foo/bar/./.'], + ['/foo/bar', '/foo/bar/././'], + ['/foo/bar/', '/foo/bar/././', false], + ['/foo/bar', '/foo/./bar/'], + ['/foo/bar/', '/foo/./bar/', false], + ['/foo/.bar', '/foo/.bar/'], + ['/foo/.bar/', '/foo/.bar/', false], + ['/foo/.bar/tee', '/foo/.bar/tee'], + + ['/foo/bar', '/.///././//./foo/.///././//./bar/./././.'], + ['/foo/bar/', '/.///././//./foo/.///././//./bar/./././.', false], + ['/foo/bar', '/.///././//./foo/.///././//./bar/././././'], + ['/foo/bar/', '/.///././//./foo/.///././//./bar/././././', false], // Windows paths - array('/', ''), - array('/', '\\'), - array('/', '\\', false), - array('/', '\\\\'), - array('/', '\\\\', false), - - array('/path', '\\path'), - array('/path', '\\path', false), - array('/path', '\\path\\'), - array('/path/', '\\path\\', false), - - array('/foo/bar', '\\foo\\\\bar\\'), - array('/foo/bar/', '\\foo\\\\bar\\', false), - array('/foo/bar', '\\foo\\\\\\\\bar'), - array('/foo/bar', '\\foo\\\\\\\\\\bar'), - array('/foo/bar', '\\foo\\bar\\.'), - array('/foo/bar', '\\foo\\bar\\.\\'), - array('/foo/bar/', '\\foo\\bar\\.\\', false), - array('/foo/bar', '\\foo\\bar\\.\\.'), - array('/foo/bar', '\\foo\\bar\\.\\.\\'), - array('/foo/bar/', '\\foo\\bar\\.\\.\\', false), - array('/foo/bar', '\\foo\\.\\bar\\'), - array('/foo/bar/', '\\foo\\.\\bar\\', false), - array('/foo/.bar', '\\foo\\.bar\\'), - array('/foo/.bar/', '\\foo\\.bar\\', false), - array('/foo/.bar/tee', '\\foo\\.bar\\tee'), + ['/', ''], + ['/', '\\'], + ['/', '\\', false], + ['/', '\\\\'], + ['/', '\\\\', false], + + ['/path', '\\path'], + ['/path', '\\path', false], + ['/path', '\\path\\'], + ['/path/', '\\path\\', false], + + ['/foo/bar', '\\foo\\\\bar\\'], + ['/foo/bar/', '\\foo\\\\bar\\', false], + ['/foo/bar', '\\foo\\\\\\\\bar'], + ['/foo/bar', '\\foo\\\\\\\\\\bar'], + ['/foo/bar', '\\foo\\bar\\.'], + ['/foo/bar', '\\foo\\bar\\.\\'], + ['/foo/bar/', '\\foo\\bar\\.\\', false], + ['/foo/bar', '\\foo\\bar\\.\\.'], + ['/foo/bar', '\\foo\\bar\\.\\.\\'], + ['/foo/bar/', '\\foo\\bar\\.\\.\\', false], + ['/foo/bar', '\\foo\\.\\bar\\'], + ['/foo/bar/', '\\foo\\.\\bar\\', false], + ['/foo/.bar', '\\foo\\.bar\\'], + ['/foo/.bar/', '\\foo\\.bar\\', false], + ['/foo/.bar/tee', '\\foo\\.bar\\tee'], // Absolute windows paths NOT marked as absolute - array('/C:', 'C:\\'), - array('/C:/', 'C:\\', false), - array('/C:/tests', 'C:\\tests'), - array('/C:/tests', 'C:\\tests', false), - array('/C:/tests', 'C:\\tests\\'), - array('/C:/tests/', 'C:\\tests\\', false), + ['/C:', 'C:\\'], + ['/C:/', 'C:\\', false], + ['/C:/tests', 'C:\\tests'], + ['/C:/tests', 'C:\\tests', false], + ['/C:/tests', 'C:\\tests\\'], + ['/C:/tests/', 'C:\\tests\\', false], + ['/C:/tests/bar', 'C:\\tests\\.\\.\\bar'], + ['/C:/tests/bar/', 'C:\\tests\\.\\.\\bar\\.\\', false], // normalize does not resolve '..' (by design) - array('/foo/..', '/foo/../'), - array('/foo/..', '\\foo\\..\\'), - ); + ['/foo/..', '/foo/../'], + ['/foo/..', '\\foo\\..\\'], + ]; } /** |