From a1d72cefff8a4ecf3c94ab9299463c7a9cc10b40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Fri, 3 Apr 2026 18:01:04 +0200 Subject: [PATCH] Add LockTokenParser::parseIfCondition method and corresponding tests - Implemented the `parseIfCondition` method in `LockTokenParser` to extract lock tokens from `if` headers. - Added `LockTokenParserTest` with multiple test cases using data providers to validate parsing logic, including scenarios with no headers, resource URIs, and "not" conditions. --- .../Dav/Utils/LockTokenParser.php | 15 ++++ .../Tests/Dav/Utils/LockTokenParserTest.php | 70 +++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 src/Bundle/ChillDocStoreBundle/Tests/Dav/Utils/LockTokenParserTest.php diff --git a/src/Bundle/ChillDocStoreBundle/Dav/Utils/LockTokenParser.php b/src/Bundle/ChillDocStoreBundle/Dav/Utils/LockTokenParser.php index a254eff9a..58054a307 100644 --- a/src/Bundle/ChillDocStoreBundle/Dav/Utils/LockTokenParser.php +++ b/src/Bundle/ChillDocStoreBundle/Dav/Utils/LockTokenParser.php @@ -40,4 +40,19 @@ final readonly class LockTokenParser return $token; } + + public function parseIfCondition(Request $request): ?string + { + $if = $request->headers->get('if'); + + if (null === $if) { + return null; + } + + if (preg_match('/\((?:not\s+)?<([^>]+)>/i', $if, $matches)) { + return $matches[1]; + } + + return null; + } } diff --git a/src/Bundle/ChillDocStoreBundle/Tests/Dav/Utils/LockTokenParserTest.php b/src/Bundle/ChillDocStoreBundle/Tests/Dav/Utils/LockTokenParserTest.php new file mode 100644 index 000000000..33c2cd828 --- /dev/null +++ b/src/Bundle/ChillDocStoreBundle/Tests/Dav/Utils/LockTokenParserTest.php @@ -0,0 +1,70 @@ +parser = new LockTokenParser(); + } + + /** + * @dataProvider provideIfConditions + */ + public function testParseIfCondition(string $ifHeader, ?string $expectedToken): void + { + $request = new Request(); + $request->headers->set('if', $ifHeader); + + $this->assertSame($expectedToken, $this->parser->parseIfCondition($request)); + } + + public function provideIfConditions(): array + { + return [ + 'standard lock token' => [ + '()', + 'opaquelocktoken:f81d4fae-7dec-11d0-a765-00a0c91e6bf6', + ], + 'with resource uri' => [ + ' ()', + 'opaquelocktoken:f81d4fae-7dec-11d0-a765-00a0c91e6bf6', + ], + 'no match' => [ + 'some other value', + null, + ], + 'with NOT' => [ + '(Not )', + 'opaquelocktoken:f81d4fae-7dec-11d0-a765-00a0c91e6bf6', + ], + ]; + } + + public function testParseIfConditionWithNoHeader(): void + { + $request = new Request(); + $this->assertNull($this->parser->parseIfCondition($request)); + } +}