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
16 changes: 14 additions & 2 deletions lib/Providers/DAV/Calendar/Hybrid/EventCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,13 @@ public function childExists($id): bool {
$listFilter->condition('uuid', $id, FilterComparisonOperator::EQ);
// retrieve object properties
$entities = $this->localService->entityList($listFilter);
// fall back to a lookup by remote entity id
if (count($entities) === 0 && $this->collection->remoteId !== null) {
$listFilter = $this->localService->entityListFilter();
$listFilter->condition('cid', $this->collection->localId);
$listFilter->condition('ceid', $this->collection->remoteId . $id, FilterComparisonOperator::EQ);
$entities = $this->localService->entityList($listFilter);
}
return count($entities) > 0;
}

Expand Down Expand Up @@ -362,14 +369,19 @@ public function getMultipleChildren(array $ids): array {
* @return EventEntity|false
*/
public function getChild($id): EventEntity|false {
// remove extension
$id = str_replace('.ics', '', $id);
// construct filter
$listFilter = $this->localService->entityListFilter();
$listFilter->condition('cid', $this->collection->localId);
$listFilter->condition('uuid', $id);
// retrieve object properties
$entities = $this->localService->entityList($listFilter);
// fall back to a lookup by remote entity id
if (count($entities) === 0 && $this->collection->remoteId !== null) {
$listFilter = $this->localService->entityListFilter();
$listFilter->condition('cid', $this->collection->localId);
$listFilter->condition('ceid', $this->collection->remoteId . $id, FilterComparisonOperator::EQ);
$entities = $this->localService->entityList($listFilter);
}
// evaluate if object properties where retrieved
if (count($entities) > 0) {
return new EventEntity($this, $entities[0]);
Expand Down
14 changes: 14 additions & 0 deletions lib/Providers/DAV/Contacts/Hybrid/ContactCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,13 @@ public function childExists($id): bool {
$listFilter->condition('uuid', $id, FilterComparisonOperator::EQ);
// retrieve object properties
$entities = $this->localService->entityList($listFilter);
// fall back to a lookup by remote entity id
if (count($entities) === 0 && $this->collection->remoteId !== null) {
$listFilter = $this->localService->entityListFilter();
$listFilter->condition('cid', $this->collection->localId);
$listFilter->condition('ceid', $this->collection->remoteId . $id, FilterComparisonOperator::EQ);
$entities = $this->localService->entityList($listFilter);
}
return count($entities) > 0;
}

Expand Down Expand Up @@ -306,6 +313,13 @@ public function getChild($id): ContactEntity|false {
$listFilter->condition('uuid', $id, FilterComparisonOperator::EQ);
// retrieve object properties
$entities = $this->localService->entityList($listFilter);
// fall back to a lookup by remote entity id
if (count($entities) === 0 && $this->collection->remoteId !== null) {
$listFilter = $this->localService->entityListFilter();
$listFilter->condition('cid', $this->collection->localId);
$listFilter->condition('ceid', $this->collection->remoteId . $id, FilterComparisonOperator::EQ);
$entities = $this->localService->entityList($listFilter);
}
// evaluate if object properties where retrieved
if (count($entities) > 0) {
return new ContactEntity($this, $entities[0]);
Expand Down
3 changes: 3 additions & 0 deletions lib/Service/Remote/RemoteContactsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,9 @@ public function entityCreate(Entity $so): ?Entity {
$result = $this->dataStore->create($path, $data, 'application/vcard');

$ro = clone $so;
// persist the full resource path so the stored identifier matches what a
// subsequent pull from the remote server would report for this entity
$ro->remoteEntityId = $path;
$ro->remoteSignature = $result['etag'] ?? null;

return $ro;
Expand Down
3 changes: 3 additions & 0 deletions lib/Service/Remote/RemoteEventsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,9 @@ public function entityCreate(Entity $so): ?Entity {
$result = $this->dataStore->create($path, $data, 'application/vcalendar');

$ro = clone $so;
// persist the full resource path so the stored identifier matches what a
// subsequent pull from the remote server would report for this entity
$ro->remoteEntityId = $path;
$ro->remoteSignature = $result['etag'] ?? null;

return $ro;
Expand Down
2 changes: 1 addition & 1 deletion lib/Store/Local/Filters/ContactFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class ContactFilter extends FilterBase {
'uid' => true,
'sid' => true,
'cid' => true,
'uid' => true,
'uuid' => true,
'ceid' => true,
'label' => true,
];

Expand Down
2 changes: 1 addition & 1 deletion lib/Store/Local/Filters/EventFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class EventFilter extends FilterBase {
'uid' => true,
'sid' => true,
'cid' => true,
'uid' => true,
'uuid' => true,
'ceid' => true,
'startson' => true,
'endson' => true,
'label' => true,
Expand Down
3 changes: 2 additions & 1 deletion tests/php/dav/Remote/RemoteContactsServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,8 @@ public function testEntityCreate(): void {

$this->assertInstanceOf(Entity::class, $createdEntity);
$this->assertSame($entity->remoteCollectionId, $createdEntity->remoteCollectionId);
$this->assertSame($entity->remoteEntityId, $createdEntity->remoteEntityId);
// the created entity carries the full resource path as its remote id
$this->assertSame($resourcePath, $createdEntity->remoteEntityId);
$this->assertSame($payload, $createdEntity->data);
$this->assertIsString($createdEntity->remoteSignature);
$this->assertNotSame('', $createdEntity->remoteSignature);
Expand Down
3 changes: 2 additions & 1 deletion tests/php/dav/Remote/RemoteEventsServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,8 @@ public function testEntityCreate(): void {

$this->assertInstanceOf(Entity::class, $createdEntity);
$this->assertSame($entity->remoteCollectionId, $createdEntity->remoteCollectionId);
$this->assertSame($entity->remoteEntityId, $createdEntity->remoteEntityId);
// the created entity carries the full resource path as its remote id
$this->assertSame($resourcePath, $createdEntity->remoteEntityId);
$this->assertSame($payload, $createdEntity->data);
$this->assertIsString($createdEntity->remoteSignature);
$this->assertNotSame('', $createdEntity->remoteSignature);
Expand Down
150 changes: 150 additions & 0 deletions tests/php/unit/Providers/DAV/Calendar/Hybrid/EventCollectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\DAVC\Tests\Unit\Providers\DAV\Calendar\Hybrid;

use OCA\DAVC\Models\Calendars\Collection;
use OCA\DAVC\Models\Calendars\Entity;
use OCA\DAVC\Providers\DAV\Calendar\Hybrid\EventCollection;
use OCA\DAVC\Providers\DAV\Calendar\Hybrid\EventEntity;
use OCA\DAVC\Service\Local\LocalEventsService;
use OCA\DAVC\Service\Remote\RemoteFactory;
use OCA\DAVC\Store\Local\Filters\EventFilter;
use OCA\DAVC\Store\Local\ServicesStore;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class EventCollectionTest extends TestCase {
private ServicesStore&MockObject $servicesStore;
private LocalEventsService&MockObject $localService;
private RemoteFactory&MockObject $remoteFactory;
private Collection $collection;
private EventCollection $sut;

protected function setUp(): void {
parent::setUp();

$this->servicesStore = $this->createMock(ServicesStore::class);
$this->localService = $this->createMock(LocalEventsService::class);
$this->remoteFactory = $this->createMock(RemoteFactory::class);

$this->collection = new Collection();
$this->collection->userId = 'user1';
$this->collection->serviceId = 1;
$this->collection->localId = 42;
$this->collection->uuid = 'collection-uuid';
$this->collection->remoteId = '/remote/Calendar/';

$this->sut = new EventCollection(
$this->servicesStore,
$this->localService,
$this->remoteFactory,
$this->collection,
);

$this->localService->method('entityListFilter')
->willReturnCallback(static fn (): EventFilter => new EventFilter());
}

/**
* extract the value of a given attribute from a filter
*/
private function conditionValue(EventFilter $filter, string $attribute): mixed {
foreach ($filter->conditions() as $condition) {
if ($condition['attribute'] === $attribute) {
return $condition['value'];
}
}
return null;
}

public function testGetChildByUuid(): void {
$entity = new Entity();
$entity->uuid = 'entity-uuid';

$this->localService->expects($this->once())
->method('entityList')
->willReturnCallback(function (EventFilter $filter) use ($entity): array {
// the .ics extension is stripped before the uuid lookup
$this->assertSame('entity-uuid', $this->conditionValue($filter, 'uuid'));
return [$entity];
});

$child = $this->sut->getChild('entity-uuid.ics');

$this->assertInstanceOf(EventEntity::class, $child);
$this->assertSame('entity-uuid', $child->getName());
}

public function testGetChildFallsBackToRemoteEntityId(): void {
$entity = new Entity();
$entity->uuid = 'entity-uuid';
$entity->remoteEntityId = '/remote/Calendar/submitted-id.ics';

$this->localService->expects($this->exactly(2))
->method('entityList')
->willReturnCallback(function (EventFilter $filter) use ($entity): array {
// uuid lookup yields nothing, ceid lookup resolves the entity by
// the reconstructed full resource path (extension preserved)
if ($this->conditionValue($filter, 'uuid') === 'submitted-id') {
return [];
}
$this->assertSame('/remote/Calendar/submitted-id.ics', $this->conditionValue($filter, 'ceid'));
return [$entity];
});

$child = $this->sut->getChild('submitted-id.ics');

$this->assertInstanceOf(EventEntity::class, $child);
$this->assertSame('entity-uuid', $child->getName());
}

public function testGetChildThrowsWhenMissing(): void {
$this->localService->expects($this->exactly(2))
->method('entityList')
->willReturn([]);

$this->expectException(\Sabre\DAV\Exception\NotFound::class);

$this->sut->getChild('unknown.ics');
}

public function testChildExistsByUuid(): void {
$this->localService->expects($this->once())
->method('entityList')
->willReturnCallback(function (EventFilter $filter): array {
$this->assertSame('entity-uuid', $this->conditionValue($filter, 'uuid'));
return [new Entity()];
});

$this->assertTrue($this->sut->childExists('entity-uuid.ics'));
}

public function testChildExistsFallsBackToRemoteEntityId(): void {
$this->localService->expects($this->exactly(2))
->method('entityList')
->willReturnCallback(function (EventFilter $filter): array {
if ($this->conditionValue($filter, 'uuid') === 'submitted-id') {
return [];
}
$this->assertSame('/remote/Calendar/submitted-id.ics', $this->conditionValue($filter, 'ceid'));
return [new Entity()];
});

$this->assertTrue($this->sut->childExists('submitted-id.ics'));
}

public function testChildExistsReturnsFalseWhenMissing(): void {
$this->localService->expects($this->exactly(2))
->method('entityList')
->willReturn([]);

$this->assertFalse($this->sut->childExists('unknown.ics'));
}
}
Loading
Loading