Skip to content

Commit

Permalink
test: add basic test for guest migration
Browse files Browse the repository at this point in the history
Signed-off-by: Robin Appelman <robin@icewind.nl>
  • Loading branch information
icewind1991 committed Feb 14, 2025
1 parent 29941e3 commit b6121f2
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions tests/unit/Migration/OwncloudGuestMigrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/

namespace OCA\Guests\Tests\Migration;

use OC\Migration\ConsoleOutput;
use OCA\Guests\GuestManager;
use OCA\Guests\Migration\OwncloudGuestsMigration;
use OCA\Guests\UserBackend;
use OCP\IConfig;
use OCP\IUserManager;
use OCP\Server;
use Symfony\Component\Console\Output\BufferedOutput;
use Test\TestCase;
use Test\Util\User\Dummy;

/**
* @group DB
*/
class OwncloudGuestMigrationTest extends TestCase {
private IConfig $config;
private IUserManager $userManager;
private BufferedOutput $consoleOutput;
private ConsoleOutput $migrationOutput;
private GuestManager $guestManager;

protected function setUp():void {
$this->config = Server::get(IConfig::class);
$this->userManager = Server::get(IUserManager::class);
$this->consoleOutput = new BufferedOutput();
$this->migrationOutput = new ConsoleOutput($this->consoleOutput);
$this->guestManager = Server::get(GuestManager::class);

parent::setUp();
}

private function createOcGuest(string $userId): void {
$this->userManager->createUser($userId, $userId . '_password');
$this->config->setUserValue($userId, 'owncloud', 'isGuest', '1');
}

public function testMigrateNoGuests(): void {
$migration = Server::get(OwncloudGuestsMigration::class);

$migration->run($this->migrationOutput);
$this->assertEmpty($this->consoleOutput->fetch());

$this->assertEmpty($this->guestManager->listGuests());
}

public function testMigrateSingleGuest(): void {
$this->createOcGuest('test@example.com');
$this->assertEmpty($this->guestManager->listGuests());

$migration = Server::get(OwncloudGuestsMigration::class);

$migration->run($this->migrationOutput);
$this->assertNotEmpty($this->consoleOutput->fetch());

// clear the backend-cache
$this->userManager->removeBackend(new Dummy());

/** @var $guests */
$guests = $this->guestManager->listGuests();
$this->assertCount(1, $guests);
$this->assertEquals('test@example.com', $guests[0]);

$user = $this->userManager->get('test@example.com');
$this->assertInstanceOf(UserBackend::class, $user->getBackend());
$this->userManager->checkPassword('test@example.com', 'test@example.com_password');
}
}

0 comments on commit b6121f2

Please sign in to comment.