test: add test trait for creating temporary groups

Signed-off-by: Robin Appelman <robin@icewind.nl>
This commit is contained in:
Robin Appelman 2026-05-23 17:51:34 +02:00
parent a5ba481653
commit 9651860720
2 changed files with 66 additions and 6 deletions

View file

@ -0,0 +1,63 @@
<?php
/**
* SPDX-FileCopyrightText: 2022-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test\Traits;
use OC\Group\Group;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IUserManager;
use OCP\Server;
use Test\Util\Group\Dummy;
class DummyGroup extends Group {
public function __construct(
private string $gid,
) {
parent::__construct(
$this->gid,
[],
Server::get(IEventDispatcher::class),
Server::get(IUserManager::class),
);
}
#[\Override]
public function getGID(): string {
return $this->gid;
}
}
/**
* Allow creating users in a temporary backend
*/
trait GroupTrait {
protected Dummy $groupBackend;
protected function createGroup(string $name, array $users = []): IGroup {
$this->groupBackend->createGroup($name);
foreach ($users as $user) {
$this->groupBackend->addToGroup($user, $name);
}
return new DummyGroup($name);
}
protected function addToGroup(string $user, string $group): void {
$this->groupBackend->addToGroup($user, $group);
}
protected function setUpGroupTrait() {
$this->groupBackend = new Dummy();
Server::get(IGroupManager::class)->addBackend($this->groupBackend);
}
protected function tearDownGroupTrait() {
Server::get(IGroupManager::class)->removeBackend($this->groupBackend);
}
}

View file

@ -13,7 +13,7 @@ use OCP\EventDispatcher\IEventDispatcher;
use OCP\IUser;
use OCP\IUserManager;
use OCP\Server;
use OCP\UserInterface;
use Test\Util\User\Dummy;
class DummyUser extends User {
public function __construct(
@ -32,10 +32,7 @@ class DummyUser extends User {
* Allow creating users in a temporary backend
*/
trait UserTrait {
/**
* @var \Test\Util\User\Dummy|UserInterface
*/
protected $userBackend;
protected Dummy $userBackend;
protected function createUser($name, $password): IUser {
$this->userBackend->createUser($name, $password);
@ -43,7 +40,7 @@ trait UserTrait {
}
protected function setUpUserTrait() {
$this->userBackend = new \Test\Util\User\Dummy();
$this->userBackend = new Dummy();
Server::get(IUserManager::class)->registerBackend($this->userBackend);
}