Merge branch 'master' into activities-api

Conflicts:
	lib/private/server.php
This commit is contained in:
Thomas Müller 2013-10-16 21:15:25 +02:00
commit 221a650815
26 changed files with 233 additions and 476 deletions

View File

@ -69,8 +69,9 @@ class OC {
* check if owncloud runs in cli mode
*/
public static $CLI = false;
/*
* OC router
/**
* @var OC_Router
*/
protected static $router = null;
@ -343,6 +344,9 @@ class OC {
return OC_Config::getValue('session_lifetime', 60 * 60 * 24);
}
/**
* @return OC_Router
*/
public static function getRouter() {
if (!isset(OC::$router)) {
OC::$router = new OC_Router();

View File

@ -42,12 +42,9 @@ class App {
* @param string $controllerName the name of the controller under which it is
* stored in the DI container
* @param string $methodName the method that you want to call
* @param array $urlParams an array with variables extracted from the routes
* @param DIContainer $container an instance of a pimple container.
*/
public static function main($controllerName, $methodName, array $urlParams,
IAppContainer $container) {
$container['urlParams'] = $urlParams;
public static function main($controllerName, $methodName, IAppContainer $container) {
$controller = $container[$controllerName];
// initialize the dispatcher and run all the middleware before the controller

View File

@ -99,89 +99,6 @@ class API implements IApi{
}
/**
* Returns the translation object
* @return \OC_L10N the translation object
*/
public function getTrans(){
# TODO: use public api
return \OC_L10N::get($this->appName);
}
/**
* Returns the URL for a route
* @param string $routeName the name of the route
* @param array $arguments an array with arguments which will be filled into the url
* @return string the url
*/
public function linkToRoute($routeName, $arguments=array()){
return \OCP\Util::linkToRoute($routeName, $arguments);
}
/**
* Returns an URL for an image or file
* @param string $file the name of the file
* @param string $appName the name of the app, defaults to the current one
*/
public function linkTo($file, $appName=null){
if($appName === null){
$appName = $this->appName;
}
return \OCP\Util::linkTo($appName, $file);
}
/**
* Returns the link to an image, like link to but only with prepending img/
* @param string $file the name of the file
* @param string $appName the name of the app, defaults to the current one
*/
public function imagePath($file, $appName=null){
if($appName === null){
$appName = $this->appName;
}
return \OCP\Util::imagePath($appName, $file);
}
/**
* Makes an URL absolute
* @param string $url the url
* @return string the absolute url
*/
public function getAbsoluteURL($url){
# TODO: use public api
return \OC_Helper::makeURLAbsolute($url);
}
/**
* links to a file
* @param string $file the name of the file
* @param string $appName the name of the app, defaults to the current one
* @deprecated replaced with linkToRoute()
* @return string the url
*/
public function linkToAbsolute($file, $appName=null){
if($appName === null){
$appName = $this->appName;
}
return \OCP\Util::linkToAbsolute($appName, $file);
}
/**
* Checks if the CSRF check was correct
* @return bool true if CSRF check passed
*/
public function passesCSRFCheck(){
# TODO: use public api
return \OC_Util::isCallRegistered();
}
/**
* Checks if an app is enabled
* @param string $appName the name of an app
@ -192,44 +109,6 @@ class API implements IApi{
}
/**
* Writes a function into the error log
* @param string $msg the error message to be logged
* @param int $level the error level
*/
public function log($msg, $level=null){
switch($level){
case 'debug':
$level = \OCP\Util::DEBUG;
break;
case 'info':
$level = \OCP\Util::INFO;
break;
case 'warn':
$level = \OCP\Util::WARN;
break;
case 'fatal':
$level = \OCP\Util::FATAL;
break;
default:
$level = \OCP\Util::ERROR;
break;
}
\OCP\Util::writeLog($this->appName, $msg, $level);
}
/**
* turns an owncloud path into a path on the filesystem
* @param string path the path to the file on the oc filesystem
* @return string the filepath in the filesystem
*/
public function getLocalFilePath($path){
# TODO: use public api
return \OC_Filesystem::getLocalFile($path);
}
/**
* used to return and open a new eventsource
* @return \OC_EventSource a new open EventSource class
@ -275,15 +154,6 @@ class API implements IApi{
}
}
/**
* Gets the content of an URL by using CURL or a fallback if it is not
* installed
* @param string $url the url that should be fetched
* @return string the content of the webpage
*/
public function getUrlContent($url) {
return \OC_Util::getUrlContent($url);
}
/**
* Register a backgroundjob task
@ -295,25 +165,6 @@ class API implements IApi{
\OCP\Backgroundjob::addRegularTask($className, $methodName);
}
/**
* Returns a template
* @param string $templateName the name of the template
* @param string $renderAs how it should be rendered
* @param string $appName the name of the app
* @return \OCP\Template a new template
*/
public function getTemplate($templateName, $renderAs='user', $appName=null){
if($appName === null){
$appName = $this->appName;
}
if($renderAs === 'blank'){
return new \OCP\Template($appName, $templateName);
} else {
return new \OCP\Template($appName, $templateName, $renderAs);
}
}
/**
* Tells ownCloud to include a template in the admin overview
@ -330,19 +181,4 @@ class API implements IApi{
}
/**
* get the filesystem info
*
* @param string $path
* @return array with the following keys:
* - size
* - mtime
* - mimetype
* - encrypted
* - versioned
*/
public function getFileInfo($path) {
return \OC\Files\Filesystem::getFileInfo($path);
}
}

View File

@ -35,6 +35,7 @@ use OC\AppFramework\Utility\TimeFactory;
use OCP\AppFramework\IApi;
use OCP\AppFramework\IAppContainer;
use OCP\AppFramework\IMiddleWare;
use OCP\AppFramework\Middleware;
use OCP\IServerContainer;
@ -49,9 +50,10 @@ class DIContainer extends SimpleContainer implements IAppContainer{
* Put your class dependencies in here
* @param string $appName the name of the app
*/
public function __construct($appName){
public function __construct($appName, $urlParams = array()){
$this['AppName'] = $appName;
$this['urlParams'] = $urlParams;
$this->registerParameter('ServerContainer', \OC::$server);
@ -66,6 +68,7 @@ class DIContainer extends SimpleContainer implements IAppContainer{
/** @var $c SimpleContainer */
/** @var $server IServerContainer */
$server = $c->query('ServerContainer');
$server->registerParameter('urlParams', $c['urlParams']);
return $server->getRequest();
});
@ -86,7 +89,7 @@ class DIContainer extends SimpleContainer implements IAppContainer{
* Middleware
*/
$this['SecurityMiddleware'] = $this->share(function($c){
return new SecurityMiddleware($c['API'], $c['Request']);
return new SecurityMiddleware($this, $c['Request']);
});
$middleWares = $this->middleWares;
@ -130,10 +133,10 @@ class DIContainer extends SimpleContainer implements IAppContainer{
}
/**
* @param IMiddleWare $middleWare
* @param Middleware $middleWare
* @return boolean
*/
function registerMiddleWare(IMiddleWare $middleWare) {
function registerMiddleWare(Middleware $middleWare) {
array_push($this->middleWares, $middleWare);
}
@ -144,4 +147,49 @@ class DIContainer extends SimpleContainer implements IAppContainer{
function getAppName() {
return $this->query('AppName');
}
/**
* @return boolean
*/
function isLoggedIn() {
return \OC_User::isLoggedIn();
}
/**
* @return boolean
*/
function isAdminUser() {
$uid = $this->getUserId();
return \OC_User::isAdminUser($uid);
}
private function getUserId() {
return \OC::$session->get('user_id');
}
/**
* @param $message
* @param $level
* @return mixed
*/
function log($message, $level) {
switch($level){
case 'debug':
$level = \OCP\Util::DEBUG;
break;
case 'info':
$level = \OCP\Util::INFO;
break;
case 'warn':
$level = \OCP\Util::WARN;
break;
case 'fatal':
$level = \OCP\Util::FATAL;
break;
default:
$level = \OCP\Util::ERROR;
break;
}
\OCP\Util::writeLog($this->getAppName(), $message, $level);
}
}

View File

@ -24,8 +24,8 @@
namespace OC\AppFramework\Http;
use \OC\AppFramework\Controller\Controller;
use \OC\AppFramework\Middleware\MiddlewareDispatcher;
use OCP\AppFramework\Controller;
/**

View File

@ -24,7 +24,7 @@
namespace OC\AppFramework\Middleware;
use OC\AppFramework\Controller\Controller;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\MiddleWare;

View File

@ -24,15 +24,14 @@
namespace OC\AppFramework\Middleware\Security;
use OC\AppFramework\Controller\Controller;
use OC\AppFramework\Http\Http;
use OC\AppFramework\Http\Request;
use OC\AppFramework\Http\RedirectResponse;
use OC\AppFramework\Utility\MethodAnnotationReader;
use OC\AppFramework\Core\API;
use OCP\AppFramework\Middleware;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\IAppContainer;
use OCP\IRequest;
/**
@ -43,18 +42,22 @@ use OCP\AppFramework\Http\JSONResponse;
*/
class SecurityMiddleware extends Middleware {
private $api;
/**
* @var \OCP\AppFramework\IAppContainer
*/
private $app;
/**
* @var \OC\AppFramework\Http\Request
* @var \OCP\IRequest
*/
private $request;
/**
* @param API $api an instance of the api
* @param IAppContainer $app
* @param IRequest $request
*/
public function __construct(API $api, Request $request){
$this->api = $api;
public function __construct(IAppContainer $app, IRequest $request){
$this->app = $app;
$this->request = $request;
}
@ -74,24 +77,24 @@ class SecurityMiddleware extends Middleware {
// this will set the current navigation entry of the app, use this only
// for normal HTML requests and not for AJAX requests
$this->api->activateNavigationEntry();
$this->app->getServer()->getNavigationManager()->setActiveEntry($this->app->getAppName());
// security checks
$isPublicPage = $annotationReader->hasAnnotation('PublicPage');
if(!$isPublicPage) {
if(!$this->api->isLoggedIn()) {
if(!$this->app->isLoggedIn()) {
throw new SecurityException('Current user is not logged in', Http::STATUS_UNAUTHORIZED);
}
if(!$annotationReader->hasAnnotation('NoAdminRequired')) {
if(!$this->api->isAdminUser($this->api->getUserId())) {
if(!$this->app->isAdminUser()) {
throw new SecurityException('Logged in user must be an admin', Http::STATUS_FORBIDDEN);
}
}
}
if(!$annotationReader->hasAnnotation('NoCSRFRequired')) {
if(!$this->api->passesCSRFCheck()) {
if(!$this->request->passesCSRFCheck()) {
throw new SecurityException('CSRF check failed', Http::STATUS_PRECONDITION_FAILED);
}
}
@ -118,12 +121,13 @@ class SecurityMiddleware extends Middleware {
array('message' => $exception->getMessage()),
$exception->getCode()
);
$this->api->log($exception->getMessage(), 'debug');
$this->app->log($exception->getMessage(), 'debug');
} else {
$url = $this->api->linkToAbsolute('index.php', ''); // TODO: replace with link to route
// TODO: replace with link to route
$url = $this->app->getServer()->getURLGenerator()->getAbsoluteURL('index.php');
$response = new RedirectResponse($url);
$this->api->log($exception->getMessage(), 'debug');
$this->app->log($exception->getMessage(), 'debug');
}
return $response;

View File

@ -81,7 +81,7 @@ class OC_Helper {
* Returns a absolute url to the given app and file.
*/
public static function makeURLAbsolute($url) {
return OC::$server->getURLGenerator()->makeURLAbsolute($url);
return OC::$server->getURLGenerator()->getAbsoluteURL($url);
}
/**

View File

@ -22,7 +22,7 @@ class Factory {
* get an L10N instance
* @param $app string
* @param $lang string|null
* @return OC_L10N
* @return \OC_L10N
*/
public function get($app) {
if (!isset($this->instances[$app])) {

View File

@ -34,7 +34,6 @@ class Server extends SimpleContainer implements IServerContainer {
$requesttoken = false;
}
return new Request(
array(
'get' => $_GET,
@ -46,7 +45,6 @@ class Server extends SimpleContainer implements IServerContainer {
'method' => (isset($_SERVER) && isset($_SERVER['REQUEST_METHOD']))
? $_SERVER['REQUEST_METHOD']
: null,
'params' => $params,
'urlParams' => $urlParams,
'requesttoken' => $requesttoken,
)

View File

@ -8,18 +8,20 @@
*/
namespace OC;
use OCP\IURLGenerator;
use RuntimeException;
/**
* Class to generate URLs
*/
class URLGenerator {
class URLGenerator implements IURLGenerator {
/**
* @brief Creates an url using a defined route
* @param $route
* @param array $parameters
* @return
* @internal param array $args with param=>value, will be appended to the returned url
* @returns the url
* @returns string the url
*
* Returns a url to the given app and file.
*/
@ -97,15 +99,13 @@ class URLGenerator {
}
}
/**
* @brief Makes an $url absolute
* @param string $url the url
* @return string the absolute url
*
* Returns a absolute url to the given app and file.
* Makes an URL absolute
* @param string $url the url in the owncloud host
* @return string the absolute version of the url
*/
public function makeURLAbsolute($url) {
public function getAbsoluteURL($url) {
return \OC_Request::serverProtocol() . '://' . \OC_Request::serverHost() . $url;
}
}

View File

@ -31,8 +31,11 @@ namespace OCP\AppFramework;
* to be registered using IContainer::registerService
*/
class App {
public function __construct($appName) {
$this->container = new \OC\AppFramework\DependencyInjection\DIContainer($appName);
/**
* @param array $urlParams an array with variables extracted from the routes
*/
public function __construct($appName, $urlParams = array()) {
$this->container = new \OC\AppFramework\DependencyInjection\DIContainer($appName, $urlParams);
}
private $container;
@ -50,8 +53,8 @@ class App {
* Example code in routes.php of the task app:
* $this->create('tasks_index', '/')->get()->action(
* function($params){
* $app = new TaskApp();
* $app->dispatch('PageController', 'index', $params);
* $app = new TaskApp($params);
* $app->dispatch('PageController', 'index');
* }
* );
*
@ -59,8 +62,8 @@ class App {
* Example for for TaskApp implementation:
* class TaskApp extends \OCP\AppFramework\App {
*
* public function __construct(){
* parent::__construct('tasks');
* public function __construct($params){
* parent::__construct('tasks', $params);
*
* $this->getContainer()->registerService('PageController', function(IAppContainer $c){
* $a = $c->query('API');
@ -73,9 +76,8 @@ class App {
* @param string $controllerName the name of the controller under which it is
* stored in the DI container
* @param string $methodName the method that you want to call
* @param array $urlParams an array with variables extracted from the routes
*/
public function dispatch($controllerName, $methodName, array $urlParams) {
\OC\AppFramework\App::main($controllerName, $methodName, $urlParams, $this->container);
public function dispatch($controllerName, $methodName) {
\OC\AppFramework\App::main($controllerName, $methodName, $this->container);
}
}

View File

@ -22,11 +22,11 @@
*/
namespace OC\AppFramework\Controller;
namespace OCP\AppFramework;
use OC\AppFramework\Http\Request;
use OC\AppFramework\Core\API;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\IAppContainer;
use OCP\IRequest;
/**
@ -35,18 +35,21 @@ use OCP\AppFramework\Http\TemplateResponse;
abstract class Controller {
/**
* @var API instance of the api layer
* @var \OCP\AppFramework\IAppContainer
*/
protected $api;
protected $app;
/**
* @var \OCP\IRequest
*/
protected $request;
/**
* @param API $api an api wrapper instance
* @param Request $request an instance of the request
* @param IAppContainer $app interface to the app
* @param IRequest $request an instance of the request
*/
public function __construct(API $api, Request $request){
$this->api = $api;
public function __construct(IAppContainer $app, IRequest $request){
$this->app = $app;
$this->request = $request;
}
@ -127,7 +130,7 @@ abstract class Controller {
*/
public function render($templateName, array $params=array(),
$renderAs='user', array $headers=array()){
$response = new TemplateResponse($this->api, $templateName);
$response = new TemplateResponse($this->app->getAppName(), $templateName);
$response->setParams($params);
$response->renderAs($renderAs);

View File

@ -24,8 +24,6 @@
namespace OCP\AppFramework\Http;
use OC\AppFramework\Core\API;
/**
* Response for a normal template
@ -34,20 +32,16 @@ class TemplateResponse extends Response {
protected $templateName;
protected $params;
protected $api;
protected $renderAs;
protected $appName;
/**
* @param API $api an API instance
* @param string $templateName the name of the template
* @param string $appName optional if you want to include a template from
* a different app
* @param string $appName the name of the app to load the template from
*/
public function __construct(API $api, $templateName, $appName=null) {
public function __construct($appName, $templateName) {
$this->templateName = $templateName;
$this->appName = $appName;
$this->api = $api;
$this->params = array();
$this->renderAs = 'user';
}
@ -108,13 +102,7 @@ class TemplateResponse extends Response {
*/
public function render(){
if($this->appName !== null){
$appName = $this->appName;
} else {
$appName = $this->api->getAppName();
}
$template = $this->api->getTemplate($this->templateName, $this->renderAs, $appName);
$template = new \OCP\Template($this->appName, $this->templateName, $this->renderAs);
foreach($this->params as $key => $value){
$template->assign($key, $value);

View File

@ -67,59 +67,6 @@ interface IApi {
*/
function add3rdPartyStyle($name);
/**
* Returns the translation object
* @return \OC_L10N the translation object
*
* FIXME: returns private object / should be retrieved from teh ServerContainer
*/
function getTrans();
/**
* Returns the URL for a route
* @param string $routeName the name of the route
* @param array $arguments an array with arguments which will be filled into the url
* @return string the url
*/
function linkToRoute($routeName, $arguments=array());
/**
* Returns an URL for an image or file
* @param string $file the name of the file
* @param string $appName the name of the app, defaults to the current one
*/
function linkTo($file, $appName=null);
/**
* Returns the link to an image, like link to but only with prepending img/
* @param string $file the name of the file
* @param string $appName the name of the app, defaults to the current one
*/
function imagePath($file, $appName = null);
/**
* Makes an URL absolute
* @param string $url the url
* @return string the absolute url
*
* FIXME: function should live in Request / Response
*/
function getAbsoluteURL($url);
/**
* links to a file
* @param string $file the name of the file
* @param string $appName the name of the app, defaults to the current one
* @deprecated replaced with linkToRoute()
* @return string the url
*/
function linkToAbsolute($file, $appName = null);
/**
* Checks if an app is enabled
@ -128,24 +75,4 @@ interface IApi {
*/
public function isAppEnabled($appName);
/**
* Writes a function into the error log
* @param string $msg the error message to be logged
* @param int $level the error level
*
* FIXME: add logger instance to ServerContainer
*/
function log($msg, $level = null);
/**
* Returns a template
* @param string $templateName the name of the template
* @param string $renderAs how it should be rendered
* @param string $appName the name of the app
* @return \OCP\Template a new template
*/
function getTemplate($templateName, $renderAs='user', $appName=null);
}

View File

@ -50,8 +50,26 @@ interface IAppContainer extends IContainer{
function getServer();
/**
* @param IMiddleWare $middleWare
* @param Middleware $middleWare
* @return boolean
*/
function registerMiddleWare(IMiddleWare $middleWare);
function registerMiddleWare(Middleware $middleWare);
/**
* @return boolean
*/
function isLoggedIn();
/**
* @return boolean
*/
function isAdminUser();
/**
* @param $message
* @param $level
* @return mixed
*/
function log($message, $level);
}

View File

@ -24,6 +24,7 @@
namespace OCP\AppFramework;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\Response;

View File

@ -38,9 +38,9 @@ class AppTest extends \PHPUnit_Framework_TestCase {
private $controllerMethod;
protected function setUp() {
$this->container = new \OC\AppFramework\DependencyInjection\DIContainer('test');
$this->container = new \OC\AppFramework\DependencyInjection\DIContainer('test', array());
$this->controller = $this->getMockBuilder(
'OC\AppFramework\Controller\Controller')
'OCP\AppFramework\Controller')
->disableOriginalConstructor()
->getMock();
$this->dispatcher = $this->getMockBuilder(
@ -56,6 +56,7 @@ class AppTest extends \PHPUnit_Framework_TestCase {
$this->container[$this->controllerName] = $this->controller;
$this->container['Dispatcher'] = $this->dispatcher;
$this->container['urlParams'] = array();
}
@ -69,7 +70,7 @@ class AppTest extends \PHPUnit_Framework_TestCase {
$this->expectOutputString('');
App::main($this->controllerName, $this->controllerMethod, array(),
App::main($this->controllerName, $this->controllerMethod,
$this->container);
}

View File

@ -25,13 +25,10 @@
namespace Test\AppFramework\Controller;
use OC\AppFramework\Http\Request;
use OC\AppFramework\Controller\Controller;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\TemplateResponse;
//require_once __DIR__ . "/../classloader.php";
class ChildController extends Controller {};
class ControllerTest extends \PHPUnit_Framework_TestCase {
@ -40,7 +37,7 @@ class ControllerTest extends \PHPUnit_Framework_TestCase {
* @var Controller
*/
private $controller;
private $api;
private $app;
protected function setUp(){
$request = new Request(
@ -55,13 +52,13 @@ class ControllerTest extends \PHPUnit_Framework_TestCase {
)
);
$this->api = $this->getMock('OC\AppFramework\Core\API',
$this->app = $this->getMock('OC\AppFramework\DependencyInjection\DIContainer',
array('getAppName'), array('test'));
$this->api->expects($this->any())
$this->app->expects($this->any())
->method('getAppName')
->will($this->returnValue('apptemplate_advanced'));
$this->controller = new ChildController($this->api, $request);
$this->controller = new ChildController($this->app, $request);
}
@ -114,26 +111,6 @@ class ControllerTest extends \PHPUnit_Framework_TestCase {
}
public function testRenderRenderAs(){
$ocTpl = $this->getMock('Template', array('fetchPage'));
$ocTpl->expects($this->once())
->method('fetchPage');
$api = $this->getMock('OC\AppFramework\Core\API',
array('getAppName', 'getTemplate'), array('app'));
$api->expects($this->any())
->method('getAppName')
->will($this->returnValue('app'));
$api->expects($this->once())
->method('getTemplate')
->with($this->equalTo('home'), $this->equalTo('admin'), $this->equalTo('app'))
->will($this->returnValue($ocTpl));
$this->controller = new ChildController($api, new Request());
$this->controller->render('home', array(), 'admin')->render();
}
public function testRenderHeaders(){
$headers = array('one', 'two');
$response = $this->controller->render('', array(), '', $headers);

View File

@ -29,23 +29,14 @@ namespace OC\AppFramework\DependencyInjection;
use \OC\AppFramework\Http\Request;
//require_once(__DIR__ . "/../classloader.php");
class DIContainerTest extends \PHPUnit_Framework_TestCase {
private $container;
private $api;
protected function setUp(){
$this->container = new DIContainer('name');
$this->api = $this->getMock('OC\AppFramework\Core\API', array('getTrans'), array('hi'));
}
private function exchangeAPI(){
$this->api->expects($this->any())
->method('getTrans')
->will($this->returnValue('yo'));
$this->container['API'] = $this->api;
$this->api = $this->getMock('OC\AppFramework\Core\API', array(), array('hi'));
}
public function testProvidesAPI(){
@ -87,12 +78,4 @@ class DIContainerTest extends \PHPUnit_Framework_TestCase {
}
public function testMiddlewareDispatcherDoesNotIncludeTwigWhenTplDirectoryNotSet(){
$this->container['Request'] = new Request();
$this->exchangeAPI();
$dispatcher = $this->container['MiddlewareDispatcher'];
$this->assertEquals(1, count($dispatcher->getMiddlewares()));
}
}

View File

@ -44,8 +44,8 @@ class DispatcherTest extends \PHPUnit_Framework_TestCase {
protected function setUp() {
$this->controllerMethod = 'test';
$api = $this->getMockBuilder(
'\OC\AppFramework\Core\API')
$app = $this->getMockBuilder(
'OC\AppFramework\DependencyInjection\DIContainer')
->disableOriginalConstructor()
->getMock();
$request = $this->getMockBuilder(
@ -62,8 +62,8 @@ class DispatcherTest extends \PHPUnit_Framework_TestCase {
->disableOriginalConstructor()
->getMock();
$this->controller = $this->getMock(
'\OC\AppFramework\Controller\Controller',
array($this->controllerMethod), array($api, $request));
'\OCP\AppFramework\Controller',
array($this->controllerMethod), array($app, $request));
$this->dispatcher = new Dispatcher(
$this->http, $this->middlewareDispatcher);

View File

@ -63,93 +63,33 @@ class TemplateResponseTest extends \PHPUnit_Framework_TestCase {
}
public function testRender(){
$ocTpl = $this->getMock('Template', array('fetchPage'));
$ocTpl->expects($this->once())
->method('fetchPage');
$api = $this->getMock('OC\AppFramework\Core\API',
array('getAppName', 'getTemplate'), array('app'));
$api->expects($this->any())
->method('getAppName')
->will($this->returnValue('app'));
$api->expects($this->once())
->method('getTemplate')
->with($this->equalTo('home'), $this->equalTo('user'), $this->equalTo('app'))
->will($this->returnValue($ocTpl));
$tpl = new TemplateResponse($api, 'home');
$tpl->render();
}
public function testRenderAssignsParams(){
$params = array('john' => 'doe');
$ocTpl = $this->getMock('Template', array('assign', 'fetchPage'));
$ocTpl->expects($this->once())
->method('assign')
->with($this->equalTo('john'), $this->equalTo('doe'));
$api = $this->getMock('OC\AppFramework\Core\API',
array('getAppName', 'getTemplate'), array('app'));
$api->expects($this->any())
->method('getAppName')
->will($this->returnValue('app'));
$api->expects($this->once())
->method('getTemplate')
->with($this->equalTo('home'), $this->equalTo('user'), $this->equalTo('app'))
->will($this->returnValue($ocTpl));
$tpl = new TemplateResponse($api, 'home');
$tpl->setParams($params);
$tpl->render();
}
public function testRenderDifferentApp(){
$ocTpl = $this->getMock('Template', array('fetchPage'));
$ocTpl->expects($this->once())
->method('fetchPage');
$api = $this->getMock('OC\AppFramework\Core\API',
array('getAppName', 'getTemplate'), array('app'));
$api->expects($this->any())
->method('getAppName')
->will($this->returnValue('app'));
$api->expects($this->once())
->method('getTemplate')
->with($this->equalTo('home'), $this->equalTo('user'), $this->equalTo('app2'))
->will($this->returnValue($ocTpl));
$tpl = new TemplateResponse($api, 'home', 'app2');
$tpl->render();
}
public function testRenderDifferentRenderAs(){
$ocTpl = $this->getMock('Template', array('fetchPage'));
$ocTpl->expects($this->once())
->method('fetchPage');
$api = $this->getMock('OC\AppFramework\Core\API',
array('getAppName', 'getTemplate'), array('app'));
$api->expects($this->any())
->method('getAppName')
->will($this->returnValue('app'));
$api->expects($this->once())
->method('getTemplate')
->with($this->equalTo('home'), $this->equalTo('admin'), $this->equalTo('app'))
->will($this->returnValue($ocTpl));
$tpl = new TemplateResponse($api, 'home');
$tpl->renderAs('admin');
$tpl->render();
}
// public function testRender(){
// $ocTpl = $this->getMock('Template', array('fetchPage'));
// $ocTpl->expects($this->once())
// ->method('fetchPage');
//
// $tpl = new TemplateResponse('core', 'error');
//
// $tpl->render();
// }
//
//
// public function testRenderAssignsParams(){
// $params = array('john' => 'doe');
//
// $tpl = new TemplateResponse('app', 'home');
// $tpl->setParams($params);
//
// $tpl->render();
// }
//
//
// public function testRenderDifferentApp(){
//
// $tpl = new TemplateResponse('app', 'home', 'app2');
//
// $tpl->render();
// }
public function testGetRenderAs(){

View File

@ -122,13 +122,13 @@ class MiddlewareDispatcherTest extends \PHPUnit_Framework_TestCase {
private function getAPIMock(){
return $this->getMock('OC\AppFramework\Core\API',
return $this->getMock('OC\AppFramework\DependencyInjection\DIContainer',
array('getAppName'), array('app'));
}
private function getControllerMock(){
return $this->getMock('OC\AppFramework\Controller\Controller', array('method'),
return $this->getMock('OCP\AppFramework\Controller', array('method'),
array($this->getAPIMock(), new Request()));
}

View File

@ -44,10 +44,10 @@ class MiddlewareTest extends \PHPUnit_Framework_TestCase {
protected function setUp(){
$this->middleware = new ChildMiddleware();
$this->api = $this->getMock('OC\AppFramework\Core\API',
$this->api = $this->getMock('OC\AppFramework\DependencyInjection\DIContainer',
array(), array('test'));
$this->controller = $this->getMock('OC\AppFramework\Controller\Controller',
$this->controller = $this->getMock('OCP\AppFramework\Controller',
array(), array($this->api, new Request()));
$this->exception = new \Exception();
$this->response = $this->getMock('OCP\AppFramework\Http\Response');

View File

@ -39,8 +39,8 @@ class SecurityMiddlewareTest extends \PHPUnit_Framework_TestCase {
private $request;
public function setUp() {
$api = $this->getMock('OC\AppFramework\Core\API', array(), array('test'));
$this->controller = $this->getMock('OC\AppFramework\Controller\Controller',
$api = $this->getMock('OC\AppFramework\DependencyInjection\DIContainer', array(), array('test'));
$this->controller = $this->getMock('OCP\AppFramework\Controller',
array(), array($api, new Request()));
$this->request = new Request();
@ -51,24 +51,19 @@ class SecurityMiddlewareTest extends \PHPUnit_Framework_TestCase {
private function getAPI(){
return $this->getMock('OC\AppFramework\Core\API',
return $this->getMock('OC\AppFramework\DependencyInjection\DIContainer',
array('isLoggedIn', 'passesCSRFCheck', 'isAdminUser',
'isSubAdminUser', 'activateNavigationEntry',
'getUserId'),
'isSubAdminUser', 'getUserId'),
array('app'));
}
private function checkNavEntry($method, $shouldBeActivated=false){
private function checkNavEntry($method){
$api = $this->getAPI();
if($shouldBeActivated){
$api->expects($this->once())
->method('activateNavigationEntry');
} else {
$api->expects($this->never())
->method('activateNavigationEntry');
}
$serverMock = $this->getMock('\OC\Server', array());
$api->expects($this->any())->method('getServer')
->will($this->returnValue($serverMock));
$sec = new SecurityMiddleware($api, $this->request);
$sec->beforeController('\OC\AppFramework\Middleware\Security\SecurityMiddlewareTest', $method);
@ -80,7 +75,7 @@ class SecurityMiddlewareTest extends \PHPUnit_Framework_TestCase {
* @NoCSRFRequired
*/
public function testSetNavigationEntry(){
$this->checkNavEntry('testSetNavigationEntry', true);
$this->checkNavEntry('testSetNavigationEntry');
}
@ -215,9 +210,33 @@ class SecurityMiddlewareTest extends \PHPUnit_Framework_TestCase {
/**
* @PublicPage
* @expectedException \OC\AppFramework\Middleware\Security\SecurityException
*/
public function testCsrfCheck(){
$this->securityCheck('testCsrfCheck', 'passesCSRFCheck');
$api = $this->getAPI();
$request = $this->getMock('OC\AppFramework\Http\Request', array('passesCSRFCheck'));
$request->expects($this->once())
->method('passesCSRFCheck')
->will($this->returnValue(false));
$sec = new SecurityMiddleware($api, $request);
$sec->beforeController('\OC\AppFramework\Middleware\Security\SecurityMiddlewareTest', 'testCsrfCheck');
}
/**
* @PublicPage
* @NoCSRFRequired
*/
public function testNoCsrfCheck(){
$api = $this->getAPI();
$request = $this->getMock('OC\AppFramework\Http\Request', array('passesCSRFCheck'));
$request->expects($this->never())
->method('passesCSRFCheck')
->will($this->returnValue(false));
$sec = new SecurityMiddleware($api, $request);
$sec->beforeController('\OC\AppFramework\Middleware\Security\SecurityMiddlewareTest', 'testNoCsrfCheck');
}
@ -225,7 +244,14 @@ class SecurityMiddlewareTest extends \PHPUnit_Framework_TestCase {
* @PublicPage
*/
public function testFailCsrfCheck(){
$this->securityCheck('testFailCsrfCheck', 'passesCSRFCheck', true);
$api = $this->getAPI();
$request = $this->getMock('OC\AppFramework\Http\Request', array('passesCSRFCheck'));
$request->expects($this->once())
->method('passesCSRFCheck')
->will($this->returnValue(true));
$sec = new SecurityMiddleware($api, $request);
$sec->beforeController('\OC\AppFramework\Middleware\Security\SecurityMiddlewareTest', 'testFailCsrfCheck');
}
@ -271,8 +297,12 @@ class SecurityMiddlewareTest extends \PHPUnit_Framework_TestCase {
public function testAfterExceptionReturnsRedirect(){
$api = $this->getMock('OC\AppFramework\Core\API', array(), array('test'));
$this->controller = $this->getMock('OC\AppFramework\Controller\Controller',
$api = $this->getMock('OC\AppFramework\DependencyInjection\DIContainer', array(), array('test'));
$serverMock = $this->getMock('\OC\Server', array('getNavigationManager'));
$api->expects($this->once())->method('getServer')
->will($this->returnValue($serverMock));
$this->controller = $this->getMock('OCP\AppFramework\Controller',
array(), array($api, new Request()));
$this->request = new Request(