From 7b94c7f9c1d2dd8115506d90f04f9e3e2f989cf5 Mon Sep 17 00:00:00 2001 From: Lukas Reschke Date: Mon, 20 Oct 2014 12:37:32 +0200 Subject: [PATCH 1/4] Refer to relative path instead of absolute path There is no need to refer to the absolute path here if we can use the relative one. Conflicts: lib/private/templatelayout.php --- lib/private/templatelayout.php | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/lib/private/templatelayout.php b/lib/private/templatelayout.php index 10abff6267..558ddad4af 100644 --- a/lib/private/templatelayout.php +++ b/lib/private/templatelayout.php @@ -157,7 +157,7 @@ class OC_TemplateLayout extends OC_Template { public function generateAssets() { $jsFiles = self::findJavascriptFiles(OC_Util::$scripts); - $jsHash = self::hashScriptNames($jsFiles); + $jsHash = self::hashFileNames($jsFiles); if (!file_exists("assets/$jsHash.js")) { $jsFiles = array_map(function ($item) { @@ -179,7 +179,7 @@ class OC_TemplateLayout extends OC_Template { } $cssFiles = self::findStylesheetFiles(OC_Util::$styles); - $cssHash = self::hashScriptNames($cssFiles); + $cssHash = self::hashFileNames($cssFiles); if (!file_exists("assets/$cssHash.css")) { $cssFiles = array_map(function ($item) { @@ -210,17 +210,30 @@ class OC_TemplateLayout extends OC_Template { $this->append('cssfiles', OC_Helper::linkTo('assets', "$cssHash.css")); } + /** + * Converts the absolute filepath to a relative path from \OC::$SERVERROOT + * @param string $filePath Absolute path + * @return string Relative path + * @throws Exception If $filePath is not under \OC::$SERVERROOT + */ + public static function convertToRelativePath($filePath) { + $relativePath = explode(\OC::$SERVERROOT, $filePath); + if(count($relativePath) !== 2) { + throw new \Exception('$filePath is not under the \OC::$SERVERROOT'); + } + + return $relativePath[1]; + } + /** * @param array $files * @return string */ - private static function hashScriptNames($files) { - $files = array_map(function ($item) { - $root = $item[0]; - $file = $item[2]; - return $root . '/' . $file; - }, $files); + private static function hashFileNames($files) { + foreach($files as $i => $file) { + $files[$i] = self::convertToRelativePath($file[0]).'/'.$file[2]; + } sort($files); // include the apps' versions hash to invalidate the cached assets From 8f8abdbaeef7bcaff1f822714a7f227933cc43c1 Mon Sep 17 00:00:00 2001 From: Lukas Reschke Date: Wed, 15 Oct 2014 13:43:04 +0200 Subject: [PATCH 2/4] Add unit tests for convertToRelativePath --- lib/private/request.php | 2 +- tests/lib/templatelayout.php | 44 ++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 tests/lib/templatelayout.php diff --git a/lib/private/request.php b/lib/private/request.php index fa446837a9..1cfa4a150c 100755 --- a/lib/private/request.php +++ b/lib/private/request.php @@ -245,7 +245,7 @@ class OC_Request { * @return string Path info or false when not found */ public static function getRawPathInfo() { - $requestUri = $_SERVER['REQUEST_URI']; + $requestUri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : ''; // remove too many leading slashes - can be caused by reverse proxy configuration if (strpos($requestUri, '/') === 0) { $requestUri = '/' . ltrim($requestUri, '/'); diff --git a/tests/lib/templatelayout.php b/tests/lib/templatelayout.php new file mode 100644 index 0000000000..f87db4fa54 --- /dev/null +++ b/tests/lib/templatelayout.php @@ -0,0 +1,44 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Test; + +/** + * @package OC\Test + */ +class OC_TemplateLayout extends \PHPUnit_Framework_TestCase { + + /** + * Contains valid file paths in the scheme array($absolutePath, $expectedPath) + * @return array + */ + public function validFilePathProvider() { + return array( + array(\OC::$SERVERROOT . '/apps/files/js/fancyJS.js', '/apps/files/js/fancyJS.js'), + array(\OC::$SERVERROOT. '/test.js', '/test.js'), + array(\OC::$SERVERROOT . '/core/test.js', '/core/test.js'), + array(\OC::$SERVERROOT, ''), + ); + } + + /** + * @dataProvider validFilePathProvider + */ + public function testConvertToRelativePath($absolutePath, $expected) { + $relativePath = \Test_Helper::invokePrivate(new \OC_TemplateLayout('user'), 'convertToRelativePath', array($absolutePath)); + $this->assertEquals($expected, $relativePath); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage $filePath is not under the \OC::$SERVERROOT + */ + public function testInvalidConvertToRelativePath() { + \Test_Helper::invokePrivate(new \OC_TemplateLayout('user'), 'convertToRelativePath', array('/this/file/is/invalid')); + } +} From 4ce3c25c5c90725e203a8fc2ab24eb5d7cc514bb Mon Sep 17 00:00:00 2001 From: Lukas Reschke Date: Mon, 20 Oct 2014 13:35:23 +0200 Subject: [PATCH 3/4] Add "$_SERVER['REQUEST_URI']" to fix the unit tests Let's hope that works --- tests/lib/templatelayout.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/lib/templatelayout.php b/tests/lib/templatelayout.php index f87db4fa54..87e5f1b50b 100644 --- a/tests/lib/templatelayout.php +++ b/tests/lib/templatelayout.php @@ -30,6 +30,9 @@ class OC_TemplateLayout extends \PHPUnit_Framework_TestCase { * @dataProvider validFilePathProvider */ public function testConvertToRelativePath($absolutePath, $expected) { + $_SERVER['REQUEST_URI'] = $expected; + $_SERVER['SCRIPT_NAME'] = '/'; + $relativePath = \Test_Helper::invokePrivate(new \OC_TemplateLayout('user'), 'convertToRelativePath', array($absolutePath)); $this->assertEquals($expected, $relativePath); } @@ -39,6 +42,10 @@ class OC_TemplateLayout extends \PHPUnit_Framework_TestCase { * @expectedExceptionMessage $filePath is not under the \OC::$SERVERROOT */ public function testInvalidConvertToRelativePath() { - \Test_Helper::invokePrivate(new \OC_TemplateLayout('user'), 'convertToRelativePath', array('/this/file/is/invalid')); + $invalidFile = '/this/file/is/invalid'; + $_SERVER['REQUEST_URI'] = $invalidFile; + $_SERVER['SCRIPT_NAME'] = '/'; + + \Test_Helper::invokePrivate(new \OC_TemplateLayout('user'), 'convertToRelativePath', array($invalidFile)); } } From 51976b2729a496cb2aa79d844f00d27112a66930 Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Mon, 20 Oct 2014 17:11:08 +0200 Subject: [PATCH 4/4] Add proper setup and teardown Properly restore REQUEST_URI and SCRIPT_NAME after test runs --- tests/lib/templatelayout.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/lib/templatelayout.php b/tests/lib/templatelayout.php index 87e5f1b50b..0335c7c88e 100644 --- a/tests/lib/templatelayout.php +++ b/tests/lib/templatelayout.php @@ -13,6 +13,23 @@ namespace OC\Test; */ class OC_TemplateLayout extends \PHPUnit_Framework_TestCase { + private $oldServerUri; + private $oldScriptName; + + public function setUp() { + $this->oldServerURI = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : null; + $this->oldScriptName = $_SERVER['SCRIPT_NAME']; + } + + public function tearDown() { + if ($this->oldServerURI === null) { + unset($_SERVER['REQUEST_URI']); + } else { + $_SERVER['REQUEST_URI'] = $this->oldServerURI; + } + $_SERVER['SCRIPT_NAME'] = $this->oldScriptName; + } + /** * Contains valid file paths in the scheme array($absolutePath, $expectedPath) * @return array