From c46fb8a60c2afacad1d0b9f597f584f0d0751fbf Mon Sep 17 00:00:00 2001 From: Jonas Date: Tue, 19 Nov 2024 14:48:57 +0100 Subject: [PATCH] test(phpunit): Add basic phpunit integration tests for CirclesManager Signed-off-by: Jonas --- tests/phpunit.xml | 33 ++-- tests/unit/CirclesManagerTest.php | 162 ++++++++++++++++++ .../lib/Controller/AdminControllerTest.php | 2 +- .../lib/Controller/LocalControllerTest.php | 2 +- 4 files changed, 178 insertions(+), 21 deletions(-) create mode 100644 tests/unit/CirclesManagerTest.php diff --git a/tests/phpunit.xml b/tests/phpunit.xml index 3df457589..062b37156 100644 --- a/tests/phpunit.xml +++ b/tests/phpunit.xml @@ -1,24 +1,19 @@ - + - - - . - - - - ../appinfo - ../lib - - - - - - - - - - + + + . + + + + + + + ../appinfo + ../lib + + diff --git a/tests/unit/CirclesManagerTest.php b/tests/unit/CirclesManagerTest.php new file mode 100644 index 000000000..c10c55721 --- /dev/null +++ b/tests/unit/CirclesManagerTest.php @@ -0,0 +1,162 @@ +circleName = sha1(uniqId(mt_rand(), true)); + + // Create test user + $userManager = \OC::$server->get(IUserManager::class); + if (!$userManager->userExists($this->userId)) { + $user = $userManager->createUser($this->userId, $this->userId); + } else { + $user = $userManager->get($this->userId); + } + + // Create test group and add user + $groupManager = \OC::$server->get(IGroupManager::class); + if (!$groupManager->groupExists($this->groupId)) { + $group = $groupManager->createGroup($this->groupId); + $group->addUser($user); + } + + $this->circlesManager = \OC::$server->get(CirclesManager::class); + + } + + // Start user session as user (default: test user) + private function startSession(?string $userId = null): FederatedUser { + if (!$userId) { + $userId = $this->userId; + } + $federatedUser = $this->circlesManager->getLocalFederatedUser($userId); + $this->circlesManager->startSession($federatedUser, true); + return $federatedUser; + } + + public function testCreateCircle(): void { + $federatedUser = $this->startSession(); + + // Created circle has properties + $circle = $this->circlesManager->createCircle($this->circleName); + $this->assertEquals($this->circleName, $circle->getName()); + $this->assertEquals($this->circleName, $circle->getDisplayName()); + $this->assertEquals($this->circleName, $circle->getSanitizedName()); + $this->assertEquals($federatedUser->getSingleId(), $circle->getOwner()->getSingleId()); + $this->assertEquals($federatedUser->getSingleId(), $circle->getInitiator()->getSingleId()); + + // Created circle returned by probeCircle() + $circles = $this->circlesManager->probeCircles(); + $this->assertCount(1, array_filter($circles, function (Circle $c) { return $c->getName() === $this->circleName; })); + + // Destroyed circle not returned by probeCircle() + $this->circlesManager->destroyCircle($circle->getSingleId()); + $circles = $this->circlesManager->probeCircles(); + $this->assertCount(0, array_filter($circles, function (Circle $c) { return $c->getName() === $this->circleName; })); + } + + public function testGetCirclesWithInitiator(): void { + // Create circle as user 'admin' and add test user as member + $this->startSession('admin'); + $adminCircle = $this->circlesManager->createCircle($this->circleName); + $this->circlesManager->addMember($adminCircle->getSingleId(), $this->circlesManager->getLocalFederatedUser($this->userId)); + + // Get circles as test user + $federatedUser = $this->startSession(); + $circles = $this->circlesManager->getCircles(); + $circle = null; + foreach ($circles as $c) { + if ($c->getSingleId() === $adminCircle->getSingleId()) { + $circle = $c; + } + } + + // Initiator of probed circle has correct properties + $this->assertEquals($federatedUser->getSingleId(), $circle->getInitiator()->getSingleId()); + $this->assertEquals(1, $circle->getInitiator()->getLevel()); + + // Destroy circle as user 'admin' + $this->startSession('admin'); + $this->circlesManager->destroyCircle($adminCircle->getSingleId()); + } + + public function testProbeCirclesWithInitiator(): void { + // Create circle as user 'admin' and add test user as member + $this->startSession('admin'); + $adminCircle = $this->circlesManager->createCircle($this->circleName); + $this->circlesManager->addMember($adminCircle->getSingleId(), $this->circlesManager->getLocalFederatedUser($this->userId)); + + // Probe circles as test user + $federatedUser = $this->startSession(); + $dataProbe = new DataProbe(); + $dataProbe->add(DataProbe::INITIATOR); + $circles = $this->circlesManager->probeCircles(null, $dataProbe); + $circle = null; + foreach ($circles as $c) { + if ($c->getSingleId() === $adminCircle->getSingleId()) { + $circle = $c; + } + } + + // Initiator of probed circle has correct properties + $this->assertEquals($federatedUser->getSingleId(), $circle->getInitiator()->getSingleId()); + $this->assertEquals(1, $circle->getInitiator()->getLevel()); + + // Destroy circle as user 'admin' + $this->startSession('admin'); + $this->circlesManager->destroyCircle($adminCircle->getSingleId()); + } + + public function testProbeCirclesWithInitiatorAsGroupMember(): void { + // Create circle as user 'admin' and add test group as member + $this->startSession('admin'); + $adminCircle = $this->circlesManager->createCircle($this->circleName); + $federatedGroup = $this->circlesManager->getFederatedUser($this->groupId, Member::TYPE_GROUP); + $this->circlesManager->addMember($adminCircle->getSingleId(), $federatedGroup); + + // Probe circles as test user + $federatedUser = $this->startSession(); + $dataProbe = new DataProbe(); + $dataProbe->add(DataProbe::INITIATOR); + $circles = $this->circlesManager->probeCircles(null, $dataProbe); + $circle = null; + foreach ($circles as $c) { + if ($c->getSingleId() === $adminCircle->getSingleId()) { + $circle = $c; + } + } + + // Initiator of probed circle has correct properties + $this->assertEquals($federatedGroup->getSingleId(), $circle->getInitiator()->getSingleId()); + $this->assertEquals(1, $circle->getInitiator()->getLevel()); + + // Destroy circle as user 'admin' + $this->startSession('admin'); + $this->circlesManager->destroyCircle($adminCircle->getSingleId()); + } +} diff --git a/tests/unit/lib/Controller/AdminControllerTest.php b/tests/unit/lib/Controller/AdminControllerTest.php index 9a2cb007f..a992a6a22 100644 --- a/tests/unit/lib/Controller/AdminControllerTest.php +++ b/tests/unit/lib/Controller/AdminControllerTest.php @@ -108,7 +108,7 @@ public function testCirclesList(int $limit, int $offset): void { $this->assertEquals($response, $this->adminController->circles('an-user-id', $limit, $offset)); } - public function dataForCirclesList(): array { + public static function dataForCirclesList(): array { return [ [-1, 0], [1, 1] diff --git a/tests/unit/lib/Controller/LocalControllerTest.php b/tests/unit/lib/Controller/LocalControllerTest.php index 0ad9cea15..188e93f13 100644 --- a/tests/unit/lib/Controller/LocalControllerTest.php +++ b/tests/unit/lib/Controller/LocalControllerTest.php @@ -117,7 +117,7 @@ public function testCirclesList(int $limit, int $offset): void { $this->assertEquals($response, $this->localController->circles($limit, $offset)); } - public function dataForCirclesList(): array { + public static function dataForCirclesList(): array { return [ [-1, 0], [1, 1]