Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 25 additions & 28 deletions src/OcisResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use OpenAPI\Client\Model\DriveItemInvite;
use OpenAPI\Client\Model\DriveRecipient;
use OpenAPI\Client\Model\OdataError;
use OpenAPI\Client\Model\Permission;
use OpenAPI\Client\Model\SharingLinkType;
use Owncloud\OcisPhpSdk\Exception\BadRequestException;
use Owncloud\OcisPhpSdk\Exception\ExceptionHelper;
Expand Down Expand Up @@ -175,13 +176,13 @@ public function getRoles(): array
}

/**
* Invite one or multiple people(user/group) to the resource.
* Invite a user or group to the resource.
* Every recipient will result in an own ShareCreated object in the returned array.
*
* @param array<int, User|Group> $recipients
* @param User|Group $recipient
* @param SharingRole $role
* @param \DateTimeImmutable|null $expiration
* @return array<ShareCreated>
* @return ShareCreated
* @throws BadRequestException
* @throws ForbiddenException
* @throws HttpException
Expand All @@ -190,18 +191,16 @@ public function getRoles(): array
* @throws UnauthorizedException
* @throws InternalServerErrorException
*/
public function invite($recipients, SharingRole $role, ?\DateTimeImmutable $expiration = null): array
public function invite($recipient, SharingRole $role, ?\DateTimeImmutable $expiration = null): ShareCreated
{
$driveItemInviteData = [];
$driveItemInviteData['recipients'] = [];
foreach ($recipients as $recipient) {
$recipientData = [];
$recipientData['object_id'] = $recipient->getId();
if ($recipient instanceof Group) {
$recipientData['at_libre_graph_recipient_type'] = "group";
}
$driveItemInviteData['recipients'][] = new DriveRecipient($recipientData);
$recipientData = [];
$recipientData['object_id'] = $recipient->getId();
if ($recipient instanceof Group) {
$recipientData['at_libre_graph_recipient_type'] = "group";
}
$driveItemInviteData['recipients'][] = new DriveRecipient($recipientData);
$driveItemInviteData['roles'] = [$role->getId()];
if ($expiration !== null) {
$expirationMutable = \DateTime::createFromImmutable($expiration);
Expand Down Expand Up @@ -231,27 +230,25 @@ public function invite($recipients, SharingRole $role, ?\DateTimeImmutable $expi
"invite returned an OdataError - " . $permissions->getError()
);
}
if ($permissions->getValue() === null) {
$permissionsValue = $permissions->getValue();
if (
$permissionsValue === null ||
!array_key_exists(0, $permissionsValue) ||
!($permissionsValue[0] instanceof Permission)
) {
throw new InvalidResponseException(
"invite returned 'null' where an array of permissions were expected"
"invite returned invalid data " . print_r($permissionsValue, true)
);
}

/**
* @var array<ShareCreated> $shares
*/
$shares = [];
foreach ($permissions->getValue() as $permission) {
$shares[] = new ShareCreated(
$permission,
$this->getId(),
$this->getSpaceId(),
$this->connectionConfig,
$this->serviceUrl,
$this->accessToken
);
}
return $shares;
return new ShareCreated(
$permissionsValue[0],
$this->getId(),
$this->getSpaceId(),
$this->connectionConfig,
$this->serviceUrl,
$this->accessToken
);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/Owncloud/OcisPhpSdk/OcisResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private function share(string $receiverName, string $roleName, string $resourceN
foreach ($resources as $resource) {
if ($resource->getName() === $resourceName) {
$role = $this->getRoleByName($resource, $roleName);
$resource->invite([$receiver], $role);
$resource->invite($receiver, $role);
break;
}
}
Expand Down Expand Up @@ -265,7 +265,7 @@ public function testUploadFileNoPermission(): void
foreach ($resources as $resource) {
if ($resource->getName() === 'subfolder') {
$role = $this->getRoleByName($resource, 'Viewer');
$resource->invite([$einstein], $role);
$resource->invite($einstein, $role);
break;
}
}
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/Owncloud/OcisPhpSdk/OcisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function testGetMyDrives(): void
"manager role not found "
);
}
$sharedResource->invite([$marie], $managerRole);
$sharedResource->invite($marie, $managerRole);

$marieDrive = $marieOcis->getMyDrives();
$this->assertContainsOnlyInstancesOf(Drive::class, $marieDrive);
Expand Down Expand Up @@ -125,7 +125,7 @@ public function testGetAllDrives(): void
"manager role not found "
);
}
$sharedResource->invite([$katherine], $managerRole);
$sharedResource->invite($katherine, $managerRole);

$drives = $adminOcis->getAllDrives();
foreach ($drives as $drive) {
Expand Down Expand Up @@ -220,7 +220,7 @@ public function testGetAllDrivesType(DriveType $driveType): void
);
}

$sharedResource->invite([$katherine], $managerRole);
$sharedResource->invite($katherine, $managerRole);
}

$drives = $adminOcis->getAllDrives(
Expand Down
133 changes: 36 additions & 97 deletions tests/integration/Owncloud/OcisPhpSdk/ResourceInviteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,37 +77,24 @@ public function setUp(): void

public function testInviteUser(): void
{
$shares = $this->fileToShare->invite([$this->einstein], $this->viewerRole);
$this->assertCount(1, $shares);
$this->assertNull($shares[0]->getExpiration());
$share = $this->fileToShare->invite($this->einstein, $this->viewerRole);
$this->assertNull($share->getExpiration());
$this->assertSame($this->fileToShare->getId(), $share->getResourceId());
$receivedShares = $this->einsteinOcis->getSharedWithMe();
$this->assertCount(1, $receivedShares);
$this->assertSame($this->fileToShare->getName(), $receivedShares[0]->getName());
}

public function testInviteAnotherUser(): void
{
$this->fileToShare->invite([$this->einstein], $this->viewerRole);
$shares = $this->fileToShare->invite([$this->marie], $this->viewerRole);
$this->assertCount(1, $shares);
$this->fileToShare->invite($this->einstein, $this->viewerRole);
$share = $this->fileToShare->invite($this->marie, $this->viewerRole);
$this->assertSame($this->fileToShare->getId(), $share->getResourceId());
$receivedShares = $this->marieOcis->getSharedWithMe();
$this->assertCount(1, $receivedShares);
$this->assertSame($this->fileToShare->getName(), $receivedShares[0]->getName());
}

public function testInviteMultipleUsersAtOnce(): void
{
$shares = $this->fileToShare->invite([$this->einstein,$this->marie], $this->viewerRole);
$this->assertCount(2, $shares);
$receivedShares = $this->marieOcis->getSharedWithMe();
$this->assertCount(1, $receivedShares);
$this->assertSame($this->fileToShare->getName(), $receivedShares[0]->getName());

$receivedShares = $this->einsteinOcis->getSharedWithMe();
$this->assertCount(1, $receivedShares);
$this->assertSame($this->fileToShare->getName(), $receivedShares[0]->getName());
}

public function testInviteGroup(): void
{
$philosophyHatersGroup = $this->ocis->createGroup(
Expand All @@ -116,78 +103,37 @@ public function testInviteGroup(): void
);
$this->createdGroups = [$philosophyHatersGroup];
$philosophyHatersGroup->addUser($this->einstein);
$shares = $this->fileToShare->invite([$philosophyHatersGroup], $this->viewerRole);
$this->assertCount(1, $shares);
$share = $this->fileToShare->invite($philosophyHatersGroup, $this->viewerRole);
$this->assertSame($this->fileToShare->getId(), $share->getResourceId());
$receivedShares = $this->einsteinOcis->getSharedWithMe();
$this->assertCount(1, $receivedShares);
$this->assertSame($this->fileToShare->getName(), $receivedShares[0]->getName());
}

public function testInviteGroupAndUserOfTheGroup(): void
{
$philosophyHatersGroup = $this->ocis->createGroup(
'philosophyhaters',
'philosophy haters group'
);
$this->createdGroups = [$philosophyHatersGroup];
$philosophyHatersGroup->addUser($this->einstein);
$shares = $this->fileToShare->invite([$philosophyHatersGroup, $this->einstein], $this->viewerRole);
$this->assertCount(2, $shares);
$receivedShares = $this->einsteinOcis->getSharedWithMe();
$this->assertCount(2, $receivedShares);
$this->assertSame($this->fileToShare->getName(), $receivedShares[0]->getName());
$this->assertSame($this->fileToShare->getName(), $receivedShares[1]->getName());
}

public function testInviteMultipleGroups(): void
{
$philosophyHatersGroup = $this->ocis->createGroup(
'philosophyhaters',
'philosophy haters group'
);
$physicsLoversGroup = $this->ocis->createGroup(
'physicslovers',
'physics lovers group'
);
$this->createdGroups = [$philosophyHatersGroup, $physicsLoversGroup];
$philosophyHatersGroup->addUser($this->einstein);
$physicsLoversGroup->addUser($this->einstein);
$physicsLoversGroup->addUser($this->marie);
$shares = $this->fileToShare->invite([$physicsLoversGroup, $philosophyHatersGroup], $this->viewerRole);
$this->assertCount(2, $shares);
$receivedShares = $this->einsteinOcis->getSharedWithMe();
$this->assertCount(2, $receivedShares);
$this->assertSame($this->fileToShare->getName(), $receivedShares[0]->getName());
$this->assertSame($this->fileToShare->getName(), $receivedShares[1]->getName());

$receivedShares = $this->marieOcis->getSharedWithMe();
$this->assertCount(1, $receivedShares);
$this->assertSame($this->fileToShare->getName(), $receivedShares[0]->getName());
}

public function testInviteSameUserAgain(): void
{
$this->markTestSkipped('https://github.com/owncloud/ocis/issues/7842');
// @phpstan-ignore-next-line because the test is skipped
$this->expectException(ForbiddenException::class);
$this->fileToShare->invite([$this->einstein], $this->viewerRole);
$this->fileToShare->invite([$this->einstein], $this->viewerRole);
$this->fileToShare->invite($this->einstein, $this->viewerRole);
$this->fileToShare->invite($this->einstein, $this->viewerRole);
}

public function testInviteSameUserAgainWithDifferentRole(): void
{
$this->markTestSkipped('https://github.com/owncloud/ocis/issues/7842');
// @phpstan-ignore-next-line because the test is skipped
$this->expectException(ForbiddenException::class);
$this->fileToShare->invite([$this->einstein], $this->viewerRole);
$this->fileToShare->invite([$this->einstein], $this->managerRole);
$this->fileToShare->invite($this->einstein, $this->viewerRole);
$this->fileToShare->invite($this->einstein, $this->managerRole);
}

public function testInviteWithExpiry(): void
{
$tomorrow = new \DateTimeImmutable('tomorrow');
$shares = $this->fileToShare->invite([$this->einstein], $this->viewerRole, $tomorrow);
$this->assertCount(1, $shares);
$share = $this->fileToShare->invite($this->einstein, $this->viewerRole, $tomorrow);
$this->assertInstanceOf(\DateTimeImmutable::class, $share->getExpiration());
$this->assertSame($tomorrow->getTimestamp(), $share->getExpiration()->getTimestamp());
$createdShares = $this->ocis->getSharedByMe();
$this->assertCount(1, $createdShares);
$this->assertInstanceOf(\DateTimeImmutable::class, $createdShares[0]->getExpiration());
Expand All @@ -198,14 +144,17 @@ public function testInviteWithPastExpiry(): void
{
$this->expectException(BadRequestException::class);
$yesterday = new \DateTimeImmutable('yesterday');
$this->fileToShare->invite([$this->einstein], $this->viewerRole, $yesterday);
$this->fileToShare->invite($this->einstein, $this->viewerRole, $yesterday);
}

public function testInviteWithExpiryTimezone(): void
{
$expiry = new \DateTimeImmutable('2060-01-01 12:00:00', new \DateTimeZone('Europe/Kyiv'));
$shares = $this->fileToShare->invite([$this->marie], $this->viewerRole, $expiry);
$this->assertCount(1, $shares);
$share = $this->fileToShare->invite($this->marie, $this->viewerRole, $expiry);
$this->assertInstanceOf(\DateTimeImmutable::class, $share->getExpiration());
// The returned expiry is in UTC timezone (2 hours earlier than the expiry time in Kyiv)
$this->assertSame("Thu, 01 Jan 2060 10:00:00 +0000", $share->getExpiration()->format('r'));
$this->assertSame("Z", $share->getExpiration()->getTimezone()->getName());
$createdShares = $this->ocis->getSharedByMe();
$this->assertCount(1, $createdShares);
$this->assertInstanceOf(\DateTimeImmutable::class, $createdShares[0]->getExpiration());
Expand All @@ -222,21 +171,13 @@ public function testGetReceiversOfShareCreatedByInvite(): void
);
$this->createdGroups = [$philosophyHatersGroup];
$philosophyHatersGroup->addUser($this->einstein);
$shares = $this->fileToShare->invite(
[$this->einstein, $this->marie, $philosophyHatersGroup],
$this->viewerRole
);
$this->assertCount(3, $shares);
for($i = 0; $i < 3; $i++) {
$this->assertThat(
$shares[$i]->getReceiver()->getDisplayName(),
$this->logicalOr(
$this->equalTo("philosophyhaters"),
$this->equalTo("Marie Curie"),
$this->equalTo("Albert Einstein")
)
);
}
$shares = [];
$shares[] = $this->fileToShare->invite($this->einstein, $this->viewerRole);
$shares[] = $this->fileToShare->invite($this->marie, $this->viewerRole);
$shares[] = $this->fileToShare->invite($philosophyHatersGroup, $this->viewerRole);
$this->assertSame("Albert Einstein", $shares[0]->getReceiver()->getDisplayName());
$this->assertSame("Marie Curie", $shares[1]->getReceiver()->getDisplayName());
$this->assertSame("philosophyhaters", $shares[2]->getReceiver()->getDisplayName());
}

public function testGetReceiversOfShareReturnedBySharedByMe(): void
Expand All @@ -247,14 +188,12 @@ public function testGetReceiversOfShareReturnedBySharedByMe(): void
);
$this->createdGroups = [$philosophyHatersGroup];
$philosophyHatersGroup->addUser($this->einstein);
$this->fileToShare->invite(
[$this->einstein, $this->marie, $philosophyHatersGroup],
$this->viewerRole
);
$this->folderToShare->invite(
[$this->einstein, $this->marie, $philosophyHatersGroup],
$this->viewerRole
);
$this->fileToShare->invite($this->einstein, $this->viewerRole);
$this->fileToShare->invite($this->marie, $this->viewerRole);
$this->fileToShare->invite($philosophyHatersGroup, $this->viewerRole);
$this->folderToShare->invite($this->einstein, $this->viewerRole);
$this->folderToShare->invite($this->marie, $this->viewerRole);
$this->folderToShare->invite($philosophyHatersGroup, $this->viewerRole);
$shares = $this->ocis->getSharedByMe();
$this->assertCount(6, $shares);
for($i = 0; $i < 6; $i++) {
Expand All @@ -281,13 +220,13 @@ public function testGetReceiversOfShareReturnedBySharedByMe(): void

public function testInviteUserToAReceivedShare(): void
{
$this->fileToShare->invite([$this->einstein], $this->managerRole);
$this->fileToShare->invite($this->einstein, $this->managerRole);
/**
* @var ShareReceived $receivedShare
*/
$receivedShare = $this->einsteinOcis->getSharedWithMe()[0];
$resource = $this->einsteinOcis->getResourceById($receivedShare->getRemoteItemId());
$resource->invite([$this->marie], $this->viewerRole);
$resource->invite($this->marie, $this->viewerRole);
/**
* @var ShareReceived $receivedShare
*/
Expand Down
Loading