Skip to content

Commit

Permalink
Adjust to work with unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mrow4a committed Nov 28, 2018
1 parent 442a6b9 commit 083f261
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 165 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
tests/clover.xml
*.tar.gz
build/

Expand Down
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
134 changes: 67 additions & 67 deletions lib/appinfo/application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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());
}
}
File renamed without changes.
File renamed without changes.
9 changes: 6 additions & 3 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?xml version="1.0" encoding="utf-8" ?>
<phpunit bootstrap="../../tests/bootstrap.php"
verbose="true" failOnWarning="true"
verbose="true"
failOnWarning="true"
timeoutForSmallTests="900"
timeoutForMediumTests="900"
timeoutForLargeTests="900"
>
<testsuite name='phpunit'>
<directory suffix='test.php'>./tests</directory>
<testsuite name='unit'>
<directory suffix='test.php'>./tests/unit</directory>
</testsuite>
<!-- filters for code coverage -->
<filter>
Expand All @@ -15,6 +16,8 @@
<exclude>
<directory suffix=".php">./l10n</directory>
<directory suffix=".php">./tests</directory>
<directory suffix=".php">./templates</directory>
<directory suffix=".php">./appinfo</directory>
</exclude>
</whitelist>
</filter>
Expand Down
13 changes: 0 additions & 13 deletions tests/bootstrap.php

This file was deleted.

70 changes: 0 additions & 70 deletions tests/controller/documentcontrollertest.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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
*/
Expand All @@ -117,25 +126,25 @@ 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);
}

/**
* Ensure viewer scripts are registered for authenticated user when there are no collabora groups specified
*/
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();
}
Expand Down
Loading

0 comments on commit 083f261

Please sign in to comment.