|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 6 | + * SPDX-License-Identifier: AGPL-3.0-only |
| 7 | + */ |
| 8 | + |
| 9 | +namespace OCA\DAV\Tests\unit\Connector\Sabre; |
| 10 | + |
| 11 | +use OCA\DAV\Connector\Sabre\Exception\FileLocked; |
| 12 | +use OCA\DAV\Connector\Sabre\SerializeMoveCopyPlugin; |
| 13 | +use OCP\IConfig; |
| 14 | +use OCP\Lock\ILockingProvider; |
| 15 | +use OCP\Lock\LockedException; |
| 16 | +use PHPUnit\Framework\MockObject\MockObject; |
| 17 | +use Sabre\DAV\Server; |
| 18 | +use Sabre\HTTP\Request; |
| 19 | +use Sabre\HTTP\Response; |
| 20 | +use Test\TestCase; |
| 21 | + |
| 22 | +class SerializeMoveCopyPluginTest extends TestCase { |
| 23 | + private const LOCK_KEY_PREFIX = 'webdav-serialize:'; |
| 24 | + private const CONFIG_KEY = 'dav.serialize_move_copy'; |
| 25 | + |
| 26 | + private ILockingProvider&MockObject $lockingProvider; |
| 27 | + private IConfig&MockObject $config; |
| 28 | + private SerializeMoveCopyPlugin $plugin; |
| 29 | + |
| 30 | + protected function setUp(): void { |
| 31 | + parent::setUp(); |
| 32 | + $this->lockingProvider = $this->createMock(ILockingProvider::class); |
| 33 | + $this->config = $this->createMock(IConfig::class); |
| 34 | + $this->plugin = new SerializeMoveCopyPlugin($this->lockingProvider, $this->config); |
| 35 | + } |
| 36 | + |
| 37 | + private function toggle(bool $enabled): void { |
| 38 | + $this->config->method('getSystemValueBool')->with(self::CONFIG_KEY, false)->willReturn($enabled); |
| 39 | + } |
| 40 | + |
| 41 | + /** @param list<array{key: string, type: int}> $calls appended by the mock callback in call order */ |
| 42 | + private function captureAcquireCalls(array &$calls): void { |
| 43 | + $this->lockingProvider |
| 44 | + ->method('acquireLock') |
| 45 | + ->willReturnCallback(function (string $key, int $type) use (&$calls): void { |
| 46 | + $calls[] = ['key' => $key, 'type' => $type]; |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + public function testInitializeSubscribesToExpectedEvents(): void { |
| 51 | + $server = new Server(); |
| 52 | + $this->plugin->initialize($server); |
| 53 | + foreach (['beforeMove', 'beforeCopy', 'afterMethod:MOVE', 'afterMethod:COPY', 'exception'] as $event) { |
| 54 | + $this->assertNotEmpty($server->listeners($event), "no listener registered for $event"); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + public function testToggleOffIsNoop(): void { |
| 59 | + $this->toggle(false); |
| 60 | + $this->lockingProvider->expects($this->never())->method('acquireLock'); |
| 61 | + $this->assertTrue($this->plugin->beforeMove('files/a/src.txt', 'files/a/dst.txt')); |
| 62 | + $this->assertTrue($this->plugin->beforeCopy('files/a/src.txt', 'files/a/dst.txt')); |
| 63 | + } |
| 64 | + |
| 65 | + public function testMoveAcquiresExclusiveOnBothInSortedOrder(): void { |
| 66 | + $this->toggle(true); |
| 67 | + $calls = []; |
| 68 | + $this->captureAcquireCalls($calls); |
| 69 | + $this->plugin->beforeMove('files/a/1-src.txt', 'files/a/2-dst.txt'); |
| 70 | + $this->assertSame([ |
| 71 | + ['key' => self::LOCK_KEY_PREFIX . 'files/a/1-src.txt', 'type' => ILockingProvider::LOCK_EXCLUSIVE], |
| 72 | + ['key' => self::LOCK_KEY_PREFIX . 'files/a/2-dst.txt', 'type' => ILockingProvider::LOCK_EXCLUSIVE], |
| 73 | + ], $calls); |
| 74 | + } |
| 75 | + |
| 76 | + public function testCopyAcquiresSharedSourceAndExclusiveDestination(): void { |
| 77 | + $this->toggle(true); |
| 78 | + $calls = []; |
| 79 | + $this->captureAcquireCalls($calls); |
| 80 | + $this->plugin->beforeCopy('files/a/1-src.txt', 'files/a/2-dst.txt'); |
| 81 | + $this->assertSame([ |
| 82 | + ['key' => self::LOCK_KEY_PREFIX . 'files/a/1-src.txt', 'type' => ILockingProvider::LOCK_SHARED], |
| 83 | + ['key' => self::LOCK_KEY_PREFIX . 'files/a/2-dst.txt', 'type' => ILockingProvider::LOCK_EXCLUSIVE], |
| 84 | + ], $calls); |
| 85 | + } |
| 86 | + |
| 87 | + public function testAcquisitionOrderFollowsPathSortNotArgumentOrder(): void { |
| 88 | + // destination sorts before source alphabetically. The plugin MUST still lock destination first. |
| 89 | + $this->toggle(true); |
| 90 | + $calls = []; |
| 91 | + $this->captureAcquireCalls($calls); |
| 92 | + $this->plugin->beforeMove('files/a/z-src.txt', 'files/a/a-dst.txt'); |
| 93 | + $this->assertSame([ |
| 94 | + self::LOCK_KEY_PREFIX . 'files/a/a-dst.txt', |
| 95 | + self::LOCK_KEY_PREFIX . 'files/a/z-src.txt', |
| 96 | + ], array_column($calls, 'key')); |
| 97 | + } |
| 98 | + |
| 99 | + public function testSourceEqualsDestinationShortCircuits(): void { |
| 100 | + $this->toggle(true); |
| 101 | + $this->lockingProvider->expects($this->never())->method('acquireLock'); |
| 102 | + $this->assertTrue($this->plugin->beforeMove('files/a/src.txt', 'files/a/src.txt')); |
| 103 | + $this->assertTrue($this->plugin->beforeCopy('files/a/src.txt', 'files/a/src.txt')); |
| 104 | + } |
| 105 | + |
| 106 | + public function testLockedExceptionOnFirstLockMapsTo423(): void { |
| 107 | + $this->toggle(true); |
| 108 | + $this->lockingProvider->method('acquireLock') |
| 109 | + ->willThrowException(new LockedException('files/a/src.txt')); |
| 110 | + $this->lockingProvider->expects($this->never())->method('releaseLock'); |
| 111 | + |
| 112 | + $this->expectException(FileLocked::class); |
| 113 | + try { |
| 114 | + $this->plugin->beforeMove('files/a/src.txt', 'files/a/dst.txt'); |
| 115 | + } catch (FileLocked $e) { |
| 116 | + $this->assertSame(423, $e->getHTTPCode()); |
| 117 | + throw $e; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + public function testLockedExceptionOnSecondLockRollsBackFirst(): void { |
| 122 | + $this->toggle(true); |
| 123 | + $callCount = 0; |
| 124 | + $this->lockingProvider->expects($this->exactly(2)) |
| 125 | + ->method('acquireLock') |
| 126 | + ->willReturnCallback(function () use (&$callCount): void { |
| 127 | + $callCount++; |
| 128 | + if ($callCount === 2) { |
| 129 | + throw new LockedException('files/a/2-dst.txt'); |
| 130 | + } |
| 131 | + }); |
| 132 | + $this->lockingProvider->expects($this->once()) |
| 133 | + ->method('releaseLock') |
| 134 | + ->with(self::LOCK_KEY_PREFIX . 'files/a/1-src.txt', ILockingProvider::LOCK_EXCLUSIVE); |
| 135 | + |
| 136 | + $this->expectException(FileLocked::class); |
| 137 | + $this->plugin->beforeMove('files/a/1-src.txt', 'files/a/2-dst.txt'); |
| 138 | + } |
| 139 | + |
| 140 | + public function testAfterMethodReleasesAllHeldLocks(): void { |
| 141 | + $this->toggle(true); |
| 142 | + $this->lockingProvider->expects($this->exactly(2))->method('releaseLock'); |
| 143 | + $this->plugin->beforeMove('files/a/1-src.txt', 'files/a/2-dst.txt'); |
| 144 | + $this->plugin->afterMethod(new Request('MOVE', 'files/a/1-src.txt'), new Response()); |
| 145 | + } |
| 146 | + |
| 147 | + public function testOnExceptionReleasesAllHeldLocks(): void { |
| 148 | + $this->toggle(true); |
| 149 | + $this->lockingProvider->expects($this->exactly(2))->method('releaseLock'); |
| 150 | + $this->plugin->beforeCopy('files/a/1-src.txt', 'files/a/2-dst.txt'); |
| 151 | + $this->plugin->onException(new \RuntimeException('boom')); |
| 152 | + } |
| 153 | + |
| 154 | + public function testReleaseIsIdempotent(): void { |
| 155 | + $this->toggle(true); |
| 156 | + $this->lockingProvider->expects($this->exactly(2))->method('releaseLock'); |
| 157 | + $this->plugin->beforeMove('files/a/1-src.txt', 'files/a/2-dst.txt'); |
| 158 | + $this->plugin->afterMethod(new Request('MOVE', 'files/a/1-src.txt'), new Response()); |
| 159 | + $this->plugin->onException(new \RuntimeException('later')); |
| 160 | + } |
| 161 | +} |
0 commit comments