test: Add some basic integration test skeleton for import

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl 2023-07-18 10:56:25 +02:00
Родитель 894c415bfd
Коммит 1147d3ab53
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4C614C6ED2CDE6DF
4 изменённых файлов: 227 добавлений и 2 удалений

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

@ -0,0 +1,132 @@
<?php
/**
* @copyright Copyright (c) 2017 Julius Härtl <jus@bitgrid.net>
*
* @author Julius Härtl <jus@bitgrid.net>
*
* @license GNU AGPL version 3 or any later version
*
* 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\Deck\Db;
use OCA\Deck\Command\BoardImport;
use OCA\Deck\Service\Importer\BoardImportService;
use OCA\Deck\Service\Importer\Systems\DeckJsonService;
use OCP\IDBConnection;
use OCP\IGroupManager;
use OCP\IUserManager;
use OCP\Server;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* @group DB
*/
class ImportExportTest extends \Test\TestCase {
private IDBConnection $connection;
private const TEST_USER1 = 'test-share-user1';
private const TEST_USER3 = 'test-share-user3';
private const TEST_USER2 = 'test-share-user2';
private const TEST_USER4 = 'test-share-user4';
private const TEST_GROUP1 = 'test-share-group1';
public static function setUpBeforeClass(): void {
parent::setUpBeforeClass();
$backend = new \Test\Util\User\Dummy();
\OC_User::useBackend($backend);
Server::get(IUserManager::class)->registerBackend($backend);
$backend->createUser(self::TEST_USER1, self::TEST_USER1);
$backend->createUser(self::TEST_USER2, self::TEST_USER2);
$backend->createUser(self::TEST_USER3, self::TEST_USER3);
$backend->createUser(self::TEST_USER4, self::TEST_USER4);
// create group
$groupBackend = new \Test\Util\Group\Dummy();
$groupBackend->createGroup(self::TEST_GROUP1);
$groupBackend->createGroup('group');
$groupBackend->createGroup('group1');
$groupBackend->createGroup('group2');
$groupBackend->createGroup('group3');
$groupBackend->addToGroup(self::TEST_USER1, 'group');
$groupBackend->addToGroup(self::TEST_USER2, 'group');
$groupBackend->addToGroup(self::TEST_USER3, 'group');
$groupBackend->addToGroup(self::TEST_USER2, 'group1');
$groupBackend->addToGroup(self::TEST_USER3, 'group2');
$groupBackend->addToGroup(self::TEST_USER4, 'group3');
$groupBackend->addToGroup(self::TEST_USER2, self::TEST_GROUP1);
Server::get(IGroupManager::class)->addBackend($groupBackend);
}
public function setUp(): void {
parent::setUp();
$this->connection = \OCP\Server::get(IDBConnection::class);
$this->connection->beginTransaction();
}
public function testImportOcc() {
$input = $this->createMock(InputInterface::class);
$input->expects($this->any())
->method('getOption')
->willReturnCallback(function ($arg) {
return match ($arg) {
'system' => 'DeckJson',
'data' => __DIR__ . '/../../data/deck.json',
'config' => __DIR__ . '/../../data/config-trelloJson.json',
};
});
$output = $this->createMock(OutputInterface::class);
$importer = \OCP\Server::get(BoardImport::class);
$application = new Application();
$importer->setApplication($application);
$importer->run($input, $output);
$this->assertDatabase();
}
public function testImport() {
$importer = \OCP\Server::get(BoardImportService::class);
$deckJsonService = \OCP\Server::get(DeckJsonService::class);
$deckJsonService->setImportService($importer);
$importer->setSystem('DeckJson');
$importer->setImportSystem($deckJsonService);
$importer->setConfigInstance(json_decode(file_get_contents(__DIR__ . '/../../data/config-trelloJson.json')));
$importer->setData(json_decode(file_get_contents(__DIR__ . '/../../data/deck.json')));
$importer->import();
$this->assertDatabase();
}
public function assertDatabase() {
$boardMapper = \OCP\Server::get(BoardMapper::class);
$boards = $boardMapper->findAllByOwner('admin');
self::assertEquals('My test board', $boards[0]->getTitle());
self::assertEquals('Shared board', $boards[1]->getTitle());
self::assertEquals(2, count($boards));
}
public function tearDown(): void {
if ($this->connection->inTransaction()) {
$this->connection->rollBack();
}
parent::tearDown();
}
}

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

@ -12,5 +12,8 @@
<testsuite name="integration-app">
<directory>./integration/app</directory>
</testsuite>
<testsuite name="integration-import">
<directory>./integration/import</directory>
</testsuite>
</testsuites>
</phpunit>

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

@ -31,12 +31,14 @@ use OCA\Deck\Db\CardMapper;
use OCA\Deck\Db\Stack;
use OCA\Deck\Db\StackMapper;
use OCA\Deck\Service\BoardService;
use OCP\App\IAppManager;
use OCP\IGroupManager;
use OCP\IUserManager;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class UserExportTest extends \Test\TestCase {
protected $appManager;
protected $boardMapper;
protected $boardService;
protected $stackMapper;
@ -45,10 +47,11 @@ class UserExportTest extends \Test\TestCase {
protected $userManager;
protected $groupManager;
private $userExport;
private UserExport $userExport;
public function setUp(): void {
parent::setUp();
$this->appManager = $this->createMock(IAppManager::class);
$this->boardMapper = $this->createMock(BoardMapper::class);
$this->boardService = $this->createMock(BoardService::class);
$this->stackMapper = $this->createMock(StackMapper::class);
@ -56,7 +59,7 @@ class UserExportTest extends \Test\TestCase {
$this->assignedUserMapper = $this->createMock(AssignmentMapper::class);
$this->userManager = $this->createMock(IUserManager::class);
$this->groupManager = $this->createMock(IGroupManager::class);
$this->userExport = new UserExport($this->boardMapper, $this->boardService, $this->stackMapper, $this->cardMapper, $this->assignedUserMapper, $this->userManager, $this->groupManager);
$this->userExport = new UserExport($this->appManager, $this->boardMapper, $this->boardService, $this->stackMapper, $this->cardMapper, $this->assignedUserMapper, $this->userManager, $this->groupManager);
}
public function getBoard($id) {
@ -114,5 +117,6 @@ class UserExportTest extends \Test\TestCase {
->method('findAll')
->willReturn([]);
$result = $this->invokePrivate($this->userExport, 'execute', [$input, $output]);
self::assertEquals(0, $result);
}
}

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

@ -0,0 +1,86 @@
<?php
/**
* @copyright Copyright (c) 2021 Vitor Mattos <vitor@php.rio>
*
* @author Vitor Mattos <vitor@php.rio>
*
* @license GNU AGPL version 3 or any later version
*
* 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\Deck\Service\Importer\Systems;
use OCA\Deck\Service\Importer\BoardImportService;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserManager;
use OCP\Server;
use PHPUnit\Framework\MockObject\MockObject;
/**
* @group DB
*/
class DeckJsonServiceTest extends \Test\TestCase {
private DeckJsonService $service;
/** @var IURLGenerator|MockObject */
private $urlGenerator;
/** @var IUserManager|MockObject */
private $userManager;
/** @var IL10N */
private $l10n;
public function setUp(): void {
$this->userManager = $this->createMock(IUserManager::class);
$this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->l10n = $this->createMock(IL10N::class);
$this->service = new DeckJsonService(
$this->userManager,
$this->urlGenerator,
$this->l10n
);
}
public function testGetBoardWithNoName() {
$this->expectExceptionMessage('Invalid name of board');
$importService = $this->createMock(BoardImportService::class);
$this->service->setImportService($importService);
$this->service->getBoard();
}
public function testGetBoardWithSuccess() {
$importService = Server::get(BoardImportService::class);
$data = json_decode(file_get_contents(__DIR__ . '/../../../../data/deck.json'));
$importService->setData($data);
$configInstance = json_decode(file_get_contents(__DIR__ . '/../../../../data/config-trelloJson.json'));
$importService->setConfigInstance($configInstance);
$owner = $this->createMock(IUser::class);
$owner
->method('getUID')
->willReturn('owner');
$importService->setConfig('owner', $owner);
$this->service->setImportService($importService);
$boards = $this->service->getBoards();
$importService->setData($boards[0]);
$actual = $this->service->getBoard();
$this->assertEquals('My test board', $actual->getTitle());
$this->assertEquals('admin', $actual->getOwner());
$this->assertEquals('e0ed31', $actual->getColor());
}
}