Skip to content

Commit

Permalink
test: Migrate away from deprecated PHPUnit code
Browse files Browse the repository at this point in the history
Signed-off-by: provokateurin <kate@provokateurin.de>
  • Loading branch information
provokateurin committed Sep 19, 2024
1 parent 261f85c commit 9e6a97f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 27 deletions.
43 changes: 24 additions & 19 deletions tests/ACL/RuleManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
use OCA\GroupFolders\ACL\UserMapping\IUserMappingManager;
use OCA\GroupFolders\ACL\UserMapping\UserMapping;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IDBConnection;
use OCP\IUser;
use OCP\Log\Audit\CriticalActionPerformedEvent;
use OCP\Server;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

Expand All @@ -41,7 +43,7 @@ protected function setUp(): void {
->willReturnCallback(fn (string $type, string $id): UserMapping => new UserMapping($type, $id));

$this->eventDispatcher = $this->createMock(IEventDispatcher::class);
$this->ruleManager = new RuleManager(\OC::$server->getDatabaseConnection(), $this->userMappingManager, $this->eventDispatcher);
$this->ruleManager = new RuleManager(Server::get(IDBConnection::class), $this->userMappingManager, $this->eventDispatcher);
}

public function testGetSetRule(): void {
Expand All @@ -51,41 +53,44 @@ public function testGetSetRule(): void {
->with($this->user)
->willReturn([$mapping]);

$parameters = null;
$this->eventDispatcher->expects($this->any())
->method('dispatchTyped')
->withConsecutive(
[$this->callback(fn (CriticalActionPerformedEvent $event): bool => $event->getParameters() === [
'permissions' => 0b00001001,
'mask' => 0b00001111,
'fileId' => 10,
'user' => '1 (1)',
])],
[$this->callback(fn (CriticalActionPerformedEvent $event): bool => $event->getParameters() === [
'permissions' => 0b00001000,
'mask' => 0b00001111,
'fileId' => 10,
'user' => '1 (1)',
])],
[$this->callback(fn (CriticalActionPerformedEvent $event): bool => $event->getParameters() === [
'fileId' => 10,
'user' => '1 (1)',
])],
);
->willReturnCallback(function (CriticalActionPerformedEvent $event) use (&$parameters): bool {
$parameters = $event->getParameters();
return true;
});

$rule = new Rule($mapping, 10, 0b00001111, 0b00001001);
$this->ruleManager->saveRule($rule);
$this->assertEquals([
'permissions' => 0b00001001,
'mask' => 0b00001111,
'fileId' => 10,
'user' => '1 (1)',
], $parameters);

$result = $this->ruleManager->getRulesForFilesById($this->user, [10]);
$this->assertEquals([10 => [$rule]], $result);

$updatedRule = new Rule($mapping, 10, 0b00001111, 0b00001000);
$this->ruleManager->saveRule($updatedRule);
$this->assertEquals([
'permissions' => 0b00001000,
'mask' => 0b00001111,
'fileId' => 10,
'user' => '1 (1)',
], $parameters);

$result = $this->ruleManager->getRulesForFilesById($this->user, [10]);
$this->assertEquals([10 => [$updatedRule]], $result);

// cleanup
$this->ruleManager->deleteRule($rule);
$this->assertEquals([
'fileId' => 10,
'user' => '1 (1)',
], $parameters);
}

public function testGetMultiple(): void {
Expand Down
17 changes: 9 additions & 8 deletions tests/Folder/FolderManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use OCP\IDBConnection;
use OCP\IGroupManager;
use OCP\IUser;
use OCP\Server;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Test\TestCase;
Expand Down Expand Up @@ -42,7 +43,7 @@ protected function setUp(): void {
->with('groupfolders.quota.default', -3)
->willReturn(-3);
$this->manager = new FolderManager(
\OC::$server->getDatabaseConnection(),
Server::get(IDBConnection::class),
$this->groupManager,
$this->mimeLoader,
$this->logger,
Expand All @@ -53,11 +54,11 @@ protected function setUp(): void {
}

private function clean(): void {
$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$query->delete('group_folders')->execute();
$query = Server::get(IDBConnection::class)->getQueryBuilder();
$query->delete('group_folders')->executeStatement();

$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$query->delete('group_folders_groups')->execute();
$query = Server::get(IDBConnection::class)->getQueryBuilder();
$query->delete('group_folders_groups')->executeStatement();
}

private function assertHasFolders(array $folders): void {
Expand Down Expand Up @@ -315,7 +316,7 @@ public function testGetFoldersForUserSimple(): void {
$db = $this->createMock(IDBConnection::class);
$manager = $this->getMockBuilder(FolderManager::class)
->setConstructorArgs([$db, $this->groupManager, $this->mimeLoader, $this->logger, $this->eventDispatcher, $this->config])
->setMethods(['getFoldersForGroups'])
->onlyMethods(['getFoldersForGroups'])
->getMock();

$folder = [
Expand All @@ -337,7 +338,7 @@ public function testGetFoldersForUserMerge(): void {
$db = $this->createMock(IDBConnection::class);
$manager = $this->getMockBuilder(FolderManager::class)
->setConstructorArgs([$db, $this->groupManager, $this->mimeLoader, $this->logger, $this->eventDispatcher, $this->config])
->setMethods(['getFoldersForGroups'])
->onlyMethods(['getFoldersForGroups'])
->getMock();

$folder1 = [
Expand Down Expand Up @@ -372,7 +373,7 @@ public function testGetFolderPermissionsForUserMerge(): void {
$db = $this->createMock(IDBConnection::class);
$manager = $this->getMockBuilder(FolderManager::class)
->setConstructorArgs([$db, $this->groupManager, $this->mimeLoader, $this->logger, $this->eventDispatcher, $this->config])
->setMethods(['getFoldersForGroups'])
->onlyMethods(['getFoldersForGroups'])
->getMock();

$folder1 = [
Expand Down

0 comments on commit 9e6a97f

Please sign in to comment.