This commit is contained in:
Olivier Paroz 2015-08-17 04:22:57 +02:00
Родитель 82443c0714
Коммит 083aa7ecfa
18 изменённых файлов: 398 добавлений и 80 удалений

Просмотреть файл

@ -14,6 +14,9 @@ filter:
- 'tests/*'
- 'build/*'
- 'documentation/*'
- 'controller/configapicontroller.php'
- 'controller/filesapicontroller.php'
- 'controller/previewapicontroller.php'
tools:
sensiolabs_security_checker: true

Просмотреть файл

@ -53,6 +53,9 @@ before_script:
- curl http://localhost:4444/wd/hub/status
#- phantomjs --version
# Install the Imagick PHP extension
- sh -c "if [ '$TRAVIS_PHP_VERSION' != 'hhvm' ]; then printf "\n" | pecl install imagick; fi;"
script:
# Test lint
- cd apps/$APP_NAME
@ -66,8 +69,8 @@ script:
# PHP 5.6 ONLY: Unit, integration and api tests with code coverage
- sh -c "if [ '$TRAVIS_PHP_VERSION' = '5.6' ]; then php vendor/bin/codecept run unit,integration,api --coverage --coverage-xml --coverage-html; fi;"
# Send coverage report to Scrutinizer
- sh -c "if [ '$TRAVIS_PHP_VERSION' != 'hhvm' ] && [ '$TRAVIS_PHP_VERSION' != '7' ]; then wget https://scrutinizer-ci.com/ocular.phar; fi"
- sh -c "if [ '$TRAVIS_PHP_VERSION' != 'hhvm' ] && [ '$TRAVIS_PHP_VERSION' != '7' ]; then php ocular.phar code-coverage:upload --format=php-clover tests/_output/coverage.xml; fi"
- sh -c "if [ '$TRAVIS_PHP_VERSION' = '5.6' ]; then wget https://scrutinizer-ci.com/ocular.phar; fi"
- sh -c "if [ '$TRAVIS_PHP_VERSION' = '5.6' ]; then php ocular.phar code-coverage:upload --format=php-clover tests/_output/coverage.xml; fi"
# Generate API documentation
- sh -c "if [ '$TRAVIS_PHP_VERSION' = '5.6' ]; then php vendor/bin/phpdoc run; fi"

Просмотреть файл

@ -27,11 +27,13 @@ use OCP\AppFramework\IAppContainer;
use OCA\Gallery\Controller\PageController;
use OCA\Gallery\Controller\ConfigController;
use OCA\Gallery\Controller\ConfigPublicController;
use OCA\Gallery\Controller\ConfigApiController;
use OCA\Gallery\Controller\FilesController;
use OCA\Gallery\Controller\FilesPublicController;
use OCA\Gallery\Controller\FilesApiController;
use OCA\Gallery\Controller\PreviewController;
use OCA\Gallery\Controller\PublicConfigController;
use OCA\Gallery\Controller\PublicPreviewController;
use OCA\Gallery\Environment\Environment;
@ -93,8 +95,19 @@ class Application extends App {
}
);
$container->registerService(
'PublicConfigController', function (IContainer $c) {
return new PublicConfigController(
'ConfigPublicController', function (IContainer $c) {
return new ConfigPublicController(
$c->query('AppName'),
$c->query('Request'),
$c->query('ConfigService'),
$c->query('PreviewService'),
$c->query('Logger')
);
}
);
$container->registerService(
'ConfigApiController', function (IContainer $c) {
return new ConfigApiController(
$c->query('AppName'),
$c->query('Request'),
$c->query('ConfigService'),

Просмотреть файл

@ -67,7 +67,7 @@ return [
*/
// Gallery config, as well as supported media types
[
'name' => 'config#get_config',
'name' => 'config#get',
'url' => '/config',
'verb' => 'GET'
],
@ -93,7 +93,7 @@ return [
* Public services
*/
[
'name' => 'public_config#get_config',
'name' => 'config_public#get',
'url' => '/config.public',
'verb' => 'GET'
],
@ -116,11 +116,16 @@ return [
* API
*/
[
'name' => 'files_api#preflighted_cors', // Valid for all API end points
'name' => 'config_api#preflighted_cors', // Valid for all API end points
'url' => '/api/{path}',
'verb' => 'OPTIONS',
'requirements' => ['path' => '.+']
],
[
'name' => 'config_api#get',
'url' => '/api/config',
'verb' => 'GET'
],
[
'name' => 'files_api#get_list',
'url' => '/api/files/list',

Просмотреть файл

@ -30,6 +30,7 @@ coverage:
- preview/*
- service/*
exclude:
- appinfo/info.xml
- build/*
- css/*
- documentation/*

81
controller/config.php Normal file
Просмотреть файл

@ -0,0 +1,81 @@
<?php
/**
* ownCloud - gallery
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Olivier Paroz <owncloud@interfasys.ch>
*
* @copyright Olivier Paroz 2015
*/
namespace OCA\Gallery\Controller;
use OCP\ILogger;
use OCP\AppFramework\Http;
use OCA\Gallery\Service\ConfigService;
use OCA\Gallery\Service\PreviewService;
/**
* Trait Config
*
* @package OCA\Gallery\Controller
*/
trait Config {
/**
* @var ConfigService
*/
private $configService;
/**
* @var PreviewService
*/
private $previewService;
/**
* @var ILogger
*/
private $logger;
/**
* @NoAdminRequired
*
* Returns an app configuration array
*
* @param bool $extraMediaTypes
*
* @return array <string,null|array>
*/
private function getConfig($extraMediaTypes = false) {
$features = $this->configService->getFeaturesList();
//$this->logger->debug("Features: {features}", ['features' => $features]);
$nativeSvgSupport = $this->isNativeSvgActivated($features);
$mediaTypes =
$this->previewService->getSupportedMediaTypes($extraMediaTypes, $nativeSvgSupport);
return ['features' => $features, 'mediatypes' => $mediaTypes];
}
/**
* Determines if the native SVG feature has been activated
*
* @param array $features
*
* @return bool
*/
private function isNativeSvgActivated($features) {
$nativeSvgSupport = false;
if (!empty($features)
&& array_key_exists('native_svg', $features)
&& $features['native_svg'] === 'yes'
) {
$nativeSvgSupport = true;
}
return $nativeSvgSupport;
}
}

Просмотреть файл

@ -0,0 +1,76 @@
<?php
/**
* ownCloud - gallery
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Olivier Paroz <owncloud@interfasys.ch>
*
* @copyright Olivier Paroz 2015
*/
namespace OCA\Gallery\Controller;
use OCP\IRequest;
use OCP\ILogger;
use OCP\AppFramework\ApiController;
use OCP\AppFramework\Http;
use OCA\Gallery\Service\ConfigService;
use OCA\Gallery\Service\PreviewService;
/**
* Class ConfigApiController
*
* @package OCA\Gallery\Controller
*/
class ConfigApiController extends ApiController {
use Config;
use JsonHttpError;
/**
* Constructor
*
* @param string $appName
* @param IRequest $request
* @param ConfigService $configService
* @param PreviewService $previewService
* @param ILogger $logger
*/
public function __construct(
$appName,
IRequest $request,
ConfigService $configService,
PreviewService $previewService,
ILogger $logger
) {
parent::__construct($appName, $request);
$this->configService = $configService;
$this->previewService = $previewService;
$this->logger = $logger;
}
/**
* @NoAdminRequired
* @NoCSRFRequired
* @CORS
*
* Returns an app configuration array
*
* @param bool $extramediatypes
*
* @return array <string,null|array>
*/
public function get($extramediatypes = false) {
try {
return $this->getConfig($extramediatypes);
} catch (\Exception $exception) {
return $this->error($exception);
}
}
}

Просмотреть файл

@ -28,21 +28,9 @@ use OCA\Gallery\Service\PreviewService;
*/
class ConfigController extends Controller {
use Config;
use JsonHttpError;
/**
* @var ConfigService
*/
private $configService;
/**
* @var PreviewService
*/
private $previewService;
/**
* @var ILogger
*/
private $logger;
/**
* Constructor
*
@ -71,38 +59,16 @@ class ConfigController extends Controller {
*
* Returns an app configuration array
*
* @param bool $slideshow
* @param bool $extramediatypes
*
* @return array<string,null|array>
* @return array <string,null|array>
*/
public function getConfig($slideshow = false) {
$features = $this->configService->getFeaturesList();
//$this->logger->debug("Features: {features}", ['features' => $features]);
$nativeSvgSupport = $this->isNativeSvgActivated($features);
$mediaTypes = $this->previewService->getSupportedMediaTypes($slideshow, $nativeSvgSupport);
return ['features' => $features, 'mediatypes' => $mediaTypes];
}
/**
* Determines if the native SVG feature has been activated
*
* @param array $features
*
* @return bool
*/
private function isNativeSvgActivated($features) {
$nativeSvgSupport = false;
if (!empty($features)
&& array_key_exists('native_svg', $features)
&& $features['native_svg'] === 'yes'
) {
$nativeSvgSupport = true;
public function get($extramediatypes = false) {
try {
return $this->getConfig($extramediatypes);
} catch (\Exception $exception) {
return $this->error($exception);
}
return $nativeSvgSupport;
}
}

Просмотреть файл

@ -13,14 +13,14 @@
namespace OCA\Gallery\Controller;
/**
* Class PublicConfigController
* Class ConfigPublicController
*
* Note: Type casting only works if the "@param" parameters are also included in this class as
* their not yet inherited
*
* @package OCA\Gallery\Controller
*/
class PublicConfigController extends ConfigController {
class ConfigPublicController extends ConfigController {
/**
* @PublicPage
@ -29,10 +29,10 @@ class PublicConfigController extends ConfigController {
*
* @inheritDoc
*
* @param bool $slideshow
* @param bool $extramediatypes
*/
public function getConfig($slideshow = false) {
return parent::getConfig($slideshow);
public function get($extramediatypes = false) {
return parent::get($extramediatypes);
}
}

Просмотреть файл

@ -68,7 +68,7 @@ trait Files {
*
* @return array <string,array<string,string|int>>|Http\JSONResponse
*/
public function getFiles($location, $features, $etag, $mediatypes) {
private function getFiles($location, $features, $etag, $mediatypes) {
$files = [];
/** @var Folder $folderNode */
list($folderPathFromRoot, $folderNode, $locationHasChanged) =

Просмотреть файл

@ -35,7 +35,7 @@ class PublicPreviewController extends PreviewController {
*
* @param string $ids the ID of the files of which we need thumbnail previews of
* @param bool $square
* @param bool $scale
* @param float $scale
*/
public function getThumbnails($ids, $square, $scale) {
return parent::getThumbnails($ids, $square, $scale);

Просмотреть файл

@ -84,15 +84,15 @@ class PreviewService extends Service {
*
* @todo Native SVG could be disabled via admin settings
*
* @param bool $slideshow
* @param bool $extraMediaTypes
* @param bool $nativeSvgSupport
*
* @return \string[] all supported media types
*/
public function getSupportedMediaTypes($slideshow, $nativeSvgSupport) {
public function getSupportedMediaTypes($extraMediaTypes, $nativeSvgSupport) {
$supportedMimes = [];
$wantedMimes = $this->baseMimeTypes;
if ($slideshow) {
if ($extraMediaTypes) {
$wantedMimes = array_merge($wantedMimes, $this->slideshowMimeTypes);
}
foreach ($wantedMimes as $wantedMime) {

Просмотреть файл

@ -3,7 +3,27 @@ namespace Helper;
// here you can define custom actions
// all public methods declared in helper class will be available in $I
class Api extends \Codeception\Module
{
class Api extends \Codeception\Module {
/**
* @return mixed
* @throws \Codeception\Exception\ModuleException
*/
public function getUserCredentials() {
$user = $this->getModule('\Helper\DataSetup')->userId;
$password = $this->getModule('\Helper\DataSetup')->userPassword;
return [$user, $password];
}
/**
* @return mixed
* @throws \Codeception\Exception\ModuleException
*/
public function getMediaTypes() {
$mediaTypes = $this->getModule('\Helper\DataSetup')->mediaTypes;
$extraMediaTypes = $this->getModule('\Helper\DataSetup')->extraMediaTypes;
return [$mediaTypes, $extraMediaTypes];
}
}

Просмотреть файл

@ -31,6 +31,10 @@ use OCP\AppFramework\IAppContainer;
*/
class DataSetup extends \Codeception\Module {
/** @var array<string> */
public $mediaTypes;
/** @var array<string> */
public $extraMediaTypes;
/** @var string */
public $userId = 'tester';
/** @var string */
@ -178,10 +182,23 @@ class DataSetup extends \Codeception\Module {
'OC\\Preview\\JPEG',
'OC\\Preview\\PNG',
'OC\\Preview\\GIF',
'OC\\Preview\\Postscript'
'OC\\Preview\\Postscript',
'OC\\Preview\\Font'
];
$this->server->getConfig()
->setSystemValue('enabledPreviewProviders', $providers);
$this->mediaTypes = [
'image/jpeg',
'image/png',
'image/gif',
'application/postscript'
];
$this->extraMediaTypes = [
'application/font-sfnt',
'application/x-font',
];
}
/**

Просмотреть файл

@ -1,4 +1,4 @@
<?php //[STAMP] ab39405f934d9fde1e32aec71c833817
<?php //[STAMP] 0f88c0cce4c0d1b1984d8ae16fa4644b
namespace _generated;
// This class was automatically generated by build task
@ -17,6 +17,30 @@ trait ApiTesterActions
abstract protected function getScenario();
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* @return mixed
* @throws \Codeception\Exception\ModuleException
* @see \Helper\Api::getUserCredentials()
*/
public function getUserCredentials() {
return $this->getScenario()->runStep(new \Codeception\Step\Action('getUserCredentials', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* @return mixed
* @throws \Codeception\Exception\ModuleException
* @see \Helper\Api::getMediaTypes()
*/
public function getMediaTypes() {
return $this->getScenario()->runStep(new \Codeception\Step\Action('getMediaTypes', func_get_args()));
}
/**
* [!] Method is generated. Documentation taken from corresponding module.
*

111
tests/api/GetConfigCest.php Normal file
Просмотреть файл

@ -0,0 +1,111 @@
<?php
/**
* ownCloud - gallery
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Olivier Paroz <owncloud@interfasys.ch>
*
* @copyright Olivier Paroz 2015
*/
use Page\Gallery as GalleryApp;
/**
* Class GetConfigCest
*
* @todo Inject config items and compare the result
*/
class GetConfigCest {
private $userId;
private $password;
private $configApi;
public function _before(ApiTester $I) {
$this->configApi = GalleryApp::$URL . 'api/config';
list ($this->userId, $this->password) = $I->getUserCredentials();
}
public function _after(ApiTester $I) {
}
public function unauthorizedAccess(ApiTester $I) {
$I->am('an app');
$I->wantTo('connect to the Config API without credentials');
$I->sendGET($this->configApi);
$I->seeResponseCodeIs(401);
$I->seeResponseIsJson();
}
/**
* Retrieves the configuration
*
* @todo figure out why seeResponseJsonMatchesXpath returns
* [DOMException] Invalid Character Error
*
* @param ApiTester $I
*/
public function getConfig(ApiTester $I) {
$I->am('an app');
$I->wantTo('get the current Gallery configuration');
$I->amHttpAuthenticated($this->userId, $this->password);
$params = ['extramediatypes' => false];
$I->sendGET($this->configApi, $params);
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();
$I->seeResponseContainsJson(['features' => []]);
/**
* TODO Replace with JSONPath once the library is fixed
*/
$I->seeResponseContainsJson(
[
"mediatypes" => [
"image/png" => "/core/img/filetypes/image.png",
"image/jpeg" => "/core/img/filetypes/image.png",
"image/gif" => "/core/img/filetypes/image.png",
"application/postscript" => "/core/img/filetypes/image-vector.png"
]
]
);
}
/**
* @depends getConfig
*
* @param ApiTester $I
* @param $scenario
*/
public function getConfigWithExtraMediaTypes(ApiTester $I, \Codeception\Scenario $scenario) {
$I->am('an app');
$I->wantTo('get the current Gallery configuration which should include extra media types');
$I->amHttpAuthenticated($this->userId, $this->password);
$params = ['extramediatypes' => true];
$I->sendGET($this->configApi, $params);
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();
/**
* TODO Replace with JSONPath once the library is fixed
*/
$I->seeResponseContainsJson(
[
"mediatypes" => [
"image/png" => "/core/img/filetypes/image.png",
"image/jpeg" => "/core/img/filetypes/image.png",
"image/gif" => "/core/img/filetypes/image.png",
"application/postscript" => "/core/img/filetypes/image-vector.png",
"application/font-sfnt" => "/core/img/filetypes/font.png",
"application/x-font" => "/core/img/filetypes/font.png"
]
]
);
}
}

Просмотреть файл

@ -11,7 +11,6 @@
*/
use Page\Gallery as GalleryApp;
use Helper\DataSetup;
/**
* Class GetFilesCest
@ -20,27 +19,26 @@ use Helper\DataSetup;
*/
class GetFilesCest {
private $setupData;
private $userId;
private $password;
private $filesApi;
private $params = [
'mediatypes' => 'image/png;image/jpeg;image/gif;application/postscript'
];
private $params;
/**
* Injects objects we need
* Sets up the environment for this series of tests
*
* @param DataSetup $setupData
* We use custom methods defined in _support/Helper/Api
* If these are re-usable across suites, they may move to _support/Step
*
* @param ApiTester $I
*/
protected function _inject(DataSetup $setupData) {
$this->setupData = $setupData;
}
public function _before(ApiTester $I) {
$this->filesApi = GalleryApp::$URL . 'api/files/list';
$this->userId = $this->setupData->userId;
$this->password = $this->setupData->userPassword;
list ($this->userId, $this->password) = $I->getUserCredentials();
list($mediaTypes) = $I->getMediaTypes();
$this->params = [
'mediatypes' => implode(';', $mediaTypes)
];
}
public function _after(ApiTester $I) {
@ -77,7 +75,7 @@ class GetFilesCest {
}
/**
* @after getStandardList
* @depends getStandardList
*
* @param ApiTester $I
*/

Просмотреть файл

@ -148,7 +148,7 @@ class ConfigControllerTest extends \Test\TestCase {
$this->mockFeaturesList($features);
$this->mockSupportedMediaTypes($slideshow, $nativeSvgSupport, $mimeTypes);
$response = $this->controller->getConfig($slideshow);
$response = $this->controller->get($slideshow);
$this->assertEquals(['features' => $features, 'mediatypes' => $mimeTypes], $response);
}
@ -162,7 +162,7 @@ class ConfigControllerTest extends \Test\TestCase {
$nativeSvgSupport = false;
$this->mockSupportedMediaTypes($slideshow, $nativeSvgSupport, $this->baseMimeTypes);
$response = $this->controller->getConfig($slideshow);
$response = $this->controller->get($slideshow);
$this->assertEquals(
['features' => $features, 'mediatypes' => $this->baseMimeTypes], $response