diff --git a/.gitignore b/.gitignore index 4f4067781..37b787e86 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ -tests/clover.xml *.tar.gz build/ diff --git a/.travis.yml b/.travis.yml index 5301b81d5..9bb70437f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,8 +25,7 @@ script: - find . -name \*.php -exec php -l "{}" \; # Run phpunit tests - - phpunit --configuration phpunit.xml - + - ../../lib/composer/bin/phpunit --configuration ./phpunit.xml after_success: - bash <(curl -s https://codecov.io/bash) diff --git a/lib/appinfo/application.php b/lib/appinfo/application.php index 827ca8037..0e6b7d93b 100644 --- a/lib/appinfo/application.php +++ b/lib/appinfo/application.php @@ -29,73 +29,6 @@ public function __construct (array $urlParams = array()) { $this->registerServices(); } - public function registerScripts() { - $container = $this->getContainer(); - - if ($this->isUserAllowedToUseCollabora()) { - $menuOption = $container->getServer()->getConfig()->getAppValue('richdocuments', 'menu_option'); - if ($menuOption !== 'false') { - $navigationEntry = function () use ($container) { - return [ - 'id' => 'richdocuments_index', - 'order' => 2, - 'href' => $container->query('ServerContainer')->getURLGenerator()->linkToRoute('richdocuments.document.index'), - 'icon' => $container->query('ServerContainer')->getURLGenerator()->imagePath('richdocuments', 'app.svg'), - 'name' => $container->query('L10N')->t('Office') - ]; - }; - $container->getServer()->getNavigationManager()->add($navigationEntry); - } - - //Script for registering file actions - $container->getServer()->getEventDispatcher()->addListener( - 'OCA\Files::loadAdditionalScripts', - function() { - \OCP\Util::addScript('richdocuments', 'viewer/viewer'); - \OCP\Util::addStyle('richdocuments', 'viewer/odfviewer'); - } - ); - - if (class_exists('\OC\Files\Type\TemplateManager')) { - $manager = \OC_Helper::getFileTemplateManager(); - - $manager->registerTemplate('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'apps/richdocuments/assets/docxtemplate.docx'); - $manager->registerTemplate('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'apps/richdocuments/assets/xlsxtemplate.xlsx'); - $manager->registerTemplate('application/vnd.openxmlformats-officedocument.presentationml.presentation', 'apps/richdocuments/assets/pptxtemplate.pptx'); - } - } - - if ($this->publicLinksAllowedToUseCollabora()) { - \OCP\Util::connectHook(Share::class, "share_link_access", \OCA\Richdocuments\HookHandler::class, "addViewerScripts"); - } - } - - private function publicLinksAllowedToUseCollabora() { - // FIXME: some more rules? additional collabora flag? - return ($this->getContainer()->getServer()->getConfig()->getAppValue('core', 'shareapi_allow_links', 'yes') == 'yes'); - } - - private function isUserAllowedToUseCollabora() { - // no user -> no - $userSession = $this->getContainer()->getServer()->getUserSession(); - if ($userSession === null || !$userSession->isLoggedIn()) { - return false; - } - // no group set -> all users are allowed - $groupName = $this->getContainer()->getServer()->getConfig()->getSystemValue('collabora_group', null); - if ($groupName === null) { - return true; - } - // group unknown -> error and allow nobody - $group = $this->getContainer()->getServer()->getGroupManager()->get($groupName); - if ($group === null) { - $this->getContainer()->getServer()->getLogger()->error("Group is unknown $groupName", ['app' => 'collabora']); - return false; - } - - return $group->inGroup($userSession->getUser()); - } - private function registerServices(){ $container = $this->getContainer(); @@ -162,4 +95,71 @@ private function registerServices(){ return $c->query('ServerContainer')->getMemCacheFactory(); }); } + + public function registerScripts() { + $container = $this->getContainer(); + + if ($this->isUserAllowedToUseCollabora()) { + $menuOption = $container->getServer()->getConfig()->getAppValue('richdocuments', 'menu_option'); + if ($menuOption !== 'false') { + $navigationEntry = function () use ($container) { + return [ + 'id' => 'richdocuments_index', + 'order' => 2, + 'href' => $container->query('ServerContainer')->getURLGenerator()->linkToRoute('richdocuments.document.index'), + 'icon' => $container->query('ServerContainer')->getURLGenerator()->imagePath('richdocuments', 'app.svg'), + 'name' => $container->query('L10N')->t('Office') + ]; + }; + $container->getServer()->getNavigationManager()->add($navigationEntry); + } + + //Script for registering file actions + $container->getServer()->getEventDispatcher()->addListener( + 'OCA\Files::loadAdditionalScripts', + function() { + \OCP\Util::addScript('richdocuments', 'viewer/viewer'); + \OCP\Util::addStyle('richdocuments', 'viewer/odfviewer'); + } + ); + + if (class_exists('\OC\Files\Type\TemplateManager')) { + $manager = \OC_Helper::getFileTemplateManager(); + + $manager->registerTemplate('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'apps/richdocuments/assets/docxtemplate.docx'); + $manager->registerTemplate('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'apps/richdocuments/assets/xlsxtemplate.xlsx'); + $manager->registerTemplate('application/vnd.openxmlformats-officedocument.presentationml.presentation', 'apps/richdocuments/assets/pptxtemplate.pptx'); + } + } + + if ($this->publicLinksAllowedToUseCollabora()) { + \OCP\Util::connectHook(Share::class, "share_link_access", \OCA\Richdocuments\HookHandler::class, "addViewerScripts"); + } + } + + private function publicLinksAllowedToUseCollabora() { + // FIXME: some more rules? additional collabora flag? + return ($this->getContainer()->getServer()->getConfig()->getAppValue('core', 'shareapi_allow_links', 'yes') == 'yes'); + } + + private function isUserAllowedToUseCollabora() { + // no user -> no + $userSession = $this->getContainer()->getServer()->getUserSession(); + if ($userSession === null || !$userSession->isLoggedIn()) { + return false; + } + // no group set -> all users are allowed + $groupName = $this->getContainer()->getServer()->getConfig()->getSystemValue('collabora_group', null); + if ($groupName === null) { + return true; + } + // group unknown -> error and allow nobody + $group = $this->getContainer()->getServer()->getGroupManager()->get($groupName); + if ($group === null) { + $this->getContainer()->getServer()->getLogger()->error("Group is unknown $groupName", ['app' => 'collabora']); + return false; + } + + return $group->inGroup($userSession->getUser()); + } } diff --git a/controller/documentcontroller.php b/lib/controller/documentcontroller.php similarity index 100% rename from controller/documentcontroller.php rename to lib/controller/documentcontroller.php diff --git a/controller/settingscontroller.php b/lib/controller/settingscontroller.php similarity index 100% rename from controller/settingscontroller.php rename to lib/controller/settingscontroller.php diff --git a/phpunit.xml b/phpunit.xml index 3bf609581..341361550 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,12 +1,13 @@ - - ./tests + + ./tests/unit @@ -15,6 +16,8 @@ ./l10n ./tests + ./templates + ./appinfo diff --git a/tests/bootstrap.php b/tests/bootstrap.php deleted file mode 100644 index 6ef476d17..000000000 --- a/tests/bootstrap.php +++ /dev/null @@ -1,13 +0,0 @@ -request = $this->getMockBuilder('\OCP\IRequest') - ->disableOriginalConstructor() - ->getMock() - ; - $this->settings = $this->getMockBuilder('\OCP\IConfig') - ->disableOriginalConstructor() - ->getMock() - ; - $this->appConfig = $this->getMockBuilder('\OCA\Richdocuments\AppConfig') - ->disableOriginalConstructor() - ->getMock() - ; - $this->l10n = $this->getMockBuilder('\OCP\IL10N') - ->disableOriginalConstructor() - ->getMock() - ; - $this->cache = $this->getMockBuilder('\OCP\ICacheFactory') - ->disableOriginalConstructor() - ->getMock() - ; - $this->logger = $this->getMockBuilder('\OCP\ILogger') - ->disableOriginalConstructor() - ->getMock() - ; - $this->controller = new DocumentController( - $this->appName, - $this->request, - $this->settings, - $this->appConfig, - $this->l10n, - $this->uid, - $this->cache, - $this->logger - ); - - $userManager = \OC::$server->getUserManager(); - $userSession = \OC::$server->getUserSession(); - if (!$userManager->userExists($this->uid)){ - $userManager->createUser($this->uid, $this->password); - \OC::$server->getUserFolder($this->uid); - } - $userSession->login($this->uid, $this->password); - \OC_Util::setupFS(); - } -} diff --git a/tests/appinfo/applicationtest.php b/tests/unit/appinfo/applicationtest.php similarity index 84% rename from tests/appinfo/applicationtest.php rename to tests/unit/appinfo/applicationtest.php index aa7ea04cd..14449cdd0 100644 --- a/tests/appinfo/applicationtest.php +++ b/tests/unit/appinfo/applicationtest.php @@ -8,16 +8,17 @@ * This file is licensed under the Affero General Public License version 3 or * later. */ -namespace OCA\Richdocuments\AppInfo; +namespace OCA\Richdocuments\Tests\unit; -use OC_Hook; +use \OC_Hook; +use OCA\Richdocuments\AppInfo\Application; /** * Class ApplicationTest * * @group DB * - * @package OCA\Richdocuments\AppInfo + * @package OCA\Richdocuments\Tests\Unit */ class ApplicationTest extends \Test\TestCase { @@ -101,6 +102,14 @@ public function testRegisterForPublicLinksAllowed() { $this->assertArrayHasKey("share_link_access", $hooks["OCP\Share"]); } + private function countListeners($name) { + $listeners = $this->app->getContainer()->getServer()->getEventDispatcher()->getListeners(); + if (array_key_exists($name, $listeners)) { + return count($listeners[$name]); + } + return 0; + } + /** * Ensure that hook is not registered for viewer scripts if disallowed */ @@ -117,12 +126,12 @@ public function testRegisterForPublicLinksNotAllowed() { * Ensure viewer scripts are not registered for non-authenticated user */ public function testRegisterForUserAuthUnauthenticated() { - $listenersBefore = $this->app->getContainer()->getServer()->getEventDispatcher()->getListeners()['OCA\Files::loadAdditionalScripts']; + $listenersBefore = $this->countListeners('OCA\Files::loadAdditionalScripts'); $this->app->registerScripts(); - $listenersAfter = $this->app->getContainer()->getServer()->getEventDispatcher()->getListeners()['OCA\Files::loadAdditionalScripts']; - $this->assertCount(count($listenersBefore), $listenersAfter); + $listenersAfter = $this->countListeners('OCA\Files::loadAdditionalScripts'); + $this->assertEquals($listenersAfter, $listenersBefore); } /** @@ -130,12 +139,12 @@ public function testRegisterForUserAuthUnauthenticated() { */ public function testRegisterForUserAuthAllowed() { $this->setUpUser(false); - $listenersBefore = $this->app->getContainer()->getServer()->getEventDispatcher()->getListeners()['OCA\Files::loadAdditionalScripts']; + $listenersBefore = $this->countListeners('OCA\Files::loadAdditionalScripts'); $this->app->registerScripts(); - $listenersAfter = $this->app->getContainer()->getServer()->getEventDispatcher()->getListeners()['OCA\Files::loadAdditionalScripts']; - $this->assertCount(count($listenersBefore)+1, $listenersAfter); + $listenersAfter = $this->countListeners('OCA\Files::loadAdditionalScripts'); + $this->assertEquals($listenersAfter, $listenersBefore + 1); $this->tearDownUser(); } diff --git a/tests/unit/controller/documentcontrollertest.php b/tests/unit/controller/documentcontrollertest.php new file mode 100644 index 000000000..9d48e7d55 --- /dev/null +++ b/tests/unit/controller/documentcontrollertest.php @@ -0,0 +1,82 @@ +request = $this->createMock(IRequest::class); + $this->settings = $this->createMock(IConfig::class); + $this->appConfig = $this->createMock(AppConfig::class); + $this->l10n = $this->createMock(IL10N::class); + $this->cache = $this->createMock(ICacheFactory::class); + $this->logger = $this->createMock(ILogger::class); + $this->storage = $this->createMock(Storage::class); + } + + public function testConstructor() { + $documentController = new DocumentController( + 'richdocuments', + $this->request, + $this->settings, + $this->appConfig, + $this->l10n, + 'test', + $this->cache, + $this->logger, + $this->storage + ); + } +}