-
Notifications
You must be signed in to change notification settings - Fork 7
fix(activesync): stabilize folder UIDs after hierarchy changes #81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
TDannhauer
merged 4 commits into
FRAMEWORK_6_0
from
fix/deterministic-folder-uids-moveitems-gone
Jul 9, 2026
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e719488
fix(activesync): stabilize folder UIDs after hierarchy changes
TDannhauer 8b9d15f
Potential fix for pull request finding
TDannhauer f399f3a
Potential fix for pull request finding
TDannhauer 1580dd7
Potential fix for pull request finding
TDannhauer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,99 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Unit tests for deterministic EAS folder UID generation. | ||
| * | ||
| * @author Torben Dannhauer <torben@dannhauer.de> | ||
| * @license http://www.horde.org/licenses/gpl GPLv2 | ||
| * @copyright 2026 The Horde Project (http://www.horde.org/) | ||
| * @package ActiveSync | ||
| */ | ||
|
|
||
| namespace Horde\ActiveSync; | ||
|
|
||
| use Horde_ActiveSync; | ||
| use Horde_ActiveSync_Driver_Base; | ||
| use Horde_ActiveSync_State_Base; | ||
| use PHPUnit\Framework\Attributes\CoversNothing; | ||
| use PHPUnit\Framework\TestCase; | ||
| use ReflectionMethod; | ||
|
|
||
| #[CoversNothing] | ||
| class FolderUidDeterministicTest extends TestCase | ||
| { | ||
| public function testFolderUidIsDeterministicForRepeatedLookups() | ||
| { | ||
| $state = $this->createMock(Horde_ActiveSync_State_Base::class); | ||
| $state->method('getFolderUidToBackendIdMap')->willReturn([]); | ||
|
|
||
| $driver = $this->getMockBuilder(Horde_ActiveSync_Driver_Base::class) | ||
| ->setConstructorArgs([['state' => $state]]) | ||
| ->getMockForAbstractClass(); | ||
|
|
||
| $method = new ReflectionMethod(Horde_ActiveSync_Driver_Base::class, '_getFolderUidForBackendId'); | ||
| $method->setAccessible(true); | ||
|
|
||
| $inbox = $method->invoke($driver, 'INBOX', Horde_ActiveSync::FOLDER_TYPE_INBOX); | ||
| $again = $method->invoke($driver, 'INBOX', Horde_ActiveSync::FOLDER_TYPE_INBOX); | ||
|
|
||
| $this->assertSame($inbox, $again); | ||
| $this->assertSame( | ||
| 'F' . sprintf('%08x', crc32('F:' . 'INBOX')), | ||
| $inbox | ||
| ); | ||
|
Copilot marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| public function testFolderUidIsDeterministicAcrossDriverInstances() | ||
| { | ||
| $state = $this->createMock(Horde_ActiveSync_State_Base::class); | ||
| $state->method('getFolderUidToBackendIdMap')->willReturn([]); | ||
|
|
||
| $method = new ReflectionMethod(Horde_ActiveSync_Driver_Base::class, '_getFolderUidForBackendId'); | ||
| $method->setAccessible(true); | ||
|
|
||
| $driverA = $this->getMockBuilder(Horde_ActiveSync_Driver_Base::class) | ||
| ->setConstructorArgs([['state' => $state]]) | ||
| ->getMockForAbstractClass(); | ||
| $driverB = $this->getMockBuilder(Horde_ActiveSync_Driver_Base::class) | ||
| ->setConstructorArgs([['state' => $state]]) | ||
| ->getMockForAbstractClass(); | ||
|
|
||
| $contactBackendId = Horde_ActiveSync::CLASS_CONTACTS . ':42'; | ||
| $uidA = $method->invoke( | ||
| $driverA, | ||
| $contactBackendId, | ||
| Horde_ActiveSync::FOLDER_TYPE_USER_CONTACT | ||
| ); | ||
| $uidB = $method->invoke( | ||
| $driverB, | ||
| $contactBackendId, | ||
| Horde_ActiveSync::FOLDER_TYPE_USER_CONTACT | ||
| ); | ||
|
|
||
| $this->assertSame($uidA, $uidB); | ||
| $this->assertSame( | ||
| 'C' . sprintf('%08x', crc32('C:' . $contactBackendId)), | ||
| $uidA | ||
| ); | ||
|
Copilot marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| public function testFolderUidUsesPersistedMapWhenPresent() | ||
| { | ||
| $state = $this->createMock(Horde_ActiveSync_State_Base::class); | ||
| $state->method('getFolderUidToBackendIdMap')->willReturn([ | ||
| 'INBOX' => 'F85953279', | ||
| ]); | ||
|
|
||
| $driver = $this->getMockBuilder(Horde_ActiveSync_Driver_Base::class) | ||
| ->setConstructorArgs([['state' => $state]]) | ||
| ->getMockForAbstractClass(); | ||
|
|
||
| $method = new ReflectionMethod(Horde_ActiveSync_Driver_Base::class, '_getFolderUidForBackendId'); | ||
| $method->setAccessible(true); | ||
|
|
||
| $this->assertSame( | ||
| 'F85953279', | ||
| $method->invoke($driver, 'INBOX', Horde_ActiveSync::FOLDER_TYPE_INBOX) | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or 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,224 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Unit tests for MoveItems handling of stale folder UIDs. | ||
| * | ||
| * @author Torben Dannhauer <torben@dannhauer.de> | ||
| * @license http://www.horde.org/licenses/gpl GPLv2 | ||
| * @copyright 2026 The Horde Project (http://www.horde.org/) | ||
| * @package ActiveSync | ||
| */ | ||
|
|
||
| namespace Horde\ActiveSync; | ||
|
|
||
| use Horde_ActiveSync; | ||
| use Horde_ActiveSync_Connector_Importer; | ||
| use Horde_ActiveSync_Driver_Base; | ||
| use Horde_ActiveSync_Exception_FolderGone; | ||
| use Horde_ActiveSync_Log_Logger; | ||
| use Horde_ActiveSync_Request_MoveItems; | ||
| use Horde_ActiveSync_State_Base; | ||
| use Horde_ActiveSync_Wbxml; | ||
| use Horde_ActiveSync_Wbxml_Decoder; | ||
| use Horde_ActiveSync_Wbxml_Encoder; | ||
| use Horde_Controller_Request_Mock; | ||
| use Horde_Log_Handler_Null; | ||
| use PHPUnit\Framework\Attributes\CoversClass; | ||
| use PHPUnit\Framework\TestCase; | ||
| use ReflectionMethod; | ||
| use ReflectionProperty; | ||
|
|
||
| #[CoversClass(Horde_ActiveSync_Request_MoveItems::class)] | ||
| class MoveItemsFolderGoneTest extends TestCase | ||
| { | ||
| protected function tearDown(): void | ||
| { | ||
| $deviceRef = new ReflectionProperty(Horde_ActiveSync::class, '_device'); | ||
| $deviceRef->setAccessible(true); | ||
| $deviceRef->setValue(null, null); | ||
| parent::tearDown(); | ||
| } | ||
|
|
||
| public function testMoveItemsReturnsInvalidSrcWhenSourceFolderIsGone() | ||
| { | ||
| $fixture = $this->_createHandler(); | ||
| $fixture->importer->expects($this->once()) | ||
| ->method('init') | ||
| ->willThrowException(new Horde_ActiveSync_Exception_FolderGone('Folder not found in cache.')); | ||
|
|
||
| $this->_writeWbxml($fixture->input, function (Horde_ActiveSync_Wbxml_Encoder $encoder) { | ||
| $encoder->startTag(Horde_ActiveSync_Request_MoveItems::MOVES); | ||
| $encoder->startTag(Horde_ActiveSync_Request_MoveItems::MOVE); | ||
| $encoder->startTag(Horde_ActiveSync_Request_MoveItems::SRCMSGID); | ||
| $encoder->content('169462'); | ||
| $encoder->endTag(); | ||
| $encoder->startTag(Horde_ActiveSync_Request_MoveItems::SRCFLDID); | ||
| $encoder->content('F85953279'); | ||
| $encoder->endTag(); | ||
| $encoder->startTag(Horde_ActiveSync_Request_MoveItems::DSTFLDID); | ||
| $encoder->content('F37c56728'); | ||
| $encoder->endTag(); | ||
| $encoder->endTag(); | ||
| $encoder->endTag(); | ||
| }); | ||
| $fixture->decoder->readWbxmlHeader(); | ||
|
|
||
| $method = new ReflectionMethod(Horde_ActiveSync_Request_MoveItems::class, '_handle'); | ||
| $method->setAccessible(true); | ||
| $this->assertTrue($method->invoke($fixture->handler)); | ||
|
|
||
| $response = $this->_decodeMoveItemsResponse( | ||
| $this->_readStream($fixture->output) | ||
| ); | ||
| $this->assertSame(Horde_ActiveSync_Request_MoveItems::STATUS_INVALID_SRC, $response['status']); | ||
| $this->assertSame('169462', $response['srcMsgId']); | ||
| } | ||
|
|
||
| public function testMoveItemsReturnsInvalidSrcWhenDestinationFolderIsGone() | ||
| { | ||
| $fixture = $this->_createHandler(); | ||
| $fixture->importer->expects($this->once())->method('init'); | ||
| $fixture->importer->expects($this->once()) | ||
| ->method('importMessageMove') | ||
| ->willThrowException(new Horde_ActiveSync_Exception_FolderGone('Folder not found in cache.')); | ||
|
|
||
| $this->_writeWbxml($fixture->input, function (Horde_ActiveSync_Wbxml_Encoder $encoder) { | ||
| $encoder->startTag(Horde_ActiveSync_Request_MoveItems::MOVES); | ||
| $encoder->startTag(Horde_ActiveSync_Request_MoveItems::MOVE); | ||
| $encoder->startTag(Horde_ActiveSync_Request_MoveItems::SRCMSGID); | ||
| $encoder->content('169462'); | ||
| $encoder->endTag(); | ||
| $encoder->startTag(Horde_ActiveSync_Request_MoveItems::SRCFLDID); | ||
| $encoder->content('F85953279'); | ||
| $encoder->endTag(); | ||
| $encoder->startTag(Horde_ActiveSync_Request_MoveItems::DSTFLDID); | ||
| $encoder->content('F37c56728'); | ||
| $encoder->endTag(); | ||
| $encoder->endTag(); | ||
| $encoder->endTag(); | ||
| }); | ||
| $fixture->decoder->readWbxmlHeader(); | ||
|
|
||
| $method = new ReflectionMethod(Horde_ActiveSync_Request_MoveItems::class, '_handle'); | ||
| $method->setAccessible(true); | ||
| $this->assertTrue($method->invoke($fixture->handler)); | ||
|
|
||
| $response = $this->_decodeMoveItemsResponse( | ||
| $this->_readStream($fixture->output) | ||
| ); | ||
| $this->assertSame(Horde_ActiveSync_Request_MoveItems::STATUS_INVALID_SRC, $response['status']); | ||
| } | ||
|
|
||
| /** | ||
| * @return object{ | ||
| * handler: Horde_ActiveSync_Request_MoveItems, | ||
| * input: resource, | ||
| * output: resource, | ||
| * importer: Horde_ActiveSync_Connector_Importer&\PHPUnit\Framework\MockObject\MockObject, | ||
| * state: Horde_ActiveSync_State_Base&\PHPUnit\Framework\MockObject\MockObject | ||
| * } | ||
| */ | ||
| protected function _createHandler(): object | ||
| { | ||
| $logger = new Horde_ActiveSync_Log_Logger(new Horde_Log_Handler_Null()); | ||
| $input = fopen('php://memory', 'wb+'); | ||
| $decoder = new Horde_ActiveSync_Wbxml_Decoder($input); | ||
|
|
||
| $output = fopen('php://memory', 'wb+'); | ||
| $encoder = new Horde_ActiveSync_Wbxml_Encoder($output); | ||
|
|
||
| $driver = $this->createMock(Horde_ActiveSync_Driver_Base::class); | ||
| $state = $this->createMock(Horde_ActiveSync_State_Base::class); | ||
| $importer = $this->createMock(Horde_ActiveSync_Connector_Importer::class); | ||
|
|
||
| $request = new Horde_Controller_Request_Mock([ | ||
| 'get' => [ | ||
| 'Cmd' => 'MoveItems', | ||
| 'DeviceId' => 'TESTDEVICE', | ||
| ], | ||
| 'server' => [ | ||
| 'HTTP_MS_ASPROTOCOLVERSION' => Horde_ActiveSync::VERSION_SIXTEENONE, | ||
| ], | ||
| ]); | ||
|
|
||
| $server = $this->getMockBuilder(Horde_ActiveSync::class) | ||
| ->setConstructorArgs([$driver, $decoder, $encoder, $state, $request]) | ||
| ->onlyMethods(['getImporter']) | ||
| ->getMock(); | ||
| $server->method('getImporter')->willReturn($importer); | ||
|
|
||
| $device = new \Horde_ActiveSync_Device($state); | ||
| $device->id = 'TESTDEVICE'; | ||
| $device->version = Horde_ActiveSync::VERSION_SIXTEENONE; | ||
| $deviceRef = new ReflectionProperty(Horde_ActiveSync::class, '_device'); | ||
| $deviceRef->setAccessible(true); | ||
| $deviceRef->setValue(null, $device); | ||
|
|
||
| $handler = new Horde_ActiveSync_Request_MoveItems($server); | ||
| $handler->setLogger($logger); | ||
|
|
||
| return (object) [ | ||
| 'handler' => $handler, | ||
| 'decoder' => $decoder, | ||
| 'input' => $input, | ||
| 'output' => $output, | ||
| 'importer' => $importer, | ||
| 'state' => $state, | ||
| ]; | ||
| } | ||
|
|
||
| protected function _writeWbxml($stream, callable $writer): void | ||
| { | ||
| $encoder = new Horde_ActiveSync_Wbxml_Encoder($stream); | ||
| $encoder->startWBXML(); | ||
| $writer($encoder); | ||
| rewind($stream); | ||
| } | ||
|
|
||
| /** | ||
| * @return array{status: ?int, srcMsgId: ?string} | ||
| */ | ||
| protected function _decodeMoveItemsResponse(string $wbxml): array | ||
| { | ||
| $stream = fopen('php://memory', 'wb+'); | ||
| fwrite($stream, $wbxml); | ||
| rewind($stream); | ||
|
|
||
| $decoder = new Horde_ActiveSync_Wbxml_Decoder($stream); | ||
| $decoder->readWbxmlHeader(); | ||
| $decoder->getElementStartTag(Horde_ActiveSync_Request_MoveItems::MOVES); | ||
| $decoder->getElementStartTag(Horde_ActiveSync_Request_MoveItems::RESPONSE); | ||
|
|
||
| $response = [ | ||
| 'status' => null, | ||
| 'srcMsgId' => null, | ||
| ]; | ||
|
|
||
| while (($child = $decoder->peek()) !== false | ||
| && $child[Horde_ActiveSync_Wbxml::EN_TYPE] === Horde_ActiveSync_Wbxml::EN_TYPE_STARTTAG | ||
| ) { | ||
| $tag = $child[Horde_ActiveSync_Wbxml::EN_TAG]; | ||
| $decoder->getElementStartTag($tag); | ||
| $content = $decoder->getElementContent(); | ||
| $decoder->getElementEndTag(); | ||
|
|
||
| if ($tag === Horde_ActiveSync_Request_MoveItems::STATUS) { | ||
| $response['status'] = (int) $content; | ||
| } elseif ($tag === Horde_ActiveSync_Request_MoveItems::SRCMSGID) { | ||
| $response['srcMsgId'] = $content; | ||
| } | ||
| } | ||
|
|
||
| $decoder->getElementEndTag(); | ||
| $decoder->getElementEndTag(); | ||
| fclose($stream); | ||
|
|
||
| return $response; | ||
| } | ||
|
|
||
| protected function _readStream($stream): string | ||
| { | ||
| rewind($stream); | ||
| return stream_get_contents($stream); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.