server/tests/lib/templatelayout.php
Lukas Reschke 886bda5f81 Refactor OC_Request into TrustedDomainHelper and IRequest
This changeset removes the static class `OC_Request` and moves the functions either into `IRequest` which is accessible via `\OC::$server::->getRequest()` or into a separated `TrustedDomainHelper` class for some helper methods which should not be publicly exposed.

This changes only internal methods and nothing on the public API. Some public functions in `util.php` have been deprecated though in favour of the new non-static functions.

Unfortunately some part of this code uses things like `__DIR__` and thus is not completely unit-testable. Where tests where possible they ahve been added though.

Fixes https://github.com/owncloud/core/issues/13976 which was requested in https://github.com/owncloud/core/pull/13973#issuecomment-73492969
2015-02-16 22:13:00 +01:00

72 lines
1.9 KiB
PHP

<?php
/**
* Copyright (c) 2014 Lukas Reschke <lukas@owncloud.com>
* 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 \Test\TestCase {
private $oldServerURI;
private $oldScriptName;
protected function setUp() {
parent::setUp();
$this->oldServerURI = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : null;
$this->oldScriptName = $_SERVER['SCRIPT_NAME'];
}
protected function tearDown() {
if ($this->oldServerURI === null) {
unset($_SERVER['REQUEST_URI']);
} else {
$_SERVER['REQUEST_URI'] = $this->oldServerURI;
}
$_SERVER['SCRIPT_NAME'] = $this->oldScriptName;
parent::tearDown();
}
/**
* 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) {
$_SERVER['REQUEST_URI'] = $expected;
$_SERVER['SCRIPT_NAME'] = $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() {
$invalidFile = '/this/file/is/invalid';
$_SERVER['REQUEST_URI'] = $invalidFile;
$_SERVER['SCRIPT_NAME'] = '/';
\Test_Helper::invokePrivate(new \OC_TemplateLayout('user'), 'convertToRelativePath', array($invalidFile));
}
}