test(View): Setup users and groups once

Signed-off-by: Louis Chmn <louis@chmn.me>
This commit is contained in:
Louis Chmn 2026-06-10 14:42:18 +02:00 committed by Carl Schwan
parent 8bff1b4968
commit 6bb9ad60f2
No known key found for this signature in database
GPG key ID: 02325448204E452A

View file

@ -45,6 +45,7 @@ use OCP\Server;
use OCP\Share\IManager as IShareManager;
use OCP\Share\IShare;
use OCP\Util;
use Override;
use PHPUnit\Framework\MockObject\MockObject;
use Test\HookHelper;
use Test\TestMoveableMountPoint;
@ -105,46 +106,31 @@ class ViewTest extends \Test\TestCase {
/**
* @var Storage[] $storages
*/
private $storages = [];
private array $storages = [];
private static string $user = 'test';
private static IUser $userObject;
private static IGroup $groupObject;
private ?Storage $tempStorage = null;
/**
* @var string
*/
private $user;
public static function setUpBeforeClass(): void {
Server::get(IUserManager::class)->clearBackends();
Server::get(IUserManager::class)->registerBackend(new \Test\Util\User\Dummy());
/**
* @var IUser
*/
private $userObject;
$userManager = Server::get(IUserManager::class);
$groupManager = Server::get(IGroupManager::class);
/**
* @var IGroup
*/
private $groupObject;
/** @var Storage */
private $tempStorage;
self::$userObject = $userManager->createUser(self::$user, self::$user);
self::$groupObject = $groupManager->createGroup('group1');
self::$groupObject->addUser(self::$userObject);
}
#[\Override]
protected function setUp(): void {
parent::setUp();
\OC_Hook::clear();
Server::get(IUserManager::class)->clearBackends();
Server::get(IUserManager::class)->registerBackend(new \Test\Util\User\Dummy());
self::loginAsUser(self::$user);
//login
$userManager = Server::get(IUserManager::class);
$groupManager = Server::get(IGroupManager::class);
$this->user = 'test';
$this->userObject = $userManager->createUser('test', 'test');
$this->groupObject = $groupManager->createGroup('group1');
$this->groupObject->addUser($this->userObject);
self::loginAsUser($this->user);
/** @var IMountManager $manager */
$manager = Server::get(IMountManager::class);
$manager->removeMount('/test');
@ -153,7 +139,7 @@ class ViewTest extends \Test\TestCase {
#[\Override]
protected function tearDown(): void {
\OC_User::setUserId($this->user);
\OC_User::setUserId(self::$user);
foreach ($this->storages as $storage) {
$cache = $storage->getCache();
$ids = $cache->getAll();
@ -170,15 +156,18 @@ class ViewTest extends \Test\TestCase {
$setupManager = Server::get(SetupManager::class);
$setupManager->setupRoot();
$this->userObject->delete();
$this->groupObject->delete();
$mountProviderCollection = Server::get(IMountProviderCollection::class);
self::invokePrivate($mountProviderCollection, 'providers', [[]]);
parent::tearDown();
}
#[Override]
public static function tearDownAfterClass(): void {
self::$userObject->delete();
self::$groupObject->delete();
}
public function testCacheAPI(): void {
$storage1 = $this->getTestStorage();
$storage2 = $this->getTestStorage();
@ -510,10 +499,10 @@ class ViewTest extends \Test\TestCase {
}
public function moveBetweenStorages($storage1, $storage2) {
Filesystem::mount($storage1, [], '/' . $this->user . '/');
Filesystem::mount($storage2, [], '/' . $this->user . '/substorage');
Filesystem::mount($storage1, [], '/' . self::$user . '/');
Filesystem::mount($storage2, [], '/' . self::$user . '/substorage');
$rootView = new View('/' . $this->user);
$rootView = new View('/' . self::$user);
$rootView->rename('foo.txt', 'substorage/folder/foo.txt');
$this->assertFalse($rootView->file_exists('foo.txt'));
$this->assertTrue($rootView->file_exists('substorage/folder/foo.txt'));
@ -1582,11 +1571,11 @@ class ViewTest extends \Test\TestCase {
* Test mount point move
*/
public function testMountPointMove(): void {
self::loginAsUser($this->user);
self::loginAsUser(self::$user);
[$mount1, $mount2] = $this->createTestMovableMountPoints([
$this->user . '/files/mount1',
$this->user . '/files/mount2',
self::$user . '/files/mount1',
self::$user . '/files/mount2',
]);
$mount1->expects($this->once())
->method('moveMount')
@ -1596,7 +1585,7 @@ class ViewTest extends \Test\TestCase {
->method('moveMount')
->willReturn(true);
$view = new View('/' . $this->user . '/files/');
$view = new View('/' . self::$user . '/files/');
$view->mkdir('sub');
$this->assertTrue($view->rename('mount1', 'renamed_mount'), 'Can rename mount point');
@ -1604,11 +1593,11 @@ class ViewTest extends \Test\TestCase {
}
public function testMoveMountPointOverwrite(): void {
self::loginAsUser($this->user);
self::loginAsUser(self::$user);
[$mount1, $mount2] = $this->createTestMovableMountPoints([
$this->user . '/files/mount1',
$this->user . '/files/mount2',
self::$user . '/files/mount1',
self::$user . '/files/mount2',
]);
$mount1->expects($this->never())
@ -1617,18 +1606,18 @@ class ViewTest extends \Test\TestCase {
$mount2->expects($this->never())
->method('moveMount');
$view = new View('/' . $this->user . '/files/');
$view = new View('/' . self::$user . '/files/');
$this->expectException(ForbiddenException::class);
$view->rename('mount1', 'mount2');
}
public function testMoveMountPointIntoMount(): void {
self::loginAsUser($this->user);
self::loginAsUser(self::$user);
[$mount1, $mount2] = $this->createTestMovableMountPoints([
$this->user . '/files/mount1',
$this->user . '/files/mount2',
self::$user . '/files/mount1',
self::$user . '/files/mount2',
]);
$mount1->expects($this->never())
@ -1637,7 +1626,7 @@ class ViewTest extends \Test\TestCase {
$mount2->expects($this->never())
->method('moveMount');
$view = new View('/' . $this->user . '/files/');
$view = new View('/' . self::$user . '/files/');
$this->expectException(ForbiddenException::class);
$view->rename('mount1', 'mount2/sub');
@ -1647,11 +1636,11 @@ class ViewTest extends \Test\TestCase {
* Test that moving a mount point into a shared folder is forbidden
*/
public function testMoveMountPointIntoSharedFolder(): void {
self::loginAsUser($this->user);
self::loginAsUser(self::$user);
[$mount1, $mount2] = $this->createTestMovableMountPoints([
$this->user . '/files/mount1',
$this->user . '/files/mount2',
self::$user . '/files/mount1',
self::$user . '/files/mount2',
]);
$mount1->expects($this->never())
@ -1661,7 +1650,7 @@ class ViewTest extends \Test\TestCase {
->method('moveMount')
->willReturn(true);
$view = new View('/' . $this->user . '/files/');
$view = new View('/' . self::$user . '/files/');
$view->mkdir('shareddir');
$view->mkdir('shareddir/sub');
$view->mkdir('shareddir/sub2');
@ -1671,12 +1660,12 @@ class ViewTest extends \Test\TestCase {
$fileId = $view->getFileInfo('shareddir')->getId();
$userObject = Server::get(IUserManager::class)->createUser('test2', 'IHateNonMockableStaticClasses');
$userFolder = \OC::$server->getUserFolder($this->user);
$userFolder = \OC::$server->getUserFolder(self::$user);
$shareDir = $userFolder->get('shareddir');
$shareManager = Server::get(IShareManager::class);
$share = $shareManager->newShare();
$share->setSharedWith('test2')
->setSharedBy($this->user)
->setSharedBy(self::$user)
->setShareType(IShare::TYPE_USER)
->setPermissions(Constants::PERMISSION_READ)
->setNode($shareDir);
@ -1887,7 +1876,7 @@ class ViewTest extends \Test\TestCase {
?int $expectedStrayLock = null,
mixed $returnValue = true,
): void {
$view = new View('/' . $this->user . '/files/');
$view = new View('/' . self::$user . '/files/');
/** @var Temporary&MockObject $storage */
$storage = $this->getMockBuilder(Temporary::class)
@ -1899,7 +1888,7 @@ class ViewTest extends \Test\TestCase {
/* Same thing with encryption wrapper */
Server::get(IStorageFactory::class)->removeStorageWrapper('oc_encryption');
Filesystem::mount($storage, [], $this->user . '/');
Filesystem::mount($storage, [], self::$user . '/');
// work directly on disk because mkdir might be mocked
$realPath = $storage->getSourcePath('');
@ -1944,7 +1933,7 @@ class ViewTest extends \Test\TestCase {
* This code path uses $storage->fopen instead
*/
public function testLockFilePutContentWithStream(): void {
$view = new View('/' . $this->user . '/files/');
$view = new View('/' . self::$user . '/files/');
$path = 'test_file_put_contents.txt';
/** @var Temporary|\PHPUnit\Framework\MockObject\MockObject $storage */
@ -1952,7 +1941,7 @@ class ViewTest extends \Test\TestCase {
->onlyMethods(['fopen'])
->getMock();
Filesystem::mount($storage, [], $this->user . '/');
Filesystem::mount($storage, [], self::$user . '/');
$storage->mkdir('files');
$storage->expects($this->once())
@ -1983,7 +1972,7 @@ class ViewTest extends \Test\TestCase {
* Test locks for fopen with fclose at the end
*/
public function testLockFopen(): void {
$view = new View('/' . $this->user . '/files/');
$view = new View('/' . self::$user . '/files/');
$path = 'test_file_put_contents.txt';
/** @var Temporary|\PHPUnit\Framework\MockObject\MockObject $storage */
@ -1991,7 +1980,7 @@ class ViewTest extends \Test\TestCase {
->onlyMethods(['fopen'])
->getMock();
Filesystem::mount($storage, [], $this->user . '/');
Filesystem::mount($storage, [], self::$user . '/');
$storage->mkdir('files');
$storage->expects($this->once())
@ -2039,7 +2028,7 @@ class ViewTest extends \Test\TestCase {
if ($operation === 'touch') {
$this->markTestSkipped('touch handles storage exceptions internally');
}
$view = new View('/' . $this->user . '/files/');
$view = new View('/' . self::$user . '/files/');
/** @var Temporary|\PHPUnit\Framework\MockObject\MockObject $storage */
$storage = $this->getMockBuilder(Temporary::class)
@ -2051,7 +2040,7 @@ class ViewTest extends \Test\TestCase {
/* Same thing with encryption wrapper */
Server::get(IStorageFactory::class)->removeStorageWrapper('oc_encryption');
Filesystem::mount($storage, [], $this->user . '/');
Filesystem::mount($storage, [], self::$user . '/');
// work directly on disk because mkdir might be mocked
$realPath = $storage->getSourcePath('');
@ -2083,11 +2072,11 @@ class ViewTest extends \Test\TestCase {
}
public function testLockBasicOperationUnlocksAfterLockException(): void {
$view = new View('/' . $this->user . '/files/');
$view = new View('/' . self::$user . '/files/');
$storage = new Temporary([]);
Filesystem::mount($storage, [], $this->user . '/');
Filesystem::mount($storage, [], self::$user . '/');
$storage->mkdir('files');
$storage->mkdir('files/dir');
@ -2129,14 +2118,14 @@ class ViewTest extends \Test\TestCase {
$path,
$hookType,
): void {
$view = new View('/' . $this->user . '/files/');
$view = new View('/' . self::$user . '/files/');
/** @var Temporary|\PHPUnit\Framework\MockObject\MockObject $storage */
$storage = $this->getMockBuilder(Temporary::class)
->onlyMethods([$operation])
->getMock();
Filesystem::mount($storage, [], $this->user . '/');
Filesystem::mount($storage, [], self::$user . '/');
$storage->mkdir('files');
Util::connectHook(
@ -2168,7 +2157,7 @@ class ViewTest extends \Test\TestCase {
*/
#[\PHPUnit\Framework\Attributes\DataProvider('lockFileRenameOrCopyDataProvider')]
public function testLockFileRename($operation, $expectedLockTypeSourceDuring): void {
$view = new View('/' . $this->user . '/files/');
$view = new View('/' . self::$user . '/files/');
/** @var Temporary|\PHPUnit\Framework\MockObject\MockObject $storage */
$storage = $this->getMockBuilder(Temporary::class)
@ -2194,7 +2183,7 @@ class ViewTest extends \Test\TestCase {
/* Disable encryption wrapper to avoid it intercepting mocked call */
Server::get(IStorageFactory::class)->removeStorageWrapper('oc_encryption');
Filesystem::mount($storage, [], $this->user . '/');
Filesystem::mount($storage, [], self::$user . '/');
$storage->mkdir('files');
$view->file_put_contents($sourcePath, 'meh');
@ -2237,7 +2226,7 @@ class ViewTest extends \Test\TestCase {
public function testLockFileCopyException(): void {
$this->expectException(\Exception::class);
$view = new View('/' . $this->user . '/files/');
$view = new View('/' . self::$user . '/files/');
/** @var Temporary|\PHPUnit\Framework\MockObject\MockObject $storage */
$storage = $this->getMockBuilder(Temporary::class)
@ -2250,7 +2239,7 @@ class ViewTest extends \Test\TestCase {
/* Disable encryption wrapper to avoid it intercepting mocked call */
Server::get(IStorageFactory::class)->removeStorageWrapper('oc_encryption');
Filesystem::mount($storage, [], $this->user . '/');
Filesystem::mount($storage, [], self::$user . '/');
$storage->mkdir('files');
$view->file_put_contents($sourcePath, 'meh');
@ -2283,7 +2272,7 @@ class ViewTest extends \Test\TestCase {
public function testLockFileRenameUnlockOnException(): void {
self::loginAsUser('test');
$view = new View('/' . $this->user . '/files/');
$view = new View('/' . self::$user . '/files/');
$sourcePath = 'original.txt';
$targetPath = 'target.txt';
@ -2359,7 +2348,7 @@ class ViewTest extends \Test\TestCase {
*/
#[\PHPUnit\Framework\Attributes\DataProvider('lockFileRenameOrCopyCrossStorageDataProvider')]
public function testLockFileRenameCrossStorage($viewOperation, $storageOperation, $expectedLockTypeSourceDuring): void {
$view = new View('/' . $this->user . '/files/');
$view = new View('/' . self::$user . '/files/');
/** @var Temporary|\PHPUnit\Framework\MockObject\MockObject $storage */
$storage = $this->getMockBuilder(Temporary::class)
@ -2389,8 +2378,8 @@ class ViewTest extends \Test\TestCase {
/* Disable encryption wrapper to avoid it intercepting mocked call */
Server::get(IStorageFactory::class)->removeStorageWrapper('oc_encryption');
Filesystem::mount($storage, [], $this->user . '/');
Filesystem::mount($storage2, [], $this->user . '/files/substorage');
Filesystem::mount($storage, [], self::$user . '/');
Filesystem::mount($storage2, [], self::$user . '/files/substorage');
$storage->mkdir('files');
$view->file_put_contents($sourcePath, 'meh');
$storage2->getUpdater()->update('');
@ -2435,10 +2424,10 @@ class ViewTest extends \Test\TestCase {
self::loginAsUser('test');
[$mount] = $this->createTestMovableMountPoints([
$this->user . '/files/substorage',
self::$user . '/files/substorage',
]);
$view = new View('/' . $this->user . '/files/');
$view = new View('/' . self::$user . '/files/');
$view->mkdir('subdir');
$sourcePath = 'substorage';
@ -2560,7 +2549,7 @@ class ViewTest extends \Test\TestCase {
}
public function testRemoveMoveableMountPoint(): void {
$mountPoint = '/' . $this->user . '/files/mount/';
$mountPoint = '/' . self::$user . '/files/mount/';
// Mock the mount point
/** @var TestMoveableMountPoint|\PHPUnit\Framework\MockObject\MockObject $mount */
@ -2602,7 +2591,7 @@ class ViewTest extends \Test\TestCase {
);
//Delete the mountpoint
$view = new View('/' . $this->user . '/files');
$view = new View('/' . self::$user . '/files');
$this->assertEquals('foo', $view->rmdir('mount'));
}