From e2d3225e5aff343bf62b6dbb62eb3526e525cb6b Mon Sep 17 00:00:00 2001 From: Thomas Mueller Date: Mon, 29 Jul 2013 18:24:05 +0200 Subject: [PATCH 1/3] implement a platform independent version of basename --- lib/files/cache/cache.php | 2 +- lib/util.php | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/files/cache/cache.php b/lib/files/cache/cache.php index 3818fdbd84..5b8dc46b77 100644 --- a/lib/files/cache/cache.php +++ b/lib/files/cache/cache.php @@ -200,7 +200,7 @@ class Cache { $data['path'] = $file; $data['parent'] = $this->getParentId($file); - $data['name'] = basename($file); + $data['name'] = \OC_Util::basename($file); $data['encrypted'] = isset($data['encrypted']) ? ((int)$data['encrypted']) : 0; list($queryParts, $params) = $this->buildParts($data); diff --git a/lib/util.php b/lib/util.php index 981b05b2b4..004470908d 100755 --- a/lib/util.php +++ b/lib/util.php @@ -892,4 +892,10 @@ class OC_Util { return $value; } + + public static function basename($file) + { + $t = explode('/', $file); + return array_pop($t); + } } From 085fdfec2f298fd89d0265d0f97edfdda5a12954 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Mon, 29 Jul 2013 23:32:03 +0200 Subject: [PATCH 2/3] adding unit tests for OC_Util::basename --- lib/util.php | 1 + tests/lib/util.php | 34 +++++++++++++++++++++++++--------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/lib/util.php b/lib/util.php index 004470908d..7f7b9f334b 100755 --- a/lib/util.php +++ b/lib/util.php @@ -895,6 +895,7 @@ class OC_Util { public static function basename($file) { + $file = rtrim($file, '/'); $t = explode('/', $file); return array_pop($t); } diff --git a/tests/lib/util.php b/tests/lib/util.php index 9742d57ac7..2781ade099 100644 --- a/tests/lib/util.php +++ b/tests/lib/util.php @@ -8,12 +8,9 @@ class Test_Util extends PHPUnit_Framework_TestCase { - // Constructor - function Test_Util() { - date_default_timezone_set("UTC"); - } - function testFormatDate() { + date_default_timezone_set("UTC"); + $result = OC_Util::formatDate(1350129205); $expected = 'October 13, 2012 11:53'; $this->assertEquals($expected, $result); @@ -61,8 +58,27 @@ class Test_Util extends PHPUnit_Framework_TestCase { OC_Config::deleteKey('mail_domain'); } - function testGetInstanceIdGeneratesValidId() { - OC_Config::deleteKey('instanceid'); - $this->assertStringStartsWith('oc', OC_Util::getInstanceId()); - } + function testGetInstanceIdGeneratesValidId() { + OC_Config::deleteKey('instanceid'); + $this->assertStringStartsWith('oc', OC_Util::getInstanceId()); + } + + /** + * @dataProvider baseNameProvider + */ + public function testBaseName($expected, $file) + { + $base = \OC_Util::basename($file); + $this->assertEquals($expected, $base); + } + + public function baseNameProvider() + { + return array( + array('public_html', '/home/user/public_html/'), + array('public_html', '/home/user/public_html'), + array('', '/'), + array('442aa682de2a64db1e010f50e60fd9c9', 'local::C:\Users\ADMINI~1\AppData\Local\Temp\2/442aa682de2a64db1e010f50e60fd9c9/') + ); + } } From 409268f3227854dab73233a88a114f26b0fed478 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= Date: Tue, 30 Jul 2013 08:54:39 +0200 Subject: [PATCH 3/3] adding test case with no / --- tests/lib/util.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/lib/util.php b/tests/lib/util.php index 2781ade099..a038538d7e 100644 --- a/tests/lib/util.php +++ b/tests/lib/util.php @@ -78,6 +78,7 @@ class Test_Util extends PHPUnit_Framework_TestCase { array('public_html', '/home/user/public_html/'), array('public_html', '/home/user/public_html'), array('', '/'), + array('public_html', 'public_html'), array('442aa682de2a64db1e010f50e60fd9c9', 'local::C:\Users\ADMINI~1\AppData\Local\Temp\2/442aa682de2a64db1e010f50e60fd9c9/') ); }