From 26d950486ba5e48dd5ba44a4924760c88ed6c7ac Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Mon, 29 Sep 2014 21:22:34 +0200 Subject: add default locale and developer names and move the method to the pagecontroller, @cosenal --- appinfo/application.php | 18 +++---- appinfo/routes.php | 4 +- controller/appcontroller.php | 78 ---------------------------- controller/pagecontroller.php | 54 ++++++++++++++++++- tests/unit/controller/AppControllerTest.php | 70 ------------------------- tests/unit/controller/PageControllerTest.php | 43 ++++++++++++++- 6 files changed, 103 insertions(+), 164 deletions(-) delete mode 100644 controller/appcontroller.php delete mode 100644 tests/unit/controller/AppControllerTest.php diff --git a/appinfo/application.php b/appinfo/application.php index d80498a4a..f8c2bc7fa 100644 --- a/appinfo/application.php +++ b/appinfo/application.php @@ -29,7 +29,6 @@ use \OCA\News\Controller\UtilityApiController; use \OCA\News\Controller\FolderApiController; use \OCA\News\Controller\FeedApiController; use \OCA\News\Controller\ItemApiController; -use \OCA\News\Controller\AppController; use \OCA\News\Service\FolderService; use \OCA\News\Service\FeedService; @@ -79,6 +78,8 @@ class Application extends App { $c->query('AppName'), $c->query('Request'), $c->query('CoreConfig'), + $c->query('URLGenerator'), + $c->query('AppConfig'), $c->query('L10N'), $c->query('UserId') ); @@ -170,15 +171,6 @@ class Application extends App { ); }); - $container->registerService('AppController', function($c) { - return new AppController( - $c->query('AppName'), - $c->query('Request'), - $c->query('ServerContainer')->getURLGenerator(), - $c->query('AppConfig') - ); - }); - /** * Business Layer */ @@ -271,7 +263,7 @@ class Application extends App { $config = new AppConfig( $c->query('ServerContainer')->getNavigationManager(), $c->query('L10N'), - $c->query('ServerContainer')->getURLGenerator(), + $c->query('URLGenerator'), phpversion(), implode('.', Util::getVersion()), $extensions, @@ -290,6 +282,10 @@ class Application extends App { return $c->query('ServerContainer')->getL10N($c->query('AppName')); }); + $container->registerService('URLGenerator', function($c) { + return $c->query('ServerContainer')->getURLGenerator(); + }); + $container->registerService('UserId', function() { return User::getUser(); }); diff --git a/appinfo/routes.php b/appinfo/routes.php index 3e2ac189f..7703de1c7 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -21,9 +21,7 @@ $application->registerRoutes($this, ['routes' => [ ['name' => 'page#index', 'url' => '/', 'verb' => 'GET'], ['name' => 'page#settings', 'url' => '/settings', 'verb' => 'GET'], ['name' => 'page#update_settings', 'url' => '/settings', 'verb' => 'PUT'], - - // web app manifest - ['name' => 'app#manifest', 'url' => '/manifest.webapp', 'verb' => 'GET'], + ['name' => 'page#manifest', 'url' => '/manifest.webapp', 'verb' => 'GET'], // folders ['name' => 'folder#index', 'url' => '/folders', 'verb' => 'GET'], diff --git a/controller/appcontroller.php b/controller/appcontroller.php deleted file mode 100644 index 138668950..000000000 --- a/controller/appcontroller.php +++ /dev/null @@ -1,78 +0,0 @@ - - * @author Bernhard Posselt - * @copyright Alessandro Cosentino 2014 - * @copyright Bernhard Posselt 2014 - */ - -namespace OCA\News\Controller; - -use \OCP\IRequest; -use \OCP\IURLGenerator; -use \OCP\AppFramework\Controller; -use \OCP\AppFramework\Http; -use \OCP\AppFramework\Http\JSONResponse; - -use \OCA\News\Config\AppConfig; - -class AppController extends Controller { - - private $urlGenerator; - private $appConfig; - - public function __construct($appName, - IRequest $request, - IURLGenerator $urlGenerator, - AppConfig $appConfig){ - parent::__construct($appName, $request); - $this->urlGenerator = $urlGenerator; - $this->appConfig = $appConfig; - } - - /** - * @NoCSRFRequired - * @PublicPage - * - * Generates a web app manifest, according to specs in: - * https://developer.mozilla.org/en-US/Apps/Build/Manifest - */ - public function manifest() { - $config = $this->appConfig->getConfig(); - - // size of the icons: 128x128 is required by FxOS for all app manifests - $iconSizes = ['128', '512']; - $icons = []; - - foreach ($iconSizes as $size) { - $filename = 'app-' . $size . '.png'; - if (file_exists(__DIR__ . '/../img/' . $filename)) { - $icons[$size] = $this->urlGenerator->imagePath($config['id'], - $filename); - } - } - - $authors = []; - foreach ($config['authors'] as $author) { - $authors[] = $author['name']; - } - - return [ - "name" => $config['name'], - "description" => $config['description'], - "launch_path" => $this->urlGenerator->linkToRoute( - $config['id'] . '.page.index'), - "icons" => $icons, - "developer" => [ - "name" => implode(', ', $authors), - "url" => $config['homepage'] - ] - ]; - } - -} \ No newline at end of file diff --git a/controller/pagecontroller.php b/controller/pagecontroller.php index 27377eab4..e8b651608 100644 --- a/controller/pagecontroller.php +++ b/controller/pagecontroller.php @@ -13,26 +13,34 @@ namespace OCA\News\Controller; -use OCP\AppFramework\Http\TemplateResponse; +use \OCP\AppFramework\Http\TemplateResponse; use \OCP\IRequest; use \OCP\IConfig; use \OCP\IL10N; +use \OCP\IURLGenerator; use \OCP\AppFramework\Controller; +use \OCA\News\Config\AppConfig; class PageController extends Controller { private $settings; private $l10n; private $userId; + private $appConfig; + private $urlGenerator; public function __construct($appName, IRequest $request, IConfig $settings, + IURLGenerator $urlGenerator, + AppConfig $appConfig, IL10N $l10n, $userId){ parent::__construct($appName, $request); $this->settings = $settings; + $this->urlGenerator = $urlGenerator; + $this->appConfig = $appConfig; $this->l10n = $l10n; $this->userId = $userId; } @@ -82,4 +90,48 @@ class PageController extends Controller { } + /** + * @NoCSRFRequired + * @PublicPage + * + * Generates a web app manifest, according to specs in: + * https://developer.mozilla.org/en-US/Apps/Build/Manifest + */ + public function manifest() { + $config = $this->appConfig->getConfig(); + + // size of the icons: 128x128 is required by FxOS for all app manifests + $iconSizes = ['128', '512']; + $icons = []; + + $locale = str_replace('_', '-', $this->l10n->getLanguageCode()); + + foreach ($iconSizes as $size) { + $filename = 'app-' . $size . '.png'; + if (file_exists(__DIR__ . '/../img/' . $filename)) { + $icons[$size] = $this->urlGenerator->imagePath($config['id'], + $filename); + } + } + + $authors = []; + foreach ($config['authors'] as $author) { + $authors[] = $author['name']; + } + + return [ + "name" => $config['name'], + "type" => 'web', + "default_locale" => $locale, + "description" => $config['description'], + "launch_path" => $this->urlGenerator->linkToRoute( + $config['id'] . '.page.index'), + "icons" => $icons, + "developer" => [ + "name" => implode(', ', $authors), + "url" => $config['homepage'] + ] + ]; + } + } \ No newline at end of file diff --git a/tests/unit/controller/AppControllerTest.php b/tests/unit/controller/AppControllerTest.php deleted file mode 100644 index 7c1f5f12a..000000000 --- a/tests/unit/controller/AppControllerTest.php +++ /dev/null @@ -1,70 +0,0 @@ - - * @author Bernhard Posselt - * @copyright Alessandro Cosentino 2014 - * @copyright Bernhard Posselt 2014 - */ - -namespace OCA\News\Controller; - - -class AppControllerTest extends \PHPUnit_Framework_TestCase { - - private $appName; - private $request; - private $urlGenerator; - private $appConfig; - private $controller; - private $configData; - - /** - * Gets run before each test - */ - public function setUp(){ - $this->appName = 'news'; - $this->request = $this->getMockBuilder( - '\OCP\IRequest') - ->disableOriginalConstructor() - ->getMock(); - $this->urlGenerator = $this->getMockBuilder( - '\OCP\IURLGenerator') - ->disableOriginalConstructor() - ->getMock(); - $this->appConfig = $this->getMockBuilder( - '\OCA\News\Config\AppConfig') - ->disableOriginalConstructor() - ->getMock(); - - $this->configData = [ - 'name' => 'AppTest', - 'id' => 'apptest', - 'authors' => [ - ['name' => 'john'], - ['name' => 'test'] - ], - 'description' => 'This is a test app', - 'homepage' => 'https://github.com/owncloud/test' - ]; - - $this->controller = new AppController($this->appName, $this->request, - $this->urlGenerator, $this->appConfig); - } - - public function testManifest(){ - $this->appConfig->expects($this->once()) - ->method('getConfig') - ->will($this->returnValue($this->configData)); - $result = $this->controller->manifest(); - $this->assertEquals($this->configData['name'], $result['name']); - $this->assertEquals($this->configData['description'], $result['description']); - $this->assertEquals($this->configData['homepage'], $result['developer']['url']); - $this->assertEquals('john, test', $result['developer']['name']); - } - -} \ No newline at end of file diff --git a/tests/unit/controller/PageControllerTest.php b/tests/unit/controller/PageControllerTest.php index d27049f95..ec2fcae14 100644 --- a/tests/unit/controller/PageControllerTest.php +++ b/tests/unit/controller/PageControllerTest.php @@ -22,6 +22,9 @@ class PageControllerTest extends \PHPUnit_Framework_TestCase { private $controller; private $user; private $l10n; + private $urlGenerator; + private $appConfig; + private $configData; /** * Gets run before each test @@ -29,6 +32,16 @@ class PageControllerTest extends \PHPUnit_Framework_TestCase { public function setUp(){ $this->appName = 'news'; $this->user = 'becka'; + $this->configData = [ + 'name' => 'AppTest', + 'id' => 'apptest', + 'authors' => [ + ['name' => 'john'], + ['name' => 'test'] + ], + 'description' => 'This is a test app', + 'homepage' => 'https://github.com/owncloud/test' + ]; $this->l10n = $this->request = $this->getMockBuilder( '\OCP\IL10n') ->disableOriginalConstructor() @@ -41,8 +54,17 @@ class PageControllerTest extends \PHPUnit_Framework_TestCase { '\OCP\IRequest') ->disableOriginalConstructor() ->getMock(); + $this->urlGenerator = $this->getMockBuilder( + '\OCP\IURLGenerator') + ->disableOriginalConstructor() + ->getMock(); + $this->appConfig = $this->getMockBuilder( + '\OCA\News\Config\AppConfig') + ->disableOriginalConstructor() + ->getMock(); $this->controller = new PageController($this->appName, $this->request, - $this->settings, $this->l10n, $this->user); + $this->settings, $this->urlGenerator, $this->appConfig, $this->l10n, + $this->user); } @@ -125,4 +147,23 @@ class PageControllerTest extends \PHPUnit_Framework_TestCase { } + + public function testManifest(){ + $this->appConfig->expects($this->once()) + ->method('getConfig') + ->will($this->returnValue($this->configData)); + $this->l10n->expects($this->once()) + ->method('getLanguageCode') + ->will($this->returnValue('de_DE')); + + $result = $this->controller->manifest(); + $this->assertEquals($this->configData['name'], $result['name']); + $this->assertEquals('web', $result['type']); + $this->assertEquals($this->configData['description'], $result['description']); + $this->assertEquals('de-DE', $result['default_locale']); + $this->assertEquals($this->configData['homepage'], $result['developer']['url']); + $this->assertEquals('john, test', $result['developer']['name']); + } + + } \ No newline at end of file -- cgit v1.2.3