-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(phpunit): Add basic phpunit tests for CirclesManager
Signed-off-by: Jonas <jonas@freesources.org>
- Loading branch information
Showing
4 changed files
with
178 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,19 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!-- | ||
- SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors | ||
- SPDX-License-Identifier: AGPL-3.0-or-later | ||
--> | ||
<phpunit bootstrap="bootstrap.php" convertDeprecationsToExceptions="true"> | ||
<testsuite name="circles"> | ||
<directory suffix='Test.php'>.</directory> | ||
</testsuite> | ||
<filter> | ||
<whitelist> | ||
<directory suffix=".php">../appinfo</directory> | ||
<directory suffix=".php">../lib</directory> | ||
</whitelist> | ||
</filter> | ||
<logging> | ||
<log type="coverage-clover" target="clover.xml"/> | ||
<!--<log type="coverage-text" target="php://stdout" showUncoveredFiles="false"/>--> | ||
</logging> | ||
<listeners> | ||
<listener class="OCA\Circles\Tests\Env" file="TestSuiteListener.php"/> | ||
</listeners> | ||
|
||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="bootstrap.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache"> | ||
<testsuite name="circles"> | ||
<directory suffix="Test.php">.</directory> | ||
</testsuite> | ||
<logging> | ||
<!--<log type="coverage-text" target="php://stdout" showUncoveredFiles="false"/>--> | ||
</logging> | ||
<source> | ||
<include> | ||
<directory suffix=".php">../appinfo</directory> | ||
<directory suffix=".php">../lib</directory> | ||
</include> | ||
</source> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
<?php | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\Circles\Tests; | ||
|
||
use OCA\Circles\CirclesManager; | ||
use OCA\Circles\Model\Circle; | ||
use OCA\Circles\Model\FederatedUser; | ||
use OCA\Circles\Model\Member; | ||
use OCA\Circles\Model\Probes\DataProbe; | ||
use OCP\IGroupManager; | ||
use OCP\IUserManager; | ||
use Test\TestCase; | ||
|
||
/** | ||
* @group DB | ||
*/ | ||
class CirclesManagerTest extends TestCase { | ||
private CirclesManager $circlesManager; | ||
private string $userId = 'circles-testuser'; | ||
private string $groupId = 'circles-testgroup'; | ||
private string $circleName; | ||
|
||
public function setUp(): void { | ||
parent::setUp(); | ||
|
||
// Random circle name | ||
$this->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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters