From 1147d3ab53548fd699321ce986fea63ca8dd2778 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Tue, 18 Jul 2023 10:56:25 +0200 Subject: [PATCH] test: Add some basic integration test skeleton for import MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- tests/integration/import/ImportExportTest.php | 132 ++++++++++++++++++ tests/phpunit.integration.xml | 3 + tests/unit/Command/UserExportTest.php | 8 +- .../Importer/Systems/DeckJsonServiceTest.php | 86 ++++++++++++ 4 files changed, 227 insertions(+), 2 deletions(-) create mode 100644 tests/integration/import/ImportExportTest.php create mode 100644 tests/unit/Service/Importer/Systems/DeckJsonServiceTest.php diff --git a/tests/integration/import/ImportExportTest.php b/tests/integration/import/ImportExportTest.php new file mode 100644 index 000000000..f6e339d6a --- /dev/null +++ b/tests/integration/import/ImportExportTest.php @@ -0,0 +1,132 @@ + + * + * @author Julius Härtl + * + * @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 . + * + */ + +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(); + } +} diff --git a/tests/phpunit.integration.xml b/tests/phpunit.integration.xml index f62881f9a..e5534edc8 100644 --- a/tests/phpunit.integration.xml +++ b/tests/phpunit.integration.xml @@ -12,5 +12,8 @@ ./integration/app + + ./integration/import + diff --git a/tests/unit/Command/UserExportTest.php b/tests/unit/Command/UserExportTest.php index 81fc49642..1d75a35b9 100644 --- a/tests/unit/Command/UserExportTest.php +++ b/tests/unit/Command/UserExportTest.php @@ -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); } } diff --git a/tests/unit/Service/Importer/Systems/DeckJsonServiceTest.php b/tests/unit/Service/Importer/Systems/DeckJsonServiceTest.php new file mode 100644 index 000000000..1a2c41aa1 --- /dev/null +++ b/tests/unit/Service/Importer/Systems/DeckJsonServiceTest.php @@ -0,0 +1,86 @@ + + * + * @author Vitor Mattos + * + * @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 . + * + */ +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()); + } +}