Skip to content

Commit dc091c6

Browse files
committed
test: add test trait for creating temporary groups
Signed-off-by: Robin Appelman <robin@icewind.nl>
1 parent 0e02179 commit dc091c6

2 files changed

Lines changed: 66 additions & 6 deletions

File tree

tests/lib/Traits/GroupTrait.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
/**
4+
* SPDX-FileCopyrightText: 2022-2024 Nextcloud GmbH and Nextcloud contributors
5+
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
6+
* SPDX-License-Identifier: AGPL-3.0-or-later
7+
*/
8+
9+
namespace Test\Traits;
10+
11+
use OC\Group\Group;
12+
use OCP\EventDispatcher\IEventDispatcher;
13+
use OCP\IGroup;
14+
use OCP\IGroupManager;
15+
use OCP\IUserManager;
16+
use OCP\Server;
17+
use Test\Util\Group\Dummy;
18+
19+
class DummyGroup extends Group {
20+
public function __construct(
21+
private string $gid,
22+
) {
23+
parent::__construct(
24+
$this->gid,
25+
[],
26+
Server::get(IEventDispatcher::class),
27+
Server::get(IUserManager::class),
28+
);
29+
}
30+
31+
#[\Override]
32+
public function getGID(): string {
33+
return $this->gid;
34+
}
35+
}
36+
37+
/**
38+
* Allow creating users in a temporary backend
39+
*/
40+
trait GroupTrait {
41+
protected Dummy $groupBackend;
42+
43+
protected function createGroup(string $name, array $users = []): IGroup {
44+
$this->groupBackend->createGroup($name);
45+
foreach ($users as $user) {
46+
$this->groupBackend->addToGroup($user, $name);
47+
}
48+
return new DummyGroup($name);
49+
}
50+
51+
protected function addToGroup(string $user, string $group): void {
52+
$this->groupBackend->addToGroup($user, $group);
53+
}
54+
55+
protected function setUpGroupTrait() {
56+
$this->groupBackend = new Dummy();
57+
Server::get(IGroupManager::class)->addBackend($this->groupBackend);
58+
}
59+
60+
protected function tearDownGroupTrait() {
61+
Server::get(IGroupManager::class)->removeBackend($this->groupBackend);
62+
}
63+
}

tests/lib/Traits/UserTrait.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use OCP\IUser;
1414
use OCP\IUserManager;
1515
use OCP\Server;
16-
use OCP\UserInterface;
16+
use Test\Util\User\Dummy;
1717

1818
class DummyUser extends User {
1919
public function __construct(
@@ -32,18 +32,15 @@ public function getUID(): string {
3232
* Allow creating users in a temporary backend
3333
*/
3434
trait UserTrait {
35-
/**
36-
* @var \Test\Util\User\Dummy|UserInterface
37-
*/
38-
protected $userBackend;
35+
protected Dummy $userBackend;
3936

4037
protected function createUser($name, $password): IUser {
4138
$this->userBackend->createUser($name, $password);
4239
return new DummyUser($name);
4340
}
4441

4542
protected function setUpUserTrait() {
46-
$this->userBackend = new \Test\Util\User\Dummy();
43+
$this->userBackend = new Dummy();
4744
Server::get(IUserManager::class)->registerBackend($this->userBackend);
4845
}
4946

0 commit comments

Comments
 (0)