Skip to content

Commit

Permalink
Fix Tests
Browse files Browse the repository at this point in the history
Signed-off-by: Jonas Rittershofer <[email protected]>
  • Loading branch information
jotoeri committed Jun 26, 2022
1 parent 72af5d0 commit c4474fa
Show file tree
Hide file tree
Showing 4 changed files with 462 additions and 4 deletions.
130 changes: 130 additions & 0 deletions tests/Unit/Controller/ConfigControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2021 Jonas Rittershofer <[email protected]>
*
* @author Jonas Rittershofer <[email protected]>
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Forms\Tests\Unit\Controller;

use OCA\Forms\Controller\ConfigController;

use OCA\Forms\Service\ConfigService;
use OCP\AppFramework\Http\DataResponse;
use OCP\IConfig;
use OCP\ILogger;
use OCP\IRequest;

use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

class ConfigControllerTest extends TestCase {

/** @var ConfigController */
private $configController;

/** @var ConfigService */
private $configService;

/** @var IConfig|MockObject */
private $config;

/** @var ILogger|MockObject */
private $logger;

/** @var IRequest|MockObject */
private $request;

public function setUp(): void {
parent::setUp();

$this->configService = $this->createMock(ConfigService::class);
$this->config = $this->createMock(IConfig::class);
$this->logger = $this->createMock(ILogger::class);
$this->request = $this->createMock(IRequest::class);

$this->configController = new ConfigController(
'forms',
$this->configService,
$this->config,
$this->logger,
$this->request
);
}

public function testGetAppConfig() {
$this->configService->expects($this->once())
->method('getAppConfig')
->willReturn([
'allow' => 'someConfig',
'permit' => 'all'
]);

$this->assertEquals(new DataResponse([
'allow' => 'someConfig',
'permit' => 'all'
]), $this->configController->getAppConfig());
}

public function dataUpdateAppConfig() {
return [
'booleanConfig' => [
'configKey' => 'allowPermitAll',
'configValue' => true,
'strConfig' => "true"
],
'arrayConfig' => [
'configKey' => 'allowPermitAll',
'configValue' => [
"admin",
"group1"
],
'strConfig' => '["admin","group1"]'
]
];
}
/**
* @dataProvider dataUpdateAppConfig
*
* @param string $configKey
* @param mixed $configValue
* @param string $strConfig The configValue as json-string
*/
public function testUpdateAppConfig(string $configKey, $configValue, string $strConfig) {
$this->logger->expects($this->once())
->method('debug');

$this->config->expects($this->once())
->method('setAppValue')
->with('forms', $configKey, $strConfig);

$this->assertEquals(new DataResponse(), $this->configController->updateAppConfig($configKey, $configValue));
}

public function testUpdateAppConfig_unknownKey() {
$this->logger->expects($this->once())
->method('debug');

$this->config->expects($this->never())
->method('setAppValue');

$this->assertEquals(new DataResponse('Unknown appConfig key: someUnknownKey', 400), $this->configController->updateAppConfig('someUnknownKey', 'storeThisValue!'));
}
}
35 changes: 34 additions & 1 deletion tests/Unit/Controller/ShareApiControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use OCA\Forms\Db\FormMapper;
use OCA\Forms\Db\Share;
use OCA\Forms\Db\ShareMapper;
use OCA\Forms\Service\ConfigService;
use OCA\Forms\Service\FormsService;

use OCP\AppFramework\Db\DoesNotExistException;
Expand Down Expand Up @@ -61,6 +62,9 @@ class ShareApiControllerTest extends TestCase {
/** @var ShareMapper|MockObject */
private $shareMapper;

/** @var ConfigService|MockObject */
private $configService;

/** @var FormsService|MockObject */
private $formsService;

Expand All @@ -82,6 +86,7 @@ class ShareApiControllerTest extends TestCase {
public function setUp(): void {
$this->formMapper = $this->createMock(FormMapper::class);
$this->shareMapper = $this->createMock(ShareMapper::class);
$this->configService = $this->createMock(ConfigService::class);
$this->formsService = $this->createMock(FormsService::class);
$this->groupManager = $this->createMock(IGroupManager::class);
$this->logger = $this->createMock(ILogger::class);
Expand All @@ -102,6 +107,7 @@ public function setUp(): void {
'forms',
$this->formMapper,
$this->shareMapper,
$this->configService,
$this->formsService,
$this->groupManager,
$this->logger,
Expand Down Expand Up @@ -238,6 +244,10 @@ public function testNewLinkShare(int $shareType, string $shareWith, array $expec
->with($share)
->willReturn($shareWithId);

$this->configService->expects($this->once())
->method('getAllowPublicLink')
->willReturn(true);

$this->formsService->expects($this->once())
->method('getShareDisplayName')
->with($shareWithId->read())
Expand All @@ -255,7 +265,7 @@ public function testNewLinkShare(int $shareType, string $shareWith, array $expec
/**
* Test a random link hash, that is already existing.
*/
public function testExistingLinkHash() {
public function testNewLinkShare_ExistingHash() {
$form = new Form();
$form->setId('5');
$form->setOwnerId('currentUser');
Expand All @@ -265,6 +275,10 @@ public function testExistingLinkHash() {
->with('5')
->willReturn($form);

$this->configService->expects($this->once())
->method('getAllowPublicLink')
->willReturn(true);

$this->secureRandom->expects($this->any())
->method('generate')
->willReturn('abcdefgh');
Expand All @@ -281,6 +295,25 @@ public function testExistingLinkHash() {
$this->shareApiController->newShare(5, IShare::TYPE_LINK, '');
}

/**
* Test a random link hash, that is already existing.
*/
public function testNewLinkShare_PublicLinkNotAllowed() {
$form = new Form();
$form->setId('5');
$form->setOwnerId('currentUser');

$this->configService->expects($this->once())
->method('getAllowPublicLink')
->willReturn(false);

$this->shareMapper->expects($this->never())
->method('insert');

$this->expectException(OCSException::class);
$this->shareApiController->newShare(5, IShare::TYPE_LINK, '');
}

/**
* Test an unused (but existing) Share-Type
*/
Expand Down
Loading

0 comments on commit c4474fa

Please sign in to comment.